@typeonce/effect-machine-devtools 0.34.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.
- package/package.json +2 -2
- package/src/internal/browser/example-machine.ts +79 -42
- package/src/internal/browser/hierarchy-routing-example.ts +16 -25
- package/src/internal/browser/invoke-outcomes-example.ts +100 -74
- package/src/internal/browser/layout-resilience-example.ts +48 -41
- package/src/internal/browser/parallel-completion-example.ts +97 -61
- package/src/internal/browser/parallel-owner-routing-example.ts +66 -48
- package/src/internal/browser/planner-example.ts +63 -20
- package/src/internal/browser/protocol-events-example.ts +95 -73
- package/src/internal/browser/shared-terminal-routing-example.ts +9 -15
- package/src/internal/browser/transition-semantics-example.ts +105 -66
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { Machine } from "@typeonce/effect-machine"
|
|
2
2
|
import { Effect, Schema } from "effect"
|
|
3
|
-
|
|
4
3
|
const Mode = Schema.Literals(["login", "signup"])
|
|
5
4
|
const LoginMethod = Schema.Literals(["code", "password"])
|
|
6
|
-
|
|
7
5
|
const AuthState = Schema.TaggedUnion({
|
|
8
6
|
Editing: {
|
|
9
7
|
mode: Mode,
|
|
@@ -19,16 +17,12 @@ const AuthState = Schema.TaggedUnion({
|
|
|
19
17
|
},
|
|
20
18
|
Navigating: { href: Schema.String }
|
|
21
19
|
})
|
|
22
|
-
|
|
23
20
|
const AuthStates = Machine.state({
|
|
24
|
-
initial: "Editing",
|
|
25
21
|
states: {
|
|
26
22
|
Editing: {
|
|
27
23
|
schema: AuthState.cases.Editing,
|
|
28
|
-
initial: "Form",
|
|
29
24
|
states: {
|
|
30
25
|
Form: {
|
|
31
|
-
initial: "Ready",
|
|
32
26
|
states: {
|
|
33
27
|
Ready: {},
|
|
34
28
|
Failed: AuthState.cases.EditingFailed
|
|
@@ -40,7 +34,6 @@ const AuthStates = Machine.state({
|
|
|
40
34
|
},
|
|
41
35
|
Verification: {
|
|
42
36
|
schema: AuthState.cases.Verification,
|
|
43
|
-
initial: "CodeEntry",
|
|
44
37
|
states: {
|
|
45
38
|
CodeEntry: {}
|
|
46
39
|
}
|
|
@@ -48,16 +41,12 @@ const AuthStates = Machine.state({
|
|
|
48
41
|
Navigating: AuthState.cases.Navigating
|
|
49
42
|
}
|
|
50
43
|
})
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
Submit: { route: Schema.Literals(["verification", "login", "invalid"]) }
|
|
58
|
-
})
|
|
59
|
-
)
|
|
60
|
-
|
|
44
|
+
const AuthEvents = Machine.eventsFromSchemas(Schema.TaggedUnion({
|
|
45
|
+
EmailChanged: { value: Schema.String },
|
|
46
|
+
LoginMethodChanged: { value: LoginMethod },
|
|
47
|
+
PasswordChanged: { value: Schema.String },
|
|
48
|
+
Submit: { route: Schema.Literals(["verification", "login", "invalid"]) }
|
|
49
|
+
}))
|
|
61
50
|
const targets1 = Machine.targets(AuthStates)
|
|
62
51
|
export const layoutResilienceMachine = Machine.make({
|
|
63
52
|
branches: {
|
|
@@ -71,53 +60,68 @@ export const layoutResilienceMachine = Machine.make({
|
|
|
71
60
|
}
|
|
72
61
|
},
|
|
73
62
|
effects: {
|
|
74
|
-
source1: ({ containingState }: {
|
|
63
|
+
source1: ({ containingState }: {
|
|
64
|
+
readonly containingState: {
|
|
65
|
+
readonly email: string
|
|
66
|
+
}
|
|
67
|
+
}) =>
|
|
75
68
|
containingState.email === "fail"
|
|
76
69
|
? Effect.fail("invalid credentials")
|
|
77
70
|
: Effect.succeed("/creator"),
|
|
78
|
-
source2: ({ containingState }: {
|
|
71
|
+
source2: ({ containingState }: {
|
|
72
|
+
readonly containingState: {
|
|
73
|
+
readonly email: string
|
|
74
|
+
}
|
|
75
|
+
}) =>
|
|
79
76
|
containingState.email === "fail"
|
|
80
77
|
? Effect.fail("verification unavailable")
|
|
81
78
|
: Effect.succeed(undefined)
|
|
82
79
|
},
|
|
83
|
-
|
|
84
80
|
id: "layout-resilience",
|
|
85
81
|
root: AuthStates,
|
|
86
|
-
events: AuthEvents
|
|
87
|
-
initialConfiguration: (root) =>
|
|
88
|
-
root.resolve(({ target }) =>
|
|
89
|
-
target.from((to) =>
|
|
90
|
-
to.Editing.from({
|
|
91
|
-
mode: "login",
|
|
92
|
-
email: "",
|
|
93
|
-
loginMethod: "code",
|
|
94
|
-
password: ""
|
|
95
|
-
}, (editing) => editing.Form.from((form) => form.Ready.from()))
|
|
96
|
-
)
|
|
97
|
-
)
|
|
82
|
+
events: AuthEvents
|
|
98
83
|
}).handle({
|
|
84
|
+
initial: {
|
|
85
|
+
target: Machine.targets(AuthStates).root.Editing,
|
|
86
|
+
data: {
|
|
87
|
+
mode: "login",
|
|
88
|
+
email: "",
|
|
89
|
+
loginMethod: "code",
|
|
90
|
+
password: ""
|
|
91
|
+
}
|
|
92
|
+
},
|
|
99
93
|
states: {
|
|
100
94
|
Editing: {
|
|
95
|
+
initial: {
|
|
96
|
+
target: Machine.targets(AuthStates).root.Editing.Form
|
|
97
|
+
},
|
|
101
98
|
states: {
|
|
102
99
|
Form: {
|
|
100
|
+
initial: {
|
|
101
|
+
target: Machine.targets(AuthStates).root.Editing.Form.Ready
|
|
102
|
+
},
|
|
103
103
|
on: {
|
|
104
104
|
EmailChanged: {
|
|
105
105
|
update: targets1.root.Editing,
|
|
106
|
-
|
|
106
|
+
data: ({ ancestors: { "Editing": current }, event }) => ({ ...current, email: event.value })
|
|
107
107
|
},
|
|
108
108
|
LoginMethodChanged: {
|
|
109
109
|
update: targets1.root.Editing,
|
|
110
|
-
|
|
110
|
+
data: ({ ancestors: { "Editing": current }, event }) => ({ ...current, loginMethod: event.value })
|
|
111
111
|
},
|
|
112
112
|
PasswordChanged: {
|
|
113
113
|
update: targets1.root.Editing,
|
|
114
|
-
|
|
114
|
+
data: ({ ancestors: { "Editing": current }, event }) => ({ ...current, password: event.value })
|
|
115
115
|
},
|
|
116
116
|
Submit: {
|
|
117
117
|
branches: "transition4",
|
|
118
118
|
resolve: ({ event, select }) => {
|
|
119
|
-
if (event.route === "login")
|
|
120
|
-
|
|
119
|
+
if (event.route === "login") {
|
|
120
|
+
return select.login.from()
|
|
121
|
+
}
|
|
122
|
+
if (event.route === "verification") {
|
|
123
|
+
return select.requestVerification.from()
|
|
124
|
+
}
|
|
121
125
|
return select.invalid.from({ message: "Enter valid authentication details." })
|
|
122
126
|
}
|
|
123
127
|
}
|
|
@@ -132,10 +136,10 @@ export const layoutResilienceMachine = Machine.make({
|
|
|
132
136
|
src: "source1",
|
|
133
137
|
id: "submit-login",
|
|
134
138
|
input: (context) => context,
|
|
135
|
-
onDone: { target: targets1.root.Navigating,
|
|
139
|
+
onDone: { target: targets1.root.Navigating, data: ({ output }) => ({ href: output }) },
|
|
136
140
|
onFailure: {
|
|
137
141
|
target: targets1.root.Editing.Form.Failed,
|
|
138
|
-
|
|
142
|
+
data: () => ({ message: "Email or password is incorrect." })
|
|
139
143
|
}
|
|
140
144
|
}
|
|
141
145
|
},
|
|
@@ -146,7 +150,7 @@ export const layoutResilienceMachine = Machine.make({
|
|
|
146
150
|
input: (context) => context,
|
|
147
151
|
onDone: {
|
|
148
152
|
initial: targets1.root.Verification,
|
|
149
|
-
|
|
153
|
+
data: ({ containingState }) => ({
|
|
150
154
|
mode: containingState.mode,
|
|
151
155
|
email: containingState.email,
|
|
152
156
|
code: ""
|
|
@@ -154,13 +158,16 @@ export const layoutResilienceMachine = Machine.make({
|
|
|
154
158
|
},
|
|
155
159
|
onFailure: {
|
|
156
160
|
target: targets1.root.Editing.Form.Failed,
|
|
157
|
-
|
|
161
|
+
data: () => ({ message: "The verification code could not be sent." })
|
|
158
162
|
}
|
|
159
163
|
}
|
|
160
164
|
}
|
|
161
165
|
}
|
|
162
166
|
},
|
|
163
167
|
Verification: {
|
|
168
|
+
initial: {
|
|
169
|
+
target: Machine.targets(AuthStates).root.Verification.CodeEntry
|
|
170
|
+
},
|
|
164
171
|
states: {
|
|
165
172
|
CodeEntry: {}
|
|
166
173
|
}
|
|
@@ -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
|
-
|
|
15
|
-
|
|
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
|
-
|
|
25
|
-
|
|
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
|
-
|
|
38
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
{ reason: Schema.String }
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
class
|
|
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
|
-
|
|
63
|
-
class
|
|
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,11 +104,9 @@ const ParallelStates = Machine.state({
|
|
|
94
104
|
Cancelled: OrderCancelled
|
|
95
105
|
}
|
|
96
106
|
})
|
|
97
|
-
|
|
98
107
|
const targets1 = Machine.targets(ParallelStates)
|
|
99
108
|
export const parallelCompletionMachine = Machine.make({
|
|
100
109
|
timers: { source1: "5 seconds" },
|
|
101
|
-
|
|
102
110
|
id: "parallel-completion",
|
|
103
111
|
root: ParallelStates,
|
|
104
112
|
events: Machine.eventsFromSchemas(
|
|
@@ -111,66 +119,86 @@ export const parallelCompletionMachine = Machine.make({
|
|
|
111
119
|
CancelOrder,
|
|
112
120
|
RetryOrder
|
|
113
121
|
),
|
|
114
|
-
internalEvents: ParallelInternalEvents
|
|
115
|
-
initialConfiguration: (root) =>
|
|
116
|
-
root.resolve(({ target }) => target.from((to) => to.Cart.decoded(new Cart({ items: 2 }))))
|
|
122
|
+
internalEvents: ParallelInternalEvents
|
|
117
123
|
}).handle({
|
|
124
|
+
initial: {
|
|
125
|
+
target: Machine.targets(ParallelStates).root.Cart,
|
|
126
|
+
decoded: true,
|
|
127
|
+
data: new Cart({ items: 2 })
|
|
128
|
+
},
|
|
118
129
|
states: {
|
|
119
130
|
Cart: {
|
|
120
131
|
on: {
|
|
121
132
|
Checkout: {
|
|
122
133
|
initial: targets1.root.Order,
|
|
123
|
-
decoded:
|
|
134
|
+
decoded: true,
|
|
135
|
+
data: ({ event }) => (new Order({ orderId: event.orderId, total: event.total }))
|
|
124
136
|
}
|
|
125
137
|
}
|
|
126
138
|
},
|
|
127
139
|
Order: {
|
|
128
|
-
|
|
140
|
+
initial: { payment: ({}) => ({ attempts: 0 }), fulfillment: ({}) => ({ warehouse: "north" }) },
|
|
129
141
|
on: {
|
|
130
142
|
CancelOrder: {
|
|
131
143
|
target: targets1.root.Cancelled,
|
|
132
|
-
decoded:
|
|
144
|
+
decoded: true,
|
|
145
|
+
data: ({ event }) => (new OrderCancelled({ reason: event.reason }))
|
|
133
146
|
}
|
|
134
147
|
},
|
|
135
|
-
onDone: {
|
|
148
|
+
onDone: {
|
|
149
|
+
target: targets1.root.Complete,
|
|
150
|
+
decoded: true,
|
|
151
|
+
data: () => (new OrderComplete({ orderId: "completed-order" }))
|
|
152
|
+
},
|
|
136
153
|
states: {
|
|
137
154
|
payment: {
|
|
138
|
-
|
|
155
|
+
initial: {
|
|
156
|
+
target: Machine.targets(ParallelStates).root.Order.payment.AwaitingAuthorization
|
|
157
|
+
},
|
|
139
158
|
states: {
|
|
140
159
|
AwaitingAuthorization: {
|
|
141
160
|
on: {
|
|
142
161
|
Authorize: {
|
|
143
162
|
target: targets1.root.Order.payment.Authorized,
|
|
144
|
-
decoded:
|
|
163
|
+
decoded: true,
|
|
164
|
+
data: ({ event }) => (new Authorized({ authorizationId: event.authorizationId }))
|
|
145
165
|
},
|
|
146
166
|
CompleteAll: {
|
|
147
167
|
target: targets1.root.Order.payment.Authorized,
|
|
148
|
-
decoded:
|
|
168
|
+
decoded: true,
|
|
169
|
+
data: ({ event }) => (new Authorized({ authorizationId: event.authorizationId }))
|
|
149
170
|
},
|
|
150
171
|
DeclinePayment: {
|
|
151
172
|
target: targets1.root.Cancelled,
|
|
152
|
-
decoded:
|
|
173
|
+
decoded: true,
|
|
174
|
+
data: ({ event }) => (new OrderCancelled({ reason: event.reason }))
|
|
153
175
|
}
|
|
154
176
|
}
|
|
155
|
-
}
|
|
177
|
+
},
|
|
178
|
+
Authorized: {}
|
|
156
179
|
}
|
|
157
180
|
},
|
|
158
181
|
fulfillment: {
|
|
159
|
-
|
|
182
|
+
initial: {
|
|
183
|
+
target: Machine.targets(ParallelStates).root.Order.fulfillment.WaitingForPayment
|
|
184
|
+
},
|
|
160
185
|
states: {
|
|
161
186
|
WaitingForPayment: {
|
|
162
187
|
on: {
|
|
163
188
|
Authorize: {
|
|
164
189
|
target: targets1.root.Order.fulfillment.Packing,
|
|
165
|
-
decoded:
|
|
190
|
+
decoded: true,
|
|
191
|
+
data: () => (new Packing({ packageCount: 1 }))
|
|
166
192
|
},
|
|
167
193
|
Pack: {
|
|
168
194
|
target: targets1.root.Order.fulfillment.Packing,
|
|
169
|
-
decoded:
|
|
195
|
+
decoded: true,
|
|
196
|
+
data: ({ event }) => (new Packing({ packageCount: event.packages }))
|
|
170
197
|
},
|
|
171
198
|
CompleteAll: {
|
|
172
199
|
target: targets1.root.Order.fulfillment.Shipped,
|
|
173
|
-
decoded:
|
|
200
|
+
decoded: true,
|
|
201
|
+
data: ({ event }) => (new Shipped({ trackingCode: event.trackingCode }))
|
|
174
202
|
}
|
|
175
203
|
}
|
|
176
204
|
},
|
|
@@ -188,18 +216,22 @@ export const parallelCompletionMachine = Machine.make({
|
|
|
188
216
|
on: {
|
|
189
217
|
AutoShip: {
|
|
190
218
|
target: targets1.root.Order.fulfillment.Shipped,
|
|
191
|
-
decoded:
|
|
219
|
+
decoded: true,
|
|
220
|
+
data: () => (new Shipped({ trackingCode: "automatic" }))
|
|
192
221
|
},
|
|
193
222
|
Ship: {
|
|
194
223
|
target: targets1.root.Order.fulfillment.Shipped,
|
|
195
|
-
decoded:
|
|
224
|
+
decoded: true,
|
|
225
|
+
data: ({ event }) => (new Shipped({ trackingCode: event.trackingCode }))
|
|
196
226
|
},
|
|
197
227
|
CompleteAll: {
|
|
198
228
|
target: targets1.root.Order.fulfillment.Shipped,
|
|
199
|
-
decoded:
|
|
229
|
+
decoded: true,
|
|
230
|
+
data: ({ event }) => (new Shipped({ trackingCode: event.trackingCode }))
|
|
200
231
|
}
|
|
201
232
|
}
|
|
202
|
-
}
|
|
233
|
+
},
|
|
234
|
+
Shipped: {}
|
|
203
235
|
}
|
|
204
236
|
}
|
|
205
237
|
}
|
|
@@ -209,7 +241,11 @@ export const parallelCompletionMachine = Machine.make({
|
|
|
209
241
|
},
|
|
210
242
|
Cancelled: {
|
|
211
243
|
on: {
|
|
212
|
-
RetryOrder: {
|
|
244
|
+
RetryOrder: {
|
|
245
|
+
initial: targets1.root.Order,
|
|
246
|
+
decoded: true,
|
|
247
|
+
data: () => (new Order({ orderId: "retry", total: 0 }))
|
|
248
|
+
}
|
|
213
249
|
}
|
|
214
250
|
}
|
|
215
251
|
}
|
|
@@ -1,29 +1,35 @@
|
|
|
1
1
|
import { Machine } from "@typeonce/effect-machine"
|
|
2
2
|
import { Schema } from "effect"
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class Options extends Schema.TaggedClass<Options>("ParallelOwnerOptions")("Options", {}) {
|
|
6
|
-
|
|
7
|
-
class
|
|
8
|
-
|
|
9
|
-
class
|
|
10
|
-
|
|
11
|
-
class
|
|
12
|
-
|
|
13
|
-
|
|
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({
|
|
19
|
-
|
|
22
|
+
const ParallelOwnerChildStates = Machine.state({ states: { Idle: ChildIdle } })
|
|
20
23
|
export const parallelOwnerRoutingChildMachine = Machine.make({
|
|
21
24
|
effects: {
|
|
22
|
-
source1: ({
|
|
23
|
-
parent
|
|
24
|
-
}: Machine.Machine.InvokeContext<
|
|
25
|
+
source1: ({ parent }: Machine.Machine.InvokeContext<
|
|
25
26
|
{
|
|
26
|
-
readonly "": {
|
|
27
|
+
readonly "": {
|
|
28
|
+
readonly initial: "Idle"
|
|
29
|
+
readonly states: {
|
|
30
|
+
readonly Idle: typeof ChildIdle
|
|
31
|
+
}
|
|
32
|
+
} & {
|
|
27
33
|
readonly "~effect/Machine/ExplicitInitial": true
|
|
28
34
|
}
|
|
29
35
|
},
|
|
@@ -31,16 +37,24 @@ export const parallelOwnerRoutingChildMachine = Machine.make({
|
|
|
31
37
|
readonly [],
|
|
32
38
|
"Idle",
|
|
33
39
|
readonly [],
|
|
34
|
-
Machine.Machine.ParentEventSchemas<
|
|
40
|
+
Machine.Machine.ParentEventSchemas<
|
|
41
|
+
"required",
|
|
42
|
+
readonly [
|
|
43
|
+
typeof ChildSignal
|
|
44
|
+
]
|
|
45
|
+
>
|
|
35
46
|
>) => parent.send(ChildParentEvents.ChildSignal())
|
|
36
47
|
},
|
|
37
|
-
|
|
38
48
|
id: "parallel-owner-routing-child",
|
|
39
49
|
root: ParallelOwnerChildStates,
|
|
40
50
|
events: Machine.eventsFromSchemas(),
|
|
41
|
-
parent: Machine.parent(ChildParentEvents)
|
|
42
|
-
initialConfiguration: (root) => root.resolve(({ target }) => target.from((to) => to.Idle.decoded(new ChildIdle({}))))
|
|
51
|
+
parent: Machine.parent(ChildParentEvents)
|
|
43
52
|
}).handle({
|
|
53
|
+
initial: {
|
|
54
|
+
target: Machine.targets(ParallelOwnerChildStates).root.Idle,
|
|
55
|
+
decoded: true,
|
|
56
|
+
data: new ChildIdle({})
|
|
57
|
+
},
|
|
44
58
|
states: {
|
|
45
59
|
Idle: {
|
|
46
60
|
invoke: {
|
|
@@ -53,11 +67,8 @@ export const parallelOwnerRoutingChildMachine = Machine.make({
|
|
|
53
67
|
}
|
|
54
68
|
}
|
|
55
69
|
})
|
|
56
|
-
|
|
57
70
|
const ParallelOwnerChild = Machine.child("parallel-owner-child", parallelOwnerRoutingChildMachine)
|
|
58
|
-
|
|
59
71
|
const ParallelOwnerStates = Machine.state({
|
|
60
|
-
initial: "Print",
|
|
61
72
|
states: {
|
|
62
73
|
Print: {
|
|
63
74
|
schema: Print,
|
|
@@ -65,14 +76,12 @@ const ParallelOwnerStates = Machine.state({
|
|
|
65
76
|
states: {
|
|
66
77
|
Options: {
|
|
67
78
|
schema: Options,
|
|
68
|
-
initial: "Ready",
|
|
69
79
|
states: {
|
|
70
80
|
Ready: OptionsReady
|
|
71
81
|
}
|
|
72
82
|
},
|
|
73
83
|
Operation: {
|
|
74
84
|
schema: Operation,
|
|
75
|
-
initial: "Active",
|
|
76
85
|
states: {
|
|
77
86
|
Active,
|
|
78
87
|
Printing
|
|
@@ -82,42 +91,51 @@ const ParallelOwnerStates = Machine.state({
|
|
|
82
91
|
}
|
|
83
92
|
}
|
|
84
93
|
})
|
|
85
|
-
|
|
86
94
|
const targets2 = Machine.targets(ParallelOwnerStates)
|
|
87
95
|
export const parallelOwnerRoutingMachine = Machine.make({
|
|
88
96
|
children: { source1: ParallelOwnerChild },
|
|
89
|
-
|
|
90
97
|
id: "parallel-owner-routing",
|
|
91
98
|
root: ParallelOwnerStates,
|
|
92
|
-
events: Machine.eventsFromSchemas(PrintRequested, ChildParentEvents)
|
|
93
|
-
initialConfiguration: (root) =>
|
|
94
|
-
root.resolve(({ target }) =>
|
|
95
|
-
target.from((to) =>
|
|
96
|
-
to.Print.decoded(
|
|
97
|
-
new Print({}),
|
|
98
|
-
(print) =>
|
|
99
|
-
print.Options.decoded(new Options({}), (options) => options.Ready.decoded(new OptionsReady({}))).Operation
|
|
100
|
-
.decoded(new Operation({}), (operation) => operation.Active.decoded(new Active({})))
|
|
101
|
-
)
|
|
102
|
-
)
|
|
103
|
-
)
|
|
99
|
+
events: Machine.eventsFromSchemas(PrintRequested, ChildParentEvents)
|
|
104
100
|
}).handle({
|
|
101
|
+
initial: {
|
|
102
|
+
target: Machine.targets(ParallelOwnerStates).root.Print,
|
|
103
|
+
decoded: true,
|
|
104
|
+
data: new Print({})
|
|
105
|
+
},
|
|
105
106
|
states: {
|
|
106
107
|
Print: {
|
|
107
|
-
|
|
108
|
+
initial: { Options: ({}) => ({}), Operation: ({}) => ({}) },
|
|
108
109
|
invoke: {
|
|
109
110
|
src: "source1",
|
|
110
|
-
onFailure: { target: targets2.root.Print.Operation.Active, decoded: () => (new Active({})) }
|
|
111
|
+
onFailure: { target: targets2.root.Print.Operation.Active, decoded: true, data: () => (new Active({})) }
|
|
111
112
|
},
|
|
112
113
|
on: {
|
|
113
|
-
PrintRequested: {
|
|
114
|
+
PrintRequested: {
|
|
115
|
+
target: targets2.root.Print.Operation.Printing,
|
|
116
|
+
decoded: true,
|
|
117
|
+
data: () => (new Printing({}))
|
|
118
|
+
}
|
|
114
119
|
},
|
|
115
120
|
states: {
|
|
116
121
|
Options: {
|
|
117
|
-
|
|
122
|
+
initial: {
|
|
123
|
+
target: Machine.targets(ParallelOwnerStates).root.Print.Options.Ready,
|
|
124
|
+
data: ({}) => ({})
|
|
125
|
+
},
|
|
126
|
+
states: {
|
|
127
|
+
Ready: {}
|
|
128
|
+
}
|
|
118
129
|
},
|
|
119
130
|
Operation: {
|
|
120
|
-
|
|
131
|
+
initial: {
|
|
132
|
+
target: Machine.targets(ParallelOwnerStates).root.Print.Operation.Active,
|
|
133
|
+
data: ({}) => ({})
|
|
134
|
+
},
|
|
135
|
+
states: {
|
|
136
|
+
Active: {},
|
|
137
|
+
Printing: {}
|
|
138
|
+
}
|
|
121
139
|
}
|
|
122
140
|
}
|
|
123
141
|
}
|