UNPKG

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