@toa.io/core 1.0.0-alpha.3 → 1.0.0-alpha.300

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 (256) hide show
  1. package/package.json +28 -12
  2. package/transpiled/abandon.d.ts +9 -0
  3. package/transpiled/abandon.js +37 -0
  4. package/transpiled/abandon.js.map +1 -0
  5. package/transpiled/assignment.d.ts +6 -0
  6. package/transpiled/assignment.js +25 -0
  7. package/transpiled/assignment.js.map +1 -0
  8. package/transpiled/call.d.ts +10 -0
  9. package/transpiled/call.js +146 -0
  10. package/transpiled/call.js.map +1 -0
  11. package/transpiled/cascade.d.ts +7 -0
  12. package/transpiled/cascade.js +28 -0
  13. package/transpiled/cascade.js.map +1 -0
  14. package/transpiled/component.d.ts +15 -0
  15. package/transpiled/component.js +93 -0
  16. package/transpiled/component.js.map +1 -0
  17. package/transpiled/composition.d.ts +6 -0
  18. package/transpiled/composition.js +23 -0
  19. package/transpiled/composition.js.map +1 -0
  20. package/transpiled/connector.d.ts +54 -0
  21. package/transpiled/connector.js +193 -0
  22. package/transpiled/connector.js.map +1 -0
  23. package/transpiled/context.d.ts +25 -0
  24. package/transpiled/context.js +55 -0
  25. package/transpiled/context.js.map +1 -0
  26. package/transpiled/contract/contract.d.ts +10 -0
  27. package/transpiled/contract/contract.js +14 -0
  28. package/transpiled/contract/contract.js.map +1 -0
  29. package/transpiled/contract/index.d.ts +2 -0
  30. package/transpiled/contract/index.js +3 -0
  31. package/transpiled/contract/index.js.map +1 -0
  32. package/transpiled/contract/reply.d.ts +7 -0
  33. package/transpiled/contract/reply.js +48 -0
  34. package/transpiled/contract/reply.js.map +1 -0
  35. package/transpiled/contract/request.d.ts +39 -0
  36. package/transpiled/contract/request.js +111 -0
  37. package/transpiled/contract/request.js.map +1 -0
  38. package/transpiled/contract/schemas.d.ts +4 -0
  39. package/transpiled/contract/schemas.js +36 -0
  40. package/transpiled/contract/schemas.js.map +1 -0
  41. package/transpiled/discovery.d.ts +19 -0
  42. package/transpiled/discovery.js +52 -0
  43. package/transpiled/discovery.js.map +1 -0
  44. package/transpiled/effect.d.ts +5 -0
  45. package/transpiled/effect.js +11 -0
  46. package/transpiled/effect.js.map +1 -0
  47. package/transpiled/emission.d.ts +10 -0
  48. package/transpiled/emission.js +16 -0
  49. package/transpiled/emission.js.map +1 -0
  50. package/transpiled/entities/changeset.d.ts +10 -0
  51. package/transpiled/entities/changeset.js +27 -0
  52. package/transpiled/entities/changeset.js.map +1 -0
  53. package/transpiled/entities/entity.d.ts +19 -0
  54. package/transpiled/entities/entity.js +137 -0
  55. package/transpiled/entities/entity.js.map +1 -0
  56. package/transpiled/entities/factory.d.ts +27 -0
  57. package/transpiled/entities/factory.js +38 -0
  58. package/transpiled/entities/factory.js.map +1 -0
  59. package/transpiled/entities/index.d.ts +5 -0
  60. package/transpiled/entities/index.js +6 -0
  61. package/transpiled/entities/index.js.map +1 -0
  62. package/transpiled/entities/newid.d.ts +10 -0
  63. package/transpiled/entities/newid.js +35 -0
  64. package/transpiled/entities/newid.js.map +1 -0
  65. package/transpiled/entities/set.d.ts +10 -0
  66. package/transpiled/entities/set.js +19 -0
  67. package/transpiled/entities/set.js.map +1 -0
  68. package/transpiled/event.d.ts +14 -0
  69. package/transpiled/event.js +51 -0
  70. package/transpiled/event.js.map +1 -0
  71. package/transpiled/exceptions.d.ts +89 -0
  72. package/transpiled/exceptions.js +180 -0
  73. package/transpiled/exceptions.js.map +1 -0
  74. package/transpiled/exposition.d.ts +18 -0
  75. package/transpiled/exposition.js +18 -0
  76. package/transpiled/exposition.js.map +1 -0
  77. package/transpiled/guard.d.ts +7 -0
  78. package/transpiled/guard.js +12 -0
  79. package/transpiled/guard.js.map +1 -0
  80. package/transpiled/index.d.ts +37 -0
  81. package/transpiled/index.js +31 -0
  82. package/transpiled/index.js.map +1 -0
  83. package/transpiled/instance.d.ts +10 -0
  84. package/transpiled/instance.js +30 -0
  85. package/transpiled/instance.js.map +1 -0
  86. package/transpiled/locator.d.ts +12 -0
  87. package/transpiled/locator.js +32 -0
  88. package/transpiled/locator.js.map +1 -0
  89. package/transpiled/observation.d.ts +5 -0
  90. package/transpiled/observation.js +12 -0
  91. package/transpiled/observation.js.map +1 -0
  92. package/transpiled/operation.d.ts +67 -0
  93. package/transpiled/operation.js +127 -0
  94. package/transpiled/operation.js.map +1 -0
  95. package/transpiled/outbox.d.ts +45 -0
  96. package/transpiled/outbox.js +430 -0
  97. package/transpiled/outbox.js.map +1 -0
  98. package/transpiled/query/criteria.d.ts +6 -0
  99. package/transpiled/query/criteria.js +74 -0
  100. package/transpiled/query/criteria.js.map +1 -0
  101. package/transpiled/query/options.d.ts +3 -0
  102. package/transpiled/query/options.js +27 -0
  103. package/transpiled/query/options.js.map +1 -0
  104. package/transpiled/query.d.ts +9 -0
  105. package/transpiled/query.js +57 -0
  106. package/transpiled/query.js.map +1 -0
  107. package/transpiled/receiver.d.ts +20 -0
  108. package/transpiled/receiver.js +113 -0
  109. package/transpiled/receiver.js.map +1 -0
  110. package/transpiled/reflection.d.ts +9 -0
  111. package/transpiled/reflection.js +13 -0
  112. package/transpiled/reflection.js.map +1 -0
  113. package/transpiled/remote.d.ts +10 -0
  114. package/transpiled/remote.js +11 -0
  115. package/transpiled/remote.js.map +1 -0
  116. package/transpiled/state.d.ts +36 -0
  117. package/transpiled/state.js +131 -0
  118. package/transpiled/state.js.map +1 -0
  119. package/transpiled/trail.d.ts +71 -0
  120. package/transpiled/trail.js +110 -0
  121. package/transpiled/trail.js.map +1 -0
  122. package/transpiled/transition.d.ts +14 -0
  123. package/transpiled/transition.js +51 -0
  124. package/transpiled/transition.js.map +1 -0
  125. package/transpiled/transmission.d.ts +8 -0
  126. package/transpiled/transmission.js +30 -0
  127. package/transpiled/transmission.js.map +1 -0
  128. package/transpiled/types/atomicity.d.ts +56 -0
  129. package/transpiled/types/atomicity.js +2 -0
  130. package/transpiled/types/atomicity.js.map +1 -0
  131. package/transpiled/types/bindings.d.ts +77 -0
  132. package/transpiled/types/bindings.js +2 -0
  133. package/transpiled/types/bindings.js.map +1 -0
  134. package/transpiled/types/bridges.d.ts +38 -0
  135. package/transpiled/types/bridges.js +2 -0
  136. package/transpiled/types/bridges.js.map +1 -0
  137. package/transpiled/types/extensions.d.ts +92 -0
  138. package/transpiled/types/extensions.js +2 -0
  139. package/transpiled/types/extensions.js.map +1 -0
  140. package/transpiled/types/inbox.d.ts +22 -0
  141. package/transpiled/types/inbox.js +2 -0
  142. package/transpiled/types/inbox.js.map +1 -0
  143. package/transpiled/types/index.d.ts +14 -0
  144. package/transpiled/types/index.js +9 -0
  145. package/transpiled/types/index.js.map +1 -0
  146. package/transpiled/types/message.d.ts +16 -0
  147. package/transpiled/types/message.js +2 -0
  148. package/transpiled/types/message.js.map +1 -0
  149. package/transpiled/types/operations.d.ts +15 -0
  150. package/transpiled/types/operations.js +2 -0
  151. package/transpiled/types/operations.js.map +1 -0
  152. package/transpiled/types/outbox.d.ts +54 -0
  153. package/transpiled/types/outbox.js +2 -0
  154. package/transpiled/types/outbox.js.map +1 -0
  155. package/transpiled/types/receiver.d.ts +9 -0
  156. package/transpiled/types/receiver.js +2 -0
  157. package/transpiled/types/receiver.js.map +1 -0
  158. package/transpiled/types/request.d.ts +103 -0
  159. package/transpiled/types/request.js +2 -0
  160. package/transpiled/types/request.js.map +1 -0
  161. package/transpiled/types/state.d.ts +9 -0
  162. package/transpiled/types/state.js +2 -0
  163. package/transpiled/types/state.js.map +1 -0
  164. package/transpiled/types/storages.d.ts +146 -0
  165. package/transpiled/types/storages.js +2 -0
  166. package/transpiled/types/storages.js.map +1 -0
  167. package/transpiled/unmanaged.d.ts +5 -0
  168. package/transpiled/unmanaged.js +7 -0
  169. package/transpiled/unmanaged.js.map +1 -0
  170. package/src/assignment.js +0 -22
  171. package/src/call.js +0 -42
  172. package/src/cascade.js +0 -36
  173. package/src/component.js +0 -41
  174. package/src/composition.js +0 -25
  175. package/src/connector.js +0 -183
  176. package/src/context.js +0 -55
  177. package/src/contract/conditions.js +0 -21
  178. package/src/contract/index.js +0 -7
  179. package/src/contract/reply.js +0 -25
  180. package/src/contract/request.js +0 -67
  181. package/src/contract/schemas/error.yaml +0 -9
  182. package/src/contract/schemas/index.js +0 -7
  183. package/src/contract/schemas/query.yaml +0 -31
  184. package/src/discovery.js +0 -44
  185. package/src/emission.js +0 -23
  186. package/src/entities/changeset.js +0 -47
  187. package/src/entities/entity.js +0 -58
  188. package/src/entities/factory.js +0 -33
  189. package/src/entities/index.js +0 -5
  190. package/src/entities/set.js +0 -15
  191. package/src/event.js +0 -40
  192. package/src/exceptions.js +0 -99
  193. package/src/exposition.js +0 -27
  194. package/src/index.js +0 -47
  195. package/src/locator.js +0 -49
  196. package/src/observation.js +0 -20
  197. package/src/operation.js +0 -69
  198. package/src/query/criteria.js +0 -41
  199. package/src/query/options.js +0 -40
  200. package/src/query.js +0 -41
  201. package/src/receiver.js +0 -65
  202. package/src/reflection.js +0 -28
  203. package/src/remote.js +0 -17
  204. package/src/state.js +0 -98
  205. package/src/transition.js +0 -60
  206. package/src/transmission.js +0 -33
  207. package/test/call.fixtures.js +0 -27
  208. package/test/call.test.js +0 -46
  209. package/test/component.fixtures.js +0 -21
  210. package/test/component.test.js +0 -40
  211. package/test/connector.fixtures.js +0 -40
  212. package/test/connector.test.js +0 -176
  213. package/test/context.fixtures.js +0 -18
  214. package/test/context.test.js +0 -29
  215. package/test/contract/conditions.test.js +0 -26
  216. package/test/contract/contract.fixtures.js +0 -33
  217. package/test/contract/request.test.js +0 -112
  218. package/test/emission.fixtures.js +0 -17
  219. package/test/emission.test.js +0 -35
  220. package/test/entities/entity.fixtures.js +0 -26
  221. package/test/entities/entity.test.js +0 -76
  222. package/test/entities/factory.fixtures.js +0 -18
  223. package/test/entities/factory.test.js +0 -48
  224. package/test/entities/set.fixtures.js +0 -11
  225. package/test/entities/set.test.js +0 -12
  226. package/test/event.fixtures.js +0 -29
  227. package/test/event.test.js +0 -118
  228. package/test/locator.test.js +0 -113
  229. package/test/query.fixtures.js +0 -100
  230. package/test/query.test.js +0 -86
  231. package/test/receiver.fixtures.js +0 -23
  232. package/test/receiver.test.js +0 -95
  233. package/test/reflection.test.js +0 -30
  234. package/test/state.fixtures.js +0 -47
  235. package/test/state.test.js +0 -46
  236. package/test/transmission.fixtures.js +0 -15
  237. package/test/transmission.test.js +0 -46
  238. package/types/bindings.d.ts +0 -40
  239. package/types/bridges.ts +0 -30
  240. package/types/component.d.ts +0 -9
  241. package/types/connector.d.ts +0 -18
  242. package/types/context.d.ts +0 -21
  243. package/types/entity.d.ts +0 -46
  244. package/types/event.d.ts +0 -11
  245. package/types/exception.d.ts +0 -4
  246. package/types/extensions.d.ts +0 -29
  247. package/types/index.ts +0 -14
  248. package/types/locator.d.ts +0 -12
  249. package/types/message.d.ts +0 -3
  250. package/types/operations.d.ts +0 -2
  251. package/types/query.d.ts +0 -15
  252. package/types/receiver.d.ts +0 -12
  253. package/types/reflection.d.ts +0 -14
  254. package/types/request.d.ts +0 -23
  255. package/types/state.d.ts +0 -35
  256. package/types/storages.d.ts +0 -71
@@ -0,0 +1,52 @@
1
+ import { console } from 'openspan';
2
+ import { Connector } from './connector.js';
3
+ export class Discovery extends Connector {
4
+ #lookup;
5
+ #lookups = {};
6
+ /** What is still saying it is waiting, so that a discovery going away can stop it. */
7
+ #waiting = new Set();
8
+ constructor(lookup) {
9
+ super();
10
+ this.#lookup = lookup;
11
+ }
12
+ async open() {
13
+ this.#lookups = {};
14
+ }
15
+ /**
16
+ * A lookup is unbounded, so one in flight when this goes away never settles and would go on
17
+ * saying it is waiting for the life of the process. Which nothing noticed while a
18
+ * disconnected discovery meant a process on its way out — a halt takes one down and builds
19
+ * another, and every cycle would leave a voice behind.
20
+ */
21
+ async close() {
22
+ for (const warning of this.#waiting)
23
+ clearInterval(warning);
24
+ this.#waiting.clear();
25
+ }
26
+ async lookup(locator) {
27
+ const id = locator.id;
28
+ if (this.#lookups[id] === undefined) {
29
+ const lookup = await this.#lookup(locator);
30
+ this.#lookups[id] = Promise.resolve(lookup);
31
+ this.depends(lookup);
32
+ }
33
+ const since = Date.now();
34
+ // the wait is unbounded by design, as a dependency may still be starting,
35
+ // so repeating is the only way a component stuck on one stays visible
36
+ const warning = setInterval(() => {
37
+ const waiting = Math.round((Date.now() - since) / 1000);
38
+ console.warn('Waiting for lookup response', { component: id, waiting });
39
+ }, INTERVAL);
40
+ warning.unref();
41
+ this.#waiting.add(warning);
42
+ try {
43
+ return await (await this.#lookups[id]).invoke();
44
+ }
45
+ finally {
46
+ clearInterval(warning);
47
+ this.#waiting.delete(warning);
48
+ }
49
+ }
50
+ }
51
+ const INTERVAL = 5000;
52
+ //# sourceMappingURL=discovery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"discovery.js","sourceRoot":"","sources":["../source/discovery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAO1C,MAAM,OAAO,SAAU,SAAQ,SAAS;IAC7B,OAAO,CAAuC;IACvD,QAAQ,GAAoC,EAAE,CAAA;IAE9C,sFAAsF;IAC7E,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAA;IAE7C,YAAmB,MAA6C;QAC9D,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;IACvB,CAAC;IAEkB,KAAK,CAAC,IAAI;QAC3B,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAA;IACpB,CAAC;IAED;;;;;OAKG;IACgB,KAAK,CAAC,KAAK;QAC5B,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,QAAQ;YAAE,aAAa,CAAC,OAAO,CAAC,CAAA;QAE3D,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAA;IACvB,CAAC;IAEM,KAAK,CAAC,MAAM,CAAC,OAAgB;QAClC,MAAM,EAAE,GAAG,OAAO,CAAC,EAAE,CAAA;QAErB,IAAI,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,KAAK,SAAS,EAAE,CAAC;YACpC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;YAE1C,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;YAC3C,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;QACtB,CAAC;QAED,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAExB,0EAA0E;QAC1E,sEAAsE;QACtE,MAAM,OAAO,GAAG,WAAW,CAAC,GAAG,EAAE;YAC/B,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,GAAG,IAAI,CAAC,CAAA;YAEvD,OAAO,CAAC,IAAI,CAAC,6BAA6B,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC,CAAA;QACzE,CAAC,EAAE,QAAQ,CAAC,CAAA;QAEZ,OAAO,CAAC,KAAK,EAAE,CAAA;QACf,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QAE1B,IAAI,CAAC;YACH,OAAO,MAAM,CAAC,MAAM,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAA;QACjD,CAAC;gBAAS,CAAC;YACT,aAAa,CAAC,OAAO,CAAC,CAAA;YACtB,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;QAC/B,CAAC;IACH,CAAC;CACF;AAED,MAAM,QAAQ,GAAG,IAAI,CAAA"}
@@ -0,0 +1,5 @@
1
+ import { Observation } from './observation.js';
2
+ import type { Store } from './operation.js';
3
+ export declare class Effect extends Observation {
4
+ protected acquire(store: Store): Promise<void>;
5
+ }
@@ -0,0 +1,11 @@
1
+ import { Observation } from './observation.js';
2
+ export class Effect extends Observation {
3
+ async acquire(store) {
4
+ const { query, entity, input } = store.request;
5
+ if (entity === undefined)
6
+ return super.acquire(store);
7
+ store.scope = await this.scope.ensure(query, entity, input);
8
+ store.state = store.scope.get();
9
+ }
10
+ }
11
+ //# sourceMappingURL=effect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"effect.js","sourceRoot":"","sources":["../source/effect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAI9C,MAAM,OAAO,MAAO,SAAQ,WAAW;IAClB,KAAK,CAAC,OAAO,CAAC,KAAY;QAC3C,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC,OAAO,CAAA;QAE9C,IAAI,MAAM,KAAK,SAAS;YAAE,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAErD,KAAK,CAAC,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,KAAY,EAAE,MAAM,EAAE,KAAK,CAAC,CAAA;QAClE,KAAK,CAAC,KAAK,GAAI,KAAK,CAAC,KAAgB,CAAC,GAAG,EAAE,CAAA;IAC7C,CAAC;CACF"}
@@ -0,0 +1,10 @@
1
+ import { Connector } from './connector.js';
2
+ import type { Event } from './event.js';
3
+ import type { Row } from './types/outbox.js';
4
+ export declare class Emission extends Connector {
5
+ #private;
6
+ /** what a row is outstanding for while this has not published it */
7
+ readonly name = "events";
8
+ constructor(events: Event[]);
9
+ emit(row: Row): Promise<void>;
10
+ }
@@ -0,0 +1,16 @@
1
+ import { Connector } from './connector.js';
2
+ export class Emission extends Connector {
3
+ /** what a row is outstanding for while this has not published it */
4
+ name = 'events';
5
+ #events;
6
+ constructor(events) {
7
+ super();
8
+ this.#events = events;
9
+ this.depends(events);
10
+ }
11
+ async emit(row) {
12
+ const emission = this.#events.map((e) => e.emit(row));
13
+ await Promise.all(emission);
14
+ }
15
+ }
16
+ //# sourceMappingURL=emission.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"emission.js","sourceRoot":"","sources":["../source/emission.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAI1C,MAAM,OAAO,QAAS,SAAQ,SAAS;IACrC,oEAAoE;IACpD,IAAI,GAAG,QAAQ,CAAA;IAEtB,OAAO,CAAS;IAEzB,YAAmB,MAAe;QAChC,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;QAErB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IACtB,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,GAAQ;QACxB,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA;QAErD,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IAC7B,CAAC;CACF"}
@@ -0,0 +1,10 @@
1
+ import type { Schema } from '@toa.io/schemas';
2
+ import type { Query, Record } from '../types/storages.js';
3
+ export declare class Changeset {
4
+ #private;
5
+ readonly query: Query;
6
+ constructor(schema: Schema, query: Query);
7
+ get(): object;
8
+ set(value: Record): void;
9
+ export(): object;
10
+ }
@@ -0,0 +1,27 @@
1
+ import { EntityContractException } from '../exceptions.js';
2
+ export class Changeset {
3
+ query;
4
+ /** the changeset schema: the entity's properties, none of them required */
5
+ #schema;
6
+ #state;
7
+ constructor(schema, query) {
8
+ this.query = query;
9
+ this.#schema = schema;
10
+ this.#state = {};
11
+ }
12
+ get() {
13
+ return this.#state;
14
+ }
15
+ set(value) {
16
+ const error = this.#schema.fit(value);
17
+ if (error !== null)
18
+ throw new EntityContractException(error, value);
19
+ delete value.VERSION;
20
+ value.UPDATED = Date.now();
21
+ this.#state = value;
22
+ }
23
+ export() {
24
+ return this.#state;
25
+ }
26
+ }
27
+ //# sourceMappingURL=changeset.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"changeset.js","sourceRoot":"","sources":["../../source/entities/changeset.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAA;AAI1D,MAAM,OAAO,SAAS;IACJ,KAAK,CAAO;IAE5B,2EAA2E;IAClE,OAAO,CAAQ;IACxB,MAAM,CAAiB;IAEvB,YAAmB,MAAc,EAAE,KAAY;QAC7C,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAElB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;QACrB,IAAI,CAAC,MAAM,GAAG,EAAE,CAAA;IAClB,CAAC;IAEM,GAAG;QACR,OAAO,IAAI,CAAC,MAAM,CAAA;IACpB,CAAC;IAEM,GAAG,CAAC,KAAa;QACtB,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;QAErC,IAAI,KAAK,KAAK,IAAI;YAAE,MAAM,IAAI,uBAAuB,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;QAEnE,OAAQ,KAAyB,CAAC,OAAO,CAAA;QACzC,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAE1B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;IACrB,CAAC;IAEM,MAAM;QACX,OAAO,IAAI,CAAC,MAAM,CAAA;IACpB,CAAC;CACF"}
@@ -0,0 +1,19 @@
1
+ import type { Schema } from '@toa.io/schemas';
2
+ import type { Guard } from '../guard.js';
3
+ import type { Record } from '../types/storages.js';
4
+ import type { Event } from '../types/state.js';
5
+ export declare class Entity {
6
+ #private;
7
+ deleted: boolean;
8
+ /**
9
+ * A record makes an entity of what a storage holds; a string, or nothing at all, makes a
10
+ * blank one under that identity, out of what the component declared a record starts as.
11
+ *
12
+ * @param blank what the component declares a record holds before anything is written to it
13
+ * @param mutable whether the entity may be modified and committed
14
+ */
15
+ constructor(schema: Schema, blank: object, argument?: Record | string, guards?: Guard[], mutable?: boolean);
16
+ get(): Record;
17
+ set(value: Record): void;
18
+ event(input?: object): Event;
19
+ }
@@ -0,0 +1,137 @@
1
+ import { environment } from '@toa.io/generic';
2
+ import { EntityContractException, EntityGuardException } from '../exceptions.js';
3
+ import { newid } from './newid.js';
4
+ export class Entity {
5
+ deleted = false;
6
+ #schema;
7
+ #blank;
8
+ #guards;
9
+ #origin = null;
10
+ #state;
11
+ #mutable = true;
12
+ /**
13
+ * A record makes an entity of what a storage holds; a string, or nothing at all, makes a
14
+ * blank one under that identity, out of what the component declared a record starts as.
15
+ *
16
+ * @param blank what the component declares a record holds before anything is written to it
17
+ * @param mutable whether the entity may be modified and committed
18
+ */
19
+ // eslint-disable-next-line max-params
20
+ constructor(schema, blank, argument, guards, mutable = true) {
21
+ this.#schema = schema;
22
+ this.#blank = blank;
23
+ this.#guards = guards;
24
+ if (typeof argument === 'object')
25
+ this.#acquire(argument, mutable);
26
+ else
27
+ this.#write(this.#compose(argument ?? newid()));
28
+ }
29
+ get() {
30
+ return this.#state;
31
+ }
32
+ set(value) {
33
+ if (!this.#mutable)
34
+ throw new Error('Entity acquired by a read-only operation cannot be modified');
35
+ this.#guard(value);
36
+ const error = this.#schema.fit(value);
37
+ if (error !== null)
38
+ throw new EntityContractException(error, value);
39
+ this.#revive(value);
40
+ this.#write(value);
41
+ }
42
+ event(input) {
43
+ return {
44
+ origin: this.#origin,
45
+ state: this.#state,
46
+ trailers: this.#state._trailers,
47
+ input
48
+ };
49
+ }
50
+ /**
51
+ * The origin is the pre-image a commit diffs the new state against. An operation that
52
+ * cannot commit has nothing to diff, so it takes the record as it came from the storage
53
+ * instead of paying for a deep copy of every record it read.
54
+ */
55
+ #acquire(record, mutable) {
56
+ this.#mutable = mutable;
57
+ if (!mutable) {
58
+ this.#write(record);
59
+ return;
60
+ }
61
+ this.#write(structuredClone(record));
62
+ this.#origin = record;
63
+ }
64
+ /**
65
+ * A blank is not validated: what the component declared is fitted once, at boot, and the
66
+ * system properties are the runtime's own. It is copied because every entity the component
67
+ * makes is written over its own.
68
+ */
69
+ #compose(id) {
70
+ // `REGION` here as well as in `#write`, because a record is validated before it is written
71
+ // and what is composed is about to be written here: it is this region's
72
+ return {
73
+ id,
74
+ VERSION: 0,
75
+ DELETED: null,
76
+ REGION,
77
+ ...structuredClone(this.#blank)
78
+ };
79
+ }
80
+ #guard(value) {
81
+ if (this.#guards === undefined)
82
+ return;
83
+ for (const guard of this.#guards) {
84
+ const ok = guard.fit(value, this.#origin);
85
+ if (ok === false)
86
+ throw new EntityGuardException(guard.name, value);
87
+ }
88
+ }
89
+ // deletion is only expressed as a new DELETED timestamp,
90
+ // so committing over a tombstone without touching it means revival
91
+ #revive(value) {
92
+ if (this.#origin?.DELETED == null || value.DELETED !== this.#origin.DELETED)
93
+ return;
94
+ value.DELETED = null;
95
+ this.deleted = false;
96
+ }
97
+ #write(value) {
98
+ if (!('_trailers' in value))
99
+ Object.defineProperty(value, '_trailers', {
100
+ writable: false,
101
+ configurable: false,
102
+ enumerable: false,
103
+ value: {}
104
+ });
105
+ /*
106
+ * The system properties are the runtime's to write, and a record does not always come from
107
+ * a storage that wrote them: a component may bring its own, and one that answers with an
108
+ * id and a version has left the rest to whoever asked. Filling them in here is what makes
109
+ * a record's shape the runtime's guarantee rather than a storage's promise, and what lets
110
+ * the entity require them.
111
+ */
112
+ value.CREATED ??= Date.now();
113
+ value.UPDATED ??= value.CREATED;
114
+ value.VERSION ??= 0;
115
+ value.DELETED ??= null;
116
+ value.REGION ??= REGION;
117
+ if (value.DELETED !== null)
118
+ this.deleted = true;
119
+ // beside the version and the timestamp, and for the same reason: all three say what the
120
+ // write did, so all three change only where there is one. A record read here keeps the
121
+ // region that wrote it, which is not necessarily this one — that is the whole of what it
122
+ // is for, and stamping it on the way in would answer every read with the wrong region.
123
+ if (this.#state !== undefined) {
124
+ value.UPDATED = Date.now();
125
+ value.VERSION++;
126
+ value.REGION = REGION;
127
+ }
128
+ this.#state = value;
129
+ }
130
+ }
131
+ /**
132
+ * The rank of the region this deployment is, from what deployed it. An application that is one
133
+ * region has none and writes `0`, which is what the first region is and what a record written
134
+ * before any of this existed is backfilled with.
135
+ */
136
+ const REGION = Number(environment.get('TOA_REGION') ?? 0);
137
+ //# sourceMappingURL=entity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity.js","sourceRoot":"","sources":["../../source/entities/entity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAChF,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAMlC,MAAM,OAAO,MAAM;IACV,OAAO,GAAG,KAAK,CAAA;IAEb,OAAO,CAAQ;IACf,MAAM,CAAQ;IACd,OAAO,CAAqB;IACrC,OAAO,GAAkB,IAAI,CAAA;IAC7B,MAAM,CAAS;IACf,QAAQ,GAAG,IAAI,CAAA;IAEf;;;;;;OAMG;IACH,sCAAsC;IACtC,YACE,MAAc,EACd,KAAa,EACb,QAA0B,EAC1B,MAAgB,EAChB,OAAO,GAAG,IAAI;QAEd,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;QACrB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;QACnB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;QAErB,IAAI,OAAO,QAAQ,KAAK,QAAQ;YAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;;YAC7D,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,IAAI,KAAK,EAAE,CAAC,CAAC,CAAA;IACtD,CAAC;IAEM,GAAG;QACR,OAAO,IAAI,CAAC,MAAM,CAAA;IACpB,CAAC;IAEM,GAAG,CAAC,KAAa;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AK,SAAS,EAAE,CAAC;YAC9B,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;YAC1B,KAAK,CAAC,OAAO,EAAE,CAAA;YACf,KAAK,CAAC,MAAM,GAAG,MAAM,CAAA;QACvB,CAAC;QAED,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;IACrB,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAA"}
@@ -0,0 +1,27 @@
1
+ import { Entity } from './entity.js';
2
+ import { EntitySet } from './set.js';
3
+ import { Changeset } from './changeset.js';
4
+ import type { Schema } from '@toa.io/schemas';
5
+ import type { Guard } from '../guard.js';
6
+ import type { Query, Record } from '../types/storages.js';
7
+ /** The two schemas a component's records are held to, and what a new one starts as. */
8
+ export interface Schemas {
9
+ /** what a stored record must fit */
10
+ entity: Schema;
11
+ /** what a changeset must fit: the same properties, none of them required */
12
+ changeset: Schema;
13
+ }
14
+ export declare class Factory {
15
+ #private;
16
+ constructor(schemas: Schemas, blank: object, guards?: Guard[]);
17
+ /**
18
+ * What these values would make of a record, held to what a record must be. It starts from a
19
+ * blank rather than from the declared properties alone, because what is validated is the
20
+ * record and a record carries what the runtime writes into every one of them.
21
+ */
22
+ fit(values: object): void;
23
+ init(id?: string): Entity;
24
+ object(record: Record, mutable?: boolean): Entity;
25
+ objects(recordset: Record[], init?: string[], mutable?: boolean): EntitySet;
26
+ changeset(query: Query): Changeset;
27
+ }
@@ -0,0 +1,38 @@
1
+ import { Entity } from './entity.js';
2
+ import { EntitySet } from './set.js';
3
+ import { Changeset } from './changeset.js';
4
+ export class Factory {
5
+ #schemas;
6
+ #blank;
7
+ #guards;
8
+ constructor(schemas, blank, guards) {
9
+ this.#schemas = schemas;
10
+ this.#blank = blank;
11
+ this.#guards = guards;
12
+ }
13
+ /**
14
+ * What these values would make of a record, held to what a record must be. It starts from a
15
+ * blank rather than from the declared properties alone, because what is validated is the
16
+ * record and a record carries what the runtime writes into every one of them.
17
+ */
18
+ fit(values) {
19
+ this.#schemas.entity.validate({ ...this.init().get(), ...values }, 'Entity');
20
+ }
21
+ init(id) {
22
+ return new Entity(this.#schemas.entity, this.#blank, id, this.#guards);
23
+ }
24
+ object(record, mutable = true) {
25
+ return new Entity(this.#schemas.entity, this.#blank, record, this.#guards, mutable);
26
+ }
27
+ objects(recordset, init, mutable = true) {
28
+ const set = recordset.map((record) => this.object(record, mutable));
29
+ if (init !== undefined)
30
+ for (const id of init)
31
+ set.unshift(this.init(id));
32
+ return new EntitySet(set);
33
+ }
34
+ changeset(query) {
35
+ return new Changeset(this.#schemas.changeset, query);
36
+ }
37
+ }
38
+ //# sourceMappingURL=factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factory.js","sourceRoot":"","sources":["../../source/entities/factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAc1C,MAAM,OAAO,OAAO;IACT,QAAQ,CAAS;IACjB,MAAM,CAAQ;IACd,OAAO,CAAqB;IAErC,YAAmB,OAAgB,EAAE,KAAa,EAAE,MAAgB;QAClE,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAA;QACvB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;QACnB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;IACvB,CAAC;IAED;;;;OAIG;IACI,GAAG,CAAC,MAAc;QACvB,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,EAAE,EAAE,GAAG,MAAM,EAAE,EAAE,QAAQ,CAAC,CAAA;IAC9E,CAAC;IAEM,IAAI,CAAC,EAAW;QACrB,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,CAAA;IACxE,CAAC;IAEM,MAAM,CAAC,MAAc,EAAE,OAAO,GAAG,IAAI;QAC1C,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;IACrF,CAAC;IAEM,OAAO,CAAC,SAAmB,EAAE,IAAe,EAAE,OAAO,GAAG,IAAI;QACjE,MAAM,GAAG,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;QAEnE,IAAI,IAAI,KAAK,SAAS;YAAE,KAAK,MAAM,EAAE,IAAI,IAAI;gBAAE,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAA;QAEzE,OAAO,IAAI,SAAS,CAAC,GAAG,CAAC,CAAA;IAC3B,CAAC;IAEM,SAAS,CAAC,KAAY;QAC3B,OAAO,IAAI,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,KAAK,CAAC,CAAA;IACtD,CAAC;CACF"}
@@ -0,0 +1,5 @@
1
+ export { derive, newid } from './newid.js';
2
+ export { Factory } from './factory.js';
3
+ export { Entity } from './entity.js';
4
+ export { EntitySet } from './set.js';
5
+ export { Changeset } from './changeset.js';
@@ -0,0 +1,6 @@
1
+ export { derive, newid } from './newid.js';
2
+ export { Factory } from './factory.js';
3
+ export { Entity } from './entity.js';
4
+ export { EntitySet } from './set.js';
5
+ export { Changeset } from './changeset.js';
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/entities/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA"}
@@ -0,0 +1,10 @@
1
+ export declare function newid(): string;
2
+ /**
3
+ * An id that is a function of what it is made of, in the shape `newid` produces, so that an `_id`
4
+ * is one kind of thing wherever it came from.
5
+ *
6
+ * What it is for is a call whose identity must be the same on every run that makes it: the same
7
+ * duplicate then produces the same identity, and whatever it reaches refuses its own copy. A
8
+ * name-based uuid is exactly that function, and `uuid` is here already.
9
+ */
10
+ export declare function derive(...parts: Array<string | number>): string;
@@ -0,0 +1,35 @@
1
+ import * as uuid from 'uuid';
2
+ export function newid() {
3
+ const buf = Buffer.alloc(16);
4
+ uuid.v7(undefined, buf);
5
+ return buf.toString('hex');
6
+ }
7
+ /**
8
+ * An id that is a function of what it is made of, in the shape `newid` produces, so that an `_id`
9
+ * is one kind of thing wherever it came from.
10
+ *
11
+ * What it is for is a call whose identity must be the same on every run that makes it: the same
12
+ * duplicate then produces the same identity, and whatever it reaches refuses its own copy. A
13
+ * name-based uuid is exactly that function, and `uuid` is here already.
14
+ */
15
+ export function derive(...parts) {
16
+ const buf = Buffer.alloc(16);
17
+ uuid.v5(name(parts), NAMESPACE, buf);
18
+ return buf.toString('hex');
19
+ }
20
+ /**
21
+ * Each part behind its own length, so that no two different sets of parts read as one name
22
+ * whatever a part happens to contain — a separator alone would have `a` and `b:c` name what
23
+ * `a:b` and `c` do, and this decides whether two calls are one call.
24
+ */
25
+ function name(parts) {
26
+ let name = '';
27
+ for (const part of parts) {
28
+ const value = String(part);
29
+ name += value.length + ':' + value;
30
+ }
31
+ return name;
32
+ }
33
+ /** Toa's own, so that a name derived here collides with nothing derived elsewhere. */
34
+ const NAMESPACE = '26bd9bc6-675c-4465-9b42-9e008b20befe';
35
+ //# sourceMappingURL=newid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"newid.js","sourceRoot":"","sources":["../../source/entities/newid.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAA;AAE5B,MAAM,UAAU,KAAK;IACnB,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IAE5B,IAAI,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;IAEvB,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;AAC5B,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,MAAM,CAAC,GAAG,KAA6B;IACrD,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IAE5B,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,GAAG,CAAC,CAAA;IAEpC,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;AAC5B,CAAC;AAED;;;;GAIG;AACH,SAAS,IAAI,CAAC,KAA6B;IACzC,IAAI,IAAI,GAAG,EAAE,CAAA;IAEb,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAA;QAE1B,IAAI,IAAI,KAAK,CAAC,MAAM,GAAG,GAAG,GAAG,KAAK,CAAA;IACpC,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC;AAED,sFAAsF;AACtF,MAAM,SAAS,GAAG,sCAAsC,CAAA"}
@@ -0,0 +1,10 @@
1
+ import type { Entity } from './entity.js';
2
+ import type { Record } from '../types/storages.js';
3
+ import type { Event } from '../types/state.js';
4
+ export declare class EntitySet {
5
+ #private;
6
+ constructor(set: Entity[]);
7
+ get(): Record[];
8
+ set(values: Record[]): void;
9
+ events(input?: object): Event[];
10
+ }
@@ -0,0 +1,19 @@
1
+ import { SystemException } from '../exceptions.js';
2
+ export class EntitySet {
3
+ #set;
4
+ constructor(set) {
5
+ this.#set = set;
6
+ }
7
+ get() {
8
+ return this.#set.map((entity) => entity.get());
9
+ }
10
+ set(values) {
11
+ if (values.length !== this.#set.length)
12
+ throw new SystemException('Objects array must not be modified');
13
+ values.forEach((value, index) => this.#set[index].set(value));
14
+ }
15
+ events(input) {
16
+ return this.#set.map((entity) => entity.event(input));
17
+ }
18
+ }
19
+ //# sourceMappingURL=set.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set.js","sourceRoot":"","sources":["../../source/entities/set.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAKlD,MAAM,OAAO,SAAS;IACX,IAAI,CAAU;IAEvB,YAAmB,GAAa;QAC9B,IAAI,CAAC,IAAI,GAAG,GAAG,CAAA;IACjB,CAAC;IAEM,GAAG;QACR,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAA;IAChD,CAAC;IAEM,GAAG,CAAC,MAAgB;QACzB,IAAI,MAAM,CAAC,MAAM,KAAK,IAAI,CAAC,IAAI,CAAC,MAAM;YACpC,MAAM,IAAI,eAAe,CAAC,oCAAoC,CAAC,CAAA;QAEjE,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAA;IAC/D,CAAC;IAEM,MAAM,CAAC,KAAc;QAC1B,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAA;IACvD,CAAC;CACF"}
@@ -0,0 +1,14 @@
1
+ import { Connector } from './connector.js';
2
+ import type { Emitter } from './types/bindings.js';
3
+ import type { Event as Bridge } from './types/bridges.js';
4
+ import type { Row } from './types/outbox.js';
5
+ export interface Definition {
6
+ conditioned?: boolean;
7
+ subjective?: boolean;
8
+ label?: string;
9
+ }
10
+ export declare class Event extends Connector {
11
+ #private;
12
+ constructor(definition: Definition, emitter: Emitter, bridge?: Bridge);
13
+ emit(row: Row): Promise<void>;
14
+ }
@@ -0,0 +1,51 @@
1
+ import { console, current, encode } from 'openspan';
2
+ import { Connector } from './connector.js';
3
+ import { derive } from './entities/newid.js';
4
+ export class Event extends Connector {
5
+ #emitter;
6
+ #bridge;
7
+ #conditioned;
8
+ #subjective;
9
+ #label;
10
+ constructor(definition, emitter, bridge) {
11
+ super();
12
+ this.#conditioned = definition.conditioned;
13
+ this.#subjective = definition.subjective;
14
+ this.#label = definition.label ?? 'event';
15
+ this.#emitter = emitter;
16
+ this.#bridge = bridge;
17
+ this.depends(emitter);
18
+ if (bridge !== undefined)
19
+ this.depends(bridge);
20
+ }
21
+ async emit(row) {
22
+ const event = row.event;
23
+ if (this.#conditioned === false || (await this.#bridge?.condition(event)) === true) {
24
+ const payload = this.#subjective === true ? await this.#bridge?.payload(event) : event.state;
25
+ /*
26
+ * The row is committed once, so its id is what makes every publication of it one message:
27
+ * the immediate path, the pump that may run an hour later in another replica, and a
28
+ * redelivery of what either sent. Derived with the label rather than the row id itself,
29
+ * because one row is published as several events and a component may consume two of them —
30
+ * one identity across both would have the second refused as a duplicate of the first.
31
+ */
32
+ const message = { payload, id: derive(row.id, this.#label) };
33
+ // the chain the change was made by, so a receiver of this continues it rather than
34
+ // starting one; read off the row, because the pump publishes off the operation's path
35
+ if (row.trail !== undefined)
36
+ message.trail = row.trail;
37
+ const options = {
38
+ name: `${this.#label} publish`,
39
+ kind: 'producer',
40
+ attributes: { 'messaging.destination.name': this.#label }
41
+ };
42
+ await console.span(options, async () => {
43
+ const context = current();
44
+ if (context !== undefined)
45
+ message.telemetry = encode(context);
46
+ await this.#emitter.emit(message);
47
+ });
48
+ }
49
+ }
50
+ }
51
+ //# sourceMappingURL=event.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event.js","sourceRoot":"","sources":["../source/event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAoB,MAAM,UAAU,CAAA;AACrE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAY5C,MAAM,OAAO,KAAM,SAAQ,SAAS;IACzB,QAAQ,CAAS;IACjB,OAAO,CAAoB;IAC3B,YAAY,CAAqB;IACjC,WAAW,CAAqB;IAChC,MAAM,CAAQ;IAEvB,YAAmB,UAAsB,EAAE,OAAgB,EAAE,MAAe;QAC1E,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,YAAY,GAAG,UAAU,CAAC,WAAW,CAAA;QAC1C,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC,UAAU,CAAA;QACxC,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC,KAAK,IAAI,OAAO,CAAA;QACzC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAA;QACvB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;QAErB,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;QAErB,IAAI,MAAM,KAAK,SAAS;YAAE,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IAChD,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,GAAQ;QACxB,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAA;QAEvB,IAAI,IAAI,CAAC,YAAY,KAAK,KAAK,IAAI,CAAC,MAAM,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;YACn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