velocious 1.0.497 → 1.0.499

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.
@@ -10,7 +10,9 @@
10
10
  * }} StateMachineDefinition
11
11
  * @typedef {{
12
12
  * beforeEnter?: (model: import("./index.js").default) => void | Promise<void>,
13
- * afterEnter?: (model: import("./index.js").default) => void | Promise<void>
13
+ * afterEnter?: (model: import("./index.js").default) => void | Promise<void>,
14
+ * beforeExit?: (model: import("./index.js").default) => void | Promise<void>,
15
+ * afterExit?: (model: import("./index.js").default) => void | Promise<void>
14
16
  * }} StateDefinition
15
17
  * @typedef {{
16
18
  * from: string | string[],
@@ -68,6 +70,14 @@ export function stateMachine(ModelClass, definition) {
68
70
  * @type {?} */
69
71
  const dynamicClass = ModelClass
70
72
 
73
+ // Idempotent: re-declaring on the same class (or a re-evaluated module) must not
74
+ // register the before/after-save transition hooks twice. Guard on an own property
75
+ // so a subclass declaring its own machine is unaffected by the parent's flag.
76
+ if (Object.prototype.hasOwnProperty.call(dynamicClass, "_stateMachineRegistered") && dynamicClass._stateMachineRegistered) {
77
+ return
78
+ }
79
+
80
+ dynamicClass._stateMachineRegistered = true
71
81
  dynamicClass._stateMachineDefinition = definition
72
82
  dynamicClass._stateMachineColumn = column
73
83
 
@@ -229,7 +239,13 @@ export function stateMachine(ModelClass, definition) {
229
239
  await eventDef.before(model)
230
240
  }
231
241
 
232
- // Run state-level beforeEnter callback
242
+ // Run the exited state's beforeExit, then the entered state's beforeEnter
243
+ const fromStateDefinition = definition.states[pending.from]
244
+
245
+ if (fromStateDefinition?.beforeExit) {
246
+ await fromStateDefinition.beforeExit(model)
247
+ }
248
+
233
249
  const stateDefinition = definition.states[pending.to]
234
250
 
235
251
  if (stateDefinition?.beforeEnter) {
@@ -252,13 +268,19 @@ export function stateMachine(ModelClass, definition) {
252
268
  // Clear the pending transition now that save is complete
253
269
  dynamicModel[PENDING_TRANSITION_KEY] = null
254
270
 
255
- // Run state-level afterEnter callback
271
+ // Run the entered state's afterEnter, then the exited state's afterExit
256
272
  const stateDefinition = definition.states[pending.to]
257
273
 
258
274
  if (stateDefinition?.afterEnter) {
259
275
  await stateDefinition.afterEnter(model)
260
276
  }
261
277
 
278
+ const fromStateDefinition = definition.states[pending.from]
279
+
280
+ if (fromStateDefinition?.afterExit) {
281
+ await fromStateDefinition.afterExit(model)
282
+ }
283
+
262
284
  // Run event-level after callback
263
285
  const eventDef = definition.events[pending.eventName]
264
286