xstate 5.8.2 → 5.9.1

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.
Files changed (58) hide show
  1. package/actions/dist/xstate-actions.cjs.js +3 -2
  2. package/actions/dist/xstate-actions.cjs.mjs +1 -0
  3. package/actions/dist/xstate-actions.development.cjs.js +3 -2
  4. package/actions/dist/xstate-actions.development.cjs.mjs +1 -0
  5. package/actions/dist/xstate-actions.development.esm.js +2 -2
  6. package/actions/dist/xstate-actions.esm.js +2 -2
  7. package/actions/dist/xstate-actions.umd.min.js +1 -1
  8. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  9. package/actors/dist/xstate-actors.cjs.js +1 -1
  10. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  11. package/actors/dist/xstate-actors.development.esm.js +1 -1
  12. package/actors/dist/xstate-actors.esm.js +1 -1
  13. package/actors/dist/xstate-actors.umd.min.js +1 -1
  14. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  15. package/dist/declarations/src/State.d.ts +1 -1
  16. package/dist/declarations/src/StateMachine.d.ts +6 -5
  17. package/dist/declarations/src/StateNode.d.ts +9 -6
  18. package/dist/declarations/src/actions/assign.d.ts +1 -1
  19. package/dist/declarations/src/actions/emit.d.ts +44 -0
  20. package/dist/declarations/src/actions/enqueueActions.d.ts +10 -8
  21. package/dist/declarations/src/actions/raise.d.ts +1 -1
  22. package/dist/declarations/src/actions/send.d.ts +5 -5
  23. package/dist/declarations/src/actions/spawnChild.d.ts +1 -1
  24. package/dist/declarations/src/actions/stopChild.d.ts +2 -2
  25. package/dist/declarations/src/actions.d.ts +1 -0
  26. package/dist/declarations/src/actors/callback.d.ts +1 -1
  27. package/dist/declarations/src/actors/observable.d.ts +1 -1
  28. package/dist/declarations/src/actors/promise.d.ts +2 -2
  29. package/dist/declarations/src/actors/transition.d.ts +3 -3
  30. package/dist/declarations/src/createActor.d.ts +8 -4
  31. package/dist/declarations/src/createMachine.d.ts +4 -4
  32. package/dist/declarations/src/setup.d.ts +5 -5
  33. package/dist/declarations/src/typegenTypes.d.ts +3 -1
  34. package/dist/declarations/src/types.d.ts +77 -66
  35. package/dist/declarations/src/utils.d.ts +2 -2
  36. package/dist/declarations/src/waitFor.d.ts +2 -2
  37. package/dist/{log-ac1ff860.esm.js → log-0fbf8cec.development.esm.js} +96 -4
  38. package/dist/{log-12b88b7e.cjs.js → log-28ea35fe.development.cjs.js} +96 -3
  39. package/dist/{log-e73cded6.development.cjs.js → log-a312ebfc.cjs.js} +68 -25
  40. package/dist/{log-abca6761.development.esm.js → log-c48e8ab0.esm.js} +68 -26
  41. package/dist/{raise-af6a698b.cjs.js → raise-182bb5c9.cjs.js} +24 -0
  42. package/dist/{raise-a7ab421f.esm.js → raise-411df926.esm.js} +24 -0
  43. package/dist/{raise-3f85f1d9.development.esm.js → raise-80cc66b2.development.esm.js} +24 -0
  44. package/dist/{raise-7132462e.development.cjs.js → raise-933cd731.development.cjs.js} +24 -0
  45. package/dist/xstate.cjs.js +5 -3
  46. package/dist/xstate.cjs.mjs +1 -0
  47. package/dist/xstate.development.cjs.js +5 -3
  48. package/dist/xstate.development.cjs.mjs +1 -0
  49. package/dist/xstate.development.esm.js +6 -5
  50. package/dist/xstate.esm.js +6 -5
  51. package/dist/xstate.umd.min.js +1 -1
  52. package/dist/xstate.umd.min.js.map +1 -1
  53. package/guards/dist/xstate-guards.cjs.js +1 -1
  54. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  55. package/guards/dist/xstate-guards.development.esm.js +1 -1
  56. package/guards/dist/xstate-guards.esm.js +1 -1
  57. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  58. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var guards_dist_xstateGuards = require('./raise-af6a698b.cjs.js');
3
+ var guards_dist_xstateGuards = require('./raise-933cd731.development.cjs.js');
4
4
 
5
5
  // it's likely-ish that `(TActor & { src: TSrc })['logic']` would be faster
6
6
  // but it's only possible to do it since https://github.com/microsoft/TypeScript/pull/53098 (TS 5.1)
@@ -124,6 +124,9 @@ function resolveAssign(actorScope, snapshot, actionArgs, actionParams, {
124
124
  */
125
125
  function assign(assignment) {
126
126
  function assign(args, params) {
127
+ {
128
+ throw new Error(`This isn't supposed to be called`);
129
+ }
127
130
  }
128
131
  assign.type = 'xstate.assign';
129
132
  assign.assignment = assignment;
@@ -131,6 +134,73 @@ function assign(assignment) {
131
134
  return assign;
132
135
  }
133
136
 
137
+ function resolveEmit(_, snapshot, args, actionParams, {
138
+ event: eventOrExpr
139
+ }) {
140
+ if (typeof eventOrExpr === 'string') {
141
+ throw new Error(`Only event objects may be used with emit; use emit({ type: "${eventOrExpr}" }) instead`);
142
+ }
143
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
144
+ return [snapshot, {
145
+ event: resolvedEvent
146
+ }];
147
+ }
148
+ function executeEmit(actorScope, {
149
+ event
150
+ }) {
151
+ actorScope.defer(() => actorScope.emit(event));
152
+ }
153
+ /**
154
+ * Emits an event to event handlers registered on the actor via `actor.on(event, handler)`.
155
+ *
156
+ * @example
157
+ ```ts
158
+ import { emit } from 'xstate';
159
+
160
+ const machine = createMachine({
161
+ // ...
162
+ on: {
163
+ something: {
164
+ actions: emit({
165
+ type: 'emitted',
166
+ some: 'data'
167
+ })
168
+ }
169
+ }
170
+ // ...
171
+ });
172
+
173
+ const actor = createActor(machine).start();
174
+
175
+ actor.on('emitted', (event) => {
176
+ console.log(event);
177
+ });
178
+
179
+ actor.send({ type: 'something' });
180
+ // logs:
181
+ // {
182
+ // type: 'emitted',
183
+ // some: 'data'
184
+ // }
185
+ ```
186
+ */
187
+ function emit(
188
+ /**
189
+ * The event to emit, or an expression that returns an event to emit.
190
+ */
191
+ eventOrExpr) {
192
+ function emit(args, params) {
193
+ {
194
+ throw new Error(`This isn't supposed to be called`);
195
+ }
196
+ }
197
+ emit.type = 'xstate.emit';
198
+ emit.event = eventOrExpr;
199
+ emit.resolve = resolveEmit;
200
+ emit.execute = executeEmit;
201
+ return emit;
202
+ }
203
+
134
204
  /**
135
205
  *
136
206
  * @remarks
@@ -278,6 +348,9 @@ function executeSendTo(actorScope, params) {
278
348
  */
279
349
  function sendTo(to, eventOrExpr, options) {
280
350
  function sendTo(args, params) {
351
+ {
352
+ throw new Error(`This isn't supposed to be called`);
353
+ }
281
354
  }
282
355
  sendTo.type = 'xsnapshot.sendTo';
283
356
  sendTo.to = to;
@@ -306,12 +379,22 @@ function sendParent(event, options) {
306
379
  * @param options Options to pass into the send action creator.
307
380
  */
308
381
  function forwardTo(target, options) {
382
+ if ((!target || typeof target === 'function')) {
383
+ const originalTarget = target;
384
+ target = (...args) => {
385
+ const resolvedTarget = typeof originalTarget === 'function' ? originalTarget(...args) : originalTarget;
386
+ if (!resolvedTarget) {
387
+ throw new Error(`Attempted to forward event to undefined actor. This risks an infinite loop in the sender.`);
388
+ }
389
+ return resolvedTarget;
390
+ };
391
+ }
309
392
  return sendTo(target, ({
310
393
  event
311
394
  }) => event, options);
312
395
  }
313
396
 
314
- function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
397
+ function resolveEnqueueActions(actorScope, snapshot, args, actionParams, {
315
398
  collect
316
399
  }) {
317
400
  const actions = [];
@@ -340,6 +423,9 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
340
423
  enqueue.stopChild = (...args) => {
341
424
  actions.push(guards_dist_xstateGuards.stopChild(...args));
342
425
  };
426
+ enqueue.emit = (...args) => {
427
+ actions.push(emit(...args));
428
+ };
343
429
  collect({
344
430
  context: args.context,
345
431
  event: args.event,
@@ -347,7 +433,7 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
347
433
  check: guard => guards_dist_xstateGuards.evaluateGuard(guard, snapshot.context, args.event, snapshot),
348
434
  self: actorScope.self,
349
435
  system: actorScope.system
350
- });
436
+ }, actionParams);
351
437
  return [snapshot, undefined, actions];
352
438
  }
353
439
  /**
@@ -372,6 +458,9 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
372
458
  */
373
459
  function enqueueActions(collect) {
374
460
  function enqueueActions(args, params) {
461
+ {
462
+ throw new Error(`This isn't supposed to be called`);
463
+ }
375
464
  }
376
465
  enqueueActions.type = 'xstate.enqueueActions';
377
466
  enqueueActions.collect = collect;
@@ -416,6 +505,9 @@ function log(value = ({
416
505
  event
417
506
  }), label) {
418
507
  function log(args, params) {
508
+ {
509
+ throw new Error(`This isn't supposed to be called`);
510
+ }
419
511
  }
420
512
  log.type = 'xstate.log';
421
513
  log.value = value;
@@ -427,6 +519,7 @@ function log(value = ({
427
519
 
428
520
  exports.SpecialTargets = SpecialTargets;
429
521
  exports.assign = assign;
522
+ exports.emit = emit;
430
523
  exports.enqueueActions = enqueueActions;
431
524
  exports.forwardTo = forwardTo;
432
525
  exports.log = log;
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var guards_dist_xstateGuards = require('./raise-7132462e.development.cjs.js');
3
+ var guards_dist_xstateGuards = require('./raise-182bb5c9.cjs.js');
4
4
 
5
5
  // it's likely-ish that `(TActor & { src: TSrc })['logic']` would be faster
6
6
  // but it's only possible to do it since https://github.com/microsoft/TypeScript/pull/53098 (TS 5.1)
@@ -124,9 +124,6 @@ function resolveAssign(actorScope, snapshot, actionArgs, actionParams, {
124
124
  */
125
125
  function assign(assignment) {
126
126
  function assign(args, params) {
127
- {
128
- throw new Error(`This isn't supposed to be called`);
129
- }
130
127
  }
131
128
  assign.type = 'xstate.assign';
132
129
  assign.assignment = assignment;
@@ -134,6 +131,67 @@ function assign(assignment) {
134
131
  return assign;
135
132
  }
136
133
 
134
+ function resolveEmit(_, snapshot, args, actionParams, {
135
+ event: eventOrExpr
136
+ }) {
137
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
138
+ return [snapshot, {
139
+ event: resolvedEvent
140
+ }];
141
+ }
142
+ function executeEmit(actorScope, {
143
+ event
144
+ }) {
145
+ actorScope.defer(() => actorScope.emit(event));
146
+ }
147
+ /**
148
+ * Emits an event to event handlers registered on the actor via `actor.on(event, handler)`.
149
+ *
150
+ * @example
151
+ ```ts
152
+ import { emit } from 'xstate';
153
+
154
+ const machine = createMachine({
155
+ // ...
156
+ on: {
157
+ something: {
158
+ actions: emit({
159
+ type: 'emitted',
160
+ some: 'data'
161
+ })
162
+ }
163
+ }
164
+ // ...
165
+ });
166
+
167
+ const actor = createActor(machine).start();
168
+
169
+ actor.on('emitted', (event) => {
170
+ console.log(event);
171
+ });
172
+
173
+ actor.send({ type: 'something' });
174
+ // logs:
175
+ // {
176
+ // type: 'emitted',
177
+ // some: 'data'
178
+ // }
179
+ ```
180
+ */
181
+ function emit(
182
+ /**
183
+ * The event to emit, or an expression that returns an event to emit.
184
+ */
185
+ eventOrExpr) {
186
+ function emit(args, params) {
187
+ }
188
+ emit.type = 'xstate.emit';
189
+ emit.event = eventOrExpr;
190
+ emit.resolve = resolveEmit;
191
+ emit.execute = executeEmit;
192
+ return emit;
193
+ }
194
+
137
195
  /**
138
196
  *
139
197
  * @remarks
@@ -281,9 +339,6 @@ function executeSendTo(actorScope, params) {
281
339
  */
282
340
  function sendTo(to, eventOrExpr, options) {
283
341
  function sendTo(args, params) {
284
- {
285
- throw new Error(`This isn't supposed to be called`);
286
- }
287
342
  }
288
343
  sendTo.type = 'xsnapshot.sendTo';
289
344
  sendTo.to = to;
@@ -312,22 +367,12 @@ function sendParent(event, options) {
312
367
  * @param options Options to pass into the send action creator.
313
368
  */
314
369
  function forwardTo(target, options) {
315
- if ((!target || typeof target === 'function')) {
316
- const originalTarget = target;
317
- target = (...args) => {
318
- const resolvedTarget = typeof originalTarget === 'function' ? originalTarget(...args) : originalTarget;
319
- if (!resolvedTarget) {
320
- throw new Error(`Attempted to forward event to undefined actor. This risks an infinite loop in the sender.`);
321
- }
322
- return resolvedTarget;
323
- };
324
- }
325
370
  return sendTo(target, ({
326
371
  event
327
372
  }) => event, options);
328
373
  }
329
374
 
330
- function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
375
+ function resolveEnqueueActions(actorScope, snapshot, args, actionParams, {
331
376
  collect
332
377
  }) {
333
378
  const actions = [];
@@ -356,6 +401,9 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
356
401
  enqueue.stopChild = (...args) => {
357
402
  actions.push(guards_dist_xstateGuards.stopChild(...args));
358
403
  };
404
+ enqueue.emit = (...args) => {
405
+ actions.push(emit(...args));
406
+ };
359
407
  collect({
360
408
  context: args.context,
361
409
  event: args.event,
@@ -363,7 +411,7 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
363
411
  check: guard => guards_dist_xstateGuards.evaluateGuard(guard, snapshot.context, args.event, snapshot),
364
412
  self: actorScope.self,
365
413
  system: actorScope.system
366
- });
414
+ }, actionParams);
367
415
  return [snapshot, undefined, actions];
368
416
  }
369
417
  /**
@@ -388,9 +436,6 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
388
436
  */
389
437
  function enqueueActions(collect) {
390
438
  function enqueueActions(args, params) {
391
- {
392
- throw new Error(`This isn't supposed to be called`);
393
- }
394
439
  }
395
440
  enqueueActions.type = 'xstate.enqueueActions';
396
441
  enqueueActions.collect = collect;
@@ -435,9 +480,6 @@ function log(value = ({
435
480
  event
436
481
  }), label) {
437
482
  function log(args, params) {
438
- {
439
- throw new Error(`This isn't supposed to be called`);
440
- }
441
483
  }
442
484
  log.type = 'xstate.log';
443
485
  log.value = value;
@@ -449,6 +491,7 @@ function log(value = ({
449
491
 
450
492
  exports.SpecialTargets = SpecialTargets;
451
493
  exports.assign = assign;
494
+ exports.emit = emit;
452
495
  exports.enqueueActions = enqueueActions;
453
496
  exports.forwardTo = forwardTo;
454
497
  exports.log = log;
@@ -1,4 +1,4 @@
1
- import { T as ProcessingStatus, z as resolveReferencedActor, A as createActor, U as cloneMachineSnapshot, V as XSTATE_ERROR, W as createErrorActorEvent, e as evaluateGuard, M as cancel, O as raise, P as spawnChild, R as stopChild } from './raise-3f85f1d9.development.esm.js';
1
+ import { T as ProcessingStatus, z as resolveReferencedActor, A as createActor, U as cloneMachineSnapshot, V as XSTATE_ERROR, W as createErrorActorEvent, e as evaluateGuard, M as cancel, O as raise, P as spawnChild, R as stopChild } from './raise-411df926.esm.js';
2
2
 
3
3
  // it's likely-ish that `(TActor & { src: TSrc })['logic']` would be faster
4
4
  // but it's only possible to do it since https://github.com/microsoft/TypeScript/pull/53098 (TS 5.1)
@@ -122,9 +122,6 @@ function resolveAssign(actorScope, snapshot, actionArgs, actionParams, {
122
122
  */
123
123
  function assign(assignment) {
124
124
  function assign(args, params) {
125
- {
126
- throw new Error(`This isn't supposed to be called`);
127
- }
128
125
  }
129
126
  assign.type = 'xstate.assign';
130
127
  assign.assignment = assignment;
@@ -132,6 +129,67 @@ function assign(assignment) {
132
129
  return assign;
133
130
  }
134
131
 
132
+ function resolveEmit(_, snapshot, args, actionParams, {
133
+ event: eventOrExpr
134
+ }) {
135
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
136
+ return [snapshot, {
137
+ event: resolvedEvent
138
+ }];
139
+ }
140
+ function executeEmit(actorScope, {
141
+ event
142
+ }) {
143
+ actorScope.defer(() => actorScope.emit(event));
144
+ }
145
+ /**
146
+ * Emits an event to event handlers registered on the actor via `actor.on(event, handler)`.
147
+ *
148
+ * @example
149
+ ```ts
150
+ import { emit } from 'xstate';
151
+
152
+ const machine = createMachine({
153
+ // ...
154
+ on: {
155
+ something: {
156
+ actions: emit({
157
+ type: 'emitted',
158
+ some: 'data'
159
+ })
160
+ }
161
+ }
162
+ // ...
163
+ });
164
+
165
+ const actor = createActor(machine).start();
166
+
167
+ actor.on('emitted', (event) => {
168
+ console.log(event);
169
+ });
170
+
171
+ actor.send({ type: 'something' });
172
+ // logs:
173
+ // {
174
+ // type: 'emitted',
175
+ // some: 'data'
176
+ // }
177
+ ```
178
+ */
179
+ function emit(
180
+ /**
181
+ * The event to emit, or an expression that returns an event to emit.
182
+ */
183
+ eventOrExpr) {
184
+ function emit(args, params) {
185
+ }
186
+ emit.type = 'xstate.emit';
187
+ emit.event = eventOrExpr;
188
+ emit.resolve = resolveEmit;
189
+ emit.execute = executeEmit;
190
+ return emit;
191
+ }
192
+
135
193
  /**
136
194
  *
137
195
  * @remarks
@@ -279,9 +337,6 @@ function executeSendTo(actorScope, params) {
279
337
  */
280
338
  function sendTo(to, eventOrExpr, options) {
281
339
  function sendTo(args, params) {
282
- {
283
- throw new Error(`This isn't supposed to be called`);
284
- }
285
340
  }
286
341
  sendTo.type = 'xsnapshot.sendTo';
287
342
  sendTo.to = to;
@@ -310,22 +365,12 @@ function sendParent(event, options) {
310
365
  * @param options Options to pass into the send action creator.
311
366
  */
312
367
  function forwardTo(target, options) {
313
- if ((!target || typeof target === 'function')) {
314
- const originalTarget = target;
315
- target = (...args) => {
316
- const resolvedTarget = typeof originalTarget === 'function' ? originalTarget(...args) : originalTarget;
317
- if (!resolvedTarget) {
318
- throw new Error(`Attempted to forward event to undefined actor. This risks an infinite loop in the sender.`);
319
- }
320
- return resolvedTarget;
321
- };
322
- }
323
368
  return sendTo(target, ({
324
369
  event
325
370
  }) => event, options);
326
371
  }
327
372
 
328
- function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
373
+ function resolveEnqueueActions(actorScope, snapshot, args, actionParams, {
329
374
  collect
330
375
  }) {
331
376
  const actions = [];
@@ -354,6 +399,9 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
354
399
  enqueue.stopChild = (...args) => {
355
400
  actions.push(stopChild(...args));
356
401
  };
402
+ enqueue.emit = (...args) => {
403
+ actions.push(emit(...args));
404
+ };
357
405
  collect({
358
406
  context: args.context,
359
407
  event: args.event,
@@ -361,7 +409,7 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
361
409
  check: guard => evaluateGuard(guard, snapshot.context, args.event, snapshot),
362
410
  self: actorScope.self,
363
411
  system: actorScope.system
364
- });
412
+ }, actionParams);
365
413
  return [snapshot, undefined, actions];
366
414
  }
367
415
  /**
@@ -386,9 +434,6 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
386
434
  */
387
435
  function enqueueActions(collect) {
388
436
  function enqueueActions(args, params) {
389
- {
390
- throw new Error(`This isn't supposed to be called`);
391
- }
392
437
  }
393
438
  enqueueActions.type = 'xstate.enqueueActions';
394
439
  enqueueActions.collect = collect;
@@ -433,9 +478,6 @@ function log(value = ({
433
478
  event
434
479
  }), label) {
435
480
  function log(args, params) {
436
- {
437
- throw new Error(`This isn't supposed to be called`);
438
- }
439
481
  }
440
482
  log.type = 'xstate.log';
441
483
  log.value = value;
@@ -445,4 +487,4 @@ function log(value = ({
445
487
  return log;
446
488
  }
447
489
 
448
- export { SpecialTargets as S, assign as a, sendTo as b, enqueueActions as e, forwardTo as f, log as l, sendParent as s };
490
+ export { SpecialTargets as S, assign as a, enqueueActions as b, sendTo as c, emit as e, forwardTo as f, log as l, sendParent as s };
@@ -453,6 +453,7 @@ class Actor {
453
453
  this.id = void 0;
454
454
  this.mailbox = new Mailbox(this._process.bind(this));
455
455
  this.observers = new Set();
456
+ this.eventListeners = new Map();
456
457
  this.logger = void 0;
457
458
  /** @internal */
458
459
  this._processingStatus = ProcessingStatus.NotStarted;
@@ -519,6 +520,15 @@ class Actor {
519
520
  throw new Error(`Cannot stop child actor ${child.id} of ${this.id} because it is not a child`);
520
521
  }
521
522
  child._stop();
523
+ },
524
+ emit: emittedEvent => {
525
+ const listeners = this.eventListeners.get(emittedEvent.type);
526
+ if (!listeners) {
527
+ return;
528
+ }
529
+ for (const handler of Array.from(listeners)) {
530
+ handler(emittedEvent);
531
+ }
522
532
  }
523
533
  };
524
534
 
@@ -702,6 +712,20 @@ class Actor {
702
712
  }
703
713
  };
704
714
  }
715
+ on(type, handler) {
716
+ let listeners = this.eventListeners.get(type);
717
+ if (!listeners) {
718
+ listeners = new Set();
719
+ this.eventListeners.set(type, listeners);
720
+ }
721
+ const wrappedHandler = handler.bind(undefined);
722
+ listeners.add(wrappedHandler);
723
+ return {
724
+ unsubscribe: () => {
725
+ listeners.delete(wrappedHandler);
726
+ }
727
+ };
728
+ }
705
729
 
706
730
  /**
707
731
  * Starts the Actor from the initial state
@@ -451,6 +451,7 @@ class Actor {
451
451
  this.id = void 0;
452
452
  this.mailbox = new Mailbox(this._process.bind(this));
453
453
  this.observers = new Set();
454
+ this.eventListeners = new Map();
454
455
  this.logger = void 0;
455
456
  /** @internal */
456
457
  this._processingStatus = ProcessingStatus.NotStarted;
@@ -517,6 +518,15 @@ class Actor {
517
518
  throw new Error(`Cannot stop child actor ${child.id} of ${this.id} because it is not a child`);
518
519
  }
519
520
  child._stop();
521
+ },
522
+ emit: emittedEvent => {
523
+ const listeners = this.eventListeners.get(emittedEvent.type);
524
+ if (!listeners) {
525
+ return;
526
+ }
527
+ for (const handler of Array.from(listeners)) {
528
+ handler(emittedEvent);
529
+ }
520
530
  }
521
531
  };
522
532
 
@@ -700,6 +710,20 @@ class Actor {
700
710
  }
701
711
  };
702
712
  }
713
+ on(type, handler) {
714
+ let listeners = this.eventListeners.get(type);
715
+ if (!listeners) {
716
+ listeners = new Set();
717
+ this.eventListeners.set(type, listeners);
718
+ }
719
+ const wrappedHandler = handler.bind(undefined);
720
+ listeners.add(wrappedHandler);
721
+ return {
722
+ unsubscribe: () => {
723
+ listeners.delete(wrappedHandler);
724
+ }
725
+ };
726
+ }
703
727
 
704
728
  /**
705
729
  * Starts the Actor from the initial state
@@ -454,6 +454,7 @@ class Actor {
454
454
  this.id = void 0;
455
455
  this.mailbox = new Mailbox(this._process.bind(this));
456
456
  this.observers = new Set();
457
+ this.eventListeners = new Map();
457
458
  this.logger = void 0;
458
459
  /** @internal */
459
460
  this._processingStatus = ProcessingStatus.NotStarted;
@@ -520,6 +521,15 @@ class Actor {
520
521
  throw new Error(`Cannot stop child actor ${child.id} of ${this.id} because it is not a child`);
521
522
  }
522
523
  child._stop();
524
+ },
525
+ emit: emittedEvent => {
526
+ const listeners = this.eventListeners.get(emittedEvent.type);
527
+ if (!listeners) {
528
+ return;
529
+ }
530
+ for (const handler of Array.from(listeners)) {
531
+ handler(emittedEvent);
532
+ }
523
533
  }
524
534
  };
525
535
 
@@ -703,6 +713,20 @@ class Actor {
703
713
  }
704
714
  };
705
715
  }
716
+ on(type, handler) {
717
+ let listeners = this.eventListeners.get(type);
718
+ if (!listeners) {
719
+ listeners = new Set();
720
+ this.eventListeners.set(type, listeners);
721
+ }
722
+ const wrappedHandler = handler.bind(undefined);
723
+ listeners.add(wrappedHandler);
724
+ return {
725
+ unsubscribe: () => {
726
+ listeners.delete(wrappedHandler);
727
+ }
728
+ };
729
+ }
706
730
 
707
731
  /**
708
732
  * Starts the Actor from the initial state
@@ -456,6 +456,7 @@ class Actor {
456
456
  this.id = void 0;
457
457
  this.mailbox = new Mailbox(this._process.bind(this));
458
458
  this.observers = new Set();
459
+ this.eventListeners = new Map();
459
460
  this.logger = void 0;
460
461
  /** @internal */
461
462
  this._processingStatus = ProcessingStatus.NotStarted;
@@ -522,6 +523,15 @@ class Actor {
522
523
  throw new Error(`Cannot stop child actor ${child.id} of ${this.id} because it is not a child`);
523
524
  }
524
525
  child._stop();
526
+ },
527
+ emit: emittedEvent => {
528
+ const listeners = this.eventListeners.get(emittedEvent.type);
529
+ if (!listeners) {
530
+ return;
531
+ }
532
+ for (const handler of Array.from(listeners)) {
533
+ handler(emittedEvent);
534
+ }
525
535
  }
526
536
  };
527
537
 
@@ -705,6 +715,20 @@ class Actor {
705
715
  }
706
716
  };
707
717
  }
718
+ on(type, handler) {
719
+ let listeners = this.eventListeners.get(type);
720
+ if (!listeners) {
721
+ listeners = new Set();
722
+ this.eventListeners.set(type, listeners);
723
+ }
724
+ const wrappedHandler = handler.bind(undefined);
725
+ listeners.add(wrappedHandler);
726
+ return {
727
+ unsubscribe: () => {
728
+ listeners.delete(wrappedHandler);
729
+ }
730
+ };
731
+ }
708
732
 
709
733
  /**
710
734
  * Starts the Actor from the initial state
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var actors_dist_xstateActors = require('../actors/dist/xstate-actors.cjs.js');
6
- var guards_dist_xstateGuards = require('./raise-af6a698b.cjs.js');
7
- var log = require('./log-12b88b7e.cjs.js');
6
+ var guards_dist_xstateGuards = require('./raise-182bb5c9.cjs.js');
7
+ var log = require('./log-a312ebfc.cjs.js');
8
8
  require('../dev/dist/xstate-dev.cjs.js');
9
9
 
10
10
  class SimulatedClock {
@@ -756,7 +756,8 @@ function createInertActorScope(actorLogic) {
756
756
  logger: () => {},
757
757
  sessionId: '',
758
758
  stopChild: () => {},
759
- system: self.system
759
+ system: self.system,
760
+ emit: () => {}
760
761
  };
761
762
  return inertActorScope;
762
763
  }
@@ -911,6 +912,7 @@ exports.stopChild = guards_dist_xstateGuards.stopChild;
911
912
  exports.toObserver = guards_dist_xstateGuards.toObserver;
912
913
  exports.SpecialTargets = log.SpecialTargets;
913
914
  exports.assign = log.assign;
915
+ exports.emit = log.emit;
914
916
  exports.enqueueActions = log.enqueueActions;
915
917
  exports.forwardTo = log.forwardTo;
916
918
  exports.log = log.log;
@@ -12,6 +12,7 @@ export {
12
12
  createActor,
13
13
  createEmptyActor,
14
14
  createMachine,
15
+ emit,
15
16
  enqueueActions,
16
17
  forwardTo,
17
18
  fromCallback,