Skip to content

Commit 43843ea

Browse files
Remove pure and choose (#4529)
* Remove `pure` and `choose` * remove invalid comment * Changeset --------- Co-authored-by: David Khourshid <[email protected]>
1 parent 5dc9777 commit 43843ea

File tree

8 files changed

+71
-1246
lines changed

8 files changed

+71
-1246
lines changed

.changeset/red-shoes-type.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
'xstate': major
3+
---
4+
5+
The `pure()` and `choose()` action creators have been removed, in favor of the more flexible `enqueueActions()` action creator:
6+
7+
```ts
8+
entry: [
9+
// pure(() => {
10+
// return [
11+
// 'action1',
12+
// 'action2'
13+
// ]
14+
// }),
15+
enqueueActions(({ enqueue }) => {
16+
enqueue('action1');
17+
enqueue('action2');
18+
})
19+
];
20+
```
21+
22+
```ts
23+
entry: [
24+
// choose([
25+
// {
26+
// guard: 'someGuard',
27+
// actions: ['action1', 'action2']
28+
// }
29+
// ]),
30+
enqueueActions(({ enqueue, check }) => {
31+
if (check('someGuard')) {
32+
enqueue('action1');
33+
enqueue('action2');
34+
}
35+
})
36+
];
37+
```

packages/core/src/actions.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ export {
44
type AssignArgs
55
} from './actions/assign.ts';
66
export { cancel, type CancelAction } from './actions/cancel.ts';
7-
export { choose, type ChooseAction } from './actions/choose.ts';
87
export {
98
enqueueActions,
109
type EnqueueActionsAction
1110
} from './actions/enqueueActions.ts';
1211
export { log, type LogAction } from './actions/log.ts';
13-
export { pure } from './actions/pure.ts';
1412
export { raise, type RaiseAction } from './actions/raise.ts';
1513
export {
1614
escalate,

packages/core/src/actions/choose.ts

Lines changed: 0 additions & 107 deletions
This file was deleted.

packages/core/src/actions/enqueueActions.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ export function enqueueActions<
172172
TEvent extends EventObject = TExpressionEvent,
173173
TActor extends ProvidedActor = ProvidedActor,
174174
TAction extends ParameterizedObject = ParameterizedObject,
175-
// `TGuard` is only required by `choose`, if we remove `choose` then we can remove this whole type parameter
176175
TGuard extends ParameterizedObject = ParameterizedObject,
177176
TDelay extends string = string
178177
>(

packages/core/src/actions/pure.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

packages/core/src/scxml.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Element as XMLElement, xml2js } from 'xml-js';
22
import { assign } from './actions/assign.ts';
33
import { cancel } from './actions/cancel.ts';
4-
import { choose } from './actions/choose.ts';
54
import { log } from './actions/log.ts';
65
import { raise } from './actions/raise.ts';
76
import { sendTo } from './actions/send.ts';
@@ -11,17 +10,16 @@ import {
1110
ActionFunction,
1211
MachineContext,
1312
SpecialTargets,
14-
createMachine
13+
createMachine,
14+
enqueueActions
1515
} from './index.ts';
1616
import {
1717
AnyStateMachine,
1818
AnyStateNode,
1919
AnyStateNodeConfig,
20-
ChooseBranch,
2120
DelayExpr,
2221
EventObject,
23-
SendExpr,
24-
StateNodeConfig
22+
SendExpr
2523
} from './types.ts';
2624
import { mapValues } from './utils.ts';
2725

@@ -133,7 +131,8 @@ const evaluateExecutableContent = <
133131
context: TContext,
134132
event: TEvent,
135133
_meta: any,
136-
body: string
134+
body: string,
135+
...extraArgs: any[]
137136
) => {
138137
const scope = ['const _sessionid = "NOT_IMPLEMENTED";']
139138
.filter(Boolean)
@@ -148,7 +147,7 @@ with (context) {
148147
}
149148
`;
150149

151-
const fn = new Function(...args, fnBody);
150+
const fn = new Function(...args, ...extraArgs, fnBody);
152151

153152
return fn(context, { name: event.type, data: event });
154153
};
@@ -286,9 +285,12 @@ return ${element.attributes!.expr};
286285
);
287286
}
288287
case 'if': {
289-
const branches: Array<ChooseBranch<MachineContext, EventObject>> = [];
288+
const branches: Array<{
289+
guard?: (...args: any) => any;
290+
actions: any[];
291+
}> = [];
290292

291-
let current: ChooseBranch<MachineContext, EventObject> = {
293+
let current: (typeof branches)[number] = {
292294
guard: createGuard(element.attributes!.cond as string),
293295
actions: []
294296
};
@@ -317,7 +319,15 @@ return ${element.attributes!.expr};
317319
}
318320

319321
branches.push(current);
320-
return choose(branches);
322+
323+
return enqueueActions(({ context, event, enqueue, check, ...meta }) => {
324+
for (const branch of branches) {
325+
if (!branch.guard || check(branch.guard)) {
326+
branch.actions.forEach(enqueue);
327+
break;
328+
}
329+
}
330+
});
321331
}
322332
default:
323333
throw new Error(

0 commit comments

Comments
 (0)