Repository navigation
Docs: clarify actions vs. reduce - #274
Merged
Merged
Conversation
…ntext action() discards its function's return value, but the docs said actions update context and must return it. Rewrite the actions guide and update the action/reduce API pages, core concepts, transitions, and guards pages to match the implementation. Also fix the core concepts example, which wrapped a guard in guard() and passed context as an object. Fixes #266
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #266
The docs said actions update context and must always return it. In the code,
action(fn)isreduce((ctx, ev) => (fn(ctx, ev), ctx)), so the function's return value is thrown away and context passes through unchanged.Changes
concepts-actions.md), mostly rewritten:actionis for side effects and its return value is ignored.reduceis how you update context, and its return value becomes the new context.return ctxfrom action examples, and converted context-updatingactions toreduce.reducewith a new object.test-invoke.jsdepends on this behavior.reduce(...)as if it were a function, butreducereturns a descriptor object.action/reduceAPI pages: say what happens to the return value, and that reducers and actions run in order.reduce. It also had two bugs that made it throw: it wrapped a guard inguard()again, and it passed context as an object instead of a function.transitionTo.machine.js:160). I reworded it to say "before any reducers update it".Verification
packages/core/machine.js. Confirmed that guards see the original context, actions see the context from earlier reducers, action return values are discarded, and mutations made in actions persist.npm run --workspace=docs buildpasses.