Remove "context" from "enqueueActions". #5058
Replies: 2 comments 1 reply
-
What if you want to pass the previous context to the "logger" event? For example, log the old value just before it changes. The simple solution is to save a copy of the previous context. Thus, whether the context has already been updated or not, you always have the opportunity to access the value before the change. Even if we actually want to pass the updated value to the "logger" event, we can still pre-calculate it before calling the However, with your suggestion, it seems impossible to access the previous value in the |
Beta Was this translation helpful? Give feedback.
-
@nightire, the first question is easy: just send the "old (original)" value before the increment: enqueueActions(({ enqueue }) => {
enqueue.sendTo('logger', {
type: 'incremented',
data: {
// original context before any changes
count: ({context}) => context.count,
previous: ({context}) => context.previous,
}
});
enqueue.assign({
count: ({context}) => context.count + 1,
previous: ({context}) => context.count,
});
})
I don't see the need to precalculate anything. I think we should always trust the last updated increment: enqueueActions(({ enqueue }) => {
let originalContext; // if you really need it for some reason.
// sending to logger before assignment
enqueue.sendTo('logger', {
type: 'incremented',
data: {
count: ({context}) => {
originalContext = context;
return context.count;
},
previous: ({context}) => context.previous,
}
});
enqueue.assign({
count: ({context}) => context.count + 1,
previous: ({context}) => context.count,
});
// sending to logger after assignment
enqueue.sendTo('logger', {
type: 'incremented',
data: {
count: ({context}) => context.count,
previous: ({context}) => context.previous,
}
});
})
Not really impossible, as I have demonstrated above with the usage of |
Beta Was this translation helpful? Give feedback.
-
Good design is about making good choices easy and bad ones hard, right?
So, the action creator
enqueueActions
makes a bad choice to be easy. Consider this:So, what will be sent to logger? The original values of
count
andprevious
when the action was fired, or the updated ones after assignment?Such action would be more clear like this:
So, what do you think?
Beta Was this translation helpful? Give feedback.
All reactions