@toa.io/core 1.0.0-alpha.32 → 1.0.0-alpha.321

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 (286) 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 +187 -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 +17 -0
  15. package/transpiled/component.js +151 -0
  16. package/transpiled/component.js.map +1 -0
  17. package/transpiled/composition.d.ts +6 -0
  18. package/transpiled/composition.js +20 -0
  19. package/transpiled/composition.js.map +1 -0
  20. package/transpiled/connector.d.ts +71 -0
  21. package/transpiled/connector.js +248 -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/component.d.ts +37 -0
  27. package/transpiled/contract/component.js +94 -0
  28. package/transpiled/contract/component.js.map +1 -0
  29. package/transpiled/contract/contract.d.ts +10 -0
  30. package/transpiled/contract/contract.js +14 -0
  31. package/transpiled/contract/contract.js.map +1 -0
  32. package/transpiled/contract/index.d.ts +4 -0
  33. package/transpiled/contract/index.js +5 -0
  34. package/transpiled/contract/index.js.map +1 -0
  35. package/transpiled/contract/reply.d.ts +7 -0
  36. package/transpiled/contract/reply.js +48 -0
  37. package/transpiled/contract/reply.js.map +1 -0
  38. package/transpiled/contract/request.d.ts +49 -0
  39. package/transpiled/contract/request.js +152 -0
  40. package/transpiled/contract/request.js.map +1 -0
  41. package/transpiled/contract/schemas.d.ts +4 -0
  42. package/transpiled/contract/schemas.js +38 -0
  43. package/transpiled/contract/schemas.js.map +1 -0
  44. package/transpiled/contract/system.d.ts +33 -0
  45. package/transpiled/contract/system.js +76 -0
  46. package/transpiled/contract/system.js.map +1 -0
  47. package/transpiled/deliveries.d.ts +22 -0
  48. package/transpiled/deliveries.js +31 -0
  49. package/transpiled/deliveries.js.map +1 -0
  50. package/transpiled/effect.d.ts +7 -0
  51. package/transpiled/effect.js +13 -0
  52. package/transpiled/effect.js.map +1 -0
  53. package/transpiled/emission.d.ts +10 -0
  54. package/transpiled/emission.js +16 -0
  55. package/transpiled/emission.js.map +1 -0
  56. package/transpiled/encoded.d.ts +16 -0
  57. package/transpiled/encoded.js +25 -0
  58. package/transpiled/encoded.js.map +1 -0
  59. package/transpiled/entities/changeset.d.ts +10 -0
  60. package/transpiled/entities/changeset.js +27 -0
  61. package/transpiled/entities/changeset.js.map +1 -0
  62. package/transpiled/entities/entity.d.ts +19 -0
  63. package/transpiled/entities/entity.js +137 -0
  64. package/transpiled/entities/entity.js.map +1 -0
  65. package/transpiled/entities/factory.d.ts +27 -0
  66. package/transpiled/entities/factory.js +38 -0
  67. package/transpiled/entities/factory.js.map +1 -0
  68. package/transpiled/entities/index.d.ts +5 -0
  69. package/transpiled/entities/index.js +6 -0
  70. package/transpiled/entities/index.js.map +1 -0
  71. package/transpiled/entities/newid.d.ts +11 -0
  72. package/transpiled/entities/newid.js +41 -0
  73. package/transpiled/entities/newid.js.map +1 -0
  74. package/transpiled/entities/set.d.ts +10 -0
  75. package/transpiled/entities/set.js +19 -0
  76. package/transpiled/entities/set.js.map +1 -0
  77. package/transpiled/event.d.ts +20 -0
  78. package/transpiled/event.js +65 -0
  79. package/transpiled/event.js.map +1 -0
  80. package/transpiled/exceptions.d.ts +110 -0
  81. package/transpiled/exceptions.js +215 -0
  82. package/transpiled/exceptions.js.map +1 -0
  83. package/transpiled/gate.d.ts +40 -0
  84. package/transpiled/gate.js +84 -0
  85. package/transpiled/gate.js.map +1 -0
  86. package/transpiled/guard.d.ts +7 -0
  87. package/transpiled/guard.js +12 -0
  88. package/transpiled/guard.js.map +1 -0
  89. package/transpiled/halting.d.ts +16 -0
  90. package/transpiled/halting.js +25 -0
  91. package/transpiled/halting.js.map +1 -0
  92. package/transpiled/index.d.ts +45 -0
  93. package/transpiled/index.js +37 -0
  94. package/transpiled/index.js.map +1 -0
  95. package/transpiled/instance.d.ts +10 -0
  96. package/transpiled/instance.js +30 -0
  97. package/transpiled/instance.js.map +1 -0
  98. package/transpiled/locator.d.ts +12 -0
  99. package/transpiled/locator.js +32 -0
  100. package/transpiled/locator.js.map +1 -0
  101. package/transpiled/measurements.d.ts +20 -0
  102. package/transpiled/measurements.js +40 -0
  103. package/transpiled/measurements.js.map +1 -0
  104. package/transpiled/observation.d.ts +7 -0
  105. package/transpiled/observation.js +14 -0
  106. package/transpiled/observation.js.map +1 -0
  107. package/transpiled/operation.d.ts +77 -0
  108. package/transpiled/operation.js +216 -0
  109. package/transpiled/operation.js.map +1 -0
  110. package/transpiled/outbox.d.ts +45 -0
  111. package/transpiled/outbox.js +448 -0
  112. package/transpiled/outbox.js.map +1 -0
  113. package/transpiled/outbox.measurements.d.ts +4 -0
  114. package/transpiled/outbox.measurements.js +38 -0
  115. package/transpiled/outbox.measurements.js.map +1 -0
  116. package/transpiled/parts.d.ts +20 -0
  117. package/transpiled/parts.js +23 -0
  118. package/transpiled/parts.js.map +1 -0
  119. package/transpiled/query/criteria.d.ts +6 -0
  120. package/transpiled/query/criteria.js +177 -0
  121. package/transpiled/query/criteria.js.map +1 -0
  122. package/transpiled/query/options.d.ts +3 -0
  123. package/transpiled/query/options.js +34 -0
  124. package/transpiled/query/options.js.map +1 -0
  125. package/transpiled/query.d.ts +9 -0
  126. package/transpiled/query.js +57 -0
  127. package/transpiled/query.js.map +1 -0
  128. package/transpiled/receiver.d.ts +20 -0
  129. package/transpiled/receiver.js +113 -0
  130. package/transpiled/receiver.js.map +1 -0
  131. package/transpiled/reflection.d.ts +9 -0
  132. package/transpiled/reflection.js +13 -0
  133. package/transpiled/reflection.js.map +1 -0
  134. package/transpiled/regional.d.ts +22 -0
  135. package/transpiled/regional.js +55 -0
  136. package/transpiled/regional.js.map +1 -0
  137. package/transpiled/remote.d.ts +10 -0
  138. package/transpiled/remote.js +11 -0
  139. package/transpiled/remote.js.map +1 -0
  140. package/transpiled/rendering.d.ts +13 -0
  141. package/transpiled/rendering.js +21 -0
  142. package/transpiled/rendering.js.map +1 -0
  143. package/transpiled/safety.d.ts +5 -0
  144. package/transpiled/safety.js +26 -0
  145. package/transpiled/safety.js.map +1 -0
  146. package/transpiled/state.d.ts +36 -0
  147. package/transpiled/state.js +131 -0
  148. package/transpiled/state.js.map +1 -0
  149. package/transpiled/trail.d.ts +78 -0
  150. package/transpiled/trail.js +112 -0
  151. package/transpiled/trail.js.map +1 -0
  152. package/transpiled/transition.d.ts +14 -0
  153. package/transpiled/transition.js +51 -0
  154. package/transpiled/transition.js.map +1 -0
  155. package/transpiled/transmission.d.ts +8 -0
  156. package/transpiled/transmission.js +30 -0
  157. package/transpiled/transmission.js.map +1 -0
  158. package/transpiled/types/atomicity.d.ts +58 -0
  159. package/transpiled/types/atomicity.js +2 -0
  160. package/transpiled/types/atomicity.js.map +1 -0
  161. package/transpiled/types/bindings.d.ts +82 -0
  162. package/transpiled/types/bindings.js +2 -0
  163. package/transpiled/types/bindings.js.map +1 -0
  164. package/transpiled/types/bridges.d.ts +41 -0
  165. package/transpiled/types/bridges.js +2 -0
  166. package/transpiled/types/bridges.js.map +1 -0
  167. package/transpiled/types/extensions.d.ts +121 -0
  168. package/transpiled/types/extensions.js +2 -0
  169. package/transpiled/types/extensions.js.map +1 -0
  170. package/transpiled/types/inbox.d.ts +27 -0
  171. package/transpiled/types/inbox.js +2 -0
  172. package/transpiled/types/inbox.js.map +1 -0
  173. package/transpiled/types/index.d.ts +14 -0
  174. package/transpiled/types/index.js +9 -0
  175. package/transpiled/types/index.js.map +1 -0
  176. package/transpiled/types/message.d.ts +16 -0
  177. package/transpiled/types/message.js +2 -0
  178. package/transpiled/types/message.js.map +1 -0
  179. package/transpiled/types/operations.d.ts +15 -0
  180. package/transpiled/types/operations.js +2 -0
  181. package/transpiled/types/operations.js.map +1 -0
  182. package/transpiled/types/outbox.d.ts +93 -0
  183. package/transpiled/types/outbox.js +2 -0
  184. package/transpiled/types/outbox.js.map +1 -0
  185. package/transpiled/types/receiver.d.ts +9 -0
  186. package/transpiled/types/receiver.js +2 -0
  187. package/transpiled/types/receiver.js.map +1 -0
  188. package/transpiled/types/request.d.ts +119 -0
  189. package/transpiled/types/request.js +2 -0
  190. package/transpiled/types/request.js.map +1 -0
  191. package/transpiled/types/state.d.ts +9 -0
  192. package/transpiled/types/state.js +2 -0
  193. package/transpiled/types/state.js.map +1 -0
  194. package/transpiled/types/storages.d.ts +158 -0
  195. package/transpiled/types/storages.js +2 -0
  196. package/transpiled/types/storages.js.map +1 -0
  197. package/transpiled/unmanaged.d.ts +5 -0
  198. package/transpiled/unmanaged.js +7 -0
  199. package/transpiled/unmanaged.js.map +1 -0
  200. package/src/assignment.js +0 -31
  201. package/src/call.js +0 -42
  202. package/src/cascade.js +0 -35
  203. package/src/component.js +0 -41
  204. package/src/composition.js +0 -25
  205. package/src/connector.js +0 -183
  206. package/src/context.js +0 -55
  207. package/src/contract/conditions.js +0 -21
  208. package/src/contract/index.js +0 -7
  209. package/src/contract/reply.js +0 -25
  210. package/src/contract/request.js +0 -67
  211. package/src/contract/schemas/error.yaml +0 -9
  212. package/src/contract/schemas/index.js +0 -7
  213. package/src/contract/schemas/query.yaml +0 -30
  214. package/src/discovery.js +0 -44
  215. package/src/emission.js +0 -23
  216. package/src/entities/changeset.js +0 -38
  217. package/src/entities/entity.js +0 -74
  218. package/src/entities/factory.js +0 -33
  219. package/src/entities/index.js +0 -5
  220. package/src/entities/set.js +0 -15
  221. package/src/event.js +0 -40
  222. package/src/exceptions.js +0 -100
  223. package/src/exposition.js +0 -27
  224. package/src/index.js +0 -47
  225. package/src/locator.js +0 -54
  226. package/src/observation.js +0 -20
  227. package/src/operation.js +0 -69
  228. package/src/query/criteria.js +0 -41
  229. package/src/query/options.js +0 -40
  230. package/src/query.js +0 -41
  231. package/src/receiver.js +0 -61
  232. package/src/reflection.js +0 -28
  233. package/src/remote.js +0 -17
  234. package/src/state.js +0 -99
  235. package/src/transition.js +0 -67
  236. package/src/transmission.js +0 -33
  237. package/test/call.fixtures.js +0 -27
  238. package/test/call.test.js +0 -46
  239. package/test/component.fixtures.js +0 -21
  240. package/test/component.test.js +0 -40
  241. package/test/connector.fixtures.js +0 -40
  242. package/test/connector.test.js +0 -176
  243. package/test/context.fixtures.js +0 -18
  244. package/test/context.test.js +0 -29
  245. package/test/contract/conditions.test.js +0 -26
  246. package/test/contract/contract.fixtures.js +0 -33
  247. package/test/contract/request.test.js +0 -112
  248. package/test/emission.fixtures.js +0 -17
  249. package/test/emission.test.js +0 -35
  250. package/test/entities/entity.fixtures.js +0 -29
  251. package/test/entities/entity.test.js +0 -65
  252. package/test/entities/factory.fixtures.js +0 -18
  253. package/test/entities/factory.test.js +0 -48
  254. package/test/entities/set.fixtures.js +0 -11
  255. package/test/entities/set.test.js +0 -12
  256. package/test/event.fixtures.js +0 -29
  257. package/test/event.test.js +0 -118
  258. package/test/locator.test.js +0 -113
  259. package/test/query.fixtures.js +0 -100
  260. package/test/query.test.js +0 -86
  261. package/test/receiver.fixtures.js +0 -23
  262. package/test/receiver.test.js +0 -95
  263. package/test/reflection.test.js +0 -30
  264. package/test/state.fixtures.js +0 -47
  265. package/test/state.test.js +0 -46
  266. package/test/transmission.fixtures.js +0 -15
  267. package/test/transmission.test.js +0 -46
  268. package/types/bindings.d.ts +0 -40
  269. package/types/bridges.ts +0 -30
  270. package/types/component.d.ts +0 -9
  271. package/types/connector.d.ts +0 -18
  272. package/types/context.d.ts +0 -21
  273. package/types/entity.d.ts +0 -46
  274. package/types/event.d.ts +0 -11
  275. package/types/exception.d.ts +0 -4
  276. package/types/extensions.d.ts +0 -29
  277. package/types/index.ts +0 -14
  278. package/types/locator.d.ts +0 -13
  279. package/types/message.d.ts +0 -3
  280. package/types/operations.d.ts +0 -2
  281. package/types/query.d.ts +0 -15
  282. package/types/receiver.d.ts +0 -12
  283. package/types/reflection.d.ts +0 -14
  284. package/types/request.d.ts +0 -23
  285. package/types/state.d.ts +0 -35
  286. package/types/storages.d.ts +0 -71
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Whether an operation of this type is incapable of changing the State. What a readonly request may
3
+ * reach, and nothing else; see `documentation/readonly.md`.
4
+ *
5
+ * Exhaustive on purpose, as the outcome of an exception code is: a type added later does not
6
+ * compile until it says which it is, because a type nobody classified would otherwise be reachable
7
+ * from a request that promised to read.
8
+ */
9
+ const SAFE = {
10
+ transition: false,
11
+ observation: true,
12
+ assignment: false,
13
+ computation: true,
14
+ // the type for an operation that reaches outside; what it reaches is not the runtime's to read
15
+ effect: false,
16
+ // its scope is the driver's own handle, and the runtime sees nothing it does with one
17
+ unmanaged: false
18
+ };
19
+ /**
20
+ * Takes a `string` because it comes off a manifest: an operation of a type this does not know is
21
+ * unsafe, which is what an endpoint nothing can classify has to be.
22
+ */
23
+ export function safe(type) {
24
+ return SAFE[type] === true;
25
+ }
26
+ //# sourceMappingURL=safety.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"safety.js","sourceRoot":"","sources":["../source/safety.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,MAAM,IAAI,GAA0B;IAClC,UAAU,EAAE,KAAK;IACjB,WAAW,EAAE,IAAI;IACjB,UAAU,EAAE,KAAK;IACjB,WAAW,EAAE,IAAI;IACjB,+FAA+F;IAC/F,MAAM,EAAE,KAAK;IACb,sFAAsF;IACtF,SAAS,EAAE,KAAK;CACjB,CAAA;AAED;;;GAGG;AACH,MAAM,UAAU,IAAI,CAAC,IAAY;IAC/B,OAAO,IAAI,CAAC,IAAY,CAAC,KAAK,IAAI,CAAA;AACpC,CAAC"}
@@ -0,0 +1,36 @@
1
+ import { EntitySet } from './entities/set.ts';
2
+ import type { Readable } from 'node:stream';
3
+ import type { Factory } from './entities/factory.ts';
4
+ import type { Entity } from './entities/entity.ts';
5
+ import type { Changeset } from './entities/changeset.ts';
6
+ import type { Outbox } from './outbox.ts';
7
+ import type { Call } from './types/inbox.ts';
8
+ import type { Query, Record, Storage } from './types/storages.ts';
9
+ /**
10
+ * What an operation acquires and commits. One method per scope a manifest may declare, which
11
+ * is how an operation reaches the one it was declared with.
12
+ */
13
+ export declare class State {
14
+ #private;
15
+ readonly storage: Storage;
16
+ constructor(storage: Storage, entities: Factory, outbox: Outbox | undefined, associated?: boolean);
17
+ init(id?: string): Entity;
18
+ fit(values: object): void;
19
+ /** scope `entry` */
20
+ entry(query: Query, mutable?: boolean): Promise<Entity | null>;
21
+ /** scope `entries` */
22
+ entries(query: Query, mutable?: boolean): Promise<EntitySet>;
23
+ /** scope `stream` */
24
+ stream(query: Query): Promise<Readable>;
25
+ /** scope `changeset` */
26
+ changeset(query: Query): Changeset;
27
+ /** scope `none`: an operation that acquires nothing still asks for its scope */
28
+ none(): null;
29
+ /** get-or-create, in one indivisible step */
30
+ /** what the call under this identity answered, or `null` where it was never made */
31
+ recall(id: string): Promise<object | null>;
32
+ ensure(query: Query | undefined, properties: object, input?: object, call?: Call): Promise<Entity>;
33
+ commit(state: Entity | EntitySet, input?: object, call?: Call): Promise<boolean>;
34
+ massCommit(state: EntitySet, input?: object): Promise<boolean>;
35
+ apply(state: Changeset, input?: object, call?: Call): Promise<Record>;
36
+ }
@@ -0,0 +1,131 @@
1
+ import { EntitySet } from './entities/set.js';
2
+ import { StatePreconditionException, StateNotFoundException } from './exceptions.js';
3
+ /**
4
+ * What an operation acquires and commits. One method per scope a manifest may declare, which
5
+ * is how an operation reaches the one it was declared with.
6
+ */
7
+ export class State {
8
+ storage;
9
+ #entities;
10
+ #outbox;
11
+ /** whether an entity exists by virtue of the identity it is looked up by */
12
+ #associated;
13
+ // eslint-disable-next-line max-params
14
+ constructor(storage, entities, outbox, associated) {
15
+ this.storage = storage;
16
+ this.#entities = entities;
17
+ this.#outbox = outbox;
18
+ this.#associated = associated === true;
19
+ }
20
+ init(id) {
21
+ return this.#entities.init(id);
22
+ }
23
+ fit(values) {
24
+ this.#entities.fit(values);
25
+ }
26
+ /** scope `entry` */
27
+ async entry(query, mutable = true) {
28
+ const record = await this.storage.get(query);
29
+ if (record !== null)
30
+ return this.#entities.object(record, mutable);
31
+ // an associated entity is whatever the identity it is looked up by names, so a miss on
32
+ // that identity alone is a blank rather than an absence
33
+ if (this.#associated &&
34
+ query.id !== undefined &&
35
+ query.criteria === undefined &&
36
+ query.version === undefined)
37
+ return this.init(query.id);
38
+ if (query.version !== undefined)
39
+ throw new StatePreconditionException();
40
+ return null;
41
+ }
42
+ /** scope `entries` */
43
+ async entries(query, mutable = true) {
44
+ const recordset = await this.storage.find(query);
45
+ const ids = query.ids;
46
+ const missing = this.#associated && ids !== undefined && recordset.length < ids.length;
47
+ const init = missing
48
+ ? ids.filter((id) => !recordset.some((record) => record.id === id))
49
+ : undefined;
50
+ return this.#entities.objects(recordset, init, mutable);
51
+ }
52
+ /** scope `stream` */
53
+ async stream(query) {
54
+ return this.storage.stream(query);
55
+ }
56
+ /** scope `changeset` */
57
+ changeset(query) {
58
+ return this.#entities.changeset(query);
59
+ }
60
+ /** scope `none`: an operation that acquires nothing still asks for its scope */
61
+ none() {
62
+ return null;
63
+ }
64
+ /** get-or-create, in one indivisible step */
65
+ /** what the call under this identity answered, or `null` where it was never made */
66
+ async recall(id) {
67
+ return (await this.storage.inbox?.recall(id)) ?? null;
68
+ }
69
+ // eslint-disable-next-line max-params
70
+ async ensure(query, properties, input, call) {
71
+ const object = this.#entities.init();
72
+ const blank = object.get();
73
+ Object.assign(blank, properties);
74
+ object.set(blank);
75
+ const row = this.#outbox?.row(object.event(input));
76
+ const record = await this.storage.ensure(query, properties, object.get(), row, call);
77
+ // whatever came back under another id was already there, and an effect never commits it
78
+ if (record.id !== blank.id)
79
+ return this.#entities.object(record, NOT_MUTABLE);
80
+ await this.#publish(row);
81
+ return object;
82
+ }
83
+ async commit(state, input, call) {
84
+ if (state instanceof EntitySet)
85
+ return this.massCommit(state, input);
86
+ // the row is built before the write so that the storage can commit it in the same
87
+ // transaction, closing the window this used to have
88
+ const row = this.#outbox?.row(state.event(input));
89
+ const ok = await this.storage.store(state.get(), row, call);
90
+ if (ok)
91
+ await this.#publish(row);
92
+ return ok;
93
+ }
94
+ async massCommit(state, input) {
95
+ const outbox = this.#outbox;
96
+ const rows = outbox === undefined
97
+ ? undefined
98
+ : state.events(input).map((event) => outbox.row(event));
99
+ const ok = await this.storage.massStore(state.get(), rows);
100
+ if (ok && rows !== undefined)
101
+ await Promise.all(rows.map(async (row) => this.#publish(row)));
102
+ return ok;
103
+ }
104
+ async apply(state, input, call) {
105
+ // an assignment's images are the write's own, so the storage fills them in
106
+ const row = this.#outbox?.row({ input });
107
+ const result = await this.storage.upsert(state.query, state.export(), row, call);
108
+ if (result === null)
109
+ throw state.query.version === undefined
110
+ ? new StateNotFoundException()
111
+ : new StatePreconditionException();
112
+ if (row !== undefined) {
113
+ // a storage that does not know how leaves the event with what it was given
114
+ row.event.state ??= result;
115
+ row.event.origin ??= null;
116
+ await this.#publish(row);
117
+ }
118
+ return result;
119
+ }
120
+ /**
121
+ * Without a durable outbox this is the emission itself, and the operation waits for it;
122
+ * with one the row is already committed and this returns at once.
123
+ */
124
+ async #publish(row) {
125
+ if (row !== undefined)
126
+ await this.#outbox?.publish(row);
127
+ }
128
+ }
129
+ /** an effect never commits what `ensure` hands it, see `Effect` */
130
+ const NOT_MUTABLE = false;
131
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../source/state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,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C,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACtB,2EAA2E;YAC3E,GAAG,CAAC,KAAK,CAAC,KAAK,KAAK,MAAM,CAAA;YAC1B,GAAG,CAAC,KAAK,CAAC,MAAM,KAAK,IAAI,CAAA;YAEzB,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;QAC1B,CAAC;QAED,OAAO,MAAM,CAAA;IACf,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,QAAQ,CAAC,GAAoB;QACjC,IAAI,GAAG,KAAK,SAAS;YAAE,MAAM,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,CAAA;IACzD,CAAC;CACF;AAED,mEAAmE;AACnE,MAAM,WAAW,GAAG,KAAK,CAAA"}
@@ -0,0 +1,78 @@
1
+ /** What one invocation carries for the calls it makes. */
2
+ export interface Invocation {
3
+ /** the hops it came by, oldest first, ending with its own */
4
+ hops: string[];
5
+ /**
6
+ * The identity of the request being served. Absent where the caller stamped none, which is a
7
+ * caller from before this existed; a call made under one mints its own identity instead.
8
+ */
9
+ id?: string;
10
+ /**
11
+ * Whether what it may reach is a safe operation and nothing else. Carried onto every call it
12
+ * makes, and never dropped: the request that arrived said so, and so does every one below it.
13
+ */
14
+ readonly?: boolean;
15
+ /**
16
+ * How many calls this invocation has already made to each endpoint. Two calls to one endpoint
17
+ * are two calls, and their identities have to say so — without this an operation looping over
18
+ * five items would make one identity five times and have four of them refused.
19
+ *
20
+ * Made where the first call is made: most operations make none.
21
+ */
22
+ calls?: Map<string, number>;
23
+ }
24
+ /**
25
+ * How many times one hop may appear before the call is refused, and how long a chain may grow
26
+ * at all. Repetition is the signal and names the cycle; depth is the backstop for a chain that
27
+ * grows without repeating a hop, and it is what bounds the array on the wire.
28
+ */
29
+ export interface Limits {
30
+ repeats: number;
31
+ depth: number;
32
+ }
33
+ /**
34
+ * What the environment says, read where a component is built rather than once for the process,
35
+ * as the outbox reads its own — so a composition booted after a variable was set sees it.
36
+ *
37
+ * `TOA_TRAIL_REPEATS=0` refuses nothing: the chain is still stamped, carried and bounded. It is
38
+ * the off switch, and it exists because this refuses calls an application may be making today —
39
+ * a handshake written as `a > b > a > b > a` is three occurrences of one hop. A breaker with no
40
+ * way to open it is itself the outage.
41
+ */
42
+ export declare function limits(): Limits;
43
+ /** The invocation running now, where there is one. */
44
+ export declare function current(): Invocation | undefined;
45
+ /** Runs `task` as the invocation the chain now ends with. */
46
+ export declare function follow<T>(invocation: Invocation, task: () => Promise<T>): Promise<T>;
47
+ /**
48
+ * The ordinal of the call `invocation` is about to make to `endpoint`, counting from zero, and
49
+ * counts it. Read once per call, on the caller's own path, which is where the order is the order
50
+ * the algorithm made them in.
51
+ */
52
+ export declare function ordinal(invocation: Invocation, endpoint: string): number;
53
+ /**
54
+ * The chain this hop makes, or a raise where it is one hop too many: a hop already taken
55
+ * `repeats` times is a cycle, and a chain past `depth` is one nothing meant to make. Both are
56
+ * permanent, so what hits one is set aside rather than tried again into the same loop.
57
+ *
58
+ * The chain is copied rather than appended to, which is what makes it a path down the call
59
+ * tree rather than a log of everything that happened — an operation calling one endpoint fifty
60
+ * times makes fifty chains of one hop, not one chain of fifty.
61
+ */
62
+ export declare function extend(inbound: unknown, hop: string, limits: Limits): string[];
63
+ /**
64
+ * How an event is named in a chain. The sigil is what tells it from an operation: the two are
65
+ * the same shape, a component may declare one of each under a single name, and `sync` is both
66
+ * the event every component inherits and an ordinary name for an operation.
67
+ *
68
+ * What an operator rewires to break a cycle is the subscription rather than the operation, so a
69
+ * chain that named only operations would not say how a component was re-entered.
70
+ */
71
+ export declare function event(destination: string): string;
72
+ /**
73
+ * What came off the wire, as a chain and nothing else. Neither the request contract nor a
74
+ * message validates this — a request is not validated at all once it is `authentic` — so a
75
+ * malformed one would otherwise become a `TypeError` where a named exception was contracted
76
+ * for. Bounding it is `extend`'s, one hop later, which is the only place the limits are known.
77
+ */
78
+ export declare function received(value: unknown): string[];
@@ -0,0 +1,112 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
+ import { environment } from '@toa.io/generic';
3
+ import { LoopException } from './exceptions.js';
4
+ /**
5
+ * What the invocation running now came by: the chain of hops it passed through and the rule that
6
+ * refuses one that has come back to where it had been, and the identity of the call it is
7
+ * serving, from which the identity of every call it makes is derived.
8
+ *
9
+ * Ambient, like the trace context and for the same reason: an algorithm's `context` is built
10
+ * once per operation at boot and shared by every invocation of it, so nothing per-invocation
11
+ * can be handed to it. It is not the trace context's own store, which `console.span` replaces
12
+ * on every span and does not enter at all when the trace is unsampled — a chain that
13
+ * disappears under sampling is a breaker that stops breaking in production.
14
+ *
15
+ * One writer, `Component.invoke`, where the hop is appended and the identity and the safety of
16
+ * what may be reached are put in scope; two readers, `Call.invoke`, which puts them on the request
17
+ * it is about to send, and `Outbox.row`, which writes the chain onto the row so it outlives the
18
+ * operation that caused it.
19
+ */
20
+ // as openspan holds its own: a process may carry two copies of this module, and a chain that
21
+ // is empty because the writer sat in the other one is a breaker that never fires. The name says
22
+ // what is stored, so a copy carrying the chain alone does not answer where the whole is expected
23
+ const KEY = Symbol.for('toa.core.invocation');
24
+ const storage = (globalThis[KEY] ??= new AsyncLocalStorage());
25
+ /**
26
+ * What the environment says, read where a component is built rather than once for the process,
27
+ * as the outbox reads its own — so a composition booted after a variable was set sees it.
28
+ *
29
+ * `TOA_TRAIL_REPEATS=0` refuses nothing: the chain is still stamped, carried and bounded. It is
30
+ * the off switch, and it exists because this refuses calls an application may be making today —
31
+ * a handshake written as `a > b > a > b > a` is three occurrences of one hop. A breaker with no
32
+ * way to open it is itself the outage.
33
+ */
34
+ export function limits() {
35
+ return {
36
+ repeats: number('TOA_TRAIL_REPEATS', 3),
37
+ depth: number('TOA_TRAIL_DEPTH', 32)
38
+ };
39
+ }
40
+ /** The invocation running now, where there is one. */
41
+ export function current() {
42
+ return storage.getStore();
43
+ }
44
+ /** Runs `task` as the invocation the chain now ends with. */
45
+ export async function follow(invocation, task) {
46
+ return storage.run(invocation, task);
47
+ }
48
+ /**
49
+ * The ordinal of the call `invocation` is about to make to `endpoint`, counting from zero, and
50
+ * counts it. Read once per call, on the caller's own path, which is where the order is the order
51
+ * the algorithm made them in.
52
+ */
53
+ export function ordinal(invocation, endpoint) {
54
+ const calls = (invocation.calls ??= new Map());
55
+ const made = calls.get(endpoint) ?? 0;
56
+ calls.set(endpoint, made + 1);
57
+ return made;
58
+ }
59
+ /**
60
+ * The chain this hop makes, or a raise where it is one hop too many: a hop already taken
61
+ * `repeats` times is a cycle, and a chain past `depth` is one nothing meant to make. Both are
62
+ * permanent, so what hits one is set aside rather than tried again into the same loop.
63
+ *
64
+ * The chain is copied rather than appended to, which is what makes it a path down the call
65
+ * tree rather than a log of everything that happened — an operation calling one endpoint fifty
66
+ * times makes fifty chains of one hop, not one chain of fifty.
67
+ */
68
+ export function extend(inbound, hop, limits) {
69
+ const hops = clip(received(inbound), limits.depth);
70
+ const trail = [...hops, hop];
71
+ if (limits.repeats === 0)
72
+ return trail;
73
+ if (trail.length > limits.depth)
74
+ throw new LoopException(`Call chain is ${trail.length} hops deep`, trail);
75
+ let seen = 0;
76
+ for (const passed of hops)
77
+ if (passed === hop)
78
+ seen++;
79
+ if (seen + 1 >= limits.repeats)
80
+ throw new LoopException(`'${hop}' is hop ${seen + 1} of this chain`, trail);
81
+ return trail;
82
+ }
83
+ /**
84
+ * How an event is named in a chain. The sigil is what tells it from an operation: the two are
85
+ * the same shape, a component may declare one of each under a single name, and `sync` is both
86
+ * the event every component inherits and an ordinary name for an operation.
87
+ *
88
+ * What an operator rewires to break a cycle is the subscription rather than the operation, so a
89
+ * chain that named only operations would not say how a component was re-entered.
90
+ */
91
+ export function event(destination) {
92
+ return '~' + destination;
93
+ }
94
+ /**
95
+ * What came off the wire, as a chain and nothing else. Neither the request contract nor a
96
+ * message validates this — a request is not validated at all once it is `authentic` — so a
97
+ * malformed one would otherwise become a `TypeError` where a named exception was contracted
98
+ * for. Bounding it is `extend`'s, one hop later, which is the only place the limits are known.
99
+ */
100
+ export function received(value) {
101
+ return Array.isArray(value) ? value.filter((hop) => typeof hop === 'string') : [];
102
+ }
103
+ // with the hop being appended that is one past the cap, which is enough to be refused; the
104
+ // rest is a message buying memory, and where the rule is off it is what bounds the chain
105
+ function clip(hops, depth) {
106
+ return hops.length > depth ? hops.slice(0, depth) : hops;
107
+ }
108
+ function number(variable, fallback) {
109
+ const declared = Number(environment.get(variable));
110
+ return Number.isNaN(declared) || declared < 0 ? fallback : declared;
111
+ }
112
+ //# sourceMappingURL=trail.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trail.js","sourceRoot":"","sources":["../source/trail.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAE/C;;;;;;;;;;;;;;;GAeG;AAEH,6FAA6F;AAC7F,gGAAgG;AAChG,iGAAiG;AACjG,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAA;AAI7C,MAAM,OAAO,GAAG,CAAE,UAAoB,CAAC,GAAG,CAAC,KAAK,IAAI,iBAAiB,EAAc,CAAC,CAAA;AAuCpF;;;;;;;;GAQG;AACH,MAAM,UAAU,MAAM;IACpB,OAAO;QACL,OAAO,EAAE,MAAM,CAAC,mBAAmB,EAAE,CAAC,CAAC;QACvC,KAAK,EAAE,MAAM,CAAC,iBAAiB,EAAE,EAAE,CAAC;KACrC,CAAA;AACH,CAAC;AAED,sDAAsD;AACtD,MAAM,UAAU,OAAO;IACrB,OAAO,OAAO,CAAC,QAAQ,EAAE,CAAA;AAC3B,CAAC;AAED,6DAA6D;AAC7D,MAAM,CAAC,KAAK,UAAU,MAAM,CAC1B,UAAsB,EACtB,IAAsB;IAEtB,OAAO,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,IAAI,C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CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;AACnF,CAAC;AAED,2FAA2F;AAC3F,yFAAyF;AACzF,SAAS,IAAI,CAAC,IAAc,EAAE,KAAa;IACzC,OAAO,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;AAC1D,CAAC;AAED,SAAS,MAAM,CAAC,QAAgB,EAAE,QAAgB;IAChD,MAAM,QAAQ,GAAG,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAA;IAElD,OAAO,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAA;AACrE,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { Operation } from './operation.ts';
2
+ import type { Contracts, Definition, Store } from './operation.ts';
3
+ import type { Cascade } from './cascade.ts';
4
+ import type { State } from './state.ts';
5
+ import type { Query as Translator } from './query.ts';
6
+ export declare class Transition extends Operation {
7
+ #private;
8
+ /** a transition is the only operation that commits */
9
+ protected mutable: boolean;
10
+ constructor(cascade: Cascade, scope: State, contract: Contracts, query: Translator, definition: Definition);
11
+ protected process(store: Store): Promise<any>;
12
+ protected acquire(store: Store): Promise<void>;
13
+ protected commit(store: Store): Promise<void>;
14
+ }
@@ -0,0 +1,51 @@
1
+ import { retry } from '@toa.io/generic';
2
+ import { Operation } from './operation.js';
3
+ import { StateConcurrencyException, StateNotFoundException } from './exceptions.js';
4
+ export class Transition extends Operation {
5
+ /** a transition is the only operation that commits */
6
+ mutable = true;
7
+ #concurrency;
8
+ // eslint-disable-next-line max-params
9
+ constructor(cascade, scope, contract, query, definition) {
10
+ super(cascade, scope, contract, query, definition);
11
+ this.#concurrency = definition.concurrency;
12
+ }
13
+ async process(store) {
14
+ return retry(async (retry) => this.#retry(store, retry), RETRY);
15
+ }
16
+ async acquire(store) {
17
+ const { request } = store;
18
+ store.scope =
19
+ request.query === undefined ? this.scope.init() : await this.query(request.query);
20
+ const entity = store.scope;
21
+ if (entity === null ||
22
+ (entity.deleted && request.query?.options?.deleted !== true))
23
+ throw new StateNotFoundException();
24
+ store.state = entity.get();
25
+ }
26
+ async commit(store) {
27
+ const { scope, state, reply, retry } = store;
28
+ if (reply.error !== undefined)
29
+ return;
30
+ const entity = scope;
31
+ entity.set(state);
32
+ const result = await this.scope.commit(entity, store.request.input, this.call(store));
33
+ if (result !== false)
34
+ return;
35
+ if (this.#concurrency === 'retry')
36
+ await retry?.();
37
+ else
38
+ throw new StateConcurrencyException();
39
+ }
40
+ async #retry(store, retry) {
41
+ store.retry = retry;
42
+ return super.process(store);
43
+ }
44
+ }
45
+ const RETRY = {
46
+ base: 10,
47
+ max: 5000,
48
+ dispersion: 1,
49
+ retries: 32
50
+ };
51
+ //# sourceMappingURL=transition.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transition.js","sourceRoot":"","sources":["../source/transition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,yBAAyB,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAA;AAOnF,MAAM,OAAO,UAAW,SAAQ,SAAS;IACvC,sDAAsD;IACnC,OAAO,GAAG,IAAI,CAAA;IAExB,YAAY,CAAoB;IAEzC,sCAAsC;IACtC,YACE,OAAgB,EAChB,KAAY,EACZ,QAAmB,EACnB,KA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AK,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;QAErF,IAAI,MAAM,KAAK,KAAK;YAAE,OAAM;QAE5B,IAAI,IAAI,CAAC,YAAY,KAAK,OAAO;YAAE,MAAM,KAAK,EAAE,EAAE,CAAA;;YAC7C,MAAM,IAAI,yBAAyB,EAAE,CAAA;IAC5C,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,KAAY,EAAE,KAAyB;QAClD,KAAK,CAAC,KAAK,GAAG,KAAK,CAAA;QAEnB,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC7B,CAAC;CACF;AAED,MAAM,KAAK,GAAG;IACZ,IAAI,EAAE,EAAE;IACR,GAAG,EAAE,IAAI;IACT,UAAU,EAAE,CAAC;IACb,OAAO,EAAE,EAAE;CACZ,CAAA"}
@@ -0,0 +1,8 @@
1
+ import { Connector } from './connector.ts';
2
+ import type { Consumer, Terms } from './types/bindings.ts';
3
+ import type { Request } from './types/request.ts';
4
+ export declare class Transmission extends Connector {
5
+ #private;
6
+ constructor(bindings: Consumer[]);
7
+ request(request: Request, terms?: Terms): Promise<any>;
8
+ }
@@ -0,0 +1,30 @@
1
+ import { Connector } from './connector.js';
2
+ import { TransmissionException } from './exceptions.js';
3
+ export class Transmission extends Connector {
4
+ #bindings;
5
+ constructor(bindings) {
6
+ super();
7
+ this.#bindings = bindings;
8
+ this.depends(bindings);
9
+ }
10
+ async request(request, terms) {
11
+ let reply = false;
12
+ let i = 0;
13
+ while (reply === false && i < this.#bindings.length) {
14
+ const binding = this.#bindings[i];
15
+ i++;
16
+ if (request?.task === true) {
17
+ if (binding.task === undefined)
18
+ continue;
19
+ await binding.task(request);
20
+ reply = null;
21
+ }
22
+ else
23
+ reply = await binding.request(request, terms);
24
+ }
25
+ if (reply === false)
26
+ throw new TransmissionException(`All (${this.#bindings.length}) bindings rejected.`);
27
+ return reply;
28
+ }
29
+ }
30
+ //# sourceMappingURL=transmission.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transmission.js","sourceRoot":"","sources":["../source/transmission.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAA;AAIvD,MAAM,OAAO,YAAa,SAAQ,SAAS;IAChC,SAAS,CAAY;IAE9B,YAAmB,QAAoB;QACrC,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAA;QACzB,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;IACxB,CAAC;IAEM,KAAK,CAAC,OAAO,CAAC,OAAgB,EAAE,KAAa;QAClD,IAAI,KAAK,GAAQ,KAAK,CAAA;QACtB,IAAI,CAAC,GAAG,CAAC,CAAA;QAET,OAAO,KAAK,KAAK,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;YACpD,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;YAEjC,CAAC,EAAE,CAAA;YAEH,IAAI,OAAO,EAAE,IAAI,KAAK,IAAI,EAAE,CAAC;gBAC3B,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS;oBAAE,SAAQ;gBAExC,MAAM,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;gBAC3B,KAAK,GAAG,IAAI,CAAA;YACd,CAAC;;gBAAM,KAAK,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;QACtD,CAAC;QAED,IAAI,KAAK,KAAK,KAAK;YACjB,MAAM,IAAI,qBAAqB,CAAC,QAAQ,IAAI,CAAC,SAAS,CAAC,MAAM,sBAAsB,CAAC,CAAA;QAEtF,OAAO,KAAK,CAAA;IACd,CAAC;CACF"}
@@ -0,0 +1,58 @@
1
+ import type { Connector } from '../connector.ts';
2
+ /** Which of the group this replica is, and how many of them there are. */
3
+ export interface Assignment {
4
+ i: number;
5
+ n: number;
6
+ }
7
+ /**
8
+ * What one group of replicas decides together, in one place. The decisions here are the ones
9
+ * processes cannot arrange by talking to each other: they need a single arbiter and a step
10
+ * indivisible from its point of view.
11
+ */
12
+ export interface Atom extends Connector {
13
+ /**
14
+ * An exclusive claim on slots of `0..total`: while this replica holds one, no other
15
+ * replica of the group does. Answered from memory, so it costs nothing to ask.
16
+ *
17
+ * `null` while this replica owns nothing — after a restart, during a rollout, or while
18
+ * coordination is unreachable. Whoever asks must be able to stand down: acting on a claim
19
+ * that cannot be supported is a different guarantee, not a degraded one.
20
+ */
21
+ slots(total: number): number[] | null;
22
+ /**
23
+ * Calls `listener` with the assignment this replica holds, and again whenever it changes —
24
+ * one arrived, was lost, or the group resized. Answers with what removes the listener
25
+ * again.
26
+ *
27
+ * A change and not a heartbeat: a group that stays as it is never calls back. It is called
28
+ * once as it is added, with the claim as it stands.
29
+ */
30
+ onassigned(listener: (assignment: Assignment | null) => void): () => void;
31
+ /**
32
+ * Debt the group has run up under each key, in milliseconds. Every call adds its own deltas
33
+ * and reads back where the group stands, so a replica reports what it alone has spent and
34
+ * still decides on what all of them have.
35
+ *
36
+ * Rejects where there is nothing to arbitrate through.
37
+ */
38
+ meter(keys: string[], deltas: number[]): Promise<number[]>;
39
+ /**
40
+ * Runs `routine` holding `keys`, and while it holds them no other replica of the group
41
+ * does. Waits for as long as it takes to acquire them.
42
+ *
43
+ * The lease is extended for as long as the routine runs. An extension that fails aborts the
44
+ * signal the routine is given, which is the only way it learns it no longer holds what it
45
+ * is working under.
46
+ *
47
+ * Rejects where there is nothing to arbitrate through.
48
+ */
49
+ lock<T>(keys: string | string[], routine: (signal: AbortSignal & {
50
+ error: Error;
51
+ }, context: unknown) => Promise<T>): Promise<T>;
52
+ }
53
+ export interface Factory {
54
+ /** @param group what the replicas deciding together have in common */
55
+ atom(group: string, options?: {
56
+ interval?: number;
57
+ }): Atom;
58
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=atomicity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"atomicity.js","sourceRoot":"","sources":["../../source/types/atomicity.ts"],"names":[],"mappings":""}