@typeonce/effect-machine-devtools 0.33.0 → 0.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -95,7 +95,10 @@ const ParallelStates = Machine.state({
95
95
  }
96
96
  })
97
97
 
98
+ const targets1 = Machine.targets(ParallelStates)
98
99
  export const parallelCompletionMachine = Machine.make({
100
+ timers: { source1: "5 seconds" },
101
+
99
102
  id: "parallel-completion",
100
103
  root: ParallelStates,
101
104
  events: Machine.eventsFromSchemas(
@@ -115,40 +118,39 @@ export const parallelCompletionMachine = Machine.make({
115
118
  states: {
116
119
  Cart: {
117
120
  on: {
118
- Checkout: (to) =>
119
- to.branch.Order.initial.resolve(({ event, target }) =>
120
- target.decoded(new Order({ orderId: event.orderId, total: event.total }))
121
- )
121
+ Checkout: {
122
+ initial: targets1.root.Order,
123
+ decoded: ({ event }) => (new Order({ orderId: event.orderId, total: event.total }))
124
+ }
122
125
  }
123
126
  },
124
127
  Order: {
125
128
  initialize: ({ builder }) => builder.payment.from({ attempts: 0 }).fulfillment.from({ warehouse: "north" }),
126
129
  on: {
127
- CancelOrder: (to) =>
128
- to.branch.Cancelled().resolve(({ event, target }) =>
129
- target.decoded(new OrderCancelled({ reason: event.reason }))
130
- )
130
+ CancelOrder: {
131
+ target: targets1.root.Cancelled,
132
+ decoded: ({ event }) => (new OrderCancelled({ reason: event.reason }))
133
+ }
131
134
  },
132
- onDone: (to) =>
133
- to.branch.Complete().resolve(({ target }) => target.decoded(new OrderComplete({ orderId: "completed-order" }))),
135
+ onDone: { target: targets1.root.Complete, decoded: () => (new OrderComplete({ orderId: "completed-order" })) },
134
136
  states: {
135
137
  payment: {
136
138
  initialize: ({ builder }) => builder.from(),
137
139
  states: {
138
140
  AwaitingAuthorization: {
139
141
  on: {
140
- Authorize: (to) =>
141
- to.local.Authorized().resolve(({ event, target }) =>
142
- target.decoded(new Authorized({ authorizationId: event.authorizationId }))
143
- ),
144
- CompleteAll: (to) =>
145
- to.local.Authorized().resolve(({ event, target }) =>
146
- target.decoded(new Authorized({ authorizationId: event.authorizationId }))
147
- ),
148
- DeclinePayment: (to) =>
149
- to.branch.Cancelled().resolve(({ event, target }) =>
150
- target.decoded(new OrderCancelled({ reason: event.reason }))
151
- )
142
+ Authorize: {
143
+ target: targets1.root.Order.payment.Authorized,
144
+ decoded: ({ event }) => (new Authorized({ authorizationId: event.authorizationId }))
145
+ },
146
+ CompleteAll: {
147
+ target: targets1.root.Order.payment.Authorized,
148
+ decoded: ({ event }) => (new Authorized({ authorizationId: event.authorizationId }))
149
+ },
150
+ DeclinePayment: {
151
+ target: targets1.root.Cancelled,
152
+ decoded: ({ event }) => (new OrderCancelled({ reason: event.reason }))
153
+ }
152
154
  }
153
155
  }
154
156
  }
@@ -158,38 +160,44 @@ export const parallelCompletionMachine = Machine.make({
158
160
  states: {
159
161
  WaitingForPayment: {
160
162
  on: {
161
- Authorize: (to) =>
162
- to.local.Packing().resolve(({ target }) => target.decoded(new Packing({ packageCount: 1 }))),
163
- Pack: (to) =>
164
- to.local.Packing().resolve(({ event, target }) =>
165
- target.decoded(new Packing({ packageCount: event.packages }))
166
- ),
167
- CompleteAll: (to) =>
168
- to.local.Shipped().resolve(({ event, target }) =>
169
- target.decoded(new Shipped({ trackingCode: event.trackingCode }))
170
- )
163
+ Authorize: {
164
+ target: targets1.root.Order.fulfillment.Packing,
165
+ decoded: () => (new Packing({ packageCount: 1 }))
166
+ },
167
+ Pack: {
168
+ target: targets1.root.Order.fulfillment.Packing,
169
+ decoded: ({ event }) => (new Packing({ packageCount: event.packages }))
170
+ },
171
+ CompleteAll: {
172
+ target: targets1.root.Order.fulfillment.Shipped,
173
+ decoded: ({ event }) => (new Shipped({ trackingCode: event.trackingCode }))
174
+ }
171
175
  }
172
176
  },
173
177
  Packing: {
174
- invoke: (from) =>
175
- from.timer("packing-sla", "5 seconds").onDone((to) =>
176
- to.none.resolve((_, enqueue) => {
178
+ invoke: {
179
+ src: "source1",
180
+ id: "packing-sla",
181
+ onDone: {
182
+ none: true,
183
+ resolve: (_, enqueue) => {
177
184
  enqueue.raise(ParallelInternalEvents.AutoShip())
178
- })
179
- ),
185
+ }
186
+ }
187
+ },
180
188
  on: {
181
- AutoShip: (to) =>
182
- to.local.Shipped().resolve(({ target }) =>
183
- target.decoded(new Shipped({ trackingCode: "automatic" }))
184
- ),
185
- Ship: (to) =>
186
- to.local.Shipped().resolve(({ event, target }) =>
187
- target.decoded(new Shipped({ trackingCode: event.trackingCode }))
188
- ),
189
- CompleteAll: (to) =>
190
- to.local.Shipped().resolve(({ event, target }) =>
191
- target.decoded(new Shipped({ trackingCode: event.trackingCode }))
192
- )
189
+ AutoShip: {
190
+ target: targets1.root.Order.fulfillment.Shipped,
191
+ decoded: () => (new Shipped({ trackingCode: "automatic" }))
192
+ },
193
+ Ship: {
194
+ target: targets1.root.Order.fulfillment.Shipped,
195
+ decoded: ({ event }) => (new Shipped({ trackingCode: event.trackingCode }))
196
+ },
197
+ CompleteAll: {
198
+ target: targets1.root.Order.fulfillment.Shipped,
199
+ decoded: ({ event }) => (new Shipped({ trackingCode: event.trackingCode }))
200
+ }
193
201
  }
194
202
  }
195
203
  }
@@ -201,8 +209,7 @@ export const parallelCompletionMachine = Machine.make({
201
209
  },
202
210
  Cancelled: {
203
211
  on: {
204
- RetryOrder: (to) =>
205
- to.branch.Order.initial.resolve(({ target }) => target.decoded(new Order({ orderId: "retry", total: 0 })))
212
+ RetryOrder: { initial: targets1.root.Order, decoded: () => (new Order({ orderId: "retry", total: 0 })) }
206
213
  }
207
214
  }
208
215
  }
@@ -18,6 +18,23 @@ const ChildParentEvents = Machine.eventsFromSchemas(ChildSignal)
18
18
  const ParallelOwnerChildStates = Machine.state({ initial: "Idle", states: { Idle: ChildIdle } })
19
19
 
20
20
  export const parallelOwnerRoutingChildMachine = Machine.make({
21
+ effects: {
22
+ source1: ({
23
+ parent
24
+ }: Machine.Machine.InvokeContext<
25
+ {
26
+ readonly "": { readonly initial: "Idle"; readonly states: { readonly Idle: typeof ChildIdle } } & {
27
+ readonly "~effect/Machine/ExplicitInitial": true
28
+ }
29
+ },
30
+ readonly [],
31
+ readonly [],
32
+ "Idle",
33
+ readonly [],
34
+ Machine.Machine.ParentEventSchemas<"required", readonly [typeof ChildSignal]>
35
+ >) => parent.send(ChildParentEvents.ChildSignal())
36
+ },
37
+
21
38
  id: "parallel-owner-routing-child",
22
39
  root: ParallelOwnerChildStates,
23
40
  events: Machine.eventsFromSchemas(),
@@ -26,11 +43,13 @@ export const parallelOwnerRoutingChildMachine = Machine.make({
26
43
  }).handle({
27
44
  states: {
28
45
  Idle: {
29
- invoke: (from) =>
30
- from.effect(
31
- "signal-parent",
32
- ({ parent }) => parent.send(ChildParentEvents.ChildSignal())
33
- ).onDone((to) => to.none).onFailure((to) => to.none)
46
+ invoke: {
47
+ src: "source1",
48
+ id: "signal-parent",
49
+ input: (context) => context,
50
+ onDone: { none: true },
51
+ onFailure: { none: true }
52
+ }
34
53
  }
35
54
  }
36
55
  })
@@ -64,7 +83,10 @@ const ParallelOwnerStates = Machine.state({
64
83
  }
65
84
  })
66
85
 
86
+ const targets2 = Machine.targets(ParallelOwnerStates)
67
87
  export const parallelOwnerRoutingMachine = Machine.make({
88
+ children: { source1: ParallelOwnerChild },
89
+
68
90
  id: "parallel-owner-routing",
69
91
  root: ParallelOwnerStates,
70
92
  events: Machine.eventsFromSchemas(PrintRequested, ChildParentEvents),
@@ -83,13 +105,12 @@ export const parallelOwnerRoutingMachine = Machine.make({
83
105
  states: {
84
106
  Print: {
85
107
  initialize: ({ builder }) => builder.Options.from({}).Operation.from({}),
86
- invoke: (from) =>
87
- from.child(ParallelOwnerChild).onFailure((to) =>
88
- to.branch.Print.Operation.Active().resolve(({ target }) => target.decoded(new Active({})))
89
- ),
108
+ invoke: {
109
+ src: "source1",
110
+ onFailure: { target: targets2.root.Print.Operation.Active, decoded: () => (new Active({})) }
111
+ },
90
112
  on: {
91
- PrintRequested: (to) =>
92
- to.branch.Print.Operation.Printing().resolve(({ target }) => target.decoded(new Printing({})))
113
+ PrintRequested: { target: targets2.root.Print.Operation.Printing, decoded: () => (new Printing({})) }
93
114
  },
94
115
  states: {
95
116
  Options: {
@@ -76,7 +76,16 @@ const States = Machine.state({
76
76
  }
77
77
  })
78
78
 
79
+ const targets1 = Machine.targets(States)
79
80
  export const plannerMachine = Machine.make({
81
+ branches: {
82
+ transition1: {
83
+ urgent: { target: targets1.root.Working, title: "Finish immediately" },
84
+ normal: { target: targets1.root.Working, title: "Wait in working" }
85
+ }
86
+ },
87
+ effects: { source1: Effect.suspend(() => Effect.never) },
88
+
80
89
  id: "planner-example",
81
90
  root: States,
82
91
  events: Events,
@@ -118,11 +127,9 @@ export const plannerMachine = Machine.make({
118
127
  states: {
119
128
  Idle: {
120
129
  on: {
121
- Begin: (to) =>
122
- to.branches({
123
- urgent: { title: "Finish immediately", target: to.branch.Working() },
124
- normal: { title: "Wait in working", target: to.branch.Working() }
125
- }).resolve(({ event, self, select, state }, enqueue) => {
130
+ Begin: {
131
+ branches: "transition1",
132
+ resolve: ({ event, self, select, state }, enqueue) => {
126
133
  enqueue.emit(new Planned({ job: event.job }))
127
134
  enqueue.sendTo(self, Events.Cancel({ reason: "planner command example" }))
128
135
  if (event.priority === "urgent") enqueue.raise(InternalEvents.AutoFinish())
@@ -130,18 +137,18 @@ export const plannerMachine = Machine.make({
130
137
  return event.priority === "urgent"
131
138
  ? select.urgent.decoded(working)
132
139
  : select.normal.decoded(working)
133
- })
140
+ }
141
+ }
134
142
  }
135
143
  },
136
144
  Working: {
137
- invoke: (from) => from.effect("monitor-job", () => Effect.never),
145
+ invoke: { src: "source1", id: "monitor-job" },
138
146
  on: {
139
- AutoFinish: (to) =>
140
- to.branch.Finished().resolve(({ state, target }) => target.decoded(new Finished({ job: state.job }))),
141
- Cancel: (to) =>
142
- to.branch.Idle().resolve(({ event, state, target }) =>
143
- target.decoded(new Idle({ owner: `${state.owner} · ${event.reason}` }))
144
- )
147
+ AutoFinish: { target: targets1.root.Finished, decoded: ({ state }) => (new Finished({ job: state.job })) },
148
+ Cancel: {
149
+ target: targets1.root.Idle,
150
+ decoded: ({ event, state }) => (new Idle({ owner: `${state.owner} · ${event.reason}` }))
151
+ }
145
152
  }
146
153
  },
147
154
  Finished: {
@@ -56,7 +56,21 @@ const ChildStates = Machine.state({
56
56
  }
57
57
  })
58
58
 
59
+ const targets1 = Machine.targets(ChildStates)
59
60
  export const requiredParentChildMachine = Machine.make({
61
+ branches: {
62
+ transition1: { destination: { target: targets1.root.Working } },
63
+ transition3: { destination: { target: targets1.root.Working } }
64
+ },
65
+ effects: {
66
+ source1: (
67
+ { parent, state }: {
68
+ readonly parent: Machine.MachineTarget<Machine.Machine.EventInput<ChildProgress | ChildFinished | ChildProblem>>
69
+ readonly state: ChildWorking
70
+ }
71
+ ) => parent.send(ParentEvents.ChildProgress({ percent: state.progress }))
72
+ },
73
+
60
74
  id: "required-parent-child",
61
75
  root: ChildStates,
62
76
  events: ChildEvents,
@@ -68,38 +82,43 @@ export const requiredParentChildMachine = Machine.make({
68
82
  states: {
69
83
  Idle: {
70
84
  on: {
71
- BeginChildWork: (to) =>
72
- to.branch.Working().resolve(({ event, target }, enqueue) => {
85
+ BeginChildWork: {
86
+ branches: "transition1",
87
+ resolve: ({ event, select: { destination: target } }, enqueue) => {
73
88
  enqueue.raise(ChildInternalEvents.Heartbeat({ percent: 25 }))
74
89
  enqueue.emit(ChildEmissions.ChildTrace({ message: `started ${event.job}` }))
75
90
  return target.decoded(new ChildWorking({ job: event.job, progress: 0 }))
76
- })
91
+ }
92
+ }
77
93
  }
78
94
  },
79
95
  Working: {
80
- invoke: (from) =>
81
- from.effect(
82
- "report-progress-to-parent",
83
- ({ parent, state }) => parent.send(ParentEvents.ChildProgress({ percent: state.progress }))
84
- ).onDone((to) => to.none).onFailure((to) =>
85
- to.branch.Cancelled().resolve(({ error, target }) =>
86
- target.decoded(new ChildCancelled({ reason: String(error) }))
87
- )
88
- ),
96
+ invoke: {
97
+ src: "source1",
98
+ id: "report-progress-to-parent",
99
+ input: (context) => context,
100
+ onDone: { none: true },
101
+ onFailure: {
102
+ target: targets1.root.Cancelled,
103
+ decoded: ({ error }) => (new ChildCancelled({ reason: String(error) }))
104
+ }
105
+ },
89
106
  on: {
90
- Heartbeat: (to) =>
91
- to.branch.Working().resolve(({ event, state, target }, enqueue) => {
107
+ Heartbeat: {
108
+ branches: "transition3",
109
+ resolve: ({ event, state, select: { destination: target } }, enqueue) => {
92
110
  enqueue.raise(ChildInternalEvents.CommitChildWork())
93
111
  return target.decoded(new ChildWorking({ job: state.job, progress: event.percent }))
94
- }),
95
- CommitChildWork: (to) =>
96
- to.branch.Done().resolve(({ state, target }) =>
97
- target.decoded(new ChildDone({ result: `${state.job}:complete` }))
98
- ),
99
- CancelChildWork: (to) =>
100
- to.branch.Cancelled().resolve(({ event, target }) =>
101
- target.decoded(new ChildCancelled({ reason: event.reason }))
102
- )
112
+ }
113
+ },
114
+ CommitChildWork: {
115
+ target: targets1.root.Done,
116
+ decoded: ({ state }) => (new ChildDone({ result: `${state.job}:complete` }))
117
+ },
118
+ CancelChildWork: {
119
+ target: targets1.root.Cancelled,
120
+ decoded: ({ event }) => (new ChildCancelled({ reason: event.reason }))
121
+ }
103
122
  }
104
123
  },
105
124
  Done: {
@@ -136,7 +155,14 @@ const ParentStates = Machine.state({
136
155
  }
137
156
  })
138
157
 
158
+ const targets2 = Machine.targets(ParentStates)
139
159
  export const parentProtocolMachine = Machine.make({
160
+ branches: {
161
+ transition1: { destination: { target: targets2.root.Supervising } },
162
+ transition3: { destination: { target: targets2.root.Complete } }
163
+ },
164
+ children: { source1: ProtocolChild },
165
+
140
166
  id: "parent-child-protocol",
141
167
  root: ParentStates,
142
168
  events: Machine.eventsFromSchemas(LaunchChild, ResetParent, ParentEvents),
@@ -145,33 +171,32 @@ export const parentProtocolMachine = Machine.make({
145
171
  states: {
146
172
  Idle: {
147
173
  on: {
148
- LaunchChild: (to) =>
149
- to.branch.Supervising().resolve(({ target }) => target.decoded(new Supervising({ latestProgress: 0 })))
174
+ LaunchChild: { target: targets2.root.Supervising, decoded: () => (new Supervising({ latestProgress: 0 })) }
150
175
  }
151
176
  },
152
177
  Supervising: {
153
- invoke: (from) =>
154
- from.child(ProtocolChild).onDone((to) =>
155
- to.branch.Complete().resolve(({ output, target }) => target.decoded(new ParentComplete({ result: output })))
156
- ).onFailure((to) =>
157
- to.branch.Failed().resolve(({ error, target }) =>
158
- target.decoded(new ParentFailed({ message: String(error) }))
159
- )
160
- ),
178
+ invoke: {
179
+ src: "source1",
180
+ onDone: { target: targets2.root.Complete, decoded: ({ output }) => (new ParentComplete({ result: output })) },
181
+ onFailure: {
182
+ target: targets2.root.Failed,
183
+ decoded: ({ error }) => (new ParentFailed({ message: String(error) }))
184
+ }
185
+ },
161
186
  on: {
162
- ChildProgress: (to) =>
163
- to.branch.Supervising().resolve(({ event, target }) =>
164
- target.decoded(new Supervising({ latestProgress: event.percent }))
165
- ),
166
- ChildFinished: (to) =>
167
- to.branch.Complete().resolve(({ event, target }) =>
168
- target.decoded(new ParentComplete({ result: event.result }))
169
- ),
170
- ChildProblem: (to) =>
171
- to.branch.Failed().resolve(({ event, target }) =>
172
- target.decoded(new ParentFailed({ message: event.message }))
173
- ),
174
- ResetParent: (to) => to.branch.Idle().resolve(({ target }) => target.decoded(new ParentIdle({})))
187
+ ChildProgress: {
188
+ target: targets2.root.Supervising,
189
+ decoded: ({ event }) => (new Supervising({ latestProgress: event.percent }))
190
+ },
191
+ ChildFinished: {
192
+ target: targets2.root.Complete,
193
+ decoded: ({ event }) => (new ParentComplete({ result: event.result }))
194
+ },
195
+ ChildProblem: {
196
+ target: targets2.root.Failed,
197
+ decoded: ({ event }) => (new ParentFailed({ message: event.message }))
198
+ },
199
+ ResetParent: { target: targets2.root.Idle, decoded: () => (new ParentIdle({})) }
175
200
  }
176
201
  },
177
202
  Complete: {
@@ -179,7 +204,7 @@ export const parentProtocolMachine = Machine.make({
179
204
  },
180
205
  Failed: {
181
206
  on: {
182
- ResetParent: (to) => to.branch.Idle().resolve(({ target }) => target.decoded(new ParentIdle({})))
207
+ ResetParent: { target: targets2.root.Idle, decoded: () => (new ParentIdle({})) }
183
208
  }
184
209
  }
185
210
  }
@@ -196,7 +221,10 @@ class PublishOutside extends Schema.TaggedClass<PublishOutside>("OptionalParentP
196
221
 
197
222
  const OptionalParentStates = Machine.state({ initial: "Detached", states: { Detached, Published } })
198
223
 
224
+ const targets3 = Machine.targets(OptionalParentStates)
199
225
  export const optionalParentMachine = Machine.make({
226
+ branches: { transition1: { destination: { target: targets3.root.Published } } },
227
+
200
228
  id: "optional-parent-protocol",
201
229
  root: OptionalParentStates,
202
230
  events: Machine.eventsFromSchemas(PublishOutside),
@@ -207,13 +235,15 @@ export const optionalParentMachine = Machine.make({
207
235
  states: {
208
236
  Detached: {
209
237
  on: {
210
- PublishOutside: (to) =>
211
- to.branch.Published().resolve(({ event, parent, target }, enqueue) => {
238
+ PublishOutside: {
239
+ branches: "transition1",
240
+ resolve: ({ event, parent, select: { destination: target } }, enqueue) => {
212
241
  if (parent !== undefined) {
213
242
  enqueue.sendTo(parent, ParentEvents.ChildFinished({ result: event.result }))
214
243
  }
215
244
  return target.decoded(new Published({ deliveredToParent: parent !== undefined }))
216
- })
245
+ }
246
+ }
217
247
  }
218
248
  },
219
249
  Published: {}