@typeonce/effect-machine-devtools 0.31.2 → 0.33.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.
Files changed (33) hide show
  1. package/dist/MachineRegistry.d.ts +4 -3
  2. package/dist/MachineRegistry.d.ts.map +1 -1
  3. package/dist/MachineRegistry.js +4 -3
  4. package/dist/MachineRegistry.js.map +1 -1
  5. package/dist/client/assets/index-2uvShdlX.js +37 -0
  6. package/dist/client/index.html +1 -1
  7. package/dist/internal/devServer.d.ts +3 -1
  8. package/dist/internal/devServer.d.ts.map +1 -1
  9. package/dist/internal/devServer.js +8 -6
  10. package/dist/internal/devServer.js.map +1 -1
  11. package/dist/internal/machineRegistry.js +9 -6
  12. package/dist/internal/machineRegistry.js.map +1 -1
  13. package/package.json +2 -2
  14. package/src/MachineRegistry.ts +4 -3
  15. package/src/internal/browser/chart-layout-policy.ts +2 -2
  16. package/src/internal/browser/chart-layout.ts +236 -51
  17. package/src/internal/browser/chart-model.ts +3 -0
  18. package/src/internal/browser/chart-renderer.ts +5 -4
  19. package/src/internal/browser/example-machine.ts +94 -85
  20. package/src/internal/browser/hierarchy-routing-example.ts +67 -58
  21. package/src/internal/browser/invoke-outcomes-example.ts +153 -143
  22. package/src/internal/browser/layout-resilience-example.ts +115 -104
  23. package/src/internal/browser/parallel-completion-example.ts +117 -107
  24. package/src/internal/browser/parallel-owner-routing-example.ts +104 -0
  25. package/src/internal/browser/planner-example.ts +44 -38
  26. package/src/internal/browser/protocol-events-example.ts +123 -102
  27. package/src/internal/browser/shared-terminal-routing-example.ts +125 -120
  28. package/src/internal/browser/text-tree.ts +2 -2
  29. package/src/internal/browser/transition-semantics-example.ts +143 -131
  30. package/src/internal/browser/visualizer-model.ts +7 -7
  31. package/src/internal/devServer.ts +15 -12
  32. package/src/internal/machineRegistry.ts +17 -17
  33. package/dist/client/assets/index-CSgOYHLh.js +0 -37
@@ -62,40 +62,43 @@ class CancelOrder extends Schema.TaggedClass<CancelOrder>("ParallelCancelOrder")
62
62
  class RetryOrder extends Schema.TaggedClass<RetryOrder>("ParallelRetryOrder")("RetryOrder", {}) {}
63
63
  class AutoShip extends Schema.TaggedClass<AutoShip>("ParallelAutoShip")("AutoShip", {}) {}
64
64
 
65
- const ParallelInternalEvents = Machine.internalEvents(AutoShip)
66
- const ParallelStates = Machine.states({
67
- Cart,
68
- Order: {
69
- schema: Order,
70
- type: "parallel",
71
- states: {
72
- payment: {
73
- schema: Payment,
74
- initial: "AwaitingAuthorization",
75
- states: {
76
- AwaitingAuthorization,
77
- Authorized: { schema: Authorized, type: "final" }
78
- }
79
- },
80
- fulfillment: {
81
- schema: Fulfillment,
82
- initial: "WaitingForPayment",
83
- states: {
84
- WaitingForPayment,
85
- Packing,
86
- Shipped: { schema: Shipped, type: "final" }
65
+ const ParallelInternalEvents = Machine.internalEventsFromSchemas(AutoShip)
66
+ const ParallelStates = Machine.state({
67
+ initial: "Cart",
68
+ states: {
69
+ Cart,
70
+ Order: {
71
+ schema: Order,
72
+ type: "parallel",
73
+ states: {
74
+ payment: {
75
+ schema: Payment,
76
+ initial: "AwaitingAuthorization",
77
+ states: {
78
+ AwaitingAuthorization,
79
+ Authorized: { schema: Authorized, type: "final" }
80
+ }
81
+ },
82
+ fulfillment: {
83
+ schema: Fulfillment,
84
+ initial: "WaitingForPayment",
85
+ states: {
86
+ WaitingForPayment,
87
+ Packing,
88
+ Shipped: { schema: Shipped, type: "final" }
89
+ }
87
90
  }
88
91
  }
89
- }
90
- },
91
- Complete: { schema: OrderComplete, type: "final", output: Schema.String },
92
- Cancelled: OrderCancelled
92
+ },
93
+ Complete: { schema: OrderComplete, type: "final", output: Schema.String },
94
+ Cancelled: OrderCancelled
95
+ }
93
96
  })
94
97
 
95
98
  export const parallelCompletionMachine = Machine.make({
96
99
  id: "parallel-completion",
97
- states: ParallelStates.states,
98
- events: Machine.events(
100
+ root: ParallelStates,
101
+ events: Machine.eventsFromSchemas(
99
102
  Checkout,
100
103
  Authorize,
101
104
  DeclinePayment,
@@ -106,94 +109,101 @@ export const parallelCompletionMachine = Machine.make({
106
109
  RetryOrder
107
110
  ),
108
111
  internalEvents: ParallelInternalEvents,
109
- initial: (to) => to.Cart().resolve(({ target }) => target.decoded(new Cart({ items: 2 })))
112
+ initialConfiguration: (root) =>
113
+ root.resolve(({ target }) => target.from((to) => to.Cart.decoded(new Cart({ items: 2 }))))
110
114
  }).handle({
111
- Cart: {
112
- on: {
113
- Checkout: (to) =>
114
- to.full.Order.initial.resolve(({ event, target }) =>
115
- target.decoded(new Order({ orderId: event.orderId, total: event.total }))
116
- )
117
- }
118
- },
119
- Order: {
120
- initialize: ({ builder }) => builder.payment.from({ attempts: 0 }).fulfillment.from({ warehouse: "north" }),
121
- on: {
122
- CancelOrder: (to) =>
123
- to.full.Cancelled().resolve(({ event, target }) => target.decoded(new OrderCancelled({ reason: event.reason })))
115
+ states: {
116
+ Cart: {
117
+ on: {
118
+ Checkout: (to) =>
119
+ to.branch.Order.initial.resolve(({ event, target }) =>
120
+ target.decoded(new Order({ orderId: event.orderId, total: event.total }))
121
+ )
122
+ }
124
123
  },
125
- onDone: (to) =>
126
- to.full.Complete().resolve(({ target }) => target.decoded(new OrderComplete({ orderId: "completed-order" }))),
127
- states: {
128
- payment: {
129
- initialize: ({ builder }) => builder.from(),
130
- states: {
131
- AwaitingAuthorization: {
132
- on: {
133
- Authorize: (to) =>
134
- to.local.Authorized().resolve(({ event, target }) =>
135
- target.decoded(new Authorized({ authorizationId: event.authorizationId }))
136
- ),
137
- CompleteAll: (to) =>
138
- to.local.Authorized().resolve(({ event, target }) =>
139
- target.decoded(new Authorized({ authorizationId: event.authorizationId }))
140
- ),
141
- DeclinePayment: (to) =>
142
- to.full.Cancelled().resolve(({ event, target }) =>
143
- target.decoded(new OrderCancelled({ reason: event.reason }))
144
- )
145
- }
146
- }
147
- }
124
+ Order: {
125
+ initialize: ({ builder }) => builder.payment.from({ attempts: 0 }).fulfillment.from({ warehouse: "north" }),
126
+ on: {
127
+ CancelOrder: (to) =>
128
+ to.branch.Cancelled().resolve(({ event, target }) =>
129
+ target.decoded(new OrderCancelled({ reason: event.reason }))
130
+ )
148
131
  },
149
- fulfillment: {
150
- initialize: ({ builder }) => builder.from(),
151
- states: {
152
- WaitingForPayment: {
153
- on: {
154
- Authorize: (to) =>
155
- to.local.Packing().resolve(({ target }) => target.decoded(new Packing({ packageCount: 1 }))),
156
- Pack: (to) =>
157
- to.local.Packing().resolve(({ event, target }) =>
158
- target.decoded(new Packing({ packageCount: event.packages }))
159
- ),
160
- CompleteAll: (to) =>
161
- to.local.Shipped().resolve(({ event, target }) =>
162
- target.decoded(new Shipped({ trackingCode: event.trackingCode }))
163
- )
132
+ onDone: (to) =>
133
+ to.branch.Complete().resolve(({ target }) => target.decoded(new OrderComplete({ orderId: "completed-order" }))),
134
+ states: {
135
+ payment: {
136
+ initialize: ({ builder }) => builder.from(),
137
+ states: {
138
+ AwaitingAuthorization: {
139
+ 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
+ )
152
+ }
164
153
  }
165
- },
166
- Packing: {
167
- invoke: (from) =>
168
- from.timer("packing-sla", "5 seconds").onDone((to) =>
169
- to.none.resolve((_, enqueue) => {
170
- enqueue.raise(ParallelInternalEvents.AutoShip())
171
- })
172
- ),
173
- on: {
174
- AutoShip: (to) =>
175
- to.local.Shipped().resolve(({ target }) => target.decoded(new Shipped({ trackingCode: "automatic" }))),
176
- Ship: (to) =>
177
- to.local.Shipped().resolve(({ event, target }) =>
178
- target.decoded(new Shipped({ trackingCode: event.trackingCode }))
154
+ }
155
+ },
156
+ fulfillment: {
157
+ initialize: ({ builder }) => builder.from(),
158
+ states: {
159
+ WaitingForPayment: {
160
+ 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
+ )
171
+ }
172
+ },
173
+ Packing: {
174
+ invoke: (from) =>
175
+ from.timer("packing-sla", "5 seconds").onDone((to) =>
176
+ to.none.resolve((_, enqueue) => {
177
+ enqueue.raise(ParallelInternalEvents.AutoShip())
178
+ })
179
179
  ),
180
- CompleteAll: (to) =>
181
- to.local.Shipped().resolve(({ event, target }) =>
182
- target.decoded(new Shipped({ trackingCode: event.trackingCode }))
183
- )
180
+ 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
+ )
193
+ }
184
194
  }
185
195
  }
186
196
  }
187
197
  }
188
- }
189
- },
190
- Complete: {
191
- output: ({ state }) => state.orderId
192
- },
193
- Cancelled: {
194
- on: {
195
- RetryOrder: (to) =>
196
- to.full.Order.initial.resolve(({ target }) => target.decoded(new Order({ orderId: "retry", total: 0 })))
198
+ },
199
+ Complete: {
200
+ output: ({ state }) => state.orderId
201
+ },
202
+ Cancelled: {
203
+ on: {
204
+ RetryOrder: (to) =>
205
+ to.branch.Order.initial.resolve(({ target }) => target.decoded(new Order({ orderId: "retry", total: 0 })))
206
+ }
197
207
  }
198
208
  }
199
209
  })
@@ -0,0 +1,104 @@
1
+ import { Machine } from "@typeonce/effect-machine"
2
+ import { Schema } from "effect"
3
+
4
+ class Print extends Schema.TaggedClass<Print>("ParallelOwnerPrint")("Print", {}) {}
5
+ class Options extends Schema.TaggedClass<Options>("ParallelOwnerOptions")("Options", {}) {}
6
+ class OptionsReady extends Schema.TaggedClass<OptionsReady>("ParallelOwnerOptionsReady")("Ready", {}) {}
7
+ class Operation extends Schema.TaggedClass<Operation>("ParallelOwnerOperation")("Operation", {}) {}
8
+ class Active extends Schema.TaggedClass<Active>("ParallelOwnerActive")("Active", {}) {}
9
+ class Printing extends Schema.TaggedClass<Printing>("ParallelOwnerPrinting")("Printing", {}) {}
10
+ class ChildIdle extends Schema.TaggedClass<ChildIdle>("ParallelOwnerChildIdle")("Idle", {}) {}
11
+ class ChildSignal extends Schema.TaggedClass<ChildSignal>("ParallelOwnerChildSignal")("ChildSignal", {}) {}
12
+ class PrintRequested extends Schema.TaggedClass<PrintRequested>("ParallelOwnerPrintRequested")(
13
+ "PrintRequested",
14
+ {}
15
+ ) {}
16
+
17
+ const ChildParentEvents = Machine.eventsFromSchemas(ChildSignal)
18
+ const ParallelOwnerChildStates = Machine.state({ initial: "Idle", states: { Idle: ChildIdle } })
19
+
20
+ export const parallelOwnerRoutingChildMachine = Machine.make({
21
+ id: "parallel-owner-routing-child",
22
+ root: ParallelOwnerChildStates,
23
+ events: Machine.eventsFromSchemas(),
24
+ parent: Machine.parent(ChildParentEvents),
25
+ initialConfiguration: (root) => root.resolve(({ target }) => target.from((to) => to.Idle.decoded(new ChildIdle({}))))
26
+ }).handle({
27
+ states: {
28
+ 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)
34
+ }
35
+ }
36
+ })
37
+
38
+ const ParallelOwnerChild = Machine.child("parallel-owner-child", parallelOwnerRoutingChildMachine)
39
+
40
+ const ParallelOwnerStates = Machine.state({
41
+ initial: "Print",
42
+ states: {
43
+ Print: {
44
+ schema: Print,
45
+ type: "parallel",
46
+ states: {
47
+ Options: {
48
+ schema: Options,
49
+ initial: "Ready",
50
+ states: {
51
+ Ready: OptionsReady
52
+ }
53
+ },
54
+ Operation: {
55
+ schema: Operation,
56
+ initial: "Active",
57
+ states: {
58
+ Active,
59
+ Printing
60
+ }
61
+ }
62
+ }
63
+ }
64
+ }
65
+ })
66
+
67
+ export const parallelOwnerRoutingMachine = Machine.make({
68
+ id: "parallel-owner-routing",
69
+ root: ParallelOwnerStates,
70
+ events: Machine.eventsFromSchemas(PrintRequested, ChildParentEvents),
71
+ initialConfiguration: (root) =>
72
+ root.resolve(({ target }) =>
73
+ target.from((to) =>
74
+ to.Print.decoded(
75
+ new Print({}),
76
+ (print) =>
77
+ print.Options.decoded(new Options({}), (options) => options.Ready.decoded(new OptionsReady({}))).Operation
78
+ .decoded(new Operation({}), (operation) => operation.Active.decoded(new Active({})))
79
+ )
80
+ )
81
+ )
82
+ }).handle({
83
+ states: {
84
+ Print: {
85
+ 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
+ ),
90
+ on: {
91
+ PrintRequested: (to) =>
92
+ to.branch.Print.Operation.Printing().resolve(({ target }) => target.decoded(new Printing({})))
93
+ },
94
+ states: {
95
+ Options: {
96
+ initialize: ({ builder }) => builder.from({})
97
+ },
98
+ Operation: {
99
+ initialize: ({ builder }) => builder.from({})
100
+ }
101
+ }
102
+ }
103
+ }
104
+ })
@@ -64,18 +64,21 @@ class Cancel extends Schema.TaggedClass<Cancel>("PlannerCancel")("Cancel", { rea
64
64
  class AutoFinish extends Schema.TaggedClass<AutoFinish>("PlannerAutoFinish")("AutoFinish", {}) {}
65
65
  class Planned extends Schema.TaggedClass<Planned>("PlannerPlanned")("Planned", { job: Schema.String }) {}
66
66
 
67
- const Events = Machine.events(Begin, Cancel)
68
- const InternalEvents = Machine.internalEvents(AutoFinish)
69
- const Emissions = Machine.emittedEvents(Planned)
70
- const States = Machine.states({
71
- Idle,
72
- Working,
73
- Finished: { schema: Finished, type: "final", output: Schema.String }
67
+ const Events = Machine.eventsFromSchemas(Begin, Cancel)
68
+ const InternalEvents = Machine.internalEventsFromSchemas(AutoFinish)
69
+ const Emissions = Machine.emittedEventsFromSchemas(Planned)
70
+ const States = Machine.state({
71
+ initial: "Idle",
72
+ states: {
73
+ Idle,
74
+ Working,
75
+ Finished: { schema: Finished, type: "final", output: Schema.String }
76
+ }
74
77
  })
75
78
 
76
79
  export const plannerMachine = Machine.make({
77
80
  id: "planner-example",
78
- states: States.states,
81
+ root: States,
79
82
  events: Events,
80
83
  internalEvents: InternalEvents,
81
84
  emittedEvents: Emissions,
@@ -109,37 +112,40 @@ export const plannerMachine = Machine.make({
109
112
  })
110
113
  )
111
114
  }),
112
- initial: (to) => to.Idle().resolve(({ input, target }) => target.decoded(new Idle({ owner: input.owner })))
115
+ initialConfiguration: (root) =>
116
+ root.resolve(({ input, target }) => target.from((to) => to.Idle.decoded(new Idle({ owner: input.owner }))))
113
117
  }).handle({
114
- Idle: {
115
- on: {
116
- Begin: (to) =>
117
- to.branches({
118
- urgent: { title: "Finish immediately", target: to.full.Working() },
119
- normal: { title: "Wait in working", target: to.full.Working() }
120
- }).resolve(({ event, self, select, state }, enqueue) => {
121
- enqueue.emit(new Planned({ job: event.job }))
122
- enqueue.sendTo(self, Events.Cancel({ reason: "planner command example" }))
123
- if (event.priority === "urgent") enqueue.raise(InternalEvents.AutoFinish())
124
- const working = new Working({ owner: state.owner, job: event.job })
125
- return event.priority === "urgent"
126
- ? select.urgent.decoded(working)
127
- : select.normal.decoded(working)
128
- })
129
- }
130
- },
131
- Working: {
132
- invoke: (from) => from.effect("monitor-job", () => Effect.never),
133
- on: {
134
- AutoFinish: (to) =>
135
- to.full.Finished().resolve(({ state, target }) => target.decoded(new Finished({ job: state.job }))),
136
- Cancel: (to) =>
137
- to.full.Idle().resolve(({ event, state, target }) =>
138
- target.decoded(new Idle({ owner: `${state.owner} · ${event.reason}` }))
139
- )
118
+ states: {
119
+ Idle: {
120
+ 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) => {
126
+ enqueue.emit(new Planned({ job: event.job }))
127
+ enqueue.sendTo(self, Events.Cancel({ reason: "planner command example" }))
128
+ if (event.priority === "urgent") enqueue.raise(InternalEvents.AutoFinish())
129
+ const working = new Working({ owner: state.owner, job: event.job })
130
+ return event.priority === "urgent"
131
+ ? select.urgent.decoded(working)
132
+ : select.normal.decoded(working)
133
+ })
134
+ }
135
+ },
136
+ Working: {
137
+ invoke: (from) => from.effect("monitor-job", () => Effect.never),
138
+ 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
+ )
145
+ }
146
+ },
147
+ Finished: {
148
+ output: ({ state }) => state.job
140
149
  }
141
- },
142
- Finished: {
143
- output: ({ state }) => state.job
144
150
  }
145
151
  })