UNPKG

1.51 kBTypeScriptView Raw
1import { ActionArgs, EventObject, MachineContext, SendExpr, ParameterizedObject, ActionFunction } from "../types.js";
2export interface EmitAction<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TEmitted extends EventObject> {
3 (args: ActionArgs<TContext, TExpressionEvent, TEvent>, params: TParams): void;
4 _out_TEmitted?: TEmitted;
5}
6/**
7 * Emits an event to event handlers registered on the actor via `actor.on(event, handler)`.
8 *
9 * @example
10 ```ts
11 import { emit } from 'xstate';
12
13 const machine = createMachine({
14 // ...
15 on: {
16 something: {
17 actions: emit({
18 type: 'emitted',
19 some: 'data'
20 })
21 }
22 }
23 // ...
24 });
25
26 const actor = createActor(machine).start();
27
28 actor.on('emitted', (event) => {
29 console.log(event);
30 });
31
32 actor.send({ type: 'something' });
33 // logs:
34 // {
35 // type: 'emitted',
36 // some: 'data'
37 // }
38 ```
39 */
40export declare function emit<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TEmitted extends EventObject>(
41/**
42 * The event to emit, or an expression that returns an event to emit.
43 */
44eventOrExpr: TEmitted | SendExpr<TContext, TExpressionEvent, TParams, TEmitted, TEvent>): ActionFunction<TContext, TExpressionEvent, TEvent, TParams, never, never, never, never, TEmitted>;