@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,14 +1,15 @@
1
1
  import { Machine } from "@typeonce/effect-machine"
2
2
  import { Schema } from "effect"
3
3
  import * as Effect from "effect/Effect"
4
-
5
- class Idle extends Schema.TaggedClass<Idle>("PlannerIdle")("Idle", { owner: Schema.String }) {}
4
+ class Idle extends Schema.TaggedClass<Idle>("PlannerIdle")("Idle", { owner: Schema.String }) {
5
+ }
6
6
  class Working extends Schema.TaggedClass<Working>("PlannerWorking")("Working", {
7
7
  owner: Schema.String,
8
8
  job: Schema.String
9
- }) {}
10
- class Finished extends Schema.TaggedClass<Finished>("PlannerFinished")("Finished", { job: Schema.String }) {}
11
-
9
+ }) {
10
+ }
11
+ class Finished extends Schema.TaggedClass<Finished>("PlannerFinished")("Finished", { job: Schema.String }) {
12
+ }
12
13
  const Owner = Schema.NonEmptyString.annotate({
13
14
  title: "Owner",
14
15
  description: "A non-empty name carried into the initial Idle state."
@@ -33,7 +34,6 @@ const Route = Schema.Union([
33
34
  title: "Route",
34
35
  description: "Choose a literal direct route or provide a queue."
35
36
  })
36
-
37
37
  class Begin extends Schema.TaggedClass<Begin>("PlannerBegin")("Begin", {
38
38
  job: Schema.NonEmptyString.annotate({
39
39
  title: "Job",
@@ -59,24 +59,65 @@ class Begin extends Schema.TaggedClass<Begin>("PlannerBegin")("Begin", {
59
59
  ),
60
60
  labels: Schema.optionalKey(Labels),
61
61
  route: Schema.optionalKey(Route)
62
- }) {}
63
- class Cancel extends Schema.TaggedClass<Cancel>("PlannerCancel")("Cancel", { reason: Schema.String }) {}
64
- class AutoFinish extends Schema.TaggedClass<AutoFinish>("PlannerAutoFinish")("AutoFinish", {}) {}
65
- class Planned extends Schema.TaggedClass<Planned>("PlannerPlanned")("Planned", { job: Schema.String }) {}
66
-
62
+ }) {
63
+ }
64
+ class Cancel extends Schema.TaggedClass<Cancel>("PlannerCancel")("Cancel", { reason: Schema.String }) {
65
+ }
66
+ class AutoFinish extends Schema.TaggedClass<AutoFinish>("PlannerAutoFinish")("AutoFinish", {}) {
67
+ }
68
+ class Planned extends Schema.TaggedClass<Planned>("PlannerPlanned")("Planned", { job: Schema.String }) {
69
+ }
67
70
  const Events = Machine.eventsFromSchemas(Begin, Cancel)
68
71
  const InternalEvents = Machine.internalEventsFromSchemas(AutoFinish)
69
72
  const Emissions = Machine.emittedEventsFromSchemas(Planned)
70
73
  const States = Machine.state({
71
- initial: "Idle",
74
+ fields: {
75
+ input: Schema.toType(Schema.Struct({
76
+ owner: Owner,
77
+ attempts: Attempts,
78
+ notifications: Schema.Boolean.annotate({
79
+ title: "Notifications",
80
+ description: "A required boolean with false as a valid value."
81
+ }),
82
+ mode: Schema.Literals(["guided", "automatic"]).annotate({
83
+ title: "Mode",
84
+ description: "A fixed set of startup modes."
85
+ }),
86
+ note: Schema.optionalKey(
87
+ Schema.String.check(Schema.isMaxLength(40)).annotate({
88
+ title: "Note",
89
+ description: "Optional startup text limited to forty characters."
90
+ })
91
+ ),
92
+ labels: Schema.optionalKey(Labels),
93
+ preferences: Schema.optionalKey(
94
+ Schema.Struct({
95
+ retries: Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 3 })).annotate({
96
+ title: "Retries"
97
+ }),
98
+ dryRun: Schema.Boolean.annotate({ title: "Dry run" })
99
+ }).annotate({
100
+ title: "Preferences",
101
+ description: "An optional nested object."
102
+ })
103
+ )
104
+ }))
105
+ },
72
106
  states: {
73
107
  Idle,
74
108
  Working,
75
109
  Finished: { schema: Finished, type: "final", output: Schema.String }
76
110
  }
77
111
  })
78
-
112
+ const targets1 = Machine.targets(States)
79
113
  export const plannerMachine = Machine.make({
114
+ branches: {
115
+ transition1: {
116
+ urgent: { target: targets1.root.Working, title: "Finish immediately" },
117
+ normal: { target: targets1.root.Working, title: "Wait in working" }
118
+ }
119
+ },
120
+ effects: { source1: Effect.suspend(() => Effect.never) },
80
121
  id: "planner-example",
81
122
  root: States,
82
123
  events: Events,
@@ -111,37 +152,46 @@ export const plannerMachine = Machine.make({
111
152
  description: "An optional nested object."
112
153
  })
113
154
  )
114
- }),
115
- initialConfiguration: (root) =>
116
- root.resolve(({ input, target }) => target.from((to) => to.Idle.decoded(new Idle({ owner: input.owner }))))
155
+ })
117
156
  }).handle({
157
+ initial: {
158
+ target: Machine.targets(States).root.Idle,
159
+ decoded: true,
160
+ data: ({ root: { input: input } }) => new Idle({ owner: input.owner })
161
+ },
162
+ root: ({ input }) => ({ input }),
118
163
  states: {
119
164
  Idle: {
120
165
  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) => {
166
+ Begin: {
167
+ branches: "transition1",
168
+ resolve: ({ event, self, select, state }, enqueue) => {
126
169
  enqueue.emit(new Planned({ job: event.job }))
127
170
  enqueue.sendTo(self, Events.Cancel({ reason: "planner command example" }))
128
- if (event.priority === "urgent") enqueue.raise(InternalEvents.AutoFinish())
171
+ if (event.priority === "urgent") {
172
+ enqueue.raise(InternalEvents.AutoFinish())
173
+ }
129
174
  const working = new Working({ owner: state.owner, job: event.job })
130
175
  return event.priority === "urgent"
131
176
  ? select.urgent.decoded(working)
132
177
  : select.normal.decoded(working)
133
- })
178
+ }
179
+ }
134
180
  }
135
181
  },
136
182
  Working: {
137
- invoke: (from) => from.effect("monitor-job", () => Effect.never),
183
+ invoke: { src: "source1", id: "monitor-job" },
138
184
  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
- )
185
+ AutoFinish: {
186
+ target: targets1.root.Finished,
187
+ decoded: true,
188
+ data: ({ state }) => (new Finished({ job: state.job }))
189
+ },
190
+ Cancel: {
191
+ target: targets1.root.Idle,
192
+ decoded: true,
193
+ data: ({ event, state }) => (new Idle({ owner: `${state.owner} · ${event.reason}` }))
194
+ }
145
195
  }
146
196
  },
147
197
  Finished: {
@@ -1,53 +1,55 @@
1
1
  import { Machine } from "@typeonce/effect-machine"
2
2
  import { Schema } from "effect"
3
-
4
3
  class ChildProgress extends Schema.TaggedClass<ChildProgress>("ProtocolChildProgress")("ChildProgress", {
5
4
  percent: Schema.Number
6
- }) {}
5
+ }) {
6
+ }
7
7
  class ChildFinished extends Schema.TaggedClass<ChildFinished>("ProtocolChildFinished")("ChildFinished", {
8
8
  result: Schema.String
9
- }) {}
9
+ }) {
10
+ }
10
11
  class ChildProblem extends Schema.TaggedClass<ChildProblem>("ProtocolChildProblem")("ChildProblem", {
11
12
  message: Schema.String
12
- }) {}
13
-
13
+ }) {
14
+ }
14
15
  const ParentEvents = Machine.eventsFromSchemas(ChildProgress, ChildFinished, ChildProblem)
15
-
16
- class ChildIdle extends Schema.TaggedClass<ChildIdle>("ProtocolChildIdle")("Idle", {}) {}
16
+ class ChildIdle extends Schema.TaggedClass<ChildIdle>("ProtocolChildIdle")("Idle", {}) {
17
+ }
17
18
  class ChildWorking extends Schema.TaggedClass<ChildWorking>("ProtocolChildWorking")("Working", {
18
19
  job: Schema.String,
19
20
  progress: Schema.Number
20
- }) {}
21
+ }) {
22
+ }
21
23
  class ChildDone extends Schema.TaggedClass<ChildDone>("ProtocolChildDone")("Done", {
22
24
  result: Schema.String
23
- }) {}
25
+ }) {
26
+ }
24
27
  class ChildCancelled extends Schema.TaggedClass<ChildCancelled>("ProtocolChildCancelled")("Cancelled", {
25
28
  reason: Schema.String
26
- }) {}
27
-
29
+ }) {
30
+ }
28
31
  class BeginChildWork extends Schema.TaggedClass<BeginChildWork>("ProtocolBeginChildWork")("BeginChildWork", {
29
32
  job: Schema.String
30
- }) {}
31
- class CancelChildWork extends Schema.TaggedClass<CancelChildWork>("ProtocolCancelChildWork")(
32
- "CancelChildWork",
33
- { reason: Schema.String }
34
- ) {}
33
+ }) {
34
+ }
35
+ class CancelChildWork
36
+ extends Schema.TaggedClass<CancelChildWork>("ProtocolCancelChildWork")("CancelChildWork", { reason: Schema.String })
37
+ {
38
+ }
35
39
  class Heartbeat extends Schema.TaggedClass<Heartbeat>("ProtocolHeartbeat")("Heartbeat", {
36
40
  percent: Schema.Number
37
- }) {}
38
- class CommitChildWork extends Schema.TaggedClass<CommitChildWork>("ProtocolCommitChildWork")(
39
- "CommitChildWork",
40
- {}
41
- ) {}
41
+ }) {
42
+ }
43
+ class CommitChildWork extends Schema.TaggedClass<CommitChildWork>("ProtocolCommitChildWork")("CommitChildWork", {}) {
44
+ }
42
45
  class ChildTrace extends Schema.TaggedClass<ChildTrace>("ProtocolChildTrace")("ChildTrace", {
43
46
  message: Schema.String
44
- }) {}
45
-
47
+ }) {
48
+ }
46
49
  const ChildEvents = Machine.eventsFromSchemas(BeginChildWork, CancelChildWork)
47
50
  const ChildInternalEvents = Machine.internalEventsFromSchemas(Heartbeat, CommitChildWork)
48
51
  const ChildEmissions = Machine.emittedEventsFromSchemas(ChildTrace)
49
52
  const ChildStates = Machine.state({
50
- initial: "Idle",
51
53
  states: {
52
54
  Idle: ChildIdle,
53
55
  Working: ChildWorking,
@@ -55,51 +57,73 @@ const ChildStates = Machine.state({
55
57
  Cancelled: { schema: ChildCancelled, type: "final", output: Schema.String }
56
58
  }
57
59
  })
58
-
60
+ const targets1 = Machine.targets(ChildStates)
59
61
  export const requiredParentChildMachine = Machine.make({
62
+ branches: {
63
+ transition1: { destination: { target: targets1.root.Working } },
64
+ transition3: { destination: { target: targets1.root.Working } }
65
+ },
66
+ effects: {
67
+ source1: ({ parent, state }: {
68
+ readonly parent: Machine.MachineTarget<Machine.Machine.EventInput<ChildProgress | ChildFinished | ChildProblem>>
69
+ readonly state: ChildWorking
70
+ }) => parent.send(ParentEvents.ChildProgress({ percent: state.progress }))
71
+ },
60
72
  id: "required-parent-child",
61
73
  root: ChildStates,
62
74
  events: ChildEvents,
63
75
  internalEvents: ChildInternalEvents,
64
76
  emittedEvents: ChildEmissions,
65
- parent: Machine.parent(ParentEvents),
66
- initialConfiguration: (root) => root.resolve(({ target }) => target.from((to) => to.Idle.decoded(new ChildIdle({}))))
77
+ parent: Machine.parent(ParentEvents)
67
78
  }).handle({
79
+ initial: {
80
+ target: Machine.targets(ChildStates).root.Idle,
81
+ decoded: true,
82
+ data: new ChildIdle({})
83
+ },
68
84
  states: {
69
85
  Idle: {
70
86
  on: {
71
- BeginChildWork: (to) =>
72
- to.branch.Working().resolve(({ event, target }, enqueue) => {
87
+ BeginChildWork: {
88
+ branches: "transition1",
89
+ resolve: ({ event, select: { destination: target } }, enqueue) => {
73
90
  enqueue.raise(ChildInternalEvents.Heartbeat({ percent: 25 }))
74
91
  enqueue.emit(ChildEmissions.ChildTrace({ message: `started ${event.job}` }))
75
92
  return target.decoded(new ChildWorking({ job: event.job, progress: 0 }))
76
- })
93
+ }
94
+ }
77
95
  }
78
96
  },
79
97
  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
- ),
98
+ invoke: {
99
+ src: "source1",
100
+ id: "report-progress-to-parent",
101
+ input: (context) => context,
102
+ onDone: { none: true },
103
+ onFailure: {
104
+ target: targets1.root.Cancelled,
105
+ decoded: true,
106
+ data: ({ error }) => (new ChildCancelled({ reason: String(error) }))
107
+ }
108
+ },
89
109
  on: {
90
- Heartbeat: (to) =>
91
- to.branch.Working().resolve(({ event, state, target }, enqueue) => {
110
+ Heartbeat: {
111
+ branches: "transition3",
112
+ resolve: ({ event, state, select: { destination: target } }, enqueue) => {
92
113
  enqueue.raise(ChildInternalEvents.CommitChildWork())
93
114
  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
- )
115
+ }
116
+ },
117
+ CommitChildWork: {
118
+ target: targets1.root.Done,
119
+ decoded: true,
120
+ data: ({ state }) => (new ChildDone({ result: `${state.job}:complete` }))
121
+ },
122
+ CancelChildWork: {
123
+ target: targets1.root.Cancelled,
124
+ decoded: true,
125
+ data: ({ event }) => (new ChildCancelled({ reason: event.reason }))
126
+ }
103
127
  }
104
128
  },
105
129
  Done: {
@@ -110,24 +134,26 @@ export const requiredParentChildMachine = Machine.make({
110
134
  }
111
135
  }
112
136
  })
113
-
114
137
  const ProtocolChild = Machine.child("protocol-child", requiredParentChildMachine)
115
-
116
- class ParentIdle extends Schema.TaggedClass<ParentIdle>("ProtocolParentIdle")("Idle", {}) {}
138
+ class ParentIdle extends Schema.TaggedClass<ParentIdle>("ProtocolParentIdle")("Idle", {}) {
139
+ }
117
140
  class Supervising extends Schema.TaggedClass<Supervising>("ProtocolSupervising")("Supervising", {
118
141
  latestProgress: Schema.Number
119
- }) {}
142
+ }) {
143
+ }
120
144
  class ParentComplete extends Schema.TaggedClass<ParentComplete>("ProtocolParentComplete")("Complete", {
121
145
  result: Schema.String
122
- }) {}
146
+ }) {
147
+ }
123
148
  class ParentFailed extends Schema.TaggedClass<ParentFailed>("ProtocolParentFailed")("Failed", {
124
149
  message: Schema.String
125
- }) {}
126
- class LaunchChild extends Schema.TaggedClass<LaunchChild>("ProtocolLaunchChild")("LaunchChild", {}) {}
127
- class ResetParent extends Schema.TaggedClass<ResetParent>("ProtocolResetParent")("ResetParent", {}) {}
128
-
150
+ }) {
151
+ }
152
+ class LaunchChild extends Schema.TaggedClass<LaunchChild>("ProtocolLaunchChild")("LaunchChild", {}) {
153
+ }
154
+ class ResetParent extends Schema.TaggedClass<ResetParent>("ProtocolResetParent")("ResetParent", {}) {
155
+ }
129
156
  const ParentStates = Machine.state({
130
- initial: "Idle",
131
157
  states: {
132
158
  Idle: ParentIdle,
133
159
  Supervising,
@@ -135,43 +161,63 @@ const ParentStates = Machine.state({
135
161
  Failed: ParentFailed
136
162
  }
137
163
  })
138
-
164
+ const targets2 = Machine.targets(ParentStates)
139
165
  export const parentProtocolMachine = Machine.make({
166
+ branches: {
167
+ transition1: { destination: { target: targets2.root.Supervising } },
168
+ transition3: { destination: { target: targets2.root.Complete } }
169
+ },
170
+ children: { source1: ProtocolChild },
140
171
  id: "parent-child-protocol",
141
172
  root: ParentStates,
142
- events: Machine.eventsFromSchemas(LaunchChild, ResetParent, ParentEvents),
143
- initialConfiguration: (root) => root.resolve(({ target }) => target.from((to) => to.Idle.decoded(new ParentIdle({}))))
173
+ events: Machine.eventsFromSchemas(LaunchChild, ResetParent, ParentEvents)
144
174
  }).handle({
175
+ initial: {
176
+ target: Machine.targets(ParentStates).root.Idle,
177
+ decoded: true,
178
+ data: new ParentIdle({})
179
+ },
145
180
  states: {
146
181
  Idle: {
147
182
  on: {
148
- LaunchChild: (to) =>
149
- to.branch.Supervising().resolve(({ target }) => target.decoded(new Supervising({ latestProgress: 0 })))
183
+ LaunchChild: {
184
+ target: targets2.root.Supervising,
185
+ decoded: true,
186
+ data: () => (new Supervising({ latestProgress: 0 }))
187
+ }
150
188
  }
151
189
  },
152
190
  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
- ),
191
+ invoke: {
192
+ src: "source1",
193
+ onDone: {
194
+ target: targets2.root.Complete,
195
+ decoded: true,
196
+ data: ({ output }) => (new ParentComplete({ result: output }))
197
+ },
198
+ onFailure: {
199
+ target: targets2.root.Failed,
200
+ decoded: true,
201
+ data: ({ error }) => (new ParentFailed({ message: String(error) }))
202
+ }
203
+ },
161
204
  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({})))
205
+ ChildProgress: {
206
+ target: targets2.root.Supervising,
207
+ decoded: true,
208
+ data: ({ event }) => (new Supervising({ latestProgress: event.percent }))
209
+ },
210
+ ChildFinished: {
211
+ target: targets2.root.Complete,
212
+ decoded: true,
213
+ data: ({ event }) => (new ParentComplete({ result: event.result }))
214
+ },
215
+ ChildProblem: {
216
+ target: targets2.root.Failed,
217
+ decoded: true,
218
+ data: ({ event }) => (new ParentFailed({ message: event.message }))
219
+ },
220
+ ResetParent: { target: targets2.root.Idle, decoded: true, data: () => (new ParentIdle({})) }
175
221
  }
176
222
  },
177
223
  Complete: {
@@ -179,41 +225,47 @@ export const parentProtocolMachine = Machine.make({
179
225
  },
180
226
  Failed: {
181
227
  on: {
182
- ResetParent: (to) => to.branch.Idle().resolve(({ target }) => target.decoded(new ParentIdle({})))
228
+ ResetParent: { target: targets2.root.Idle, decoded: true, data: () => (new ParentIdle({})) }
183
229
  }
184
230
  }
185
231
  }
186
232
  })
187
-
188
- class Detached extends Schema.TaggedClass<Detached>("OptionalParentDetached")("Detached", {}) {}
233
+ class Detached extends Schema.TaggedClass<Detached>("OptionalParentDetached")("Detached", {}) {
234
+ }
189
235
  class Published extends Schema.TaggedClass<Published>("OptionalParentPublished")("Published", {
190
236
  deliveredToParent: Schema.Boolean
191
- }) {}
192
- class PublishOutside extends Schema.TaggedClass<PublishOutside>("OptionalParentPublishOutside")(
193
- "PublishOutside",
194
- { result: Schema.String }
195
- ) {}
196
-
197
- const OptionalParentStates = Machine.state({ initial: "Detached", states: { Detached, Published } })
198
-
237
+ }) {
238
+ }
239
+ class PublishOutside extends Schema.TaggedClass<PublishOutside>("OptionalParentPublishOutside")("PublishOutside", {
240
+ result: Schema.String
241
+ }) {
242
+ }
243
+ const OptionalParentStates = Machine.state({ states: { Detached, Published } })
244
+ const targets3 = Machine.targets(OptionalParentStates)
199
245
  export const optionalParentMachine = Machine.make({
246
+ branches: { transition1: { destination: { target: targets3.root.Published } } },
200
247
  id: "optional-parent-protocol",
201
248
  root: OptionalParentStates,
202
249
  events: Machine.eventsFromSchemas(PublishOutside),
203
- parent: Machine.optionalParent(ParentEvents),
204
- initialConfiguration: (root) =>
205
- root.resolve(({ target }) => target.from((to) => to.Detached.decoded(new Detached({}))))
250
+ parent: Machine.optionalParent(ParentEvents)
206
251
  }).handle({
252
+ initial: {
253
+ target: Machine.targets(OptionalParentStates).root.Detached,
254
+ decoded: true,
255
+ data: new Detached({})
256
+ },
207
257
  states: {
208
258
  Detached: {
209
259
  on: {
210
- PublishOutside: (to) =>
211
- to.branch.Published().resolve(({ event, parent, target }, enqueue) => {
260
+ PublishOutside: {
261
+ branches: "transition1",
262
+ resolve: ({ event, parent, select: { destination: target } }, enqueue) => {
212
263
  if (parent !== undefined) {
213
264
  enqueue.sendTo(parent, ParentEvents.ChildFinished({ result: event.result }))
214
265
  }
215
266
  return target.decoded(new Published({ deliveredToParent: parent !== undefined }))
216
- })
267
+ }
268
+ }
217
269
  }
218
270
  },
219
271
  Published: {}