@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.
Files changed (253) hide show
  1. package/README.md +0 -10
  2. package/dist/index.d.ts +2135 -2
  3. package/dist/mobx-state-tree.cjs +6894 -0
  4. package/dist/mobx-state-tree.cjs.map +1 -0
  5. package/dist/mobx-state-tree.mjs +6814 -0
  6. package/dist/mobx-state-tree.mjs.map +1 -0
  7. package/package.json +30 -26
  8. package/dist/core/action.d.ts +0 -87
  9. package/dist/core/action.js +0 -228
  10. package/dist/core/action.js.map +0 -1
  11. package/dist/core/actionContext.d.ts +0 -27
  12. package/dist/core/actionContext.js +0 -42
  13. package/dist/core/actionContext.js.map +0 -1
  14. package/dist/core/flow.d.ts +0 -69
  15. package/dist/core/flow.js +0 -180
  16. package/dist/core/flow.js.map +0 -1
  17. package/dist/core/json-patch.d.ts +0 -46
  18. package/dist/core/json-patch.js +0 -133
  19. package/dist/core/json-patch.js.map +0 -1
  20. package/dist/core/mst-operations.d.ts +0 -459
  21. package/dist/core/mst-operations.js +0 -885
  22. package/dist/core/mst-operations.js.map +0 -1
  23. package/dist/core/node/BaseNode.d.ts +0 -62
  24. package/dist/core/node/BaseNode.js +0 -152
  25. package/dist/core/node/BaseNode.js.map +0 -1
  26. package/dist/core/node/Hook.d.ts +0 -17
  27. package/dist/core/node/Hook.js +0 -15
  28. package/dist/core/node/Hook.js.map +0 -1
  29. package/dist/core/node/create-node.d.ts +0 -16
  30. package/dist/core/node/create-node.js +0 -41
  31. package/dist/core/node/create-node.js.map +0 -1
  32. package/dist/core/node/identifier-cache.d.ts +0 -19
  33. package/dist/core/node/identifier-cache.js +0 -115
  34. package/dist/core/node/identifier-cache.js.map +0 -1
  35. package/dist/core/node/livelinessChecking.d.ts +0 -37
  36. package/dist/core/node/livelinessChecking.js +0 -38
  37. package/dist/core/node/livelinessChecking.js.map +0 -1
  38. package/dist/core/node/node-utils.d.ts +0 -83
  39. package/dist/core/node/node-utils.js +0 -165
  40. package/dist/core/node/node-utils.js.map +0 -1
  41. package/dist/core/node/object-node.d.ts +0 -101
  42. package/dist/core/node/object-node.js +0 -546
  43. package/dist/core/node/object-node.js.map +0 -1
  44. package/dist/core/node/scalar-node.d.ts +0 -21
  45. package/dist/core/node/scalar-node.js +0 -90
  46. package/dist/core/node/scalar-node.js.map +0 -1
  47. package/dist/core/process.d.ts +0 -50
  48. package/dist/core/process.js +0 -39
  49. package/dist/core/process.js.map +0 -1
  50. package/dist/core/type/type-checker.d.ts +0 -69
  51. package/dist/core/type/type-checker.js +0 -154
  52. package/dist/core/type/type-checker.js.map +0 -1
  53. package/dist/core/type/type.d.ts +0 -317
  54. package/dist/core/type/type.js +0 -251
  55. package/dist/core/type/type.js.map +0 -1
  56. package/dist/index.js +0 -85
  57. package/dist/index.js.map +0 -1
  58. package/dist/internal.d.ts +0 -39
  59. package/dist/internal.js +0 -60
  60. package/dist/internal.js.map +0 -1
  61. package/dist/middlewares/create-action-tracking-middleware.d.ts +0 -24
  62. package/dist/middlewares/create-action-tracking-middleware.js +0 -81
  63. package/dist/middlewares/create-action-tracking-middleware.js.map +0 -1
  64. package/dist/middlewares/createActionTrackingMiddleware2.d.ts +0 -34
  65. package/dist/middlewares/createActionTrackingMiddleware2.js +0 -133
  66. package/dist/middlewares/createActionTrackingMiddleware2.js.map +0 -1
  67. package/dist/middlewares/on-action.d.ts +0 -87
  68. package/dist/middlewares/on-action.js +0 -215
  69. package/dist/middlewares/on-action.js.map +0 -1
  70. package/dist/package.json +0 -1
  71. package/dist/types/complex-types/array.d.ts +0 -81
  72. package/dist/types/complex-types/array.js +0 -353
  73. package/dist/types/complex-types/array.js.map +0 -1
  74. package/dist/types/complex-types/map.d.ts +0 -111
  75. package/dist/types/complex-types/map.js +0 -362
  76. package/dist/types/complex-types/map.js.map +0 -1
  77. package/dist/types/complex-types/model.d.ts +0 -193
  78. package/dist/types/complex-types/model.js +0 -478
  79. package/dist/types/complex-types/model.js.map +0 -1
  80. package/dist/types/index.d.ts +0 -33
  81. package/dist/types/index.js +0 -38
  82. package/dist/types/index.js.map +0 -1
  83. package/dist/types/primitives.d.ts +0 -125
  84. package/dist/types/primitives.js +0 -183
  85. package/dist/types/primitives.js.map +0 -1
  86. package/dist/types/utility-types/custom.d.ts +0 -75
  87. package/dist/types/utility-types/custom.js +0 -111
  88. package/dist/types/utility-types/custom.js.map +0 -1
  89. package/dist/types/utility-types/enumeration.d.ts +0 -5
  90. package/dist/types/utility-types/enumeration.js +0 -34
  91. package/dist/types/utility-types/enumeration.js.map +0 -1
  92. package/dist/types/utility-types/frozen.d.ts +0 -24
  93. package/dist/types/utility-types/frozen.js +0 -98
  94. package/dist/types/utility-types/frozen.js.map +0 -1
  95. package/dist/types/utility-types/identifier.d.ts +0 -87
  96. package/dist/types/utility-types/identifier.js +0 -130
  97. package/dist/types/utility-types/identifier.js.map +0 -1
  98. package/dist/types/utility-types/late.d.ts +0 -10
  99. package/dist/types/utility-types/late.js +0 -110
  100. package/dist/types/utility-types/late.js.map +0 -1
  101. package/dist/types/utility-types/lazy.d.ts +0 -23
  102. package/dist/types/utility-types/lazy.js +0 -77
  103. package/dist/types/utility-types/lazy.js.map +0 -1
  104. package/dist/types/utility-types/literal.d.ts +0 -38
  105. package/dist/types/utility-types/literal.js +0 -64
  106. package/dist/types/utility-types/literal.js.map +0 -1
  107. package/dist/types/utility-types/maybe.d.ts +0 -26
  108. package/dist/types/utility-types/maybe.js +0 -30
  109. package/dist/types/utility-types/maybe.js.map +0 -1
  110. package/dist/types/utility-types/optional.d.ts +0 -42
  111. package/dist/types/utility-types/optional.js +0 -141
  112. package/dist/types/utility-types/optional.js.map +0 -1
  113. package/dist/types/utility-types/reference.d.ts +0 -90
  114. package/dist/types/utility-types/reference.js +0 -393
  115. package/dist/types/utility-types/reference.js.map +0 -1
  116. package/dist/types/utility-types/refinement.d.ts +0 -10
  117. package/dist/types/utility-types/refinement.js +0 -86
  118. package/dist/types/utility-types/refinement.js.map +0 -1
  119. package/dist/types/utility-types/resilient.d.ts +0 -18
  120. package/dist/types/utility-types/resilient.js +0 -121
  121. package/dist/types/utility-types/resilient.js.map +0 -1
  122. package/dist/types/utility-types/snapshotProcessor.d.ts +0 -63
  123. package/dist/types/utility-types/snapshotProcessor.js +0 -162
  124. package/dist/types/utility-types/snapshotProcessor.js.map +0 -1
  125. package/dist/types/utility-types/union.d.ts +0 -78
  126. package/dist/types/utility-types/union.js +0 -246
  127. package/dist/types/utility-types/union.js.map +0 -1
  128. package/dist/utils.d.ts +0 -230
  129. package/dist/utils.js +0 -483
  130. package/dist/utils.js.map +0 -1
  131. package/esm/core/action.d.ts +0 -87
  132. package/esm/core/action.js +0 -219
  133. package/esm/core/action.js.map +0 -1
  134. package/esm/core/actionContext.d.ts +0 -27
  135. package/esm/core/actionContext.js +0 -37
  136. package/esm/core/actionContext.js.map +0 -1
  137. package/esm/core/flow.d.ts +0 -69
  138. package/esm/core/flow.js +0 -173
  139. package/esm/core/flow.js.map +0 -1
  140. package/esm/core/json-patch.d.ts +0 -46
  141. package/esm/core/json-patch.js +0 -125
  142. package/esm/core/json-patch.js.map +0 -1
  143. package/esm/core/mst-operations.d.ts +0 -459
  144. package/esm/core/mst-operations.js +0 -844
  145. package/esm/core/mst-operations.js.map +0 -1
  146. package/esm/core/node/BaseNode.d.ts +0 -62
  147. package/esm/core/node/BaseNode.js +0 -148
  148. package/esm/core/node/BaseNode.js.map +0 -1
  149. package/esm/core/node/Hook.d.ts +0 -17
  150. package/esm/core/node/Hook.js +0 -12
  151. package/esm/core/node/Hook.js.map +0 -1
  152. package/esm/core/node/create-node.d.ts +0 -16
  153. package/esm/core/node/create-node.js +0 -36
  154. package/esm/core/node/create-node.js.map +0 -1
  155. package/esm/core/node/identifier-cache.d.ts +0 -19
  156. package/esm/core/node/identifier-cache.js +0 -111
  157. package/esm/core/node/identifier-cache.js.map +0 -1
  158. package/esm/core/node/livelinessChecking.d.ts +0 -37
  159. package/esm/core/node/livelinessChecking.js +0 -33
  160. package/esm/core/node/livelinessChecking.js.map +0 -1
  161. package/esm/core/node/node-utils.d.ts +0 -83
  162. package/esm/core/node/node-utils.js +0 -153
  163. package/esm/core/node/node-utils.js.map +0 -1
  164. package/esm/core/node/object-node.d.ts +0 -101
  165. package/esm/core/node/object-node.js +0 -542
  166. package/esm/core/node/object-node.js.map +0 -1
  167. package/esm/core/node/scalar-node.d.ts +0 -21
  168. package/esm/core/node/scalar-node.js +0 -86
  169. package/esm/core/node/scalar-node.js.map +0 -1
  170. package/esm/core/process.d.ts +0 -50
  171. package/esm/core/process.js +0 -35
  172. package/esm/core/process.js.map +0 -1
  173. package/esm/core/type/type-checker.d.ts +0 -69
  174. package/esm/core/type/type-checker.js +0 -144
  175. package/esm/core/type/type-checker.js.map +0 -1
  176. package/esm/core/type/type.d.ts +0 -317
  177. package/esm/core/type/type.js +0 -243
  178. package/esm/core/type/type.js.map +0 -1
  179. package/esm/index.d.ts +0 -2
  180. package/esm/index.js +0 -2
  181. package/esm/index.js.map +0 -1
  182. package/esm/internal.d.ts +0 -39
  183. package/esm/internal.js +0 -44
  184. package/esm/internal.js.map +0 -1
  185. package/esm/middlewares/create-action-tracking-middleware.d.ts +0 -24
  186. package/esm/middlewares/create-action-tracking-middleware.js +0 -78
  187. package/esm/middlewares/create-action-tracking-middleware.js.map +0 -1
  188. package/esm/middlewares/createActionTrackingMiddleware2.d.ts +0 -34
  189. package/esm/middlewares/createActionTrackingMiddleware2.js +0 -130
  190. package/esm/middlewares/createActionTrackingMiddleware2.js.map +0 -1
  191. package/esm/middlewares/on-action.d.ts +0 -87
  192. package/esm/middlewares/on-action.js +0 -210
  193. package/esm/middlewares/on-action.js.map +0 -1
  194. package/esm/types/complex-types/array.d.ts +0 -81
  195. package/esm/types/complex-types/array.js +0 -347
  196. package/esm/types/complex-types/array.js.map +0 -1
  197. package/esm/types/complex-types/map.d.ts +0 -111
  198. package/esm/types/complex-types/map.js +0 -356
  199. package/esm/types/complex-types/map.js.map +0 -1
  200. package/esm/types/complex-types/model.d.ts +0 -193
  201. package/esm/types/complex-types/model.js +0 -471
  202. package/esm/types/complex-types/model.js.map +0 -1
  203. package/esm/types/index.d.ts +0 -33
  204. package/esm/types/index.js +0 -35
  205. package/esm/types/index.js.map +0 -1
  206. package/esm/types/primitives.d.ts +0 -125
  207. package/esm/types/primitives.js +0 -177
  208. package/esm/types/primitives.js.map +0 -1
  209. package/esm/types/utility-types/custom.d.ts +0 -75
  210. package/esm/types/utility-types/custom.js +0 -106
  211. package/esm/types/utility-types/custom.js.map +0 -1
  212. package/esm/types/utility-types/enumeration.d.ts +0 -5
  213. package/esm/types/utility-types/enumeration.js +0 -31
  214. package/esm/types/utility-types/enumeration.js.map +0 -1
  215. package/esm/types/utility-types/frozen.d.ts +0 -24
  216. package/esm/types/utility-types/frozen.js +0 -92
  217. package/esm/types/utility-types/frozen.js.map +0 -1
  218. package/esm/types/utility-types/identifier.d.ts +0 -87
  219. package/esm/types/utility-types/identifier.js +0 -121
  220. package/esm/types/utility-types/identifier.js.map +0 -1
  221. package/esm/types/utility-types/late.d.ts +0 -10
  222. package/esm/types/utility-types/late.js +0 -106
  223. package/esm/types/utility-types/late.js.map +0 -1
  224. package/esm/types/utility-types/lazy.d.ts +0 -23
  225. package/esm/types/utility-types/lazy.js +0 -72
  226. package/esm/types/utility-types/lazy.js.map +0 -1
  227. package/esm/types/utility-types/literal.d.ts +0 -38
  228. package/esm/types/utility-types/literal.js +0 -58
  229. package/esm/types/utility-types/literal.js.map +0 -1
  230. package/esm/types/utility-types/maybe.d.ts +0 -26
  231. package/esm/types/utility-types/maybe.js +0 -26
  232. package/esm/types/utility-types/maybe.js.map +0 -1
  233. package/esm/types/utility-types/optional.d.ts +0 -42
  234. package/esm/types/utility-types/optional.js +0 -135
  235. package/esm/types/utility-types/optional.js.map +0 -1
  236. package/esm/types/utility-types/reference.d.ts +0 -90
  237. package/esm/types/utility-types/reference.js +0 -383
  238. package/esm/types/utility-types/reference.js.map +0 -1
  239. package/esm/types/utility-types/refinement.d.ts +0 -10
  240. package/esm/types/utility-types/refinement.js +0 -82
  241. package/esm/types/utility-types/refinement.js.map +0 -1
  242. package/esm/types/utility-types/resilient.d.ts +0 -18
  243. package/esm/types/utility-types/resilient.js +0 -118
  244. package/esm/types/utility-types/resilient.js.map +0 -1
  245. package/esm/types/utility-types/snapshotProcessor.d.ts +0 -63
  246. package/esm/types/utility-types/snapshotProcessor.js +0 -159
  247. package/esm/types/utility-types/snapshotProcessor.js.map +0 -1
  248. package/esm/types/utility-types/union.d.ts +0 -78
  249. package/esm/types/utility-types/union.js +0 -240
  250. package/esm/types/utility-types/union.js.map +0 -1
  251. package/esm/utils.d.ts +0 -230
  252. package/esm/utils.js +0 -449
  253. package/esm/utils.js.map +0 -1
package/esm/internal.js DELETED
@@ -1,44 +0,0 @@
1
- /*
2
- * All imports / exports should be proxied through this file.
3
- * Why? It gives us full control over the module load order, preventing circular dependency isses
4
- */
5
- export * from "./core/node/livelinessChecking.js";
6
- export * from "./core/node/Hook.js";
7
- export * from "./core/mst-operations.js";
8
- export * from "./core/node/BaseNode.js";
9
- export * from "./core/node/scalar-node.js";
10
- export * from "./core/node/object-node.js";
11
- export * from "./core/type/type.js";
12
- export * from "./middlewares/create-action-tracking-middleware.js";
13
- export * from "./middlewares/createActionTrackingMiddleware2.js";
14
- export * from "./middlewares/on-action.js";
15
- export * from "./core/action.js";
16
- export * from "./core/actionContext.js";
17
- export * from "./core/type/type-checker.js";
18
- export * from "./core/node/identifier-cache.js";
19
- export * from "./core/node/create-node.js";
20
- export * from "./core/node/node-utils.js";
21
- export * from "./core/process.js";
22
- export * from "./core/flow.js";
23
- export * from "./core/json-patch.js";
24
- export * from "./utils.js";
25
- export * from "./types/utility-types/snapshotProcessor.js";
26
- export * from "./types/complex-types/map.js";
27
- export * from "./types/complex-types/array.js";
28
- export * from "./types/complex-types/model.js";
29
- export * from "./types/primitives.js";
30
- export * from "./types/utility-types/literal.js";
31
- export * from "./types/utility-types/refinement.js";
32
- export * from "./types/utility-types/enumeration.js";
33
- export * from "./types/utility-types/union.js";
34
- export * from "./types/utility-types/optional.js";
35
- export * from "./types/utility-types/maybe.js";
36
- export * from "./types/utility-types/late.js";
37
- export * from "./types/utility-types/lazy.js";
38
- export * from "./types/utility-types/frozen.js";
39
- export * from "./types/utility-types/reference.js";
40
- export * from "./types/utility-types/identifier.js";
41
- export * from "./types/utility-types/custom.js";
42
- export * from "./types/utility-types/resilient.js";
43
- export * from "./types/index.js";
44
- //# sourceMappingURL=internal.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"internal.js","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,mCAAmC,CAAA;AACjD,cAAc,qBAAqB,CAAA;AACnC,cAAc,0BAA0B,CAAA;AACxC,cAAc,yBAAyB,CAAA;AACvC,cAAc,4BAA4B,CAAA;AAC1C,cAAc,4BAA4B,CAAA;AAC1C,cAAc,qBAAqB,CAAA;AACnC,cAAc,oDAAoD,CAAA;AAClE,cAAc,kDAAkD,CAAA;AAChE,cAAc,4BAA4B,CAAA;AAC1C,cAAc,kBAAkB,CAAA;AAChC,cAAc,yBAAyB,CAAA;AACvC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,iCAAiC,CAAA;AAC/C,cAAc,4BAA4B,CAAA;AAC1C,cAAc,2BAA2B,CAAA;AACzC,cAAc,mBAAmB,CAAA;AACjC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,sBAAsB,CAAA;AACpC,cAAc,YAAY,CAAA;AAC1B,cAAc,4CAA4C,CAAA;AAC1D,cAAc,8BAA8B,CAAA;AAC5C,cAAc,gCAAgC,CAAA;AAC9C,cAAc,gCAAgC,CAAA;AAC9C,cAAc,uBAAuB,CAAA;AACrC,cAAc,kCAAkC,CAAA;AAChD,cAAc,qCAAqC,CAAA;AACnD,cAAc,sCAAsC,CAAA;AACpD,cAAc,gCAAgC,CAAA;AAC9C,cAAc,mCAAmC,CAAA;AACjD,cAAc,gCAAgC,CAAA;AAC9C,cAAc,+BAA+B,CAAA;AAC7C,cAAc,+BAA+B,CAAA;AAC7C,cAAc,iCAAiC,CAAA;AAC/C,cAAc,oCAAoC,CAAA;AAClD,cAAc,qCAAqC,CAAA;AACnD,cAAc,iCAAiC,CAAA;AAC/C,cAAc,oCAAoC,CAAA;AAClD,cAAc,kBAAkB,CAAA","sourcesContent":["/*\n * All imports / exports should be proxied through this file.\n * Why? It gives us full control over the module load order, preventing circular dependency isses\n */\n\nexport * from \"./core/node/livelinessChecking.ts\"\nexport * from \"./core/node/Hook.ts\"\nexport * from \"./core/mst-operations.ts\"\nexport * from \"./core/node/BaseNode.ts\"\nexport * from \"./core/node/scalar-node.ts\"\nexport * from \"./core/node/object-node.ts\"\nexport * from \"./core/type/type.ts\"\nexport * from \"./middlewares/create-action-tracking-middleware.ts\"\nexport * from \"./middlewares/createActionTrackingMiddleware2.ts\"\nexport * from \"./middlewares/on-action.ts\"\nexport * from \"./core/action.ts\"\nexport * from \"./core/actionContext.ts\"\nexport * from \"./core/type/type-checker.ts\"\nexport * from \"./core/node/identifier-cache.ts\"\nexport * from \"./core/node/create-node.ts\"\nexport * from \"./core/node/node-utils.ts\"\nexport * from \"./core/process.ts\"\nexport * from \"./core/flow.ts\"\nexport * from \"./core/json-patch.ts\"\nexport * from \"./utils.ts\"\nexport * from \"./types/utility-types/snapshotProcessor.ts\"\nexport * from \"./types/complex-types/map.ts\"\nexport * from \"./types/complex-types/array.ts\"\nexport * from \"./types/complex-types/model.ts\"\nexport * from \"./types/primitives.ts\"\nexport * from \"./types/utility-types/literal.ts\"\nexport * from \"./types/utility-types/refinement.ts\"\nexport * from \"./types/utility-types/enumeration.ts\"\nexport * from \"./types/utility-types/union.ts\"\nexport * from \"./types/utility-types/optional.ts\"\nexport * from \"./types/utility-types/maybe.ts\"\nexport * from \"./types/utility-types/late.ts\"\nexport * from \"./types/utility-types/lazy.ts\"\nexport * from \"./types/utility-types/frozen.ts\"\nexport * from \"./types/utility-types/reference.ts\"\nexport * from \"./types/utility-types/identifier.ts\"\nexport * from \"./types/utility-types/custom.ts\"\nexport * from \"./types/utility-types/resilient.ts\"\nexport * from \"./types/index.ts\"\n"]}
@@ -1,24 +0,0 @@
1
- import { type IMiddlewareEvent, type IMiddlewareHandler } from "../internal.ts";
2
- export interface IActionTrackingMiddlewareHooks<T> {
3
- filter?: (call: IMiddlewareEvent) => boolean;
4
- onStart: (call: IMiddlewareEvent) => T;
5
- onResume: (call: IMiddlewareEvent, context: T) => void;
6
- onSuspend: (call: IMiddlewareEvent, context: T) => void;
7
- onSuccess: (call: IMiddlewareEvent, context: T, result: any) => void;
8
- onFail: (call: IMiddlewareEvent, context: T, error: any) => void;
9
- }
10
- /**
11
- * Note: Consider migrating to `createActionTrackingMiddleware2`, it is easier to use.
12
- *
13
- * Convenience utility to create action based middleware that supports async processes more easily.
14
- * All hooks are called for both synchronous and asynchronous actions. Except that either `onSuccess` or `onFail` is called
15
- *
16
- * The create middleware tracks the process of an action (assuming it passes the `filter`).
17
- * `onResume` can return any value, which will be passed as second argument to any other hook. This makes it possible to keep state during a process.
18
- *
19
- * See the `atomic` middleware for an example
20
- *
21
- * @param hooks
22
- * @returns
23
- */
24
- export declare function createActionTrackingMiddleware<T = any>(hooks: IActionTrackingMiddlewareHooks<T>): IMiddlewareHandler;
@@ -1,78 +0,0 @@
1
- const runningActions = new Map();
2
- /**
3
- * Note: Consider migrating to `createActionTrackingMiddleware2`, it is easier to use.
4
- *
5
- * Convenience utility to create action based middleware that supports async processes more easily.
6
- * All hooks are called for both synchronous and asynchronous actions. Except that either `onSuccess` or `onFail` is called
7
- *
8
- * The create middleware tracks the process of an action (assuming it passes the `filter`).
9
- * `onResume` can return any value, which will be passed as second argument to any other hook. This makes it possible to keep state during a process.
10
- *
11
- * See the `atomic` middleware for an example
12
- *
13
- * @param hooks
14
- * @returns
15
- */
16
- export function createActionTrackingMiddleware(hooks) {
17
- return function actionTrackingMiddleware(call, next, _abort) {
18
- switch (call.type) {
19
- case "action": {
20
- if (!hooks.filter || hooks.filter(call) === true) {
21
- const context = hooks.onStart(call);
22
- hooks.onResume(call, context);
23
- runningActions.set(call.id, {
24
- call,
25
- context,
26
- async: false
27
- });
28
- try {
29
- const res = next(call);
30
- hooks.onSuspend(call, context);
31
- if (runningActions.get(call.id).async === false) {
32
- runningActions.delete(call.id);
33
- hooks.onSuccess(call, context, res);
34
- }
35
- return res;
36
- }
37
- catch (e) {
38
- runningActions.delete(call.id);
39
- hooks.onFail(call, context, e);
40
- throw e;
41
- }
42
- }
43
- else {
44
- return next(call);
45
- }
46
- }
47
- case "flow_spawn": {
48
- const root = runningActions.get(call.rootId);
49
- root.async = true;
50
- return next(call);
51
- }
52
- case "flow_resume":
53
- case "flow_resume_error": {
54
- const root = runningActions.get(call.rootId);
55
- hooks.onResume(call, root.context);
56
- try {
57
- return next(call);
58
- }
59
- finally {
60
- hooks.onSuspend(call, root.context);
61
- }
62
- }
63
- case "flow_throw": {
64
- const root = runningActions.get(call.rootId);
65
- runningActions.delete(call.rootId);
66
- hooks.onFail(call, root.context, call.args[0]);
67
- return next(call);
68
- }
69
- case "flow_return": {
70
- const root = runningActions.get(call.rootId);
71
- runningActions.delete(call.rootId);
72
- hooks.onSuccess(call, root.context, call.args[0]);
73
- return next(call);
74
- }
75
- }
76
- };
77
- }
78
- //# sourceMappingURL=create-action-tracking-middleware.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"create-action-tracking-middleware.js","sourceRoot":"","sources":["../../src/middlewares/create-action-tracking-middleware.ts"],"names":[],"mappings":"AAEA,MAAM,cAAc,GAAG,IAAI,GAAG,EAG3B,CAAA;AAWH;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,8BAA8B,CAC5C,KAAwC;IAExC,OAAO,SAAS,wBAAwB,CACtC,IAAsB,EACtB,IAA2C,EAC3C,MAA2B;QAE3B,QAAQ,IAAI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AAC,CAAA;AACH,CAAC","sourcesContent":["import { type IMiddlewareEvent, type IMiddlewareHandler } from \"../internal.ts\"\n\nconst runningActions = new Map<\n number,\n { async: boolean; call: IMiddlewareEvent; context: any }\n>()\n\nexport interface IActionTrackingMiddlewareHooks<T> {\n filter?: (call: IMiddlewareEvent) => boolean\n onStart: (call: IMiddlewareEvent) => T\n onResume: (call: IMiddlewareEvent, context: T) => void\n onSuspend: (call: IMiddlewareEvent, context: T) => void\n onSuccess: (call: IMiddlewareEvent, context: T, result: any) => void\n onFail: (call: IMiddlewareEvent, context: T, error: any) => void\n}\n\n/**\n * Note: Consider migrating to `createActionTrackingMiddleware2`, it is easier to use.\n *\n * Convenience utility to create action based middleware that supports async processes more easily.\n * All hooks are called for both synchronous and asynchronous actions. Except that either `onSuccess` or `onFail` is called\n *\n * The create middleware tracks the process of an action (assuming it passes the `filter`).\n * `onResume` can return any value, which will be passed as second argument to any other hook. This makes it possible to keep state during a process.\n *\n * See the `atomic` middleware for an example\n *\n * @param hooks\n * @returns\n */\nexport function createActionTrackingMiddleware<T = any>(\n hooks: IActionTrackingMiddlewareHooks<T>\n): IMiddlewareHandler {\n return function actionTrackingMiddleware(\n call: IMiddlewareEvent,\n next: (actionCall: IMiddlewareEvent) => any,\n _abort: (value: any) => any\n ) {\n switch (call.type) {\n case \"action\": {\n if (!hooks.filter || hooks.filter(call) === true) {\n const context = hooks.onStart(call)\n hooks.onResume(call, context)\n runningActions.set(call.id, {\n call,\n context,\n async: false\n })\n try {\n const res = next(call)\n hooks.onSuspend(call, context)\n if (runningActions.get(call.id)!.async === false) {\n runningActions.delete(call.id)\n hooks.onSuccess(call, context, res)\n }\n return res\n } catch (e) {\n runningActions.delete(call.id)\n hooks.onFail(call, context, e)\n throw e\n }\n } else {\n return next(call)\n }\n }\n case \"flow_spawn\": {\n const root = runningActions.get(call.rootId)!\n root.async = true\n return next(call)\n }\n case \"flow_resume\":\n case \"flow_resume_error\": {\n const root = runningActions.get(call.rootId)!\n hooks.onResume(call, root.context)\n try {\n return next(call)\n } finally {\n hooks.onSuspend(call, root.context)\n }\n }\n case \"flow_throw\": {\n const root = runningActions.get(call.rootId)!\n runningActions.delete(call.rootId)\n hooks.onFail(call, root.context, call.args[0])\n return next(call)\n }\n case \"flow_return\": {\n const root = runningActions.get(call.rootId)!\n runningActions.delete(call.rootId)\n hooks.onSuccess(call, root.context, call.args[0])\n return next(call)\n }\n }\n }\n}\n"]}
@@ -1,34 +0,0 @@
1
- import { type IActionContext, type IMiddlewareHandler } from "../internal.ts";
2
- export interface IActionTrackingMiddleware2Call<TEnv> extends Readonly<IActionContext> {
3
- env: TEnv | undefined;
4
- readonly parentCall?: IActionTrackingMiddleware2Call<TEnv>;
5
- }
6
- export interface IActionTrackingMiddleware2Hooks<TEnv> {
7
- filter?: (call: IActionTrackingMiddleware2Call<TEnv>) => boolean;
8
- onStart: (call: IActionTrackingMiddleware2Call<TEnv>) => void;
9
- onFinish: (call: IActionTrackingMiddleware2Call<TEnv>, error?: any) => void;
10
- }
11
- /**
12
- * Convenience utility to create action based middleware that supports async processes more easily.
13
- * The flow is like this:
14
- * - for each action: if filter passes -> `onStart` -> (inner actions recursively) -> `onFinish`
15
- *
16
- * Example: if we had an action `a` that called inside an action `b1`, then `b2` the flow would be:
17
- * - `filter(a)`
18
- * - `onStart(a)`
19
- * - `filter(b1)`
20
- * - `onStart(b1)`
21
- * - `onFinish(b1)`
22
- * - `filter(b2)`
23
- * - `onStart(b2)`
24
- * - `onFinish(b2)`
25
- * - `onFinish(a)`
26
- *
27
- * The flow is the same no matter if the actions are sync or async.
28
- *
29
- * See the `atomic` middleware for an example
30
- *
31
- * @param hooks
32
- * @returns
33
- */
34
- export declare function createActionTrackingMiddleware2<TEnv = any>(middlewareHooks: IActionTrackingMiddleware2Hooks<TEnv>): IMiddlewareHandler;
@@ -1,130 +0,0 @@
1
- class RunningAction {
2
- hooks;
3
- call;
4
- flowsPending = 0;
5
- running = true;
6
- constructor(hooks, call) {
7
- this.hooks = hooks;
8
- this.call = call;
9
- if (hooks) {
10
- hooks.onStart(call);
11
- }
12
- }
13
- finish(error) {
14
- if (this.running) {
15
- this.running = false;
16
- if (this.hooks) {
17
- this.hooks.onFinish(this.call, error);
18
- }
19
- }
20
- }
21
- incFlowsPending() {
22
- this.flowsPending++;
23
- }
24
- decFlowsPending() {
25
- this.flowsPending--;
26
- }
27
- get hasFlowsPending() {
28
- return this.flowsPending > 0;
29
- }
30
- }
31
- /**
32
- * Convenience utility to create action based middleware that supports async processes more easily.
33
- * The flow is like this:
34
- * - for each action: if filter passes -> `onStart` -> (inner actions recursively) -> `onFinish`
35
- *
36
- * Example: if we had an action `a` that called inside an action `b1`, then `b2` the flow would be:
37
- * - `filter(a)`
38
- * - `onStart(a)`
39
- * - `filter(b1)`
40
- * - `onStart(b1)`
41
- * - `onFinish(b1)`
42
- * - `filter(b2)`
43
- * - `onStart(b2)`
44
- * - `onFinish(b2)`
45
- * - `onFinish(a)`
46
- *
47
- * The flow is the same no matter if the actions are sync or async.
48
- *
49
- * See the `atomic` middleware for an example
50
- *
51
- * @param hooks
52
- * @returns
53
- */
54
- export function createActionTrackingMiddleware2(middlewareHooks) {
55
- const runningActions = new Map();
56
- return function actionTrackingMiddleware(call, next) {
57
- // find parentRunningAction
58
- const parentRunningAction = call.parentActionEvent
59
- ? runningActions.get(call.parentActionEvent.id)
60
- : undefined;
61
- if (call.type === "action") {
62
- const newCall = {
63
- ...call,
64
- // make a shallow copy of the parent action env
65
- env: parentRunningAction && parentRunningAction.call.env,
66
- parentCall: parentRunningAction && parentRunningAction.call
67
- };
68
- const passesFilter = !middlewareHooks.filter || middlewareHooks.filter(newCall);
69
- const hooks = passesFilter ? middlewareHooks : undefined;
70
- const runningAction = new RunningAction(hooks, newCall);
71
- runningActions.set(call.id, runningAction);
72
- let res;
73
- try {
74
- res = next(call);
75
- }
76
- catch (e) {
77
- runningActions.delete(call.id);
78
- runningAction.finish(e);
79
- throw e;
80
- }
81
- // sync action finished
82
- if (!runningAction.hasFlowsPending) {
83
- runningActions.delete(call.id);
84
- runningAction.finish();
85
- }
86
- return res;
87
- }
88
- else {
89
- if (!parentRunningAction) {
90
- return next(call);
91
- }
92
- switch (call.type) {
93
- case "flow_spawn": {
94
- parentRunningAction.incFlowsPending();
95
- return next(call);
96
- }
97
- case "flow_resume":
98
- case "flow_resume_error": {
99
- return next(call);
100
- }
101
- case "flow_throw": {
102
- const error = call.args[0];
103
- try {
104
- return next(call);
105
- }
106
- finally {
107
- parentRunningAction.decFlowsPending();
108
- if (!parentRunningAction.hasFlowsPending) {
109
- runningActions.delete(call.parentActionEvent.id);
110
- parentRunningAction.finish(error);
111
- }
112
- }
113
- }
114
- case "flow_return": {
115
- try {
116
- return next(call);
117
- }
118
- finally {
119
- parentRunningAction.decFlowsPending();
120
- if (!parentRunningAction.hasFlowsPending) {
121
- runningActions.delete(call.parentActionEvent.id);
122
- parentRunningAction.finish();
123
- }
124
- }
125
- }
126
- }
127
- }
128
- };
129
- }
130
- //# sourceMappingURL=createActionTrackingMiddleware2.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createActionTrackingMiddleware2.js","sourceRoot":"","sources":["../../src/middlewares/createActionTrackingMiddleware2.ts"],"names":[],"mappings":"AAmBA,MAAM,aAAa;IAKC;IACP;IALH,YAAY,GAAG,CAAC,CAAA;IAChB,OAAO,GAAG,IAAI,CAAA;IAEtB,YACkB,KAAuD,EAC9D,IAAyC;QADlC,UAAK,GAAL,KAAK,CAAkD;QAC9D,SAAI,GAAJ,IAAI,CAAqC;QAElD,IAAI,KAAK,EAAE,CAAC;YACV,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QACrB,CAAC;IACH,CAAC;IAED,MAAM,CAAC,KAAW;QAChB,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;YACpB,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,KA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CAAC,CAAA;4BACjD,mBAAmB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;wBACnC,CAAC;oBACH,CAAC;gBACH,CAAC;gBACD,KAAK,aAAa,CAAC,CAAC,CAAC;oBACnB,IAAI,CAAC;wBACH,OAAO,IAAI,CAAC,IAAI,CAAC,CAAA;oBACnB,CAAC;4BAAS,CAAC;wBACT,mBAAmB,CAAC,eAAe,EAAE,CAAA;wBACrC,IAAI,CAAC,mBAAmB,CAAC,eAAe,EAAE,CAAC;4BACzC,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,iBAAkB,CAAC,EAAE,CAAC,CAAA;4BACjD,mBAAmB,CAAC,MAAM,EAAE,CAAA;wBAC9B,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC,CAAA;AACH,CAAC","sourcesContent":["import {\n type IActionContext,\n type IMiddlewareEvent,\n type IMiddlewareHandler\n} from \"../internal.ts\"\n\nexport interface IActionTrackingMiddleware2Call<\n TEnv\n> extends Readonly<IActionContext> {\n env: TEnv | undefined\n readonly parentCall?: IActionTrackingMiddleware2Call<TEnv>\n}\n\nexport interface IActionTrackingMiddleware2Hooks<TEnv> {\n filter?: (call: IActionTrackingMiddleware2Call<TEnv>) => boolean\n onStart: (call: IActionTrackingMiddleware2Call<TEnv>) => void\n onFinish: (call: IActionTrackingMiddleware2Call<TEnv>, error?: any) => void\n}\n\nclass RunningAction {\n private flowsPending = 0\n private running = true\n\n constructor(\n public readonly hooks: IActionTrackingMiddleware2Hooks<any> | undefined,\n readonly call: IActionTrackingMiddleware2Call<any>\n ) {\n if (hooks) {\n hooks.onStart(call)\n }\n }\n\n finish(error?: any) {\n if (this.running) {\n this.running = false\n if (this.hooks) {\n this.hooks.onFinish(this.call, error)\n }\n }\n }\n\n incFlowsPending() {\n this.flowsPending++\n }\n\n decFlowsPending() {\n this.flowsPending--\n }\n\n get hasFlowsPending() {\n return this.flowsPending > 0\n }\n}\n\n/**\n * Convenience utility to create action based middleware that supports async processes more easily.\n * The flow is like this:\n * - for each action: if filter passes -> `onStart` -> (inner actions recursively) -> `onFinish`\n *\n * Example: if we had an action `a` that called inside an action `b1`, then `b2` the flow would be:\n * - `filter(a)`\n * - `onStart(a)`\n * - `filter(b1)`\n * - `onStart(b1)`\n * - `onFinish(b1)`\n * - `filter(b2)`\n * - `onStart(b2)`\n * - `onFinish(b2)`\n * - `onFinish(a)`\n *\n * The flow is the same no matter if the actions are sync or async.\n *\n * See the `atomic` middleware for an example\n *\n * @param hooks\n * @returns\n */\nexport function createActionTrackingMiddleware2<TEnv = any>(\n middlewareHooks: IActionTrackingMiddleware2Hooks<TEnv>\n): IMiddlewareHandler {\n const runningActions = new Map<number, RunningAction>()\n\n return function actionTrackingMiddleware(\n call: IMiddlewareEvent,\n next: (actionCall: IMiddlewareEvent) => any\n ) {\n // find parentRunningAction\n const parentRunningAction = call.parentActionEvent\n ? runningActions.get(call.parentActionEvent.id)\n : undefined\n\n if (call.type === \"action\") {\n const newCall: IActionTrackingMiddleware2Call<TEnv> = {\n ...call,\n // make a shallow copy of the parent action env\n env: parentRunningAction && parentRunningAction.call.env,\n parentCall: parentRunningAction && parentRunningAction.call\n }\n\n const passesFilter =\n !middlewareHooks.filter || middlewareHooks.filter(newCall)\n const hooks = passesFilter ? middlewareHooks : undefined\n\n const runningAction = new RunningAction(hooks, newCall)\n runningActions.set(call.id, runningAction)\n\n let res\n try {\n res = next(call)\n } catch (e) {\n runningActions.delete(call.id)\n runningAction.finish(e)\n throw e\n }\n // sync action finished\n if (!runningAction.hasFlowsPending) {\n runningActions.delete(call.id)\n runningAction.finish()\n }\n return res\n } else {\n if (!parentRunningAction) {\n return next(call)\n }\n\n switch (call.type) {\n case \"flow_spawn\": {\n parentRunningAction.incFlowsPending()\n return next(call)\n }\n case \"flow_resume\":\n case \"flow_resume_error\": {\n return next(call)\n }\n case \"flow_throw\": {\n const error = call.args[0]\n try {\n return next(call)\n } finally {\n parentRunningAction.decFlowsPending()\n if (!parentRunningAction.hasFlowsPending) {\n runningActions.delete(call.parentActionEvent!.id)\n parentRunningAction.finish(error)\n }\n }\n }\n case \"flow_return\": {\n try {\n return next(call)\n } finally {\n parentRunningAction.decFlowsPending()\n if (!parentRunningAction.hasFlowsPending) {\n runningActions.delete(call.parentActionEvent!.id)\n parentRunningAction.finish()\n }\n }\n }\n }\n }\n }\n}\n"]}
@@ -1,87 +0,0 @@
1
- import { type IActionContext, type IAnyStateTreeNode, type IDisposer } from "../internal.ts";
2
- export interface ISerializedActionCall {
3
- name: string;
4
- path?: string;
5
- args?: any[];
6
- }
7
- export interface IActionRecorder {
8
- actions: ReadonlyArray<ISerializedActionCall>;
9
- readonly recording: boolean;
10
- stop(): void;
11
- resume(): void;
12
- replay(target: IAnyStateTreeNode): void;
13
- }
14
- /**
15
- * Applies an action or a series of actions in a single MobX transaction.
16
- * Does not return any value
17
- * Takes an action description as produced by the `onAction` middleware.
18
- *
19
- * @param target
20
- * @param actions
21
- */
22
- export declare function applyAction(target: IAnyStateTreeNode, actions: ISerializedActionCall | ISerializedActionCall[]): void;
23
- /**
24
- * Small abstraction around `onAction` and `applyAction`, attaches an action listener to a tree and records all the actions emitted.
25
- * Returns an recorder object with the following signature:
26
- *
27
- * Example:
28
- * ```ts
29
- * export interface IActionRecorder {
30
- * // the recorded actions
31
- * actions: ISerializedActionCall[]
32
- * // true if currently recording
33
- * recording: boolean
34
- * // stop recording actions
35
- * stop(): void
36
- * // resume recording actions
37
- * resume(): void
38
- * // apply all the recorded actions on the given object
39
- * replay(target: IAnyStateTreeNode): void
40
- * }
41
- * ```
42
- *
43
- * The optional filter function allows to skip recording certain actions.
44
- *
45
- * @param subject
46
- * @returns
47
- */
48
- export declare function recordActions(subject: IAnyStateTreeNode, filter?: (action: ISerializedActionCall, actionContext: IActionContext | undefined) => boolean): IActionRecorder;
49
- /**
50
- * Registers a function that will be invoked for each action that is called on the provided model instance, or to any of its children.
51
- * See [actions](https://github.com/mobxjs/mobx-state-tree#actions) for more details. onAction events are emitted only for the outermost called action in the stack.
52
- * Action can also be intercepted by middleware using addMiddleware to change the function call before it will be run.
53
- *
54
- * Not all action arguments might be serializable. For unserializable arguments, a struct like `{ $MST_UNSERIALIZABLE: true, type: "someType" }` will be generated.
55
- * MST Nodes are considered non-serializable as well (they could be serialized as there snapshot, but it is uncertain whether an replaying party will be able to handle such a non-instantiated snapshot).
56
- * Rather, when using `onAction` middleware, one should consider in passing arguments which are 1: an id, 2: a (relative) path, or 3: a snapshot. Instead of a real MST node.
57
- *
58
- * Example:
59
- * ```ts
60
- * const Todo = types.model({
61
- * task: types.string
62
- * })
63
- *
64
- * const TodoStore = types.model({
65
- * todos: types.array(Todo)
66
- * }).actions(self => ({
67
- * add(todo) {
68
- * self.todos.push(todo);
69
- * }
70
- * }))
71
- *
72
- * const s = TodoStore.create({ todos: [] })
73
- *
74
- * let disposer = onAction(s, (call) => {
75
- * console.log(call);
76
- * })
77
- *
78
- * s.add({ task: "Grab a coffee" })
79
- * // Logs: { name: "add", path: "", args: [{ task: "Grab a coffee" }] }
80
- * ```
81
- *
82
- * @param target
83
- * @param listener
84
- * @param attachAfter (default false) fires the listener *after* the action has executed instead of before.
85
- * @returns
86
- */
87
- export declare function onAction(target: IAnyStateTreeNode, listener: (call: ISerializedActionCall) => void, attachAfter?: boolean): IDisposer;
@@ -1,210 +0,0 @@
1
- import { runInAction } from "mobx";
2
- import { addMiddleware, applyPatch, applySnapshot, asArray, assertArg, assertIsStateTreeNode, devMode, fail, getRelativePathBetweenNodes, getRunningActionContext, getStateTreeNode, getType, isArray, isPlainObject, isPrimitive, isProtected, isRoot, isStateTreeNode, tryResolve, warnError } from "../internal.js";
3
- function serializeArgument(node, actionName, index, arg) {
4
- if (arg instanceof Date) {
5
- return { $MST_DATE: arg.getTime() };
6
- }
7
- if (isPrimitive(arg)) {
8
- return arg;
9
- }
10
- // We should not serialize MST nodes, even if we can, because we don't know if the receiving party can handle a raw snapshot instead of an
11
- // MST type instance. So if one wants to serialize a MST node that was pass in, either explitly pass: 1: an id, 2: a (relative) path, 3: a snapshot
12
- if (isStateTreeNode(arg)) {
13
- return serializeTheUnserializable(`[MSTNode: ${getType(arg).name}]`);
14
- }
15
- if (typeof arg === "function") {
16
- return serializeTheUnserializable(`[function]`);
17
- }
18
- if (typeof arg === "object" && !isPlainObject(arg) && !isArray(arg)) {
19
- return serializeTheUnserializable(`[object ${(arg && arg.constructor && arg.constructor.name) ||
20
- "Complex Object"}]`);
21
- }
22
- try {
23
- // Check if serializable, cycle free etc...
24
- // MWE: there must be a better way....
25
- JSON.stringify(arg); // or throws
26
- return arg;
27
- }
28
- catch (e) {
29
- return serializeTheUnserializable("" + e);
30
- }
31
- }
32
- function deserializeArgument(adm, value) {
33
- if (value && typeof value === "object" && "$MST_DATE" in value) {
34
- return new Date(value["$MST_DATE"]);
35
- }
36
- return value;
37
- }
38
- function serializeTheUnserializable(baseType) {
39
- return {
40
- $MST_UNSERIALIZABLE: true,
41
- type: baseType
42
- };
43
- }
44
- /**
45
- * Applies an action or a series of actions in a single MobX transaction.
46
- * Does not return any value
47
- * Takes an action description as produced by the `onAction` middleware.
48
- *
49
- * @param target
50
- * @param actions
51
- */
52
- export function applyAction(target, actions) {
53
- // check all arguments
54
- assertIsStateTreeNode(target, 1);
55
- assertArg(actions, a => typeof a === "object", "object or array", 2);
56
- runInAction(() => {
57
- asArray(actions).forEach(action => baseApplyAction(target, action));
58
- });
59
- }
60
- function baseApplyAction(target, action) {
61
- const resolvedTarget = tryResolve(target, action.path || "");
62
- if (!resolvedTarget) {
63
- throw fail(`Invalid action path: ${action.path || ""}`);
64
- }
65
- const node = getStateTreeNode(resolvedTarget);
66
- // Reserved functions
67
- if (action.name === "@APPLY_PATCHES") {
68
- return applyPatch.call(null, resolvedTarget, action.args[0]);
69
- }
70
- if (action.name === "@APPLY_SNAPSHOT") {
71
- return applySnapshot.call(null, resolvedTarget, action.args[0]);
72
- }
73
- if (!(typeof resolvedTarget[action.name] === "function")) {
74
- throw fail(`Action '${action.name}' does not exist in '${node.path}'`);
75
- }
76
- return resolvedTarget[action.name](...(action.args ? action.args.map(v => deserializeArgument(node, v)) : []));
77
- }
78
- /**
79
- * Small abstraction around `onAction` and `applyAction`, attaches an action listener to a tree and records all the actions emitted.
80
- * Returns an recorder object with the following signature:
81
- *
82
- * Example:
83
- * ```ts
84
- * export interface IActionRecorder {
85
- * // the recorded actions
86
- * actions: ISerializedActionCall[]
87
- * // true if currently recording
88
- * recording: boolean
89
- * // stop recording actions
90
- * stop(): void
91
- * // resume recording actions
92
- * resume(): void
93
- * // apply all the recorded actions on the given object
94
- * replay(target: IAnyStateTreeNode): void
95
- * }
96
- * ```
97
- *
98
- * The optional filter function allows to skip recording certain actions.
99
- *
100
- * @param subject
101
- * @returns
102
- */
103
- export function recordActions(subject, filter) {
104
- // check all arguments
105
- assertIsStateTreeNode(subject, 1);
106
- const actions = [];
107
- const listener = (call) => {
108
- const recordThis = filter ? filter(call, getRunningActionContext()) : true;
109
- if (recordThis) {
110
- actions.push(call);
111
- }
112
- };
113
- let disposer;
114
- const recorder = {
115
- actions,
116
- get recording() {
117
- return !!disposer;
118
- },
119
- stop() {
120
- if (disposer) {
121
- disposer();
122
- disposer = undefined;
123
- }
124
- },
125
- resume() {
126
- if (disposer) {
127
- return;
128
- }
129
- disposer = onAction(subject, listener);
130
- },
131
- replay(target) {
132
- applyAction(target, actions);
133
- }
134
- };
135
- recorder.resume();
136
- return recorder;
137
- }
138
- /**
139
- * Registers a function that will be invoked for each action that is called on the provided model instance, or to any of its children.
140
- * See [actions](https://github.com/mobxjs/mobx-state-tree#actions) for more details. onAction events are emitted only for the outermost called action in the stack.
141
- * Action can also be intercepted by middleware using addMiddleware to change the function call before it will be run.
142
- *
143
- * Not all action arguments might be serializable. For unserializable arguments, a struct like `{ $MST_UNSERIALIZABLE: true, type: "someType" }` will be generated.
144
- * MST Nodes are considered non-serializable as well (they could be serialized as there snapshot, but it is uncertain whether an replaying party will be able to handle such a non-instantiated snapshot).
145
- * Rather, when using `onAction` middleware, one should consider in passing arguments which are 1: an id, 2: a (relative) path, or 3: a snapshot. Instead of a real MST node.
146
- *
147
- * Example:
148
- * ```ts
149
- * const Todo = types.model({
150
- * task: types.string
151
- * })
152
- *
153
- * const TodoStore = types.model({
154
- * todos: types.array(Todo)
155
- * }).actions(self => ({
156
- * add(todo) {
157
- * self.todos.push(todo);
158
- * }
159
- * }))
160
- *
161
- * const s = TodoStore.create({ todos: [] })
162
- *
163
- * let disposer = onAction(s, (call) => {
164
- * console.log(call);
165
- * })
166
- *
167
- * s.add({ task: "Grab a coffee" })
168
- * // Logs: { name: "add", path: "", args: [{ task: "Grab a coffee" }] }
169
- * ```
170
- *
171
- * @param target
172
- * @param listener
173
- * @param attachAfter (default false) fires the listener *after* the action has executed instead of before.
174
- * @returns
175
- */
176
- export function onAction(target, listener, attachAfter = false) {
177
- // check all arguments
178
- assertIsStateTreeNode(target, 1);
179
- if (devMode()) {
180
- if (!isRoot(target)) {
181
- warnError("Warning: Attaching onAction listeners to non root nodes is dangerous: No events will be emitted for actions initiated higher up in the tree.");
182
- }
183
- if (!isProtected(target)) {
184
- warnError("Warning: Attaching onAction listeners to non protected nodes is dangerous: No events will be emitted for direct modifications without action.");
185
- }
186
- }
187
- return addMiddleware(target, function handler(rawCall, next) {
188
- if (rawCall.type === "action" && rawCall.id === rawCall.rootId) {
189
- const sourceNode = getStateTreeNode(rawCall.context);
190
- const info = {
191
- name: rawCall.name,
192
- path: getRelativePathBetweenNodes(getStateTreeNode(target), sourceNode),
193
- args: rawCall.args.map((arg, index) => serializeArgument(sourceNode, rawCall.name, index, arg))
194
- };
195
- if (attachAfter) {
196
- const res = next(rawCall);
197
- listener(info);
198
- return res;
199
- }
200
- else {
201
- listener(info);
202
- return next(rawCall);
203
- }
204
- }
205
- else {
206
- return next(rawCall);
207
- }
208
- });
209
- }
210
- //# sourceMappingURL=on-action.js.map