-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core] Transition function #4954
base: main
Are you sure you want to change the base?
Changes from 68 commits
9f6f015
abfe358
6e181ea
f1100ab
8e25fa1
b2b974a
6dd475c
6533540
7641ec5
9942382
f566149
efaac40
8b936ab
ae70cc6
f3c360f
32b278c
b1d346e
4f61ac7
bc431d1
96ba826
abf3a2c
d97cb8f
5f6a00b
c0597ee
59daf7c
1f13746
a73a0db
4c8d491
cbb0a7f
18f0015
e2e821c
ad422a8
737f67f
f7991ad
97713c5
0c29c2c
6e18296
7146bdc
e776f2c
3d76b58
7e05a9a
857c96c
59eedc2
574c4c9
4cdd545
d2e583f
fea607b
7a33175
0c80c66
f596786
062676c
2d480e1
2e450c7
a75ceb9
200fc12
edb0615
96f4ac4
9c077d6
5bd5e09
cf9d549
68654be
7622a79
4f61f39
a365418
f6de768
1977a9d
d202b93
0c344da
ece423d
9576616
85b1f67
9d3f2a0
4516833
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
'xstate': minor | ||
--- | ||
|
||
Added a new `transition` function that takes an actor logic, a snapshot, and an event, and returns a tuple containing the next snapshot and the actions to execute. This function is a pure function and does not execute the actions itself. It can be used like this: | ||
|
||
```ts | ||
import { transition } from 'xstate'; | ||
|
||
const [nextState, actions] = transition(actorLogic, currentState, event); | ||
// Execute actions as needed | ||
``` | ||
|
||
Added a new `initialTransition` function that takes an actor logic and an optional input, and returns a tuple containing the initial snapshot and the actions to execute from the initial transition. This function is also a pure function and does not execute the actions itself. It can be used like this: | ||
|
||
```ts | ||
import { initialTransition } from 'xstate'; | ||
|
||
const [initialState, actions] = initialTransition(actorLogic, input); | ||
// Execute actions as needed | ||
``` | ||
|
||
These new functions provide a way to separate the calculation of the next snapshot and actions from the execution of those actions, allowing for more control and flexibility in the transition process. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,9 +33,7 @@ function resolveCancel( | |
} | ||
|
||
function executeCancel(actorScope: AnyActorScope, resolvedSendId: string) { | ||
actorScope.defer(() => { | ||
actorScope.system.scheduler.cancel(actorScope.self, resolvedSendId); | ||
}); | ||
actorScope.system.scheduler.cancel(actorScope.self, resolvedSendId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have some new test that shows this is a desired change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no reason to defer it - it's a clean-up |
||
} | ||
|
||
export interface CancelAction< | ||
|
@@ -102,5 +100,9 @@ export function cancel< | |
cancel.resolve = resolveCancel; | ||
cancel.execute = executeCancel; | ||
|
||
cancel.toJSON = () => ({ | ||
...cancel | ||
}); | ||
|
||
return cancel; | ||
} |
davidkpiano marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import isDevelopment from '#is-development'; | ||
import { Guard, evaluateGuard } from '../guards.ts'; | ||
import { convertAction } from '../stateUtils.ts'; | ||
import { | ||
Action, | ||
ActionArgs, | ||
|
@@ -135,7 +136,16 @@ function resolveEnqueueActions( | |
const enqueue: Parameters<typeof collect>[0]['enqueue'] = function enqueue( | ||
action | ||
) { | ||
actions.push(action); | ||
actions.push( | ||
convertAction( | ||
action as any, | ||
snapshot.machine.root, | ||
'enqueue' + Math.random(), // TODO: this should come from state node ID which isn't provided | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how hard would it be to fix it here? |
||
undefined, | ||
0, | ||
actions.length | ||
) | ||
); | ||
}; | ||
enqueue.assign = (...args) => { | ||
actions.push(assign(...args)); | ||
|
@@ -323,5 +333,9 @@ export function enqueueActions< | |
enqueueActions.collect = collect; | ||
enqueueActions.resolve = resolveEnqueueActions; | ||
|
||
enqueueActions.toJSON = () => ({ | ||
...enqueueActions | ||
}); | ||
|
||
return enqueueActions; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This leaks in cases like this:
We should add a test case for this