mobx-keystone
Version:
A MobX powered state management solution based on data trees with first class support for TypeScript, snapshots, patches and much more
35 lines (34 loc) • 1.36 kB
TypeScript
import { ActionContext } from './context';
/**
* An action middleware.
*/
export interface ActionMiddleware {
/**
* Subtree root object (object and child objects) this middleware will run for.
* This target "filter" will be run before the custom filter.
*/
readonly subtreeRoot: object;
/**
* A filter function to decide if an action middleware function should be run or not.
*/
filter?: (ctx: ActionContext) => boolean;
/**
* An action middleware function.
* Rember to `return next()` if you want to continue the action or throw if you want to cancel it.
*/
middleware: (ctx: ActionContext, next: () => any) => any;
}
/**
* The disposer of an action middleware.
*/
export type ActionMiddlewareDisposer = () => void;
/**
* Adds a global action middleware to be run when an action is performed.
* It is usually preferable to use `onActionMiddleware` instead to limit it to a given tree and only to topmost level actions
* or `actionTrackingMiddleware` for a simplified middleware.
*
* @param mware Action middleware to be run.
* @returns A disposer to cancel the middleware. Note that if you don't plan to do an early disposal of the middleware
* calling this function becomes optional.
*/
export declare function addActionMiddleware(mware: ActionMiddleware): ActionMiddlewareDisposer;