@jbrowse/mobx-state-tree 5.6.9 → 5.7.0

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.
Files changed (200) hide show
  1. package/dist/core/action.js.map +1 -1
  2. package/dist/core/actionContext.js.map +1 -1
  3. package/dist/core/flow.js.map +1 -1
  4. package/dist/core/json-patch.js.map +1 -1
  5. package/dist/core/mst-operations.js.map +1 -1
  6. package/dist/core/node/BaseNode.js.map +1 -1
  7. package/dist/core/node/Hook.js.map +1 -1
  8. package/dist/core/node/create-node.js.map +1 -1
  9. package/dist/core/node/identifier-cache.js.map +1 -1
  10. package/dist/core/node/livelinessChecking.js.map +1 -1
  11. package/dist/core/node/node-utils.js.map +1 -1
  12. package/dist/core/node/object-node.d.ts +3 -1
  13. package/dist/core/node/object-node.js +2 -1
  14. package/dist/core/node/object-node.js.map +1 -1
  15. package/dist/core/node/scalar-node.d.ts +3 -1
  16. package/dist/core/node/scalar-node.js +2 -1
  17. package/dist/core/node/scalar-node.js.map +1 -1
  18. package/dist/core/process.js.map +1 -1
  19. package/dist/core/type/type-checker.js.map +1 -1
  20. package/dist/core/type/type.js.map +1 -1
  21. package/dist/index.js.map +1 -1
  22. package/dist/internal.js.map +1 -1
  23. package/dist/middlewares/create-action-tracking-middleware.js.map +1 -1
  24. package/dist/middlewares/createActionTrackingMiddleware2.js.map +1 -1
  25. package/dist/middlewares/on-action.js.map +1 -1
  26. package/dist/package.json +1 -0
  27. package/dist/types/complex-types/array.d.ts +2 -1
  28. package/dist/types/complex-types/array.js +2 -1
  29. package/dist/types/complex-types/array.js.map +1 -1
  30. package/dist/types/complex-types/map.d.ts +2 -1
  31. package/dist/types/complex-types/map.js +2 -1
  32. package/dist/types/complex-types/map.js.map +1 -1
  33. package/dist/types/complex-types/model.d.ts +2 -1
  34. package/dist/types/complex-types/model.js +2 -1
  35. package/dist/types/complex-types/model.js.map +1 -1
  36. package/dist/types/index.js.map +1 -1
  37. package/dist/types/primitives.d.ts +2 -1
  38. package/dist/types/primitives.js +2 -1
  39. package/dist/types/primitives.js.map +1 -1
  40. package/dist/types/utility-types/custom.d.ts +2 -1
  41. package/dist/types/utility-types/custom.js +2 -1
  42. package/dist/types/utility-types/custom.js.map +1 -1
  43. package/dist/types/utility-types/enumeration.js.map +1 -1
  44. package/dist/types/utility-types/frozen.d.ts +2 -1
  45. package/dist/types/utility-types/frozen.js +2 -1
  46. package/dist/types/utility-types/frozen.js.map +1 -1
  47. package/dist/types/utility-types/identifier.d.ts +2 -1
  48. package/dist/types/utility-types/identifier.js +2 -1
  49. package/dist/types/utility-types/identifier.js.map +1 -1
  50. package/dist/types/utility-types/late.js +2 -1
  51. package/dist/types/utility-types/late.js.map +1 -1
  52. package/dist/types/utility-types/lazy.d.ts +2 -1
  53. package/dist/types/utility-types/lazy.js +2 -1
  54. package/dist/types/utility-types/lazy.js.map +1 -1
  55. package/dist/types/utility-types/literal.d.ts +2 -1
  56. package/dist/types/utility-types/literal.js +2 -1
  57. package/dist/types/utility-types/literal.js.map +1 -1
  58. package/dist/types/utility-types/maybe.js.map +1 -1
  59. package/dist/types/utility-types/optional.d.ts +2 -1
  60. package/dist/types/utility-types/optional.js +2 -1
  61. package/dist/types/utility-types/optional.js.map +1 -1
  62. package/dist/types/utility-types/reference.d.ts +2 -1
  63. package/dist/types/utility-types/reference.js +2 -1
  64. package/dist/types/utility-types/reference.js.map +1 -1
  65. package/dist/types/utility-types/refinement.js +2 -1
  66. package/dist/types/utility-types/refinement.js.map +1 -1
  67. package/dist/types/utility-types/resilient.js +2 -1
  68. package/dist/types/utility-types/resilient.js.map +1 -1
  69. package/dist/types/utility-types/snapshotProcessor.js +3 -2
  70. package/dist/types/utility-types/snapshotProcessor.js.map +1 -1
  71. package/dist/types/utility-types/union.d.ts +2 -1
  72. package/dist/types/utility-types/union.js +2 -1
  73. package/dist/types/utility-types/union.js.map +1 -1
  74. package/dist/utils.js.map +1 -1
  75. package/esm/core/action.d.ts +87 -0
  76. package/esm/core/action.js +219 -0
  77. package/esm/core/action.js.map +1 -0
  78. package/esm/core/actionContext.d.ts +27 -0
  79. package/esm/core/actionContext.js +37 -0
  80. package/esm/core/actionContext.js.map +1 -0
  81. package/esm/core/flow.d.ts +69 -0
  82. package/esm/core/flow.js +173 -0
  83. package/esm/core/flow.js.map +1 -0
  84. package/esm/core/json-patch.d.ts +46 -0
  85. package/esm/core/json-patch.js +125 -0
  86. package/esm/core/json-patch.js.map +1 -0
  87. package/esm/core/mst-operations.d.ts +459 -0
  88. package/esm/core/mst-operations.js +844 -0
  89. package/esm/core/mst-operations.js.map +1 -0
  90. package/esm/core/node/BaseNode.d.ts +62 -0
  91. package/esm/core/node/BaseNode.js +148 -0
  92. package/esm/core/node/BaseNode.js.map +1 -0
  93. package/esm/core/node/Hook.d.ts +17 -0
  94. package/esm/core/node/Hook.js +12 -0
  95. package/esm/core/node/Hook.js.map +1 -0
  96. package/esm/core/node/create-node.d.ts +16 -0
  97. package/esm/core/node/create-node.js +36 -0
  98. package/esm/core/node/create-node.js.map +1 -0
  99. package/esm/core/node/identifier-cache.d.ts +19 -0
  100. package/esm/core/node/identifier-cache.js +111 -0
  101. package/esm/core/node/identifier-cache.js.map +1 -0
  102. package/esm/core/node/livelinessChecking.d.ts +37 -0
  103. package/esm/core/node/livelinessChecking.js +33 -0
  104. package/esm/core/node/livelinessChecking.js.map +1 -0
  105. package/esm/core/node/node-utils.d.ts +83 -0
  106. package/esm/core/node/node-utils.js +153 -0
  107. package/esm/core/node/node-utils.js.map +1 -0
  108. package/esm/core/node/object-node.d.ts +101 -0
  109. package/esm/core/node/object-node.js +542 -0
  110. package/esm/core/node/object-node.js.map +1 -0
  111. package/esm/core/node/scalar-node.d.ts +21 -0
  112. package/esm/core/node/scalar-node.js +86 -0
  113. package/esm/core/node/scalar-node.js.map +1 -0
  114. package/esm/core/process.d.ts +50 -0
  115. package/esm/core/process.js +35 -0
  116. package/esm/core/process.js.map +1 -0
  117. package/esm/core/type/type-checker.d.ts +69 -0
  118. package/esm/core/type/type-checker.js +144 -0
  119. package/esm/core/type/type-checker.js.map +1 -0
  120. package/esm/core/type/type.d.ts +317 -0
  121. package/esm/core/type/type.js +243 -0
  122. package/esm/core/type/type.js.map +1 -0
  123. package/esm/index.d.ts +2 -0
  124. package/esm/index.js +2 -0
  125. package/esm/index.js.map +1 -0
  126. package/esm/internal.d.ts +39 -0
  127. package/esm/internal.js +44 -0
  128. package/esm/internal.js.map +1 -0
  129. package/esm/middlewares/create-action-tracking-middleware.d.ts +24 -0
  130. package/esm/middlewares/create-action-tracking-middleware.js +78 -0
  131. package/esm/middlewares/create-action-tracking-middleware.js.map +1 -0
  132. package/esm/middlewares/createActionTrackingMiddleware2.d.ts +34 -0
  133. package/esm/middlewares/createActionTrackingMiddleware2.js +130 -0
  134. package/esm/middlewares/createActionTrackingMiddleware2.js.map +1 -0
  135. package/esm/middlewares/on-action.d.ts +87 -0
  136. package/esm/middlewares/on-action.js +210 -0
  137. package/esm/middlewares/on-action.js.map +1 -0
  138. package/esm/types/complex-types/array.d.ts +81 -0
  139. package/esm/types/complex-types/array.js +347 -0
  140. package/esm/types/complex-types/array.js.map +1 -0
  141. package/esm/types/complex-types/map.d.ts +111 -0
  142. package/esm/types/complex-types/map.js +356 -0
  143. package/esm/types/complex-types/map.js.map +1 -0
  144. package/esm/types/complex-types/model.d.ts +193 -0
  145. package/esm/types/complex-types/model.js +471 -0
  146. package/esm/types/complex-types/model.js.map +1 -0
  147. package/esm/types/index.d.ts +33 -0
  148. package/esm/types/index.js +35 -0
  149. package/esm/types/index.js.map +1 -0
  150. package/esm/types/primitives.d.ts +125 -0
  151. package/esm/types/primitives.js +177 -0
  152. package/esm/types/primitives.js.map +1 -0
  153. package/esm/types/utility-types/custom.d.ts +75 -0
  154. package/esm/types/utility-types/custom.js +106 -0
  155. package/esm/types/utility-types/custom.js.map +1 -0
  156. package/esm/types/utility-types/enumeration.d.ts +5 -0
  157. package/esm/types/utility-types/enumeration.js +31 -0
  158. package/esm/types/utility-types/enumeration.js.map +1 -0
  159. package/esm/types/utility-types/frozen.d.ts +24 -0
  160. package/esm/types/utility-types/frozen.js +92 -0
  161. package/esm/types/utility-types/frozen.js.map +1 -0
  162. package/esm/types/utility-types/identifier.d.ts +87 -0
  163. package/esm/types/utility-types/identifier.js +121 -0
  164. package/esm/types/utility-types/identifier.js.map +1 -0
  165. package/esm/types/utility-types/late.d.ts +10 -0
  166. package/esm/types/utility-types/late.js +106 -0
  167. package/esm/types/utility-types/late.js.map +1 -0
  168. package/esm/types/utility-types/lazy.d.ts +23 -0
  169. package/esm/types/utility-types/lazy.js +72 -0
  170. package/esm/types/utility-types/lazy.js.map +1 -0
  171. package/esm/types/utility-types/literal.d.ts +38 -0
  172. package/esm/types/utility-types/literal.js +58 -0
  173. package/esm/types/utility-types/literal.js.map +1 -0
  174. package/esm/types/utility-types/maybe.d.ts +26 -0
  175. package/esm/types/utility-types/maybe.js +26 -0
  176. package/esm/types/utility-types/maybe.js.map +1 -0
  177. package/esm/types/utility-types/optional.d.ts +42 -0
  178. package/esm/types/utility-types/optional.js +135 -0
  179. package/esm/types/utility-types/optional.js.map +1 -0
  180. package/esm/types/utility-types/reference.d.ts +90 -0
  181. package/esm/types/utility-types/reference.js +383 -0
  182. package/esm/types/utility-types/reference.js.map +1 -0
  183. package/esm/types/utility-types/refinement.d.ts +10 -0
  184. package/esm/types/utility-types/refinement.js +82 -0
  185. package/esm/types/utility-types/refinement.js.map +1 -0
  186. package/esm/types/utility-types/resilient.d.ts +18 -0
  187. package/esm/types/utility-types/resilient.js +118 -0
  188. package/esm/types/utility-types/resilient.js.map +1 -0
  189. package/esm/types/utility-types/snapshotProcessor.d.ts +63 -0
  190. package/esm/types/utility-types/snapshotProcessor.js +159 -0
  191. package/esm/types/utility-types/snapshotProcessor.js.map +1 -0
  192. package/esm/types/utility-types/union.d.ts +78 -0
  193. package/esm/types/utility-types/union.js +240 -0
  194. package/esm/types/utility-types/union.js.map +1 -0
  195. package/esm/utils.d.ts +230 -0
  196. package/esm/utils.js +449 -0
  197. package/esm/utils.js.map +1 -0
  198. package/package.json +23 -22
  199. package/dist/mobx-state-tree.js +0 -85
  200. package/dist/mobx-state-tree.module.js +0 -83
@@ -0,0 +1,87 @@
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;
@@ -0,0 +1,219 @@
1
+ import { action as mobxAction } from "mobx";
2
+ import { Hook, devMode, fail, getRoot, getStateTreeNode, warnError } from "../internal.js";
3
+ let nextActionId = 1;
4
+ let currentActionContext;
5
+ /**
6
+ * @internal
7
+ * @hidden
8
+ */
9
+ export function getCurrentActionContext() {
10
+ return currentActionContext;
11
+ }
12
+ /**
13
+ * @internal
14
+ * @hidden
15
+ */
16
+ export function getNextActionId() {
17
+ return nextActionId++;
18
+ }
19
+ // TODO: optimize away entire action context if there is no middleware in tree?
20
+ /**
21
+ * @internal
22
+ * @hidden
23
+ */
24
+ export function runWithActionContext(context, fn) {
25
+ const node = getStateTreeNode(context.context);
26
+ if (context.type === "action") {
27
+ node.assertAlive({
28
+ actionContext: context
29
+ });
30
+ }
31
+ const baseIsRunningAction = node._isRunningAction;
32
+ node._isRunningAction = true;
33
+ const previousContext = currentActionContext;
34
+ currentActionContext = context;
35
+ try {
36
+ return runMiddleWares(node, context, fn);
37
+ }
38
+ finally {
39
+ currentActionContext = previousContext;
40
+ node._isRunningAction = baseIsRunningAction;
41
+ }
42
+ }
43
+ /**
44
+ * @internal
45
+ * @hidden
46
+ */
47
+ export function getParentActionContext(parentContext) {
48
+ if (!parentContext) {
49
+ return undefined;
50
+ }
51
+ if (parentContext.type === "action") {
52
+ return parentContext;
53
+ }
54
+ return parentContext.parentActionEvent;
55
+ }
56
+ /**
57
+ * @internal
58
+ * @hidden
59
+ */
60
+ export function createActionInvoker(target, name, fn) {
61
+ const res = function (...args) {
62
+ const id = getNextActionId();
63
+ const parentContext = currentActionContext;
64
+ const parentActionContext = getParentActionContext(parentContext);
65
+ return runWithActionContext({
66
+ type: "action",
67
+ name,
68
+ id,
69
+ args,
70
+ context: target,
71
+ tree: getRoot(target),
72
+ rootId: parentContext ? parentContext.rootId : id,
73
+ parentId: parentContext ? parentContext.id : 0,
74
+ allParentIds: parentContext
75
+ ? [...parentContext.allParentIds, parentContext.id]
76
+ : [],
77
+ parentEvent: parentContext,
78
+ parentActionEvent: parentActionContext
79
+ }, fn);
80
+ };
81
+ res._isMSTAction = true;
82
+ res._isFlowAction = fn._isFlowAction;
83
+ return res;
84
+ }
85
+ /**
86
+ * Middleware can be used to intercept any action is invoked on the subtree where it is attached.
87
+ * If a tree is protected (by default), this means that any mutation of the tree will pass through your middleware.
88
+ *
89
+ * For more details, see the [middleware docs](concepts/middleware.md)
90
+ *
91
+ * @param target Node to apply the middleware to.
92
+ * @param middleware Middleware to apply.
93
+ * @returns A callable function to dispose the middleware.
94
+ */
95
+ export function addMiddleware(target, handler, includeHooks = true) {
96
+ const node = getStateTreeNode(target);
97
+ if (devMode()) {
98
+ if (!node.isProtectionEnabled) {
99
+ 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`");
100
+ }
101
+ }
102
+ return node.addMiddleWare(handler, includeHooks);
103
+ }
104
+ /**
105
+ * Binds middleware to a specific action.
106
+ *
107
+ * Example:
108
+ * ```ts
109
+ * type.actions(self => {
110
+ * function takeA____() {
111
+ * self.toilet.donate()
112
+ * self.wipe()
113
+ * self.wipe()
114
+ * self.toilet.flush()
115
+ * }
116
+ * return {
117
+ * takeA____: decorate(atomic, takeA____)
118
+ * }
119
+ * })
120
+ * ```
121
+ *
122
+ * @param handler
123
+ * @param fn
124
+ * @param includeHooks
125
+ * @returns The original function
126
+ */
127
+ export function decorate(handler, fn, includeHooks = true) {
128
+ const middleware = { handler, includeHooks };
129
+ fn.$mst_middleware = fn.$mst_middleware || [];
130
+ fn.$mst_middleware.push(middleware);
131
+ return fn;
132
+ }
133
+ class CollectedMiddlewares {
134
+ arrayIndex = 0;
135
+ inArrayIndex = 0;
136
+ middlewares = [];
137
+ constructor(node, fn) {
138
+ // we just push middleware arrays into an array of arrays to avoid making copies
139
+ if (fn.$mst_middleware) {
140
+ this.middlewares.push(fn.$mst_middleware);
141
+ }
142
+ let n = node;
143
+ // Find all middlewares. Optimization: cache this?
144
+ while (n) {
145
+ if (n.middlewares) {
146
+ this.middlewares.push(n.middlewares);
147
+ }
148
+ n = n.parent;
149
+ }
150
+ }
151
+ get isEmpty() {
152
+ return this.middlewares.length <= 0;
153
+ }
154
+ getNextMiddleware() {
155
+ const array = this.middlewares[this.arrayIndex];
156
+ if (!array) {
157
+ return undefined;
158
+ }
159
+ const item = array[this.inArrayIndex++];
160
+ if (!item) {
161
+ this.arrayIndex++;
162
+ this.inArrayIndex = 0;
163
+ return this.getNextMiddleware();
164
+ }
165
+ return item;
166
+ }
167
+ }
168
+ function runMiddleWares(node, baseCall, originalFn) {
169
+ const middlewares = new CollectedMiddlewares(node, originalFn);
170
+ // Short circuit
171
+ if (middlewares.isEmpty) {
172
+ return mobxAction(originalFn)(...baseCall.args);
173
+ }
174
+ let result = null;
175
+ function runNextMiddleware(call) {
176
+ const middleware = middlewares.getNextMiddleware();
177
+ const handler = middleware && middleware.handler;
178
+ if (!handler) {
179
+ return mobxAction(originalFn)(...call.args);
180
+ }
181
+ // skip hooks if asked to
182
+ if (!middleware.includeHooks && Hook[call.name]) {
183
+ return runNextMiddleware(call);
184
+ }
185
+ let nextInvoked = false;
186
+ function next(call2, callback) {
187
+ nextInvoked = true;
188
+ // the result can contain
189
+ // - the non manipulated return value from an action
190
+ // - the non manipulated abort value
191
+ // - one of the above but manipulated through the callback function
192
+ result = runNextMiddleware(call2);
193
+ if (callback) {
194
+ result = callback(result);
195
+ }
196
+ }
197
+ let abortInvoked = false;
198
+ function abort(value) {
199
+ abortInvoked = true;
200
+ // overwrite the result
201
+ // can be manipulated through middlewares earlier in the queue using the callback fn
202
+ result = value;
203
+ }
204
+ handler(call, next, abort);
205
+ if (devMode()) {
206
+ if (!nextInvoked && !abortInvoked) {
207
+ const node2 = getStateTreeNode(call.tree);
208
+ throw 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.`);
209
+ }
210
+ else if (nextInvoked && abortInvoked) {
211
+ const node2 = getStateTreeNode(call.tree);
212
+ throw fail(`The next() and abort() callback within the middleware ${handler.name} for the action: "${call.name}" on the node: ${node2.type.name} were invoked.`);
213
+ }
214
+ }
215
+ return result;
216
+ }
217
+ return runNextMiddleware(baseCall);
218
+ }
219
+ //# sourceMappingURL=action.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"action.js","sourceRoot":"","sources":["../../src/core/action.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,MAAM,CAAA;AAE3C,OAAO,EACL,IAAI,EACJ,OAAO,EACP,IAAI,EACJ,OAAO,EACP,gBAAgB,EAChB,SAAS,EACV,MAAM,gBAAgB,CAAA;AAqDvB,IAAI,YAAY,GAAG,CAAC,CAAA;AACpB,IAAI,oBAAkD,CAAA;AAEtD;;;GAGG;AACH,MAAM,UAAU,uBAAuB;IACrC,OAAO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vB,YAAY,GAAG,IAAI,CAAA;YACnB,uBAAuB;YACvB,oFAAoF;YACpF,MAAM,GAAG,KAAK,CAAA;QAChB,CAAC;QAED,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;QAC1B,IAAI,OAAO,EAAE,EAAE,CAAC;YACd,IAAI,CAAC,WAAW,IAAI,CAAC,YAAY,EAAE,CAAC;gBAClC,MAAM,KAAK,GAAG,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;gBACzC,MAAM,IAAI,CACR,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,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;gBACzC,MAAM,IAAI,CACR,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"]}
@@ -0,0 +1,27 @@
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;
@@ -0,0 +1,37 @@
1
+ import { getCurrentActionContext } from "./action.js";
2
+ /**
3
+ * Returns the currently executing MST action context, or undefined if none.
4
+ */
5
+ export function getRunningActionContext() {
6
+ let current = getCurrentActionContext();
7
+ while (current && current.type !== "action") {
8
+ current = current.parentActionEvent;
9
+ }
10
+ return current;
11
+ }
12
+ function _isActionContextThisOrChildOf(actionContext, sameOrParent, includeSame) {
13
+ const parentId = typeof sameOrParent === "number" ? sameOrParent : sameOrParent.id;
14
+ let current = includeSame
15
+ ? actionContext
16
+ : actionContext.parentActionEvent;
17
+ while (current) {
18
+ if (current.id === parentId) {
19
+ return true;
20
+ }
21
+ current = current.parentActionEvent;
22
+ }
23
+ return false;
24
+ }
25
+ /**
26
+ * Returns if the given action context is a parent of this action context.
27
+ */
28
+ export function isActionContextChildOf(actionContext, parent) {
29
+ return _isActionContextThisOrChildOf(actionContext, parent, false);
30
+ }
31
+ /**
32
+ * Returns if the given action context is this or a parent of this action context.
33
+ */
34
+ export function isActionContextThisOrChildOf(actionContext, parentOrThis) {
35
+ return _isActionContextThisOrChildOf(actionContext, parentOrThis, true);
36
+ }
37
+ //# sourceMappingURL=actionContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"actionContext.js","sourceRoot":"","sources":["../../src/core/actionContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAqBrD;;GAEG;AACH,MAAM,UAAU,uBAAuB;IACrC,IAAI,OAAO,GAAG,uBAAuB,EAAE,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,MAAM,UAAU,sBAAsB,CACpC,aAA6B,EAC7B,MAAkD;IAElD,OAAO,6BAA6B,CAAC,aAAa,EAAE,MAAM,EAAE,KAAK,CAAC,CAAA;AACpE,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,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"]}
@@ -0,0 +1,69 @@
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>;
@@ -0,0 +1,173 @@
1
+ import { fail, setImmediateWithFallback } from "../utils.js";
2
+ import { getCurrentActionContext, getNextActionId, getParentActionContext, runWithActionContext } from "./action.js";
3
+ /**
4
+ * See [asynchronous actions](concepts/async-actions.md).
5
+ *
6
+ * @returns The flow as a promise.
7
+ */
8
+ export function flow(generator) {
9
+ return createFlowSpawner(generator.name, generator);
10
+ }
11
+ /**
12
+ * @deprecated Not needed since TS3.6.
13
+ * Used for TypeScript to make flows that return a promise return the actual promise result.
14
+ *
15
+ * @param val
16
+ * @returns
17
+ */
18
+ export function castFlowReturn(val) {
19
+ return val;
20
+ }
21
+ /**
22
+ * @experimental
23
+ * experimental api - might change on minor/patch releases
24
+ *
25
+ * Convert a promise-returning function to a generator-returning one.
26
+ * This is intended to allow for usage of `yield*` in async actions to
27
+ * retain the promise return type.
28
+ *
29
+ * Example:
30
+ * ```ts
31
+ * function getDataAsync(input: string): Promise<number> { ... }
32
+ * const getDataGen = toGeneratorFunction(getDataAsync);
33
+ *
34
+ * const someModel.actions(self => ({
35
+ * someAction: flow(function*() {
36
+ * // value is typed as number
37
+ * const value = yield* getDataGen("input value");
38
+ * ...
39
+ * })
40
+ * }))
41
+ * ```
42
+ */
43
+ export function toGeneratorFunction(p) {
44
+ return function* (...args) {
45
+ return (yield p(...args));
46
+ };
47
+ }
48
+ /**
49
+ * @experimental
50
+ * experimental api - might change on minor/patch releases
51
+ *
52
+ * Convert a promise to a generator yielding that promise
53
+ * This is intended to allow for usage of `yield*` in async actions to
54
+ * retain the promise return type.
55
+ *
56
+ * Example:
57
+ * ```ts
58
+ * function getDataAsync(input: string): Promise<number> { ... }
59
+ *
60
+ * const someModel.actions(self => ({
61
+ * someAction: flow(function*() {
62
+ * // value is typed as number
63
+ * const value = yield* toGenerator(getDataAsync("input value"));
64
+ * ...
65
+ * })
66
+ * }))
67
+ * ```
68
+ */
69
+ export function* toGenerator(p) {
70
+ return (yield p);
71
+ }
72
+ /**
73
+ * @internal
74
+ * @hidden
75
+ */
76
+ export function createFlowSpawner(name, generator) {
77
+ const spawner = function flowSpawner(...flowArgs) {
78
+ // Implementation based on https://github.com/tj/co/blob/master/index.js
79
+ const runId = getNextActionId();
80
+ const parentContext = getCurrentActionContext();
81
+ if (!parentContext) {
82
+ throw fail("a mst flow must always have a parent context");
83
+ }
84
+ const parentActionContext = getParentActionContext(parentContext);
85
+ if (!parentActionContext) {
86
+ throw fail("a mst flow must always have a parent action context");
87
+ }
88
+ const contextBase = {
89
+ name,
90
+ id: runId,
91
+ tree: parentContext.tree,
92
+ context: parentContext.context,
93
+ parentId: parentContext.id,
94
+ allParentIds: [...parentContext.allParentIds, parentContext.id],
95
+ rootId: parentContext.rootId,
96
+ parentEvent: parentContext,
97
+ parentActionEvent: parentActionContext
98
+ };
99
+ function wrap(fn, type, arg) {
100
+ fn.$mst_middleware = spawner.$mst_middleware; // pick up any middleware attached to the flow
101
+ return runWithActionContext({
102
+ ...contextBase,
103
+ type,
104
+ args: [arg]
105
+ }, fn);
106
+ }
107
+ return new Promise(function (resolve, reject) {
108
+ let gen;
109
+ const init = function asyncActionInit(...initArgs) {
110
+ gen = generator(...initArgs);
111
+ onFulfilled(undefined); // kick off the flow
112
+ };
113
+ init.$mst_middleware = spawner.$mst_middleware;
114
+ runWithActionContext({
115
+ ...contextBase,
116
+ type: "flow_spawn",
117
+ args: flowArgs
118
+ }, init);
119
+ function onFulfilled(res) {
120
+ let ret;
121
+ try {
122
+ // prettier-ignore
123
+ const cancelError = wrap((_r) => { ret = gen.next(_r); }, "flow_resume", res);
124
+ if (cancelError instanceof Error) {
125
+ ret = gen.throw(cancelError);
126
+ }
127
+ }
128
+ catch (e) {
129
+ // prettier-ignore
130
+ setImmediateWithFallback(() => {
131
+ wrap((_r) => { reject(e); }, "flow_throw", e);
132
+ });
133
+ return;
134
+ }
135
+ next(ret);
136
+ return;
137
+ }
138
+ function onRejected(err) {
139
+ let ret;
140
+ try {
141
+ // prettier-ignore
142
+ wrap((_r) => { ret = gen.throw(_r); }, "flow_resume_error", err); // or yieldError?
143
+ }
144
+ catch (e) {
145
+ // prettier-ignore
146
+ setImmediateWithFallback(() => {
147
+ wrap((_r) => { reject(e); }, "flow_throw", e);
148
+ });
149
+ return;
150
+ }
151
+ next(ret);
152
+ }
153
+ function next(ret) {
154
+ if (ret.done) {
155
+ // prettier-ignore
156
+ setImmediateWithFallback(() => {
157
+ wrap((r) => { resolve(r); }, "flow_return", ret.value);
158
+ });
159
+ return;
160
+ }
161
+ // TODO: support more type of values? See https://github.com/tj/co/blob/249bbdc72da24ae44076afd716349d2089b31c4c/index.js#L100
162
+ if (!ret.value || typeof ret.value.then !== "function") {
163
+ // istanbul ignore next
164
+ throw fail("Only promises can be yielded to `async`, got: " + ret);
165
+ }
166
+ return ret.value.then(onFulfilled, onRejected);
167
+ }
168
+ });
169
+ };
170
+ spawner._isFlowAction = true;
171
+ return spawner;
172
+ }
173
+ //# sourceMappingURL=flow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flow.js","sourceRoot":"","sources":["../../src/core/flow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAA;AAC5D,OAAO,EACL,uBAAuB,EACvB,eAAe,EACf,sBAAsB,EACtB,oBAAoB,EACrB,MAAM,aAAa,CAAA;AASpB;;;;GAIG;AACH,MAAM,UAAU,IAAI,CAClB,SAAiE;IAEjE,OAAO,iBAAiB,CAAC,SAAS,CAAC,IAAI,EAAE,SAAS,CAAQ,CAAA;AAC5D,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAI,GAAM;IACtC,OAAO,GAAU,CAAA;AACnB,CAAC;AAED;;;;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AAC,WAAW,EAAE,UAAU,CAAC,CAAA;YAChD,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,CACA;IAAC,OAA4B,CAAC,aAAa,GAAG,IAAI,CAAA;IACnD,OAAO,OAAO,CAAA;AAChB,CAAC","sourcesContent":["import { fail, setImmediateWithFallback } from \"../utils.ts\"\nimport {\n getCurrentActionContext,\n getNextActionId,\n getParentActionContext,\n runWithActionContext\n} from \"./action.ts\"\n\nimport type { FunctionWithFlag, IMiddlewareEventType } from \"./action.ts\"\n\n/**\n * @hidden\n */\nexport type FlowReturn<R> = R extends Promise<infer T> ? T : R\n\n/**\n * See [asynchronous actions](concepts/async-actions.md).\n *\n * @returns The flow as a promise.\n */\nexport function flow<R, Args extends any[]>(\n generator: (...args: Args) => Generator<PromiseLike<any>, R, any>\n): (...args: Args) => Promise<FlowReturn<R>> {\n return createFlowSpawner(generator.name, generator) as any\n}\n\n/**\n * @deprecated Not needed since TS3.6.\n * Used for TypeScript to make flows that return a promise return the actual promise result.\n *\n * @param val\n * @returns\n */\nexport function castFlowReturn<T>(val: T): T {\n return val as any\n}\n\n/**\n * @experimental\n * experimental api - might change on minor/patch releases\n *\n * Convert a promise-returning function to a generator-returning one.\n * This is intended to allow for usage of `yield*` in async actions to\n * retain the promise return type.\n *\n * Example:\n * ```ts\n * function getDataAsync(input: string): Promise<number> { ... }\n * const getDataGen = toGeneratorFunction(getDataAsync);\n *\n * const someModel.actions(self => ({\n * someAction: flow(function*() {\n * // value is typed as number\n * const value = yield* getDataGen(\"input value\");\n * ...\n * })\n * }))\n * ```\n */\nexport function toGeneratorFunction<R, Args extends any[]>(\n p: (...args: Args) => Promise<R>\n) {\n return function* (...args: Args) {\n return (yield p(...args)) as R\n }\n}\n\n/**\n * @experimental\n * experimental api - might change on minor/patch releases\n *\n * Convert a promise to a generator yielding that promise\n * This is intended to allow for usage of `yield*` in async actions to\n * retain the promise return type.\n *\n * Example:\n * ```ts\n * function getDataAsync(input: string): Promise<number> { ... }\n *\n * const someModel.actions(self => ({\n * someAction: flow(function*() {\n * // value is typed as number\n * const value = yield* toGenerator(getDataAsync(\"input value\"));\n * ...\n * })\n * }))\n * ```\n */\nexport function* toGenerator<R>(p: Promise<R>) {\n return (yield p) as R\n}\n\n/**\n * @internal\n * @hidden\n */\nexport function createFlowSpawner(name: string, generator: FunctionWithFlag) {\n const spawner = function flowSpawner(this: any, ...flowArgs: any[]) {\n // Implementation based on https://github.com/tj/co/blob/master/index.js\n const runId = getNextActionId()\n const parentContext = getCurrentActionContext()!\n if (!parentContext) {\n throw fail(\"a mst flow must always have a parent context\")\n }\n const parentActionContext = getParentActionContext(parentContext)\n if (!parentActionContext) {\n throw fail(\"a mst flow must always have a parent action context\")\n }\n\n const contextBase = {\n name,\n id: runId,\n tree: parentContext.tree,\n context: parentContext.context,\n parentId: parentContext.id,\n allParentIds: [...parentContext.allParentIds, parentContext.id],\n rootId: parentContext.rootId,\n parentEvent: parentContext,\n parentActionEvent: parentActionContext\n }\n\n function wrap(fn: any, type: IMiddlewareEventType, arg: any) {\n fn.$mst_middleware = (spawner as any).$mst_middleware // pick up any middleware attached to the flow\n return runWithActionContext(\n {\n ...contextBase,\n type,\n args: [arg]\n },\n fn\n )\n }\n\n return new Promise(function (resolve, reject) {\n let gen: any\n const init = function asyncActionInit(...initArgs: any[]) {\n gen = generator(...initArgs)\n onFulfilled(undefined) // kick off the flow\n }\n ;(init as any).$mst_middleware = (spawner as any).$mst_middleware\n\n runWithActionContext(\n {\n ...contextBase,\n type: \"flow_spawn\",\n args: flowArgs\n },\n init\n )\n\n function onFulfilled(res: any) {\n let ret\n try {\n // prettier-ignore\n const cancelError: any = wrap((_r: any) => { ret = gen.next(_r) }, \"flow_resume\", res)\n if (cancelError instanceof Error) {\n ret = gen.throw(cancelError)\n }\n } catch (e) {\n // prettier-ignore\n setImmediateWithFallback(() => {\n wrap((_r: any) => { reject(e) }, \"flow_throw\", e)\n })\n return\n }\n next(ret)\n return\n }\n\n function onRejected(err: any) {\n let ret\n try {\n // prettier-ignore\n wrap((_r: any) => { ret = gen.throw(_r) }, \"flow_resume_error\", err) // or yieldError?\n } catch (e) {\n // prettier-ignore\n setImmediateWithFallback(() => {\n wrap((_r: any) => { reject(e) }, \"flow_throw\", e)\n })\n return\n }\n next(ret)\n }\n\n function next(ret: any) {\n if (ret.done) {\n // prettier-ignore\n setImmediateWithFallback(() => {\n wrap((r: any) => { resolve(r) }, \"flow_return\", ret.value)\n })\n return\n }\n // TODO: support more type of values? See https://github.com/tj/co/blob/249bbdc72da24ae44076afd716349d2089b31c4c/index.js#L100\n if (!ret.value || typeof ret.value.then !== \"function\") {\n // istanbul ignore next\n throw fail(\"Only promises can be yielded to `async`, got: \" + ret)\n }\n return ret.value.then(onFulfilled, onRejected)\n }\n })\n }\n ;(spawner as FunctionWithFlag)._isFlowAction = true\n return spawner\n}\n"]}