@jbrowse/mobx-state-tree 5.7.0 → 5.8.6
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.
- package/README.md +0 -10
- package/dist/index.d.ts +2135 -2
- package/dist/mobx-state-tree.cjs +6894 -0
- package/dist/mobx-state-tree.cjs.map +1 -0
- package/dist/mobx-state-tree.mjs +6814 -0
- package/dist/mobx-state-tree.mjs.map +1 -0
- package/package.json +30 -26
- package/dist/core/action.d.ts +0 -87
- package/dist/core/action.js +0 -228
- package/dist/core/action.js.map +0 -1
- package/dist/core/actionContext.d.ts +0 -27
- package/dist/core/actionContext.js +0 -42
- package/dist/core/actionContext.js.map +0 -1
- package/dist/core/flow.d.ts +0 -69
- package/dist/core/flow.js +0 -180
- package/dist/core/flow.js.map +0 -1
- package/dist/core/json-patch.d.ts +0 -46
- package/dist/core/json-patch.js +0 -133
- package/dist/core/json-patch.js.map +0 -1
- package/dist/core/mst-operations.d.ts +0 -459
- package/dist/core/mst-operations.js +0 -885
- package/dist/core/mst-operations.js.map +0 -1
- package/dist/core/node/BaseNode.d.ts +0 -62
- package/dist/core/node/BaseNode.js +0 -152
- package/dist/core/node/BaseNode.js.map +0 -1
- package/dist/core/node/Hook.d.ts +0 -17
- package/dist/core/node/Hook.js +0 -15
- package/dist/core/node/Hook.js.map +0 -1
- package/dist/core/node/create-node.d.ts +0 -16
- package/dist/core/node/create-node.js +0 -41
- package/dist/core/node/create-node.js.map +0 -1
- package/dist/core/node/identifier-cache.d.ts +0 -19
- package/dist/core/node/identifier-cache.js +0 -115
- package/dist/core/node/identifier-cache.js.map +0 -1
- package/dist/core/node/livelinessChecking.d.ts +0 -37
- package/dist/core/node/livelinessChecking.js +0 -38
- package/dist/core/node/livelinessChecking.js.map +0 -1
- package/dist/core/node/node-utils.d.ts +0 -83
- package/dist/core/node/node-utils.js +0 -165
- package/dist/core/node/node-utils.js.map +0 -1
- package/dist/core/node/object-node.d.ts +0 -101
- package/dist/core/node/object-node.js +0 -546
- package/dist/core/node/object-node.js.map +0 -1
- package/dist/core/node/scalar-node.d.ts +0 -21
- package/dist/core/node/scalar-node.js +0 -90
- package/dist/core/node/scalar-node.js.map +0 -1
- package/dist/core/process.d.ts +0 -50
- package/dist/core/process.js +0 -39
- package/dist/core/process.js.map +0 -1
- package/dist/core/type/type-checker.d.ts +0 -69
- package/dist/core/type/type-checker.js +0 -154
- package/dist/core/type/type-checker.js.map +0 -1
- package/dist/core/type/type.d.ts +0 -317
- package/dist/core/type/type.js +0 -251
- package/dist/core/type/type.js.map +0 -1
- package/dist/index.js +0 -85
- package/dist/index.js.map +0 -1
- package/dist/internal.d.ts +0 -39
- package/dist/internal.js +0 -60
- package/dist/internal.js.map +0 -1
- package/dist/middlewares/create-action-tracking-middleware.d.ts +0 -24
- package/dist/middlewares/create-action-tracking-middleware.js +0 -81
- package/dist/middlewares/create-action-tracking-middleware.js.map +0 -1
- package/dist/middlewares/createActionTrackingMiddleware2.d.ts +0 -34
- package/dist/middlewares/createActionTrackingMiddleware2.js +0 -133
- package/dist/middlewares/createActionTrackingMiddleware2.js.map +0 -1
- package/dist/middlewares/on-action.d.ts +0 -87
- package/dist/middlewares/on-action.js +0 -215
- package/dist/middlewares/on-action.js.map +0 -1
- package/dist/package.json +0 -1
- package/dist/types/complex-types/array.d.ts +0 -81
- package/dist/types/complex-types/array.js +0 -353
- package/dist/types/complex-types/array.js.map +0 -1
- package/dist/types/complex-types/map.d.ts +0 -111
- package/dist/types/complex-types/map.js +0 -362
- package/dist/types/complex-types/map.js.map +0 -1
- package/dist/types/complex-types/model.d.ts +0 -193
- package/dist/types/complex-types/model.js +0 -478
- package/dist/types/complex-types/model.js.map +0 -1
- package/dist/types/index.d.ts +0 -33
- package/dist/types/index.js +0 -38
- package/dist/types/index.js.map +0 -1
- package/dist/types/primitives.d.ts +0 -125
- package/dist/types/primitives.js +0 -183
- package/dist/types/primitives.js.map +0 -1
- package/dist/types/utility-types/custom.d.ts +0 -75
- package/dist/types/utility-types/custom.js +0 -111
- package/dist/types/utility-types/custom.js.map +0 -1
- package/dist/types/utility-types/enumeration.d.ts +0 -5
- package/dist/types/utility-types/enumeration.js +0 -34
- package/dist/types/utility-types/enumeration.js.map +0 -1
- package/dist/types/utility-types/frozen.d.ts +0 -24
- package/dist/types/utility-types/frozen.js +0 -98
- package/dist/types/utility-types/frozen.js.map +0 -1
- package/dist/types/utility-types/identifier.d.ts +0 -87
- package/dist/types/utility-types/identifier.js +0 -130
- package/dist/types/utility-types/identifier.js.map +0 -1
- package/dist/types/utility-types/late.d.ts +0 -10
- package/dist/types/utility-types/late.js +0 -110
- package/dist/types/utility-types/late.js.map +0 -1
- package/dist/types/utility-types/lazy.d.ts +0 -23
- package/dist/types/utility-types/lazy.js +0 -77
- package/dist/types/utility-types/lazy.js.map +0 -1
- package/dist/types/utility-types/literal.d.ts +0 -38
- package/dist/types/utility-types/literal.js +0 -64
- package/dist/types/utility-types/literal.js.map +0 -1
- package/dist/types/utility-types/maybe.d.ts +0 -26
- package/dist/types/utility-types/maybe.js +0 -30
- package/dist/types/utility-types/maybe.js.map +0 -1
- package/dist/types/utility-types/optional.d.ts +0 -42
- package/dist/types/utility-types/optional.js +0 -141
- package/dist/types/utility-types/optional.js.map +0 -1
- package/dist/types/utility-types/reference.d.ts +0 -90
- package/dist/types/utility-types/reference.js +0 -393
- package/dist/types/utility-types/reference.js.map +0 -1
- package/dist/types/utility-types/refinement.d.ts +0 -10
- package/dist/types/utility-types/refinement.js +0 -86
- package/dist/types/utility-types/refinement.js.map +0 -1
- package/dist/types/utility-types/resilient.d.ts +0 -18
- package/dist/types/utility-types/resilient.js +0 -121
- package/dist/types/utility-types/resilient.js.map +0 -1
- package/dist/types/utility-types/snapshotProcessor.d.ts +0 -63
- package/dist/types/utility-types/snapshotProcessor.js +0 -162
- package/dist/types/utility-types/snapshotProcessor.js.map +0 -1
- package/dist/types/utility-types/union.d.ts +0 -78
- package/dist/types/utility-types/union.js +0 -246
- package/dist/types/utility-types/union.js.map +0 -1
- package/dist/utils.d.ts +0 -230
- package/dist/utils.js +0 -483
- package/dist/utils.js.map +0 -1
- package/esm/core/action.d.ts +0 -87
- package/esm/core/action.js +0 -219
- package/esm/core/action.js.map +0 -1
- package/esm/core/actionContext.d.ts +0 -27
- package/esm/core/actionContext.js +0 -37
- package/esm/core/actionContext.js.map +0 -1
- package/esm/core/flow.d.ts +0 -69
- package/esm/core/flow.js +0 -173
- package/esm/core/flow.js.map +0 -1
- package/esm/core/json-patch.d.ts +0 -46
- package/esm/core/json-patch.js +0 -125
- package/esm/core/json-patch.js.map +0 -1
- package/esm/core/mst-operations.d.ts +0 -459
- package/esm/core/mst-operations.js +0 -844
- package/esm/core/mst-operations.js.map +0 -1
- package/esm/core/node/BaseNode.d.ts +0 -62
- package/esm/core/node/BaseNode.js +0 -148
- package/esm/core/node/BaseNode.js.map +0 -1
- package/esm/core/node/Hook.d.ts +0 -17
- package/esm/core/node/Hook.js +0 -12
- package/esm/core/node/Hook.js.map +0 -1
- package/esm/core/node/create-node.d.ts +0 -16
- package/esm/core/node/create-node.js +0 -36
- package/esm/core/node/create-node.js.map +0 -1
- package/esm/core/node/identifier-cache.d.ts +0 -19
- package/esm/core/node/identifier-cache.js +0 -111
- package/esm/core/node/identifier-cache.js.map +0 -1
- package/esm/core/node/livelinessChecking.d.ts +0 -37
- package/esm/core/node/livelinessChecking.js +0 -33
- package/esm/core/node/livelinessChecking.js.map +0 -1
- package/esm/core/node/node-utils.d.ts +0 -83
- package/esm/core/node/node-utils.js +0 -153
- package/esm/core/node/node-utils.js.map +0 -1
- package/esm/core/node/object-node.d.ts +0 -101
- package/esm/core/node/object-node.js +0 -542
- package/esm/core/node/object-node.js.map +0 -1
- package/esm/core/node/scalar-node.d.ts +0 -21
- package/esm/core/node/scalar-node.js +0 -86
- package/esm/core/node/scalar-node.js.map +0 -1
- package/esm/core/process.d.ts +0 -50
- package/esm/core/process.js +0 -35
- package/esm/core/process.js.map +0 -1
- package/esm/core/type/type-checker.d.ts +0 -69
- package/esm/core/type/type-checker.js +0 -144
- package/esm/core/type/type-checker.js.map +0 -1
- package/esm/core/type/type.d.ts +0 -317
- package/esm/core/type/type.js +0 -243
- package/esm/core/type/type.js.map +0 -1
- package/esm/index.d.ts +0 -2
- package/esm/index.js +0 -2
- package/esm/index.js.map +0 -1
- package/esm/internal.d.ts +0 -39
- package/esm/internal.js +0 -44
- package/esm/internal.js.map +0 -1
- package/esm/middlewares/create-action-tracking-middleware.d.ts +0 -24
- package/esm/middlewares/create-action-tracking-middleware.js +0 -78
- package/esm/middlewares/create-action-tracking-middleware.js.map +0 -1
- package/esm/middlewares/createActionTrackingMiddleware2.d.ts +0 -34
- package/esm/middlewares/createActionTrackingMiddleware2.js +0 -130
- package/esm/middlewares/createActionTrackingMiddleware2.js.map +0 -1
- package/esm/middlewares/on-action.d.ts +0 -87
- package/esm/middlewares/on-action.js +0 -210
- package/esm/middlewares/on-action.js.map +0 -1
- package/esm/types/complex-types/array.d.ts +0 -81
- package/esm/types/complex-types/array.js +0 -347
- package/esm/types/complex-types/array.js.map +0 -1
- package/esm/types/complex-types/map.d.ts +0 -111
- package/esm/types/complex-types/map.js +0 -356
- package/esm/types/complex-types/map.js.map +0 -1
- package/esm/types/complex-types/model.d.ts +0 -193
- package/esm/types/complex-types/model.js +0 -471
- package/esm/types/complex-types/model.js.map +0 -1
- package/esm/types/index.d.ts +0 -33
- package/esm/types/index.js +0 -35
- package/esm/types/index.js.map +0 -1
- package/esm/types/primitives.d.ts +0 -125
- package/esm/types/primitives.js +0 -177
- package/esm/types/primitives.js.map +0 -1
- package/esm/types/utility-types/custom.d.ts +0 -75
- package/esm/types/utility-types/custom.js +0 -106
- package/esm/types/utility-types/custom.js.map +0 -1
- package/esm/types/utility-types/enumeration.d.ts +0 -5
- package/esm/types/utility-types/enumeration.js +0 -31
- package/esm/types/utility-types/enumeration.js.map +0 -1
- package/esm/types/utility-types/frozen.d.ts +0 -24
- package/esm/types/utility-types/frozen.js +0 -92
- package/esm/types/utility-types/frozen.js.map +0 -1
- package/esm/types/utility-types/identifier.d.ts +0 -87
- package/esm/types/utility-types/identifier.js +0 -121
- package/esm/types/utility-types/identifier.js.map +0 -1
- package/esm/types/utility-types/late.d.ts +0 -10
- package/esm/types/utility-types/late.js +0 -106
- package/esm/types/utility-types/late.js.map +0 -1
- package/esm/types/utility-types/lazy.d.ts +0 -23
- package/esm/types/utility-types/lazy.js +0 -72
- package/esm/types/utility-types/lazy.js.map +0 -1
- package/esm/types/utility-types/literal.d.ts +0 -38
- package/esm/types/utility-types/literal.js +0 -58
- package/esm/types/utility-types/literal.js.map +0 -1
- package/esm/types/utility-types/maybe.d.ts +0 -26
- package/esm/types/utility-types/maybe.js +0 -26
- package/esm/types/utility-types/maybe.js.map +0 -1
- package/esm/types/utility-types/optional.d.ts +0 -42
- package/esm/types/utility-types/optional.js +0 -135
- package/esm/types/utility-types/optional.js.map +0 -1
- package/esm/types/utility-types/reference.d.ts +0 -90
- package/esm/types/utility-types/reference.js +0 -383
- package/esm/types/utility-types/reference.js.map +0 -1
- package/esm/types/utility-types/refinement.d.ts +0 -10
- package/esm/types/utility-types/refinement.js +0 -82
- package/esm/types/utility-types/refinement.js.map +0 -1
- package/esm/types/utility-types/resilient.d.ts +0 -18
- package/esm/types/utility-types/resilient.js +0 -118
- package/esm/types/utility-types/resilient.js.map +0 -1
- package/esm/types/utility-types/snapshotProcessor.d.ts +0 -63
- package/esm/types/utility-types/snapshotProcessor.js +0 -159
- package/esm/types/utility-types/snapshotProcessor.js.map +0 -1
- package/esm/types/utility-types/union.d.ts +0 -78
- package/esm/types/utility-types/union.js +0 -240
- package/esm/types/utility-types/union.js.map +0 -1
- package/esm/utils.d.ts +0 -230
- package/esm/utils.js +0 -449
- package/esm/utils.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,50 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jbrowse/mobx-state-tree",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.8.6",
|
|
4
4
|
"description": "Fork of mobx-state-tree starting from v5.4.2 for use in jbrowse 2",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"url": "git+https://github.com/GMOD/mobx-state-tree.git"
|
|
9
|
-
},
|
|
10
|
-
"type": "module",
|
|
11
|
-
"main": "dist/index.js",
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"main": "./dist/mobx-state-tree.cjs",
|
|
7
|
+
"module": "./dist/mobx-state-tree.mjs",
|
|
12
8
|
"exports": {
|
|
13
|
-
"
|
|
14
|
-
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/mobx-state-tree.mjs",
|
|
12
|
+
"require": "./dist/mobx-state-tree.cjs"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
15
|
},
|
|
16
16
|
"sideEffects": false,
|
|
17
|
-
"author": "Michel Weststrate",
|
|
18
|
-
"files": [
|
|
19
|
-
"esm",
|
|
20
|
-
"dist"
|
|
21
|
-
],
|
|
22
17
|
"scripts": {
|
|
23
|
-
"clean": "rimraf esm dist",
|
|
24
|
-
"format": "prettier --write .",
|
|
25
18
|
"lint": "eslint --report-unused-disable-directives --max-warnings 0",
|
|
19
|
+
"clean": "rimraf dist lib",
|
|
26
20
|
"prebuild": "pnpm clean",
|
|
27
|
-
"
|
|
28
|
-
"build
|
|
29
|
-
"postbuild:es5": "echo '{\"type\": \"commonjs\"}' > dist/package.json",
|
|
30
|
-
"build": "pnpm build:esm && pnpm build:es5",
|
|
21
|
+
"format": "pnpm prettier --write .",
|
|
22
|
+
"build": "tsc && rollup -c",
|
|
31
23
|
"test": "vitest",
|
|
32
24
|
"test-ci": "pnpm test:dev --run && pnpm test:prod --run",
|
|
33
|
-
"test:dev": "NODE_ENV=development vitest",
|
|
34
|
-
"test:prod": "NODE_ENV=production vitest",
|
|
25
|
+
"test:dev": "cross-env NODE_ENV=development vitest",
|
|
26
|
+
"test:prod": "cross-env NODE_ENV=production vitest",
|
|
35
27
|
"prebench": "./scripts/build-both-branches.sh",
|
|
36
|
-
"bench": "NODE_ENV=production vitest bench --run",
|
|
28
|
+
"bench": "cross-env NODE_ENV=production vitest bench --run",
|
|
37
29
|
"preversion": "pnpm build && pnpm test-ci",
|
|
38
30
|
"postversion": "git push --follow-tags"
|
|
39
31
|
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/GMOD/mobx-state-tree.git"
|
|
35
|
+
},
|
|
36
|
+
"author": "Michel Weststrate",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"files": [
|
|
39
|
+
"dist/"
|
|
40
|
+
],
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"@eslint/js": "^10.0.1",
|
|
42
|
-
"
|
|
43
|
+
"cross-env": "^10.1.0",
|
|
43
44
|
"eslint": "^10.4.0",
|
|
44
45
|
"eslint-plugin-import": "^2.32.0",
|
|
45
46
|
"mobx": "^6.15.3",
|
|
46
47
|
"prettier": "^3.8.3",
|
|
47
|
-
"rimraf": "^6.
|
|
48
|
+
"rimraf": "^6.1.3",
|
|
49
|
+
"rollup": "^4.60.4",
|
|
50
|
+
"rollup-plugin-dts": "^6.4.1",
|
|
51
|
+
"rollup-plugin-sourcemaps2": "^0.5.7",
|
|
48
52
|
"spec.ts": "^1.1.3",
|
|
49
53
|
"typescript": "^6.0.3",
|
|
50
54
|
"typescript-eslint": "^8.59.4",
|
package/dist/core/action.d.ts
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import type { IActionContext, IAnyStateTreeNode, IDisposer } from "../internal.ts";
|
|
2
|
-
export type IMiddlewareEventType = "action" | "flow_spawn" | "flow_resume" | "flow_resume_error" | "flow_return" | "flow_throw";
|
|
3
|
-
export 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
|
-
}
|
|
15
|
-
export interface FunctionWithFlag extends Function {
|
|
16
|
-
_isMSTAction?: boolean;
|
|
17
|
-
_isFlowAction?: boolean;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* @internal
|
|
21
|
-
* @hidden
|
|
22
|
-
*/
|
|
23
|
-
export type IMiddleware = {
|
|
24
|
-
handler: IMiddlewareHandler;
|
|
25
|
-
includeHooks: boolean;
|
|
26
|
-
};
|
|
27
|
-
export type IMiddlewareHandler = (actionCall: IMiddlewareEvent, next: (actionCall: IMiddlewareEvent, callback?: (value: any) => any) => void, abort: (value: any) => void) => any;
|
|
28
|
-
/**
|
|
29
|
-
* @internal
|
|
30
|
-
* @hidden
|
|
31
|
-
*/
|
|
32
|
-
export declare function getCurrentActionContext(): IMiddlewareEvent | undefined;
|
|
33
|
-
/**
|
|
34
|
-
* @internal
|
|
35
|
-
* @hidden
|
|
36
|
-
*/
|
|
37
|
-
export declare function getNextActionId(): number;
|
|
38
|
-
/**
|
|
39
|
-
* @internal
|
|
40
|
-
* @hidden
|
|
41
|
-
*/
|
|
42
|
-
export declare function runWithActionContext(context: IMiddlewareEvent, fn: Function): any;
|
|
43
|
-
/**
|
|
44
|
-
* @internal
|
|
45
|
-
* @hidden
|
|
46
|
-
*/
|
|
47
|
-
export declare function getParentActionContext(parentContext: IMiddlewareEvent | undefined): IMiddlewareEvent | undefined;
|
|
48
|
-
/**
|
|
49
|
-
* @internal
|
|
50
|
-
* @hidden
|
|
51
|
-
*/
|
|
52
|
-
export declare function createActionInvoker<T extends FunctionWithFlag>(target: IAnyStateTreeNode, name: string, fn: T): (...args: any[]) => any;
|
|
53
|
-
/**
|
|
54
|
-
* Middleware can be used to intercept any action is invoked on the subtree where it is attached.
|
|
55
|
-
* If a tree is protected (by default), this means that any mutation of the tree will pass through your middleware.
|
|
56
|
-
*
|
|
57
|
-
* For more details, see the [middleware docs](concepts/middleware.md)
|
|
58
|
-
*
|
|
59
|
-
* @param target Node to apply the middleware to.
|
|
60
|
-
* @param middleware Middleware to apply.
|
|
61
|
-
* @returns A callable function to dispose the middleware.
|
|
62
|
-
*/
|
|
63
|
-
export declare function addMiddleware(target: IAnyStateTreeNode, handler: IMiddlewareHandler, includeHooks?: boolean): IDisposer;
|
|
64
|
-
/**
|
|
65
|
-
* Binds middleware to a specific action.
|
|
66
|
-
*
|
|
67
|
-
* Example:
|
|
68
|
-
* ```ts
|
|
69
|
-
* type.actions(self => {
|
|
70
|
-
* function takeA____() {
|
|
71
|
-
* self.toilet.donate()
|
|
72
|
-
* self.wipe()
|
|
73
|
-
* self.wipe()
|
|
74
|
-
* self.toilet.flush()
|
|
75
|
-
* }
|
|
76
|
-
* return {
|
|
77
|
-
* takeA____: decorate(atomic, takeA____)
|
|
78
|
-
* }
|
|
79
|
-
* })
|
|
80
|
-
* ```
|
|
81
|
-
*
|
|
82
|
-
* @param handler
|
|
83
|
-
* @param fn
|
|
84
|
-
* @param includeHooks
|
|
85
|
-
* @returns The original function
|
|
86
|
-
*/
|
|
87
|
-
export declare function decorate<T extends Function>(handler: IMiddlewareHandler, fn: T, includeHooks?: boolean): T;
|
package/dist/core/action.js
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getCurrentActionContext = getCurrentActionContext;
|
|
4
|
-
exports.getNextActionId = getNextActionId;
|
|
5
|
-
exports.runWithActionContext = runWithActionContext;
|
|
6
|
-
exports.getParentActionContext = getParentActionContext;
|
|
7
|
-
exports.createActionInvoker = createActionInvoker;
|
|
8
|
-
exports.addMiddleware = addMiddleware;
|
|
9
|
-
exports.decorate = decorate;
|
|
10
|
-
const mobx_1 = require("mobx");
|
|
11
|
-
const internal_ts_1 = require("../internal.js");
|
|
12
|
-
let nextActionId = 1;
|
|
13
|
-
let currentActionContext;
|
|
14
|
-
/**
|
|
15
|
-
* @internal
|
|
16
|
-
* @hidden
|
|
17
|
-
*/
|
|
18
|
-
function getCurrentActionContext() {
|
|
19
|
-
return currentActionContext;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* @internal
|
|
23
|
-
* @hidden
|
|
24
|
-
*/
|
|
25
|
-
function getNextActionId() {
|
|
26
|
-
return nextActionId++;
|
|
27
|
-
}
|
|
28
|
-
// TODO: optimize away entire action context if there is no middleware in tree?
|
|
29
|
-
/**
|
|
30
|
-
* @internal
|
|
31
|
-
* @hidden
|
|
32
|
-
*/
|
|
33
|
-
function runWithActionContext(context, fn) {
|
|
34
|
-
const node = (0, internal_ts_1.getStateTreeNode)(context.context);
|
|
35
|
-
if (context.type === "action") {
|
|
36
|
-
node.assertAlive({
|
|
37
|
-
actionContext: context
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
const baseIsRunningAction = node._isRunningAction;
|
|
41
|
-
node._isRunningAction = true;
|
|
42
|
-
const previousContext = currentActionContext;
|
|
43
|
-
currentActionContext = context;
|
|
44
|
-
try {
|
|
45
|
-
return runMiddleWares(node, context, fn);
|
|
46
|
-
}
|
|
47
|
-
finally {
|
|
48
|
-
currentActionContext = previousContext;
|
|
49
|
-
node._isRunningAction = baseIsRunningAction;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* @internal
|
|
54
|
-
* @hidden
|
|
55
|
-
*/
|
|
56
|
-
function getParentActionContext(parentContext) {
|
|
57
|
-
if (!parentContext) {
|
|
58
|
-
return undefined;
|
|
59
|
-
}
|
|
60
|
-
if (parentContext.type === "action") {
|
|
61
|
-
return parentContext;
|
|
62
|
-
}
|
|
63
|
-
return parentContext.parentActionEvent;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* @internal
|
|
67
|
-
* @hidden
|
|
68
|
-
*/
|
|
69
|
-
function createActionInvoker(target, name, fn) {
|
|
70
|
-
const res = function (...args) {
|
|
71
|
-
const id = getNextActionId();
|
|
72
|
-
const parentContext = currentActionContext;
|
|
73
|
-
const parentActionContext = getParentActionContext(parentContext);
|
|
74
|
-
return runWithActionContext({
|
|
75
|
-
type: "action",
|
|
76
|
-
name,
|
|
77
|
-
id,
|
|
78
|
-
args,
|
|
79
|
-
context: target,
|
|
80
|
-
tree: (0, internal_ts_1.getRoot)(target),
|
|
81
|
-
rootId: parentContext ? parentContext.rootId : id,
|
|
82
|
-
parentId: parentContext ? parentContext.id : 0,
|
|
83
|
-
allParentIds: parentContext
|
|
84
|
-
? [...parentContext.allParentIds, parentContext.id]
|
|
85
|
-
: [],
|
|
86
|
-
parentEvent: parentContext,
|
|
87
|
-
parentActionEvent: parentActionContext
|
|
88
|
-
}, fn);
|
|
89
|
-
};
|
|
90
|
-
res._isMSTAction = true;
|
|
91
|
-
res._isFlowAction = fn._isFlowAction;
|
|
92
|
-
return res;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Middleware can be used to intercept any action is invoked on the subtree where it is attached.
|
|
96
|
-
* If a tree is protected (by default), this means that any mutation of the tree will pass through your middleware.
|
|
97
|
-
*
|
|
98
|
-
* For more details, see the [middleware docs](concepts/middleware.md)
|
|
99
|
-
*
|
|
100
|
-
* @param target Node to apply the middleware to.
|
|
101
|
-
* @param middleware Middleware to apply.
|
|
102
|
-
* @returns A callable function to dispose the middleware.
|
|
103
|
-
*/
|
|
104
|
-
function addMiddleware(target, handler, includeHooks = true) {
|
|
105
|
-
const node = (0, internal_ts_1.getStateTreeNode)(target);
|
|
106
|
-
if ((0, internal_ts_1.devMode)()) {
|
|
107
|
-
if (!node.isProtectionEnabled) {
|
|
108
|
-
(0, internal_ts_1.warnError)("It is recommended to protect the state tree before attaching action middleware, as otherwise it cannot be guaranteed that all changes are passed through middleware. See `protect`");
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return node.addMiddleWare(handler, includeHooks);
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Binds middleware to a specific action.
|
|
115
|
-
*
|
|
116
|
-
* Example:
|
|
117
|
-
* ```ts
|
|
118
|
-
* type.actions(self => {
|
|
119
|
-
* function takeA____() {
|
|
120
|
-
* self.toilet.donate()
|
|
121
|
-
* self.wipe()
|
|
122
|
-
* self.wipe()
|
|
123
|
-
* self.toilet.flush()
|
|
124
|
-
* }
|
|
125
|
-
* return {
|
|
126
|
-
* takeA____: decorate(atomic, takeA____)
|
|
127
|
-
* }
|
|
128
|
-
* })
|
|
129
|
-
* ```
|
|
130
|
-
*
|
|
131
|
-
* @param handler
|
|
132
|
-
* @param fn
|
|
133
|
-
* @param includeHooks
|
|
134
|
-
* @returns The original function
|
|
135
|
-
*/
|
|
136
|
-
function decorate(handler, fn, includeHooks = true) {
|
|
137
|
-
const middleware = { handler, includeHooks };
|
|
138
|
-
fn.$mst_middleware = fn.$mst_middleware || [];
|
|
139
|
-
fn.$mst_middleware.push(middleware);
|
|
140
|
-
return fn;
|
|
141
|
-
}
|
|
142
|
-
class CollectedMiddlewares {
|
|
143
|
-
arrayIndex = 0;
|
|
144
|
-
inArrayIndex = 0;
|
|
145
|
-
middlewares = [];
|
|
146
|
-
constructor(node, fn) {
|
|
147
|
-
// we just push middleware arrays into an array of arrays to avoid making copies
|
|
148
|
-
if (fn.$mst_middleware) {
|
|
149
|
-
this.middlewares.push(fn.$mst_middleware);
|
|
150
|
-
}
|
|
151
|
-
let n = node;
|
|
152
|
-
// Find all middlewares. Optimization: cache this?
|
|
153
|
-
while (n) {
|
|
154
|
-
if (n.middlewares) {
|
|
155
|
-
this.middlewares.push(n.middlewares);
|
|
156
|
-
}
|
|
157
|
-
n = n.parent;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
get isEmpty() {
|
|
161
|
-
return this.middlewares.length <= 0;
|
|
162
|
-
}
|
|
163
|
-
getNextMiddleware() {
|
|
164
|
-
const array = this.middlewares[this.arrayIndex];
|
|
165
|
-
if (!array) {
|
|
166
|
-
return undefined;
|
|
167
|
-
}
|
|
168
|
-
const item = array[this.inArrayIndex++];
|
|
169
|
-
if (!item) {
|
|
170
|
-
this.arrayIndex++;
|
|
171
|
-
this.inArrayIndex = 0;
|
|
172
|
-
return this.getNextMiddleware();
|
|
173
|
-
}
|
|
174
|
-
return item;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
function runMiddleWares(node, baseCall, originalFn) {
|
|
178
|
-
const middlewares = new CollectedMiddlewares(node, originalFn);
|
|
179
|
-
// Short circuit
|
|
180
|
-
if (middlewares.isEmpty) {
|
|
181
|
-
return (0, mobx_1.action)(originalFn)(...baseCall.args);
|
|
182
|
-
}
|
|
183
|
-
let result = null;
|
|
184
|
-
function runNextMiddleware(call) {
|
|
185
|
-
const middleware = middlewares.getNextMiddleware();
|
|
186
|
-
const handler = middleware && middleware.handler;
|
|
187
|
-
if (!handler) {
|
|
188
|
-
return (0, mobx_1.action)(originalFn)(...call.args);
|
|
189
|
-
}
|
|
190
|
-
// skip hooks if asked to
|
|
191
|
-
if (!middleware.includeHooks && internal_ts_1.Hook[call.name]) {
|
|
192
|
-
return runNextMiddleware(call);
|
|
193
|
-
}
|
|
194
|
-
let nextInvoked = false;
|
|
195
|
-
function next(call2, callback) {
|
|
196
|
-
nextInvoked = true;
|
|
197
|
-
// the result can contain
|
|
198
|
-
// - the non manipulated return value from an action
|
|
199
|
-
// - the non manipulated abort value
|
|
200
|
-
// - one of the above but manipulated through the callback function
|
|
201
|
-
result = runNextMiddleware(call2);
|
|
202
|
-
if (callback) {
|
|
203
|
-
result = callback(result);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
let abortInvoked = false;
|
|
207
|
-
function abort(value) {
|
|
208
|
-
abortInvoked = true;
|
|
209
|
-
// overwrite the result
|
|
210
|
-
// can be manipulated through middlewares earlier in the queue using the callback fn
|
|
211
|
-
result = value;
|
|
212
|
-
}
|
|
213
|
-
handler(call, next, abort);
|
|
214
|
-
if ((0, internal_ts_1.devMode)()) {
|
|
215
|
-
if (!nextInvoked && !abortInvoked) {
|
|
216
|
-
const node2 = (0, internal_ts_1.getStateTreeNode)(call.tree);
|
|
217
|
-
throw (0, internal_ts_1.fail)(`Neither the next() nor the abort() callback within the middleware ${handler.name} for the action: "${call.name}" on the node: ${node2.type.name} was invoked.`);
|
|
218
|
-
}
|
|
219
|
-
else if (nextInvoked && abortInvoked) {
|
|
220
|
-
const node2 = (0, internal_ts_1.getStateTreeNode)(call.tree);
|
|
221
|
-
throw (0, internal_ts_1.fail)(`The next() and abort() callback within the middleware ${handler.name} for the action: "${call.name}" on the node: ${node2.type.name} were invoked.`);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
return result;
|
|
225
|
-
}
|
|
226
|
-
return runNextMiddleware(baseCall);
|
|
227
|
-
}
|
|
228
|
-
//# sourceMappingURL=action.js.map
|
package/dist/core/action.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"action.js","sourceRoot":"","sources":["../../src/core/action.ts"],"names":[],"mappings":";;AAqEA,0DAEC;AAMD,0CAEC;AAOD,oDAmBC;AAMD,wDAUC;AAMD,kDAgCC;AAYD,sCAcC;AAyBD,4BASC;AA3ND,+BAA2C;AAE3C,gDAOuB;AAqDvB,IAAI,YAAY,GAAG,CAAC,CAAA;AACpB,IAAI,oBAAkD,CAAA;AAEtD;;;GAGG;AACH,SAAgB,uBAAuB;IACrC,OAAO,oBAAoB,CAAA;AAC7B,CAAC;AAED;;;GAGG;AACH,SAAgB,eAAe;IAC7B,OAAO,YAAY,EAAE,CAAA;AACvB,CAAC;AAED,+EAA+E;AAC/E;;;GAGG;AACH,SAAgB,oBAAoB,CAAC,OAAyB,EAAE,EAAY;I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1B,IAAI,IAAA,qBAAO,GAAE,EAAE,CAAC;YACd,IAAI,CAAC,WAAW,IAAI,CAAC,YAAY,EAAE,CAAC;gBAClC,MAAM,KAAK,GAAG,IAAA,8BAAgB,EAAC,IAAI,CAAC,IAAI,CAAC,CAAA;gBACzC,MAAM,IAAA,kBAAI,EACR,qEAAqE,OAAO,CAAC,IAAI,qBAAqB,IAAI,CAAC,IAAI,kBAAkB,KAAK,CAAC,IAAI,CAAC,IAAI,eAAe,CAChK,CAAA;YACH,CAAC;iBAAM,IAAI,WAAW,IAAI,YAAY,EAAE,CAAC;gBACvC,MAAM,KAAK,GAAG,IAAA,8BAAgB,EAAC,IAAI,CAAC,IAAI,CAAC,CAAA;gBACzC,MAAM,IAAA,kBAAI,EACR,yDAAyD,OAAO,CAAC,IAAI,qBAAqB,IAAI,CAAC,IAAI,kBAAkB,KAAK,CAAC,IAAI,CAAC,IAAI,gBAAgB,CACrJ,CAAA;YACH,CAAC;QACH,CAAC;QACD,OAAO,MAAM,CAAA;IACf,CAAC;IACD,OAAO,iBAAiB,CAAC,QAAQ,CAAC,CAAA;AACpC,CAAC","sourcesContent":["import { action as mobxAction } from \"mobx\"\n\nimport {\n Hook,\n devMode,\n fail,\n getRoot,\n getStateTreeNode,\n warnError\n} from \"../internal.ts\"\n\nimport type {\n AnyObjectNode,\n IActionContext,\n IAnyStateTreeNode,\n IDisposer\n} from \"../internal.ts\"\n\nexport type IMiddlewareEventType =\n | \"action\"\n | \"flow_spawn\"\n | \"flow_resume\"\n | \"flow_resume_error\"\n | \"flow_return\"\n | \"flow_throw\"\n// | \"task_spawn TODO, see #273\"\n\nexport interface IMiddlewareEvent extends IActionContext {\n /** Event type */\n readonly type: IMiddlewareEventType\n\n /** Parent event unique id */\n readonly parentId: number\n /** Parent event object */\n readonly parentEvent: IMiddlewareEvent | undefined\n\n /** Root event unique id */\n readonly rootId: number\n /** Id of all events, from root until current (excluding current) */\n readonly allParentIds: number[]\n}\n\nexport interface FunctionWithFlag extends Function {\n _isMSTAction?: boolean\n _isFlowAction?: boolean\n}\n\n/**\n * @internal\n * @hidden\n */\nexport type IMiddleware = {\n handler: IMiddlewareHandler\n includeHooks: boolean\n}\n\nexport type IMiddlewareHandler = (\n actionCall: IMiddlewareEvent,\n next: (actionCall: IMiddlewareEvent, callback?: (value: any) => any) => void,\n abort: (value: any) => void\n) => any\n\nlet nextActionId = 1\nlet currentActionContext: IMiddlewareEvent | undefined\n\n/**\n * @internal\n * @hidden\n */\nexport function getCurrentActionContext() {\n return currentActionContext\n}\n\n/**\n * @internal\n * @hidden\n */\nexport function getNextActionId() {\n return nextActionId++\n}\n\n// TODO: optimize away entire action context if there is no middleware in tree?\n/**\n * @internal\n * @hidden\n */\nexport function runWithActionContext(context: IMiddlewareEvent, fn: Function) {\n const node = getStateTreeNode(context.context)\n\n if (context.type === \"action\") {\n node.assertAlive({\n actionContext: context\n })\n }\n\n const baseIsRunningAction = node._isRunningAction\n node._isRunningAction = true\n const previousContext = currentActionContext\n currentActionContext = context\n try {\n return runMiddleWares(node, context, fn)\n } finally {\n currentActionContext = previousContext\n node._isRunningAction = baseIsRunningAction\n }\n}\n\n/**\n * @internal\n * @hidden\n */\nexport function getParentActionContext(\n parentContext: IMiddlewareEvent | undefined\n) {\n if (!parentContext) {\n return undefined\n }\n if (parentContext.type === \"action\") {\n return parentContext\n }\n return parentContext.parentActionEvent\n}\n\n/**\n * @internal\n * @hidden\n */\nexport function createActionInvoker<T extends FunctionWithFlag>(\n target: IAnyStateTreeNode,\n name: string,\n fn: T\n) {\n const res = function (...args: any[]) {\n const id = getNextActionId()\n const parentContext = currentActionContext\n const parentActionContext = getParentActionContext(parentContext)\n\n return runWithActionContext(\n {\n type: \"action\",\n name,\n id,\n args,\n context: target,\n tree: getRoot(target),\n rootId: parentContext ? parentContext.rootId : id,\n parentId: parentContext ? parentContext.id : 0,\n allParentIds: parentContext\n ? [...parentContext.allParentIds, parentContext.id]\n : [],\n parentEvent: parentContext,\n parentActionEvent: parentActionContext\n },\n fn\n )\n }\n ;(res as FunctionWithFlag)._isMSTAction = true\n ;(res as FunctionWithFlag)._isFlowAction = fn._isFlowAction\n return res\n}\n\n/**\n * Middleware can be used to intercept any action is invoked on the subtree where it is attached.\n * If a tree is protected (by default), this means that any mutation of the tree will pass through your middleware.\n *\n * For more details, see the [middleware docs](concepts/middleware.md)\n *\n * @param target Node to apply the middleware to.\n * @param middleware Middleware to apply.\n * @returns A callable function to dispose the middleware.\n */\nexport function addMiddleware(\n target: IAnyStateTreeNode,\n handler: IMiddlewareHandler,\n includeHooks: boolean = true\n): IDisposer {\n const node = getStateTreeNode(target)\n if (devMode()) {\n if (!node.isProtectionEnabled) {\n warnError(\n \"It is recommended to protect the state tree before attaching action middleware, as otherwise it cannot be guaranteed that all changes are passed through middleware. See `protect`\"\n )\n }\n }\n return node.addMiddleWare(handler, includeHooks)\n}\n\n/**\n * Binds middleware to a specific action.\n *\n * Example:\n * ```ts\n * type.actions(self => {\n * function takeA____() {\n * self.toilet.donate()\n * self.wipe()\n * self.wipe()\n * self.toilet.flush()\n * }\n * return {\n * takeA____: decorate(atomic, takeA____)\n * }\n * })\n * ```\n *\n * @param handler\n * @param fn\n * @param includeHooks\n * @returns The original function\n */\nexport function decorate<T extends Function>(\n handler: IMiddlewareHandler,\n fn: T,\n includeHooks = true\n): T {\n const middleware: IMiddleware = { handler, includeHooks }\n ;(fn as any).$mst_middleware = (fn as any).$mst_middleware || []\n ;(fn as any).$mst_middleware.push(middleware)\n return fn\n}\n\nclass CollectedMiddlewares {\n private arrayIndex = 0\n private inArrayIndex = 0\n private middlewares: IMiddleware[][] = []\n\n constructor(node: AnyObjectNode, fn: Function) {\n // we just push middleware arrays into an array of arrays to avoid making copies\n if ((fn as any).$mst_middleware) {\n this.middlewares.push((fn as any).$mst_middleware)\n }\n let n: AnyObjectNode | null = node\n // Find all middlewares. Optimization: cache this?\n while (n) {\n if (n.middlewares) {\n this.middlewares.push(n.middlewares)\n }\n n = n.parent\n }\n }\n\n get isEmpty() {\n return this.middlewares.length <= 0\n }\n\n getNextMiddleware(): IMiddleware | undefined {\n const array = this.middlewares[this.arrayIndex]\n if (!array) {\n return undefined\n }\n const item = array[this.inArrayIndex++]\n if (!item) {\n this.arrayIndex++\n this.inArrayIndex = 0\n return this.getNextMiddleware()\n }\n return item\n }\n}\n\nfunction runMiddleWares(\n node: AnyObjectNode,\n baseCall: IMiddlewareEvent,\n originalFn: Function\n): any {\n const middlewares = new CollectedMiddlewares(node, originalFn)\n // Short circuit\n if (middlewares.isEmpty) {\n return mobxAction(originalFn)(...baseCall.args)\n }\n\n let result: any = null\n\n function runNextMiddleware(call: IMiddlewareEvent): any {\n const middleware = middlewares.getNextMiddleware()\n const handler = middleware && middleware.handler\n\n if (!handler) {\n return mobxAction(originalFn)(...call.args)\n }\n\n // skip hooks if asked to\n if (!middleware!.includeHooks && (Hook as any)[call.name]) {\n return runNextMiddleware(call)\n }\n\n let nextInvoked = false\n function next(\n call2: IMiddlewareEvent,\n callback?: (value: any) => any\n ): void {\n nextInvoked = true\n // the result can contain\n // - the non manipulated return value from an action\n // - the non manipulated abort value\n // - one of the above but manipulated through the callback function\n result = runNextMiddleware(call2)\n if (callback) {\n result = callback(result)\n }\n }\n\n let abortInvoked = false\n function abort(value: any) {\n abortInvoked = true\n // overwrite the result\n // can be manipulated through middlewares earlier in the queue using the callback fn\n result = value\n }\n\n handler(call, next, abort)\n if (devMode()) {\n if (!nextInvoked && !abortInvoked) {\n const node2 = getStateTreeNode(call.tree)\n throw fail(\n `Neither the next() nor the abort() callback within the middleware ${handler.name} for the action: \"${call.name}\" on the node: ${node2.type.name} was invoked.`\n )\n } else if (nextInvoked && abortInvoked) {\n const node2 = getStateTreeNode(call.tree)\n throw fail(\n `The next() and abort() callback within the middleware ${handler.name} for the action: \"${call.name}\" on the node: ${node2.type.name} were invoked.`\n )\n }\n }\n return result\n }\n return runNextMiddleware(baseCall)\n}\n"]}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { type IAnyStateTreeNode, type IMiddlewareEvent } from "../internal.ts";
|
|
2
|
-
export interface IActionContext {
|
|
3
|
-
/** Event name (action name for actions) */
|
|
4
|
-
readonly name: string;
|
|
5
|
-
/** Event unique id */
|
|
6
|
-
readonly id: number;
|
|
7
|
-
/** Parent action event object */
|
|
8
|
-
readonly parentActionEvent: IMiddlewareEvent | undefined;
|
|
9
|
-
/** Event context (node where the action was invoked) */
|
|
10
|
-
readonly context: IAnyStateTreeNode;
|
|
11
|
-
/** Event tree (root node of the node where the action was invoked) */
|
|
12
|
-
readonly tree: IAnyStateTreeNode;
|
|
13
|
-
/** Event arguments in an array (action arguments for actions) */
|
|
14
|
-
readonly args: any[];
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Returns the currently executing MST action context, or undefined if none.
|
|
18
|
-
*/
|
|
19
|
-
export declare function getRunningActionContext(): IActionContext | undefined;
|
|
20
|
-
/**
|
|
21
|
-
* Returns if the given action context is a parent of this action context.
|
|
22
|
-
*/
|
|
23
|
-
export declare function isActionContextChildOf(actionContext: IActionContext, parent: number | IActionContext | IMiddlewareEvent): boolean;
|
|
24
|
-
/**
|
|
25
|
-
* Returns if the given action context is this or a parent of this action context.
|
|
26
|
-
*/
|
|
27
|
-
export declare function isActionContextThisOrChildOf(actionContext: IActionContext, parentOrThis: number | IActionContext | IMiddlewareEvent): boolean;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getRunningActionContext = getRunningActionContext;
|
|
4
|
-
exports.isActionContextChildOf = isActionContextChildOf;
|
|
5
|
-
exports.isActionContextThisOrChildOf = isActionContextThisOrChildOf;
|
|
6
|
-
const action_ts_1 = require("./action.js");
|
|
7
|
-
/**
|
|
8
|
-
* Returns the currently executing MST action context, or undefined if none.
|
|
9
|
-
*/
|
|
10
|
-
function getRunningActionContext() {
|
|
11
|
-
let current = (0, action_ts_1.getCurrentActionContext)();
|
|
12
|
-
while (current && current.type !== "action") {
|
|
13
|
-
current = current.parentActionEvent;
|
|
14
|
-
}
|
|
15
|
-
return current;
|
|
16
|
-
}
|
|
17
|
-
function _isActionContextThisOrChildOf(actionContext, sameOrParent, includeSame) {
|
|
18
|
-
const parentId = typeof sameOrParent === "number" ? sameOrParent : sameOrParent.id;
|
|
19
|
-
let current = includeSame
|
|
20
|
-
? actionContext
|
|
21
|
-
: actionContext.parentActionEvent;
|
|
22
|
-
while (current) {
|
|
23
|
-
if (current.id === parentId) {
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
current = current.parentActionEvent;
|
|
27
|
-
}
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Returns if the given action context is a parent of this action context.
|
|
32
|
-
*/
|
|
33
|
-
function isActionContextChildOf(actionContext, parent) {
|
|
34
|
-
return _isActionContextThisOrChildOf(actionContext, parent, false);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Returns if the given action context is this or a parent of this action context.
|
|
38
|
-
*/
|
|
39
|
-
function isActionContextThisOrChildOf(actionContext, parentOrThis) {
|
|
40
|
-
return _isActionContextThisOrChildOf(actionContext, parentOrThis, true);
|
|
41
|
-
}
|
|
42
|
-
//# sourceMappingURL=actionContext.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"actionContext.js","sourceRoot":"","sources":["../../src/core/actionContext.ts"],"names":[],"mappings":";;AAyBA,0DAMC;AAyBD,wDAKC;AAKD,oEAKC;AAtED,2CAAqD;AAqBrD;;GAEG;AACH,SAAgB,uBAAuB;IACrC,IAAI,OAAO,GAAG,IAAA,mCAAuB,GAAE,CAAA;IACvC,OAAO,OAAO,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC5C,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAA;IACrC,CAAC;IACD,OAAO,OAAO,CAAA;AAChB,CAAC;AAED,SAAS,6BAA6B,CACpC,aAA6B,EAC7B,YAAwD,EACxD,WAAoB;IAEpB,MAAM,QAAQ,GACZ,OAAO,YAAY,KAAK,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC,EAAE,CAAA;IAEnE,IAAI,OAAO,GAAkD,WAAW;QACtE,CAAC,CAAC,aAAa;QACf,CAAC,CAAC,aAAa,CAAC,iBAAiB,CAAA;IACnC,OAAO,OAAO,EAAE,CAAC;QACf,IAAI,OAAO,CAAC,EAAE,KAAK,QAAQ,EAAE,CAAC;YAC5B,OAAO,IAAI,CAAA;QACb,CAAC;QACD,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAA;IACrC,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;GAEG;AACH,SAAgB,sBAAsB,CACpC,aAA6B,EAC7B,MAAkD;IAElD,OAAO,6BAA6B,CAAC,aAAa,EAAE,MAAM,EAAE,KAAK,CAAC,CAAA;AACpE,CAAC;AAED;;GAEG;AACH,SAAgB,4BAA4B,CAC1C,aAA6B,EAC7B,YAAwD;IAExD,OAAO,6BAA6B,CAAC,aAAa,EAAE,YAAY,EAAE,IAAI,CAAC,CAAA;AACzE,CAAC","sourcesContent":["import { type IAnyStateTreeNode, type IMiddlewareEvent } from \"../internal.ts\"\nimport { getCurrentActionContext } from \"./action.ts\"\n\nexport interface IActionContext {\n /** Event name (action name for actions) */\n readonly name: string\n\n /** Event unique id */\n readonly id: number\n\n /** Parent action event object */\n readonly parentActionEvent: IMiddlewareEvent | undefined\n\n /** Event context (node where the action was invoked) */\n readonly context: IAnyStateTreeNode\n /** Event tree (root node of the node where the action was invoked) */\n readonly tree: IAnyStateTreeNode\n\n /** Event arguments in an array (action arguments for actions) */\n readonly args: any[]\n}\n\n/**\n * Returns the currently executing MST action context, or undefined if none.\n */\nexport function getRunningActionContext(): IActionContext | undefined {\n let current = getCurrentActionContext()\n while (current && current.type !== \"action\") {\n current = current.parentActionEvent\n }\n return current\n}\n\nfunction _isActionContextThisOrChildOf(\n actionContext: IActionContext,\n sameOrParent: number | IActionContext | IMiddlewareEvent,\n includeSame: boolean\n) {\n const parentId =\n typeof sameOrParent === \"number\" ? sameOrParent : sameOrParent.id\n\n let current: IActionContext | IMiddlewareEvent | undefined = includeSame\n ? actionContext\n : actionContext.parentActionEvent\n while (current) {\n if (current.id === parentId) {\n return true\n }\n current = current.parentActionEvent\n }\n return false\n}\n\n/**\n * Returns if the given action context is a parent of this action context.\n */\nexport function isActionContextChildOf(\n actionContext: IActionContext,\n parent: number | IActionContext | IMiddlewareEvent\n) {\n return _isActionContextThisOrChildOf(actionContext, parent, false)\n}\n\n/**\n * Returns if the given action context is this or a parent of this action context.\n */\nexport function isActionContextThisOrChildOf(\n actionContext: IActionContext,\n parentOrThis: number | IActionContext | IMiddlewareEvent\n) {\n return _isActionContextThisOrChildOf(actionContext, parentOrThis, true)\n}\n"]}
|
package/dist/core/flow.d.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import type { FunctionWithFlag } from "./action.ts";
|
|
2
|
-
/**
|
|
3
|
-
* @hidden
|
|
4
|
-
*/
|
|
5
|
-
export type FlowReturn<R> = R extends Promise<infer T> ? T : R;
|
|
6
|
-
/**
|
|
7
|
-
* See [asynchronous actions](concepts/async-actions.md).
|
|
8
|
-
*
|
|
9
|
-
* @returns The flow as a promise.
|
|
10
|
-
*/
|
|
11
|
-
export declare function flow<R, Args extends any[]>(generator: (...args: Args) => Generator<PromiseLike<any>, R, any>): (...args: Args) => Promise<FlowReturn<R>>;
|
|
12
|
-
/**
|
|
13
|
-
* @deprecated Not needed since TS3.6.
|
|
14
|
-
* Used for TypeScript to make flows that return a promise return the actual promise result.
|
|
15
|
-
*
|
|
16
|
-
* @param val
|
|
17
|
-
* @returns
|
|
18
|
-
*/
|
|
19
|
-
export declare function castFlowReturn<T>(val: T): T;
|
|
20
|
-
/**
|
|
21
|
-
* @experimental
|
|
22
|
-
* experimental api - might change on minor/patch releases
|
|
23
|
-
*
|
|
24
|
-
* Convert a promise-returning function to a generator-returning one.
|
|
25
|
-
* This is intended to allow for usage of `yield*` in async actions to
|
|
26
|
-
* retain the promise return type.
|
|
27
|
-
*
|
|
28
|
-
* Example:
|
|
29
|
-
* ```ts
|
|
30
|
-
* function getDataAsync(input: string): Promise<number> { ... }
|
|
31
|
-
* const getDataGen = toGeneratorFunction(getDataAsync);
|
|
32
|
-
*
|
|
33
|
-
* const someModel.actions(self => ({
|
|
34
|
-
* someAction: flow(function*() {
|
|
35
|
-
* // value is typed as number
|
|
36
|
-
* const value = yield* getDataGen("input value");
|
|
37
|
-
* ...
|
|
38
|
-
* })
|
|
39
|
-
* }))
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
export declare function toGeneratorFunction<R, Args extends any[]>(p: (...args: Args) => Promise<R>): (...args: Args) => Generator<Promise<R>, R, R>;
|
|
43
|
-
/**
|
|
44
|
-
* @experimental
|
|
45
|
-
* experimental api - might change on minor/patch releases
|
|
46
|
-
*
|
|
47
|
-
* Convert a promise to a generator yielding that promise
|
|
48
|
-
* This is intended to allow for usage of `yield*` in async actions to
|
|
49
|
-
* retain the promise return type.
|
|
50
|
-
*
|
|
51
|
-
* Example:
|
|
52
|
-
* ```ts
|
|
53
|
-
* function getDataAsync(input: string): Promise<number> { ... }
|
|
54
|
-
*
|
|
55
|
-
* const someModel.actions(self => ({
|
|
56
|
-
* someAction: flow(function*() {
|
|
57
|
-
* // value is typed as number
|
|
58
|
-
* const value = yield* toGenerator(getDataAsync("input value"));
|
|
59
|
-
* ...
|
|
60
|
-
* })
|
|
61
|
-
* }))
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
export declare function toGenerator<R>(p: Promise<R>): Generator<Promise<R>, R, R>;
|
|
65
|
-
/**
|
|
66
|
-
* @internal
|
|
67
|
-
* @hidden
|
|
68
|
-
*/
|
|
69
|
-
export declare function createFlowSpawner(name: string, generator: FunctionWithFlag): (this: any, ...flowArgs: any[]) => Promise<unknown>;
|