UNPKG

1.95 kBTypeScriptView Raw
1import { IDisposer, IAnyStateTreeNode, IActionContext } from "../internal";
2export declare type IMiddlewareEventType = "action" | "flow_spawn" | "flow_resume" | "flow_resume_error" | "flow_return" | "flow_throw";
3export interface IMiddlewareEvent extends IActionContext {
4 /** Event type */
5 readonly type: IMiddlewareEventType;
6 /** Parent event unique id */
7 readonly parentId: number;
8 /** Parent event object */
9 readonly parentEvent: IMiddlewareEvent | undefined;
10 /** Root event unique id */
11 readonly rootId: number;
12 /** Id of all events, from root until current (excluding current) */
13 readonly allParentIds: number[];
14}
15export declare type IMiddlewareHandler = (actionCall: IMiddlewareEvent, next: (actionCall: IMiddlewareEvent, callback?: (value: any) => any) => void, abort: (value: any) => void) => any;
16/**
17 * Middleware can be used to intercept any action is invoked on the subtree where it is attached.
18 * If a tree is protected (by default), this means that any mutation of the tree will pass through your middleware.
19 *
20 * For more details, see the [middleware docs](concepts/middleware.md)
21 *
22 * @param target Node to apply the middleware to.
23 * @param middleware Middleware to apply.
24 * @returns A callable function to dispose the middleware.
25 */
26export declare function addMiddleware(target: IAnyStateTreeNode, handler: IMiddlewareHandler, includeHooks?: boolean): IDisposer;
27/**
28 * Binds middleware to a specific action.
29 *
30 * Example:
31 * ```ts
32 * type.actions(self => {
33 * function takeA____() {
34 * self.toilet.donate()
35 * self.wipe()
36 * self.wipe()
37 * self.toilet.flush()
38 * }
39 * return {
40 * takeA____: decorate(atomic, takeA____)
41 * }
42 * })
43 * ```
44 *
45 * @param handler
46 * @param fn
47 * @param includeHooks
48 * @returns The original function
49 */
50export declare function decorate<T extends Function>(handler: IMiddlewareHandler, fn: T, includeHooks?: boolean): T;