@typeonce/effect-machine-devtools 0.33.0 → 0.35.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.
@@ -1,70 +1,82 @@
1
1
  import { Machine } from "@typeonce/effect-machine"
2
2
  import { Schema } from "effect"
3
-
4
3
  class Cart extends Schema.TaggedClass<Cart>("ParallelCart")("Cart", {
5
4
  items: Schema.Number
6
- }) {}
5
+ }) {
6
+ }
7
7
  class Order extends Schema.TaggedClass<Order>("ParallelOrder")("Order", {
8
8
  orderId: Schema.String,
9
9
  total: Schema.Number
10
- }) {}
10
+ }) {
11
+ }
11
12
  class Payment extends Schema.TaggedClass<Payment>("ParallelPayment")("Payment", {
12
13
  attempts: Schema.Number
13
- }) {}
14
- class AwaitingAuthorization extends Schema.TaggedClass<AwaitingAuthorization>("ParallelAwaitingAuthorization")(
15
- "AwaitingAuthorization",
16
- {}
17
- ) {}
14
+ }) {
15
+ }
16
+ class AwaitingAuthorization
17
+ extends Schema.TaggedClass<AwaitingAuthorization>("ParallelAwaitingAuthorization")("AwaitingAuthorization", {})
18
+ {
19
+ }
18
20
  class Authorized extends Schema.TaggedClass<Authorized>("ParallelAuthorized")("Authorized", {
19
21
  authorizationId: Schema.String
20
- }) {}
22
+ }) {
23
+ }
21
24
  class Fulfillment extends Schema.TaggedClass<Fulfillment>("ParallelFulfillment")("Fulfillment", {
22
25
  warehouse: Schema.String
23
- }) {}
24
- class WaitingForPayment extends Schema.TaggedClass<WaitingForPayment>("ParallelWaitingForPayment")(
25
- "WaitingForPayment",
26
- {}
27
- ) {}
26
+ }) {
27
+ }
28
+ class WaitingForPayment
29
+ extends Schema.TaggedClass<WaitingForPayment>("ParallelWaitingForPayment")("WaitingForPayment", {})
30
+ {
31
+ }
28
32
  class Packing extends Schema.TaggedClass<Packing>("ParallelPacking")("Packing", {
29
33
  packageCount: Schema.Number
30
- }) {}
34
+ }) {
35
+ }
31
36
  class Shipped extends Schema.TaggedClass<Shipped>("ParallelShipped")("Shipped", {
32
37
  trackingCode: Schema.String
33
- }) {}
38
+ }) {
39
+ }
34
40
  class OrderComplete extends Schema.TaggedClass<OrderComplete>("ParallelOrderComplete")("OrderComplete", {
35
41
  orderId: Schema.String
36
- }) {}
37
- class OrderCancelled extends Schema.TaggedClass<OrderCancelled>("ParallelOrderCancelled")(
38
- "OrderCancelled",
39
- { reason: Schema.String }
40
- ) {}
41
-
42
+ }) {
43
+ }
44
+ class OrderCancelled
45
+ extends Schema.TaggedClass<OrderCancelled>("ParallelOrderCancelled")("OrderCancelled", { reason: Schema.String })
46
+ {
47
+ }
42
48
  class Checkout extends Schema.TaggedClass<Checkout>("ParallelCheckout")("Checkout", {
43
49
  orderId: Schema.String,
44
50
  total: Schema.Number
45
- }) {}
51
+ }) {
52
+ }
46
53
  class Authorize extends Schema.TaggedClass<Authorize>("ParallelAuthorize")("Authorize", {
47
54
  authorizationId: Schema.String
48
- }) {}
49
- class DeclinePayment extends Schema.TaggedClass<DeclinePayment>("ParallelDeclinePayment")(
50
- "DeclinePayment",
51
- { reason: Schema.String }
52
- ) {}
53
- class Pack extends Schema.TaggedClass<Pack>("ParallelPack")("Pack", { packages: Schema.Number }) {}
54
- class Ship extends Schema.TaggedClass<Ship>("ParallelShip")("Ship", { trackingCode: Schema.String }) {}
55
+ }) {
56
+ }
57
+ class DeclinePayment
58
+ extends Schema.TaggedClass<DeclinePayment>("ParallelDeclinePayment")("DeclinePayment", { reason: Schema.String })
59
+ {
60
+ }
61
+ class Pack extends Schema.TaggedClass<Pack>("ParallelPack")("Pack", { packages: Schema.Number }) {
62
+ }
63
+ class Ship extends Schema.TaggedClass<Ship>("ParallelShip")("Ship", { trackingCode: Schema.String }) {
64
+ }
55
65
  class CompleteAll extends Schema.TaggedClass<CompleteAll>("ParallelCompleteAll")("CompleteAll", {
56
66
  authorizationId: Schema.String,
57
67
  trackingCode: Schema.String
58
- }) {}
68
+ }) {
69
+ }
59
70
  class CancelOrder extends Schema.TaggedClass<CancelOrder>("ParallelCancelOrder")("CancelOrder", {
60
71
  reason: Schema.String
61
- }) {}
62
- class RetryOrder extends Schema.TaggedClass<RetryOrder>("ParallelRetryOrder")("RetryOrder", {}) {}
63
- class AutoShip extends Schema.TaggedClass<AutoShip>("ParallelAutoShip")("AutoShip", {}) {}
64
-
72
+ }) {
73
+ }
74
+ class RetryOrder extends Schema.TaggedClass<RetryOrder>("ParallelRetryOrder")("RetryOrder", {}) {
75
+ }
76
+ class AutoShip extends Schema.TaggedClass<AutoShip>("ParallelAutoShip")("AutoShip", {}) {
77
+ }
65
78
  const ParallelInternalEvents = Machine.internalEventsFromSchemas(AutoShip)
66
79
  const ParallelStates = Machine.state({
67
- initial: "Cart",
68
80
  states: {
69
81
  Cart,
70
82
  Order: {
@@ -73,7 +85,6 @@ const ParallelStates = Machine.state({
73
85
  states: {
74
86
  payment: {
75
87
  schema: Payment,
76
- initial: "AwaitingAuthorization",
77
88
  states: {
78
89
  AwaitingAuthorization,
79
90
  Authorized: { schema: Authorized, type: "final" }
@@ -81,7 +92,6 @@ const ParallelStates = Machine.state({
81
92
  },
82
93
  fulfillment: {
83
94
  schema: Fulfillment,
84
- initial: "WaitingForPayment",
85
95
  states: {
86
96
  WaitingForPayment,
87
97
  Packing,
@@ -94,8 +104,9 @@ const ParallelStates = Machine.state({
94
104
  Cancelled: OrderCancelled
95
105
  }
96
106
  })
97
-
107
+ const targets1 = Machine.targets(ParallelStates)
98
108
  export const parallelCompletionMachine = Machine.make({
109
+ timers: { source1: "5 seconds" },
99
110
  id: "parallel-completion",
100
111
  root: ParallelStates,
101
112
  events: Machine.eventsFromSchemas(
@@ -108,90 +119,119 @@ export const parallelCompletionMachine = Machine.make({
108
119
  CancelOrder,
109
120
  RetryOrder
110
121
  ),
111
- internalEvents: ParallelInternalEvents,
112
- initialConfiguration: (root) =>
113
- root.resolve(({ target }) => target.from((to) => to.Cart.decoded(new Cart({ items: 2 }))))
122
+ internalEvents: ParallelInternalEvents
114
123
  }).handle({
124
+ initial: {
125
+ target: Machine.targets(ParallelStates).root.Cart,
126
+ decoded: true,
127
+ data: new Cart({ items: 2 })
128
+ },
115
129
  states: {
116
130
  Cart: {
117
131
  on: {
118
- Checkout: (to) =>
119
- to.branch.Order.initial.resolve(({ event, target }) =>
120
- target.decoded(new Order({ orderId: event.orderId, total: event.total }))
121
- )
132
+ Checkout: {
133
+ initial: targets1.root.Order,
134
+ decoded: true,
135
+ data: ({ event }) => (new Order({ orderId: event.orderId, total: event.total }))
136
+ }
122
137
  }
123
138
  },
124
139
  Order: {
125
- initialize: ({ builder }) => builder.payment.from({ attempts: 0 }).fulfillment.from({ warehouse: "north" }),
140
+ initial: { payment: ({}) => ({ attempts: 0 }), fulfillment: ({}) => ({ warehouse: "north" }) },
126
141
  on: {
127
- CancelOrder: (to) =>
128
- to.branch.Cancelled().resolve(({ event, target }) =>
129
- target.decoded(new OrderCancelled({ reason: event.reason }))
130
- )
142
+ CancelOrder: {
143
+ target: targets1.root.Cancelled,
144
+ decoded: true,
145
+ data: ({ event }) => (new OrderCancelled({ reason: event.reason }))
146
+ }
147
+ },
148
+ onDone: {
149
+ target: targets1.root.Complete,
150
+ decoded: true,
151
+ data: () => (new OrderComplete({ orderId: "completed-order" }))
131
152
  },
132
- onDone: (to) =>
133
- to.branch.Complete().resolve(({ target }) => target.decoded(new OrderComplete({ orderId: "completed-order" }))),
134
153
  states: {
135
154
  payment: {
136
- initialize: ({ builder }) => builder.from(),
155
+ initial: {
156
+ target: Machine.targets(ParallelStates).root.Order.payment.AwaitingAuthorization
157
+ },
137
158
  states: {
138
159
  AwaitingAuthorization: {
139
160
  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
- )
161
+ Authorize: {
162
+ target: targets1.root.Order.payment.Authorized,
163
+ decoded: true,
164
+ data: ({ event }) => (new Authorized({ authorizationId: event.authorizationId }))
165
+ },
166
+ CompleteAll: {
167
+ target: targets1.root.Order.payment.Authorized,
168
+ decoded: true,
169
+ data: ({ event }) => (new Authorized({ authorizationId: event.authorizationId }))
170
+ },
171
+ DeclinePayment: {
172
+ target: targets1.root.Cancelled,
173
+ decoded: true,
174
+ data: ({ event }) => (new OrderCancelled({ reason: event.reason }))
175
+ }
152
176
  }
153
- }
177
+ },
178
+ Authorized: {}
154
179
  }
155
180
  },
156
181
  fulfillment: {
157
- initialize: ({ builder }) => builder.from(),
182
+ initial: {
183
+ target: Machine.targets(ParallelStates).root.Order.fulfillment.WaitingForPayment
184
+ },
158
185
  states: {
159
186
  WaitingForPayment: {
160
187
  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
- )
188
+ Authorize: {
189
+ target: targets1.root.Order.fulfillment.Packing,
190
+ decoded: true,
191
+ data: () => (new Packing({ packageCount: 1 }))
192
+ },
193
+ Pack: {
194
+ target: targets1.root.Order.fulfillment.Packing,
195
+ decoded: true,
196
+ data: ({ event }) => (new Packing({ packageCount: event.packages }))
197
+ },
198
+ CompleteAll: {
199
+ target: targets1.root.Order.fulfillment.Shipped,
200
+ decoded: true,
201
+ data: ({ event }) => (new Shipped({ trackingCode: event.trackingCode }))
202
+ }
171
203
  }
172
204
  },
173
205
  Packing: {
174
- invoke: (from) =>
175
- from.timer("packing-sla", "5 seconds").onDone((to) =>
176
- to.none.resolve((_, enqueue) => {
206
+ invoke: {
207
+ src: "source1",
208
+ id: "packing-sla",
209
+ onDone: {
210
+ none: true,
211
+ resolve: (_, enqueue) => {
177
212
  enqueue.raise(ParallelInternalEvents.AutoShip())
178
- })
179
- ),
213
+ }
214
+ }
215
+ },
180
216
  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
- )
217
+ AutoShip: {
218
+ target: targets1.root.Order.fulfillment.Shipped,
219
+ decoded: true,
220
+ data: () => (new Shipped({ trackingCode: "automatic" }))
221
+ },
222
+ Ship: {
223
+ target: targets1.root.Order.fulfillment.Shipped,
224
+ decoded: true,
225
+ data: ({ event }) => (new Shipped({ trackingCode: event.trackingCode }))
226
+ },
227
+ CompleteAll: {
228
+ target: targets1.root.Order.fulfillment.Shipped,
229
+ decoded: true,
230
+ data: ({ event }) => (new Shipped({ trackingCode: event.trackingCode }))
231
+ }
193
232
  }
194
- }
233
+ },
234
+ Shipped: {}
195
235
  }
196
236
  }
197
237
  }
@@ -201,8 +241,11 @@ export const parallelCompletionMachine = Machine.make({
201
241
  },
202
242
  Cancelled: {
203
243
  on: {
204
- RetryOrder: (to) =>
205
- to.branch.Order.initial.resolve(({ target }) => target.decoded(new Order({ orderId: "retry", total: 0 })))
244
+ RetryOrder: {
245
+ initial: targets1.root.Order,
246
+ decoded: true,
247
+ data: () => (new Order({ orderId: "retry", total: 0 }))
248
+ }
206
249
  }
207
250
  }
208
251
  }
@@ -1,44 +1,74 @@
1
1
  import { Machine } from "@typeonce/effect-machine"
2
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
-
3
+ class Print extends Schema.TaggedClass<Print>("ParallelOwnerPrint")("Print", {}) {
4
+ }
5
+ class Options extends Schema.TaggedClass<Options>("ParallelOwnerOptions")("Options", {}) {
6
+ }
7
+ class OptionsReady extends Schema.TaggedClass<OptionsReady>("ParallelOwnerOptionsReady")("Ready", {}) {
8
+ }
9
+ class Operation extends Schema.TaggedClass<Operation>("ParallelOwnerOperation")("Operation", {}) {
10
+ }
11
+ class Active extends Schema.TaggedClass<Active>("ParallelOwnerActive")("Active", {}) {
12
+ }
13
+ class Printing extends Schema.TaggedClass<Printing>("ParallelOwnerPrinting")("Printing", {}) {
14
+ }
15
+ class ChildIdle extends Schema.TaggedClass<ChildIdle>("ParallelOwnerChildIdle")("Idle", {}) {
16
+ }
17
+ class ChildSignal extends Schema.TaggedClass<ChildSignal>("ParallelOwnerChildSignal")("ChildSignal", {}) {
18
+ }
19
+ class PrintRequested extends Schema.TaggedClass<PrintRequested>("ParallelOwnerPrintRequested")("PrintRequested", {}) {
20
+ }
17
21
  const ChildParentEvents = Machine.eventsFromSchemas(ChildSignal)
18
- const ParallelOwnerChildStates = Machine.state({ initial: "Idle", states: { Idle: ChildIdle } })
19
-
22
+ const ParallelOwnerChildStates = Machine.state({ states: { Idle: ChildIdle } })
20
23
  export const parallelOwnerRoutingChildMachine = Machine.make({
24
+ effects: {
25
+ source1: ({ parent }: Machine.Machine.InvokeContext<
26
+ {
27
+ readonly "": {
28
+ readonly initial: "Idle"
29
+ readonly states: {
30
+ readonly Idle: typeof ChildIdle
31
+ }
32
+ } & {
33
+ readonly "~effect/Machine/ExplicitInitial": true
34
+ }
35
+ },
36
+ readonly [],
37
+ readonly [],
38
+ "Idle",
39
+ readonly [],
40
+ Machine.Machine.ParentEventSchemas<
41
+ "required",
42
+ readonly [
43
+ typeof ChildSignal
44
+ ]
45
+ >
46
+ >) => parent.send(ChildParentEvents.ChildSignal())
47
+ },
21
48
  id: "parallel-owner-routing-child",
22
49
  root: ParallelOwnerChildStates,
23
50
  events: Machine.eventsFromSchemas(),
24
- parent: Machine.parent(ChildParentEvents),
25
- initialConfiguration: (root) => root.resolve(({ target }) => target.from((to) => to.Idle.decoded(new ChildIdle({}))))
51
+ parent: Machine.parent(ChildParentEvents)
26
52
  }).handle({
53
+ initial: {
54
+ target: Machine.targets(ParallelOwnerChildStates).root.Idle,
55
+ decoded: true,
56
+ data: new ChildIdle({})
57
+ },
27
58
  states: {
28
59
  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)
60
+ invoke: {
61
+ src: "source1",
62
+ id: "signal-parent",
63
+ input: (context) => context,
64
+ onDone: { none: true },
65
+ onFailure: { none: true }
66
+ }
34
67
  }
35
68
  }
36
69
  })
37
-
38
70
  const ParallelOwnerChild = Machine.child("parallel-owner-child", parallelOwnerRoutingChildMachine)
39
-
40
71
  const ParallelOwnerStates = Machine.state({
41
- initial: "Print",
42
72
  states: {
43
73
  Print: {
44
74
  schema: Print,
@@ -46,14 +76,12 @@ const ParallelOwnerStates = Machine.state({
46
76
  states: {
47
77
  Options: {
48
78
  schema: Options,
49
- initial: "Ready",
50
79
  states: {
51
80
  Ready: OptionsReady
52
81
  }
53
82
  },
54
83
  Operation: {
55
84
  schema: Operation,
56
- initial: "Active",
57
85
  states: {
58
86
  Active,
59
87
  Printing
@@ -63,40 +91,51 @@ const ParallelOwnerStates = Machine.state({
63
91
  }
64
92
  }
65
93
  })
66
-
94
+ const targets2 = Machine.targets(ParallelOwnerStates)
67
95
  export const parallelOwnerRoutingMachine = Machine.make({
96
+ children: { source1: ParallelOwnerChild },
68
97
  id: "parallel-owner-routing",
69
98
  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
- )
99
+ events: Machine.eventsFromSchemas(PrintRequested, ChildParentEvents)
82
100
  }).handle({
101
+ initial: {
102
+ target: Machine.targets(ParallelOwnerStates).root.Print,
103
+ decoded: true,
104
+ data: new Print({})
105
+ },
83
106
  states: {
84
107
  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
- ),
108
+ initial: { Options: ({}) => ({}), Operation: ({}) => ({}) },
109
+ invoke: {
110
+ src: "source1",
111
+ onFailure: { target: targets2.root.Print.Operation.Active, decoded: true, data: () => (new Active({})) }
112
+ },
90
113
  on: {
91
- PrintRequested: (to) =>
92
- to.branch.Print.Operation.Printing().resolve(({ target }) => target.decoded(new Printing({})))
114
+ PrintRequested: {
115
+ target: targets2.root.Print.Operation.Printing,
116
+ decoded: true,
117
+ data: () => (new Printing({}))
118
+ }
93
119
  },
94
120
  states: {
95
121
  Options: {
96
- initialize: ({ builder }) => builder.from({})
122
+ initial: {
123
+ target: Machine.targets(ParallelOwnerStates).root.Print.Options.Ready,
124
+ data: ({}) => ({})
125
+ },
126
+ states: {
127
+ Ready: {}
128
+ }
97
129
  },
98
130
  Operation: {
99
- initialize: ({ builder }) => builder.from({})
131
+ initial: {
132
+ target: Machine.targets(ParallelOwnerStates).root.Print.Operation.Active,
133
+ data: ({}) => ({})
134
+ },
135
+ states: {
136
+ Active: {},
137
+ Printing: {}
138
+ }
100
139
  }
101
140
  }
102
141
  }