xstate 5.8.2 → 5.9.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.
Files changed (53) hide show
  1. package/actions/dist/xstate-actions.cjs.js +2 -2
  2. package/actions/dist/xstate-actions.development.cjs.js +2 -2
  3. package/actions/dist/xstate-actions.development.esm.js +2 -2
  4. package/actions/dist/xstate-actions.esm.js +2 -2
  5. package/actions/dist/xstate-actions.umd.min.js +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +1 -1
  8. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  9. package/actors/dist/xstate-actors.development.esm.js +1 -1
  10. package/actors/dist/xstate-actors.esm.js +1 -1
  11. package/actors/dist/xstate-actors.umd.min.js +1 -1
  12. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  13. package/dist/declarations/src/State.d.ts +1 -1
  14. package/dist/declarations/src/StateMachine.d.ts +6 -5
  15. package/dist/declarations/src/StateNode.d.ts +9 -6
  16. package/dist/declarations/src/actions/assign.d.ts +1 -1
  17. package/dist/declarations/src/actions/emit.d.ts +44 -0
  18. package/dist/declarations/src/actions/enqueueActions.d.ts +10 -8
  19. package/dist/declarations/src/actions/raise.d.ts +1 -1
  20. package/dist/declarations/src/actions/send.d.ts +5 -5
  21. package/dist/declarations/src/actions/spawnChild.d.ts +1 -1
  22. package/dist/declarations/src/actions/stopChild.d.ts +2 -2
  23. package/dist/declarations/src/actors/callback.d.ts +1 -1
  24. package/dist/declarations/src/actors/observable.d.ts +1 -1
  25. package/dist/declarations/src/actors/promise.d.ts +2 -2
  26. package/dist/declarations/src/actors/transition.d.ts +3 -3
  27. package/dist/declarations/src/createActor.d.ts +8 -4
  28. package/dist/declarations/src/createMachine.d.ts +4 -4
  29. package/dist/declarations/src/setup.d.ts +5 -5
  30. package/dist/declarations/src/typegenTypes.d.ts +3 -1
  31. package/dist/declarations/src/types.d.ts +77 -66
  32. package/dist/declarations/src/utils.d.ts +2 -2
  33. package/dist/declarations/src/waitFor.d.ts +2 -2
  34. package/dist/{log-abca6761.development.esm.js → log-2a4cc478.esm.js} +67 -25
  35. package/dist/{log-e73cded6.development.cjs.js → log-2db5cc22.cjs.js} +67 -25
  36. package/dist/{log-12b88b7e.cjs.js → log-8273c74a.development.cjs.js} +95 -3
  37. package/dist/{log-ac1ff860.esm.js → log-fc0183d1.development.esm.js} +95 -3
  38. package/dist/{raise-af6a698b.cjs.js → raise-182bb5c9.cjs.js} +24 -0
  39. package/dist/{raise-a7ab421f.esm.js → raise-411df926.esm.js} +24 -0
  40. package/dist/{raise-3f85f1d9.development.esm.js → raise-80cc66b2.development.esm.js} +24 -0
  41. package/dist/{raise-7132462e.development.cjs.js → raise-933cd731.development.cjs.js} +24 -0
  42. package/dist/xstate.cjs.js +4 -3
  43. package/dist/xstate.development.cjs.js +4 -3
  44. package/dist/xstate.development.esm.js +6 -5
  45. package/dist/xstate.esm.js +6 -5
  46. package/dist/xstate.umd.min.js +1 -1
  47. package/dist/xstate.umd.min.js.map +1 -1
  48. package/guards/dist/xstate-guards.cjs.js +1 -1
  49. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  50. package/guards/dist/xstate-guards.development.esm.js +1 -1
  51. package/guards/dist/xstate-guards.esm.js +1 -1
  52. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  53. package/package.json +1 -1
@@ -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;
@@ -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;
@@ -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-a7ab421f.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-80cc66b2.development.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,6 +122,9 @@ 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
+ }
125
128
  }
126
129
  assign.type = 'xstate.assign';
127
130
  assign.assignment = assignment;
@@ -129,6 +132,73 @@ function assign(assignment) {
129
132
  return assign;
130
133
  }
131
134
 
135
+ function resolveEmit(_, snapshot, args, actionParams, {
136
+ event: eventOrExpr
137
+ }) {
138
+ if (typeof eventOrExpr === 'string') {
139
+ throw new Error(`Only event objects may be used with emit; use emit({ type: "${eventOrExpr}" }) instead`);
140
+ }
141
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
142
+ return [snapshot, {
143
+ event: resolvedEvent
144
+ }];
145
+ }
146
+ function executeEmit(actorScope, {
147
+ event
148
+ }) {
149
+ actorScope.defer(() => actorScope.emit(event));
150
+ }
151
+ /**
152
+ * Emits an event to event handlers registered on the actor via `actor.on(event, handler)`.
153
+ *
154
+ * @example
155
+ ```ts
156
+ import { emit } from 'xstate';
157
+
158
+ const machine = createMachine({
159
+ // ...
160
+ on: {
161
+ something: {
162
+ actions: emit({
163
+ type: 'emitted',
164
+ some: 'data'
165
+ })
166
+ }
167
+ }
168
+ // ...
169
+ });
170
+
171
+ const actor = createActor(machine).start();
172
+
173
+ actor.on('emitted', (event) => {
174
+ console.log(event);
175
+ });
176
+
177
+ actor.send({ type: 'something' });
178
+ // logs:
179
+ // {
180
+ // type: 'emitted',
181
+ // some: 'data'
182
+ // }
183
+ ```
184
+ */
185
+ function emit(
186
+ /**
187
+ * The event to emit, or an expression that returns an event to emit.
188
+ */
189
+ eventOrExpr) {
190
+ function emit(args, params) {
191
+ {
192
+ throw new Error(`This isn't supposed to be called`);
193
+ }
194
+ }
195
+ emit.type = 'xstate.emit';
196
+ emit.event = eventOrExpr;
197
+ emit.resolve = resolveEmit;
198
+ emit.execute = executeEmit;
199
+ return emit;
200
+ }
201
+
132
202
  /**
133
203
  *
134
204
  * @remarks
@@ -276,6 +346,9 @@ function executeSendTo(actorScope, params) {
276
346
  */
277
347
  function sendTo(to, eventOrExpr, options) {
278
348
  function sendTo(args, params) {
349
+ {
350
+ throw new Error(`This isn't supposed to be called`);
351
+ }
279
352
  }
280
353
  sendTo.type = 'xsnapshot.sendTo';
281
354
  sendTo.to = to;
@@ -304,12 +377,22 @@ function sendParent(event, options) {
304
377
  * @param options Options to pass into the send action creator.
305
378
  */
306
379
  function forwardTo(target, options) {
380
+ if ((!target || typeof target === 'function')) {
381
+ const originalTarget = target;
382
+ target = (...args) => {
383
+ const resolvedTarget = typeof originalTarget === 'function' ? originalTarget(...args) : originalTarget;
384
+ if (!resolvedTarget) {
385
+ throw new Error(`Attempted to forward event to undefined actor. This risks an infinite loop in the sender.`);
386
+ }
387
+ return resolvedTarget;
388
+ };
389
+ }
307
390
  return sendTo(target, ({
308
391
  event
309
392
  }) => event, options);
310
393
  }
311
394
 
312
- function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
395
+ function resolveEnqueueActions(actorScope, snapshot, args, actionParams, {
313
396
  collect
314
397
  }) {
315
398
  const actions = [];
@@ -338,6 +421,9 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
338
421
  enqueue.stopChild = (...args) => {
339
422
  actions.push(stopChild(...args));
340
423
  };
424
+ enqueue.emit = (...args) => {
425
+ actions.push(emit(...args));
426
+ };
341
427
  collect({
342
428
  context: args.context,
343
429
  event: args.event,
@@ -345,7 +431,7 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
345
431
  check: guard => evaluateGuard(guard, snapshot.context, args.event, snapshot),
346
432
  self: actorScope.self,
347
433
  system: actorScope.system
348
- });
434
+ }, actionParams);
349
435
  return [snapshot, undefined, actions];
350
436
  }
351
437
  /**
@@ -370,6 +456,9 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
370
456
  */
371
457
  function enqueueActions(collect) {
372
458
  function enqueueActions(args, params) {
459
+ {
460
+ throw new Error(`This isn't supposed to be called`);
461
+ }
373
462
  }
374
463
  enqueueActions.type = 'xstate.enqueueActions';
375
464
  enqueueActions.collect = collect;
@@ -414,6 +503,9 @@ function log(value = ({
414
503
  event
415
504
  }), label) {
416
505
  function log(args, params) {
506
+ {
507
+ throw new Error(`This isn't supposed to be called`);
508
+ }
417
509
  }
418
510
  log.type = 'xstate.log';
419
511
  log.value = value;
@@ -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-2db5cc22.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
  }
@@ -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.development.cjs.js');
6
- var guards_dist_xstateGuards = require('./raise-7132462e.development.cjs.js');
7
- var log = require('./log-e73cded6.development.cjs.js');
6
+ var guards_dist_xstateGuards = require('./raise-933cd731.development.cjs.js');
7
+ var log = require('./log-8273c74a.development.cjs.js');
8
8
  require('../dev/dist/xstate-dev.development.cjs.js');
9
9
 
10
10
  class SimulatedClock {
@@ -762,7 +762,8 @@ function createInertActorScope(actorLogic) {
762
762
  logger: () => {},
763
763
  sessionId: '',
764
764
  stopChild: () => {},
765
- system: self.system
765
+ system: self.system,
766
+ emit: () => {}
766
767
  };
767
768
  return inertActorScope;
768
769
  }