@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,36 +1,40 @@
1
1
  import { Machine } from "@typeonce/effect-machine"
2
2
  import { Effect, Schema, Stream } from "effect"
3
-
4
3
  class ChildWorking extends Schema.TaggedClass<ChildWorking>("InvokeGalleryChildWorking")("Working", {
5
4
  task: Schema.String
6
- }) {}
5
+ }) {
6
+ }
7
7
  class ChildDone extends Schema.TaggedClass<ChildDone>("InvokeGalleryChildDone")("Done", {
8
8
  result: Schema.String
9
- }) {}
10
- class FinishChild extends Schema.TaggedClass<FinishChild>("InvokeGalleryFinishChild")("FinishChild", {}) {}
11
-
9
+ }) {
10
+ }
11
+ class FinishChild extends Schema.TaggedClass<FinishChild>("InvokeGalleryFinishChild")("FinishChild", {}) {
12
+ }
12
13
  const ChildStates = Machine.state({
13
- initial: "Working",
14
14
  states: {
15
15
  Working: ChildWorking,
16
16
  Done: { schema: ChildDone, type: "final", output: Schema.String }
17
17
  }
18
18
  })
19
-
19
+ const targets1 = Machine.targets(ChildStates)
20
20
  export const invokeGalleryChildMachine = Machine.make({
21
21
  id: "invoke-gallery-child",
22
22
  root: ChildStates,
23
- events: Machine.eventsFromSchemas(FinishChild),
24
- initialConfiguration: (root) =>
25
- root.resolve(({ target }) => target.from((to) => to.Working.decoded(new ChildWorking({ task: "render-preview" }))))
23
+ events: Machine.eventsFromSchemas(FinishChild)
26
24
  }).handle({
25
+ initial: {
26
+ target: Machine.targets(ChildStates).root.Working,
27
+ decoded: true,
28
+ data: new ChildWorking({ task: "render-preview" })
29
+ },
27
30
  states: {
28
31
  Working: {
29
32
  on: {
30
- FinishChild: (to) =>
31
- to.branch.Done().resolve(({ state, target }) =>
32
- target.decoded(new ChildDone({ result: `${state.task}:complete` }))
33
- )
33
+ FinishChild: {
34
+ target: targets1.root.Done,
35
+ decoded: true,
36
+ data: ({ state }) => (new ChildDone({ result: `${state.task}:complete` }))
37
+ }
34
38
  }
35
39
  },
36
40
  Done: {
@@ -38,64 +42,73 @@ export const invokeGalleryChildMachine = Machine.make({
38
42
  }
39
43
  }
40
44
  })
41
-
42
45
  const InvokedChild = Machine.child("preview-worker", invokeGalleryChildMachine)
43
-
44
46
  class Gallery extends Schema.TaggedClass<Gallery>("InvokeGallery")("Gallery", {
45
47
  selectedDemo: Schema.NullOr(Schema.String)
46
- }) {}
47
- class Choose extends Schema.TaggedClass<Choose>("InvokeGalleryChoose")("Choose", {}) {}
48
- class LoadingDocument extends Schema.TaggedClass<LoadingDocument>("InvokeGalleryLoadingDocument")(
49
- "LoadingDocument",
50
- { request: Schema.String }
51
- ) {}
52
- class StreamingUpdates extends Schema.TaggedClass<StreamingUpdates>("InvokeGalleryStreamingUpdates")(
53
- "StreamingUpdates",
54
- { values: Schema.Array(Schema.Number) }
55
- ) {}
56
- class WaitingForTimeout extends Schema.TaggedClass<WaitingForTimeout>("InvokeGalleryWaitingForTimeout")(
57
- "WaitingForTimeout",
58
- { delay: Schema.Literal("2 seconds") }
59
- ) {}
60
- class WatchingProcess extends Schema.TaggedClass<WatchingProcess>("InvokeGalleryWatchingProcess")(
61
- "WatchingProcess",
62
- { revision: Schema.Number }
63
- ) {}
64
- class RunningChild extends Schema.TaggedClass<RunningChild>("InvokeGalleryRunningChild")("RunningChild", {}) {}
48
+ }) {
49
+ }
50
+ class Choose extends Schema.TaggedClass<Choose>("InvokeGalleryChoose")("Choose", {}) {
51
+ }
52
+ class LoadingDocument extends Schema.TaggedClass<LoadingDocument>("InvokeGalleryLoadingDocument")("LoadingDocument", {
53
+ request: Schema.String
54
+ }) {
55
+ }
56
+ class StreamingUpdates
57
+ extends Schema.TaggedClass<StreamingUpdates>("InvokeGalleryStreamingUpdates")("StreamingUpdates", {
58
+ values: Schema.Array(Schema.Number)
59
+ })
60
+ {
61
+ }
62
+ class WaitingForTimeout
63
+ extends Schema.TaggedClass<WaitingForTimeout>("InvokeGalleryWaitingForTimeout")("WaitingForTimeout", {
64
+ delay: Schema.Literal("2 seconds")
65
+ })
66
+ {
67
+ }
68
+ class WatchingProcess extends Schema.TaggedClass<WatchingProcess>("InvokeGalleryWatchingProcess")("WatchingProcess", {
69
+ revision: Schema.Number
70
+ }) {
71
+ }
72
+ class RunningChild extends Schema.TaggedClass<RunningChild>("InvokeGalleryRunningChild")("RunningChild", {}) {
73
+ }
65
74
  class Completed extends Schema.TaggedClass<Completed>("InvokeGalleryCompleted")("Completed", {
66
75
  source: Schema.String,
67
76
  result: Schema.String
68
- }) {}
77
+ }) {
78
+ }
69
79
  class Failed extends Schema.TaggedClass<Failed>("InvokeGalleryFailed")("Failed", {
70
80
  source: Schema.String,
71
81
  message: Schema.String
72
- }) {}
73
-
82
+ }) {
83
+ }
74
84
  class RunEffect extends Schema.TaggedClass<RunEffect>("InvokeGalleryRunEffect")("RunEffect", {
75
85
  request: Schema.String
76
- }) {}
77
- class RunStream extends Schema.TaggedClass<RunStream>("InvokeGalleryRunStream")("RunStream", {}) {}
78
- class RunTimer extends Schema.TaggedClass<RunTimer>("InvokeGalleryRunTimer")("RunTimer", {}) {}
79
- class RunProcess extends Schema.TaggedClass<RunProcess>("InvokeGalleryRunProcess")("RunProcess", {}) {}
80
- class RunChild extends Schema.TaggedClass<RunChild>("InvokeGalleryRunChild")("RunChild", {}) {}
81
- class Reset extends Schema.TaggedClass<Reset>("InvokeGalleryReset")("Reset", {}) {}
86
+ }) {
87
+ }
88
+ class RunStream extends Schema.TaggedClass<RunStream>("InvokeGalleryRunStream")("RunStream", {}) {
89
+ }
90
+ class RunTimer extends Schema.TaggedClass<RunTimer>("InvokeGalleryRunTimer")("RunTimer", {}) {
91
+ }
92
+ class RunProcess extends Schema.TaggedClass<RunProcess>("InvokeGalleryRunProcess")("RunProcess", {}) {
93
+ }
94
+ class RunChild extends Schema.TaggedClass<RunChild>("InvokeGalleryRunChild")("RunChild", {}) {
95
+ }
96
+ class Reset extends Schema.TaggedClass<Reset>("InvokeGalleryReset")("Reset", {}) {
97
+ }
82
98
  class StreamValue extends Schema.TaggedClass<StreamValue>("InvokeGalleryStreamValue")("StreamValue", {
83
99
  value: Schema.Number
84
- }) {}
85
-
100
+ }) {
101
+ }
86
102
  const GalleryEvents = Machine.eventsFromSchemas(RunEffect, RunStream, RunTimer, RunProcess, RunChild, Reset)
87
103
  const GalleryInternalEvents = Machine.internalEventsFromSchemas(StreamValue)
88
104
  const processLogic = Machine.logic({
89
105
  initial: "starting" as "starting" | "ready",
90
106
  run: () => Effect.fail("process stopped")
91
107
  })
92
-
93
108
  const GalleryStates = Machine.state({
94
- initial: "Gallery",
95
109
  states: {
96
110
  Gallery: {
97
111
  schema: Gallery,
98
- initial: "Choose",
99
112
  states: {
100
113
  Choose,
101
114
  LoadingDocument,
@@ -109,132 +122,169 @@ const GalleryStates = Machine.state({
109
122
  Failed
110
123
  }
111
124
  })
112
-
125
+ const targets2 = Machine.targets(GalleryStates)
113
126
  export const invokeOutcomesMachine = Machine.make({
127
+ branches: {
128
+ transition13: {
129
+ ready: { target: targets2.root.Completed, title: "Worker reports ready" },
130
+ waiting: { none: true, title: "Worker is still starting" }
131
+ }
132
+ },
133
+ effects: {
134
+ source1: ({ state }: {
135
+ readonly state: LoadingDocument
136
+ }) => state.request === "fail" ? Effect.fail("document unavailable") : Effect.succeed("document loaded")
137
+ },
138
+ streams: {
139
+ source2: Stream.suspend(() =>
140
+ Stream.fromIterable([1, 2, 3]).pipe(Stream.concat(Stream.fail("stream disconnected")))
141
+ )
142
+ },
143
+ timers: { source3: "2 seconds" },
144
+ logic: { source4: processLogic },
145
+ children: { source5: InvokedChild },
114
146
  id: "invoke-outcomes",
115
147
  root: GalleryStates,
116
148
  events: GalleryEvents,
117
- internalEvents: GalleryInternalEvents,
118
- initialConfiguration: (root) =>
119
- root.resolve(({ target }) =>
120
- target.from((to) =>
121
- to.Gallery.decoded(new Gallery({ selectedDemo: null }), (gallery) => gallery.Choose.decoded(new Choose({})))
122
- )
123
- )
149
+ internalEvents: GalleryInternalEvents
124
150
  }).handle({
151
+ initial: {
152
+ target: Machine.targets(GalleryStates).root.Gallery,
153
+ decoded: true,
154
+ data: new Gallery({ selectedDemo: null })
155
+ },
125
156
  states: {
126
157
  Gallery: {
127
- initialize: ({ builder }) => builder.decoded(new Choose({})),
158
+ initial: {
159
+ target: Machine.targets(GalleryStates).root.Gallery.Choose,
160
+ decoded: true,
161
+ data: ({}) => new Choose({})
162
+ },
128
163
  on: {
129
- Reset: (to) => to.local.Choose().resolve(({ target }) => target.decoded(new Choose({})))
164
+ Reset: { target: targets2.root.Gallery.Choose, decoded: true, data: () => (new Choose({})) }
130
165
  },
131
166
  states: {
132
167
  Choose: {
133
168
  on: {
134
- RunEffect: (to) =>
135
- to.local.LoadingDocument().resolve(({ event, target }) =>
136
- target.decoded(new LoadingDocument({ request: event.request }))
137
- ),
138
- RunStream: (to) =>
139
- to.local.StreamingUpdates().resolve(({ target }) => target.decoded(new StreamingUpdates({ values: [] }))),
140
- RunTimer: (to) =>
141
- to.local.WaitingForTimeout().resolve(({ target }) =>
142
- target.decoded(new WaitingForTimeout({ delay: "2 seconds" }))
143
- ),
144
- RunProcess: (to) =>
145
- to.local.WatchingProcess().resolve(({ target }) => target.decoded(new WatchingProcess({ revision: 1 }))),
146
- RunChild: (to) => to.local.RunningChild().resolve(({ target }) => target.decoded(new RunningChild({})))
169
+ RunEffect: {
170
+ target: targets2.root.Gallery.LoadingDocument,
171
+ decoded: true,
172
+ data: ({ event }) => (new LoadingDocument({ request: event.request }))
173
+ },
174
+ RunStream: {
175
+ target: targets2.root.Gallery.StreamingUpdates,
176
+ decoded: true,
177
+ data: () => (new StreamingUpdates({ values: [] }))
178
+ },
179
+ RunTimer: {
180
+ target: targets2.root.Gallery.WaitingForTimeout,
181
+ decoded: true,
182
+ data: () => (new WaitingForTimeout({ delay: "2 seconds" }))
183
+ },
184
+ RunProcess: {
185
+ target: targets2.root.Gallery.WatchingProcess,
186
+ decoded: true,
187
+ data: () => (new WatchingProcess({ revision: 1 }))
188
+ },
189
+ RunChild: { target: targets2.root.Gallery.RunningChild, decoded: true, data: () => (new RunningChild({})) }
147
190
  }
148
191
  },
149
192
  LoadingDocument: {
150
- invoke: (from) =>
151
- from.effect(
152
- "load-document",
153
- ({ state }) =>
154
- state.request === "fail" ? Effect.fail("document unavailable") : Effect.succeed("document loaded")
155
- ).onDone((to) =>
156
- to.branch.Completed().resolve(({ output, target }) =>
157
- target.decoded(new Completed({ source: "effect", result: output }))
158
- )
159
- ).onFailure((to) =>
160
- to.branch.Failed().resolve(({ error, target }) =>
161
- target.decoded(new Failed({ source: "effect", message: error }))
162
- )
163
- )
193
+ invoke: {
194
+ src: "source1",
195
+ id: "load-document",
196
+ input: (context) => context,
197
+ onDone: {
198
+ target: targets2.root.Completed,
199
+ decoded: true,
200
+ data: ({ output }) => (new Completed({ source: "effect", result: output }))
201
+ },
202
+ onFailure: {
203
+ target: targets2.root.Failed,
204
+ decoded: true,
205
+ data: ({ error }) => (new Failed({ source: "effect", message: error }))
206
+ }
207
+ }
164
208
  },
165
209
  StreamingUpdates: {
166
- invoke: (from) =>
167
- from.stream(
168
- "document-updates",
169
- () => Stream.fromIterable([1, 2, 3]).pipe(Stream.concat(Stream.fail("stream disconnected")))
170
- ).onElement((to) =>
171
- to.none.resolve(({ element }, enqueue) => {
210
+ invoke: {
211
+ src: "source2",
212
+ id: "document-updates",
213
+ onElement: {
214
+ none: true,
215
+ resolve: ({ element }, enqueue) => {
172
216
  enqueue.raise(GalleryInternalEvents.StreamValue({ value: element }))
173
- })
174
- ).onDone((to) =>
175
- to.branch.Completed().resolve(({ state, target }) =>
176
- target.decoded(new Completed({ source: "stream", result: state.values.join(", ") }))
177
- )
178
- ).onFailure((to) =>
179
- to.branch.Failed().resolve(({ error, target }) =>
180
- target.decoded(new Failed({ source: "stream", message: error }))
181
- )
182
- ),
217
+ }
218
+ },
219
+ onDone: {
220
+ target: targets2.root.Completed,
221
+ decoded: true,
222
+ data: ({ state }) => (new Completed({ source: "stream", result: state.values.join(", ") }))
223
+ },
224
+ onFailure: {
225
+ target: targets2.root.Failed,
226
+ decoded: true,
227
+ data: ({ error }) => (new Failed({ source: "stream", message: error }))
228
+ }
229
+ },
183
230
  on: {
184
- StreamValue: (to) =>
185
- to.local.StreamingUpdates().resolve(({ event, state, target }) =>
186
- target.decoded(new StreamingUpdates({ values: [...state.values, event.value] }))
187
- )
231
+ StreamValue: {
232
+ target: targets2.root.Gallery.StreamingUpdates,
233
+ decoded: true,
234
+ data: ({ event, state }) => (new StreamingUpdates({ values: [...state.values, event.value] }))
235
+ }
188
236
  }
189
237
  },
190
238
  WaitingForTimeout: {
191
- invoke: (from) =>
192
- from.timer("request-timeout", "2 seconds").onDone((to) =>
193
- to.branch.Completed().resolve(({ target }) =>
194
- target.decoded(new Completed({ source: "timer", result: "timeout elapsed" }))
195
- )
196
- )
239
+ invoke: {
240
+ src: "source3",
241
+ id: "request-timeout",
242
+ onDone: {
243
+ target: targets2.root.Completed,
244
+ decoded: true,
245
+ data: () => (new Completed({ source: "timer", result: "timeout elapsed" }))
246
+ }
247
+ }
197
248
  },
198
249
  WatchingProcess: {
199
- invoke: (from) =>
200
- from.logic("status-worker", {
201
- address: Machine.childAddress("status-worker"),
202
- logic: processLogic
203
- }).onFailure((to) =>
204
- to.branch.Failed().resolve(({ error, target }) =>
205
- target.decoded(new Failed({ source: "process", message: String(error) }))
206
- )
207
- ).onSnapshot((to) =>
208
- to.branches({
209
- ready: { title: "Worker reports ready", target: to.branch.Completed() },
210
- waiting: { title: "Worker is still starting", target: to.none }
211
- }).resolve(({ select, snapshot }) =>
250
+ invoke: {
251
+ src: "source4",
252
+ id: "status-worker",
253
+ address: Machine.childAddress("status-worker"),
254
+ onFailure: {
255
+ target: targets2.root.Failed,
256
+ decoded: true,
257
+ data: ({ error }) => (new Failed({ source: "process", message: String(error) }))
258
+ },
259
+ onSnapshot: {
260
+ branches: "transition13",
261
+ resolve: ({ select, snapshot }) =>
212
262
  snapshot.state === "ready"
213
263
  ? select.ready.decoded(new Completed({ source: "process", result: "ready" }))
214
264
  : select.waiting()
215
- )
216
- )
265
+ }
266
+ }
217
267
  },
218
268
  RunningChild: {
219
- invoke: (from) =>
220
- from.child(InvokedChild).onDone((to) =>
221
- to.branch.Completed().resolve(({ output, target }) =>
222
- target.decoded(new Completed({ source: "child machine", result: output }))
223
- )
224
- )
269
+ invoke: {
270
+ src: "source5",
271
+ onDone: {
272
+ target: targets2.root.Completed,
273
+ decoded: true,
274
+ data: ({ output }) => (new Completed({ source: "child machine", result: output }))
275
+ }
276
+ }
225
277
  }
226
278
  }
227
279
  },
228
280
  Completed: {
229
281
  on: {
230
- Reset: (to) =>
231
- to.branch.Gallery.initial.resolve(({ target }) => target.decoded(new Gallery({ selectedDemo: null })))
282
+ Reset: { initial: targets2.root.Gallery, decoded: true, data: () => (new Gallery({ selectedDemo: null })) }
232
283
  }
233
284
  },
234
285
  Failed: {
235
286
  on: {
236
- Reset: (to) =>
237
- to.branch.Gallery.initial.resolve(({ target }) => target.decoded(new Gallery({ selectedDemo: null })))
287
+ Reset: { initial: targets2.root.Gallery, decoded: true, data: () => (new Gallery({ selectedDemo: null })) }
238
288
  }
239
289
  }
240
290
  }
@@ -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,68 +41,90 @@ const AuthStates = Machine.state({
48
41
  Navigating: AuthState.cases.Navigating
49
42
  }
50
43
  })
51
-
52
- const AuthEvents = Machine.eventsFromSchemas(
53
- Schema.TaggedUnion({
54
- EmailChanged: { value: Schema.String },
55
- LoginMethodChanged: { value: LoginMethod },
56
- PasswordChanged: { value: Schema.String },
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
+ }))
50
+ const targets1 = Machine.targets(AuthStates)
61
51
  export const layoutResilienceMachine = Machine.make({
52
+ branches: {
53
+ transition4: {
54
+ requestVerification: {
55
+ target: targets1.root.Editing.RequestingVerification,
56
+ title: "Request a verification code"
57
+ },
58
+ login: { target: targets1.root.Editing.SubmittingLogin, title: "Submit password login" },
59
+ invalid: { target: targets1.root.Editing.Form.Failed, title: "Show validation failure" }
60
+ }
61
+ },
62
+ effects: {
63
+ source1: ({ containingState }: {
64
+ readonly containingState: {
65
+ readonly email: string
66
+ }
67
+ }) =>
68
+ containingState.email === "fail"
69
+ ? Effect.fail("invalid credentials")
70
+ : Effect.succeed("/creator"),
71
+ source2: ({ containingState }: {
72
+ readonly containingState: {
73
+ readonly email: string
74
+ }
75
+ }) =>
76
+ containingState.email === "fail"
77
+ ? Effect.fail("verification unavailable")
78
+ : Effect.succeed(undefined)
79
+ },
62
80
  id: "layout-resilience",
63
81
  root: AuthStates,
64
- events: AuthEvents,
65
- initialConfiguration: (root) =>
66
- root.resolve(({ target }) =>
67
- target.from((to) =>
68
- to.Editing.from({
69
- mode: "login",
70
- email: "",
71
- loginMethod: "code",
72
- password: ""
73
- }, (editing) => editing.Form.from((form) => form.Ready.from()))
74
- )
75
- )
82
+ events: AuthEvents
76
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
+ },
77
93
  states: {
78
94
  Editing: {
95
+ initial: {
96
+ target: Machine.targets(AuthStates).root.Editing.Form
97
+ },
79
98
  states: {
80
99
  Form: {
100
+ initial: {
101
+ target: Machine.targets(AuthStates).root.Editing.Form.Ready
102
+ },
81
103
  on: {
82
- EmailChanged: (to) =>
83
- to.branch.Editing.update.resolve(({ current, event, owner }) =>
84
- owner.from({ ...current, email: event.value })
85
- ),
86
- LoginMethodChanged: (to) =>
87
- to.branch.Editing.update.resolve(({ current, event, owner }) =>
88
- owner.from({ ...current, loginMethod: event.value })
89
- ),
90
- PasswordChanged: (to) =>
91
- to.branch.Editing.update.resolve(({ current, event, owner }) =>
92
- owner.from({ ...current, password: event.value })
93
- ),
94
- Submit: (to) =>
95
- to.branches({
96
- requestVerification: {
97
- title: "Request a verification code",
98
- target: to.branch.Editing.RequestingVerification()
99
- },
100
- login: {
101
- title: "Submit password login",
102
- target: to.branch.Editing.SubmittingLogin()
103
- },
104
- invalid: {
105
- title: "Show validation failure",
106
- target: to.local.Failed()
104
+ EmailChanged: {
105
+ update: targets1.root.Editing,
106
+ data: ({ ancestors: { "Editing": current }, event }) => ({ ...current, email: event.value })
107
+ },
108
+ LoginMethodChanged: {
109
+ update: targets1.root.Editing,
110
+ data: ({ ancestors: { "Editing": current }, event }) => ({ ...current, loginMethod: event.value })
111
+ },
112
+ PasswordChanged: {
113
+ update: targets1.root.Editing,
114
+ data: ({ ancestors: { "Editing": current }, event }) => ({ ...current, password: event.value })
115
+ },
116
+ Submit: {
117
+ branches: "transition4",
118
+ resolve: ({ event, select }) => {
119
+ if (event.route === "login") {
120
+ return select.login.from()
121
+ }
122
+ if (event.route === "verification") {
123
+ return select.requestVerification.from()
107
124
  }
108
- }).resolve(({ event, select }) => {
109
- if (event.route === "login") return select.login.from()
110
- if (event.route === "verification") return select.requestVerification.from()
111
125
  return select.invalid.from({ message: "Enter valid authentication details." })
112
- })
126
+ }
127
+ }
113
128
  },
114
129
  states: {
115
130
  Ready: {},
@@ -117,36 +132,42 @@ export const layoutResilienceMachine = Machine.make({
117
132
  }
118
133
  },
119
134
  SubmittingLogin: {
120
- invoke: (from) =>
121
- from.effect("submit-login", ({ containingState }) =>
122
- containingState.email === "fail"
123
- ? Effect.fail("invalid credentials")
124
- : Effect.succeed("/creator")).onDone((to) =>
125
- to.branch.Navigating().resolve(({ output, target }) => target.from({ href: output }))).onFailure((to) =>
126
- to.branch.Editing.Form.Failed().resolve(({ target }) =>
127
- target.from({ message: "Email or password is incorrect." })
128
- ))
135
+ invoke: {
136
+ src: "source1",
137
+ id: "submit-login",
138
+ input: (context) => context,
139
+ onDone: { target: targets1.root.Navigating, data: ({ output }) => ({ href: output }) },
140
+ onFailure: {
141
+ target: targets1.root.Editing.Form.Failed,
142
+ data: () => ({ message: "Email or password is incorrect." })
143
+ }
144
+ }
129
145
  },
130
146
  RequestingVerification: {
131
- invoke: (from) =>
132
- from.effect("request-verification", ({ containingState }) =>
133
- containingState.email === "fail"
134
- ? Effect.fail("verification unavailable")
135
- : Effect.succeed(undefined)).onDone((to) =>
136
- to.branch.Verification.initial.resolve(({ containingState, target }) =>
137
- target.from({
138
- mode: containingState.mode,
139
- email: containingState.email,
140
- code: ""
141
- })
142
- )).onFailure((to) =>
143
- to.branch.Editing.Form.Failed().resolve(({ target }) =>
144
- target.from({ message: "The verification code could not be sent." })
145
- ))
147
+ invoke: {
148
+ src: "source2",
149
+ id: "request-verification",
150
+ input: (context) => context,
151
+ onDone: {
152
+ initial: targets1.root.Verification,
153
+ data: ({ containingState }) => ({
154
+ mode: containingState.mode,
155
+ email: containingState.email,
156
+ code: ""
157
+ })
158
+ },
159
+ onFailure: {
160
+ target: targets1.root.Editing.Form.Failed,
161
+ data: () => ({ message: "The verification code could not be sent." })
162
+ }
163
+ }
146
164
  }
147
165
  }
148
166
  },
149
167
  Verification: {
168
+ initial: {
169
+ target: Machine.targets(AuthStates).root.Verification.CodeEntry
170
+ },
150
171
  states: {
151
172
  CodeEntry: {}
152
173
  }