atom.io 0.23.5 → 0.24.1

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 (72) hide show
  1. package/data/dist/index.cjs +51 -27
  2. package/data/dist/index.d.ts +5 -4
  3. package/data/dist/index.js +625 -9
  4. package/data/src/join.ts +74 -46
  5. package/dist/chunk-BF4MVQF6.js +44 -0
  6. package/dist/index.cjs +27 -0
  7. package/dist/index.d.ts +63 -18
  8. package/dist/index.js +27 -3
  9. package/immortal/dist/index.cjs +4 -262
  10. package/immortal/dist/index.d.ts +4 -94
  11. package/immortal/dist/index.js +3 -215
  12. package/immortal/src/index.ts +0 -2
  13. package/immortal/src/seek-state.ts +10 -11
  14. package/internal/dist/index.cjs +279 -111
  15. package/internal/dist/index.d.ts +63 -93
  16. package/internal/dist/index.js +273 -88
  17. package/internal/src/families/dispose-from-store.ts +11 -7
  18. package/internal/src/families/find-in-store.ts +2 -2
  19. package/internal/src/families/init-family-member.ts +37 -25
  20. package/internal/src/families/seek-in-store.ts +11 -15
  21. package/internal/src/get-environment-data.ts +0 -4
  22. package/internal/src/index.ts +1 -0
  23. package/internal/src/ingest-updates/ingest-creation-disposal.ts +8 -8
  24. package/internal/src/molecule/create-molecule-family.ts +30 -0
  25. package/internal/src/molecule/dispose-molecule.ts +76 -0
  26. package/internal/src/molecule/grow-molecule-in-store.ts +89 -0
  27. package/internal/src/molecule/index.ts +5 -0
  28. package/internal/src/molecule/make-molecule-in-store.ts +122 -0
  29. package/internal/src/molecule/molecule-internal.ts +48 -0
  30. package/internal/src/not-found-error.ts +4 -3
  31. package/internal/src/set-state/emit-update.ts +1 -2
  32. package/internal/src/store/deposit.ts +16 -30
  33. package/internal/src/store/store.ts +7 -12
  34. package/internal/src/store/withdraw.ts +30 -19
  35. package/internal/src/timeline/create-timeline.ts +7 -6
  36. package/internal/src/transaction/build-transaction.ts +2 -1
  37. package/introspection/dist/index.cjs +29 -33
  38. package/introspection/dist/index.js +1 -1
  39. package/introspection/src/auditor.ts +6 -6
  40. package/json/dist/index.cjs +4 -4
  41. package/json/dist/index.js +7 -8
  42. package/json/src/select-json-family.ts +6 -5
  43. package/package.json +6 -6
  44. package/react-devtools/dist/index.js +2 -3
  45. package/realtime-client/dist/index.js +362 -15
  46. package/realtime-react/dist/index.cjs +1 -326
  47. package/realtime-react/dist/index.d.ts +1 -14
  48. package/realtime-react/dist/index.js +1 -1
  49. package/realtime-react/src/use-sync-continuity.ts +2 -2
  50. package/realtime-server/dist/index.cjs +3 -151
  51. package/realtime-server/dist/index.d.ts +1 -3
  52. package/realtime-server/dist/index.js +2 -18
  53. package/realtime-server/src/index.ts +0 -2
  54. package/realtime-server/src/realtime-continuity-synchronizer.ts +1 -1
  55. package/realtime-testing/dist/index.cjs +4 -82
  56. package/realtime-testing/dist/index.js +2 -4
  57. package/realtime-testing/src/setup-realtime-test.tsx +2 -3
  58. package/src/dispose-state.ts +2 -2
  59. package/src/index.ts +1 -0
  60. package/src/logger.ts +2 -0
  61. package/src/molecule.ts +133 -0
  62. package/src/timeline.ts +3 -3
  63. package/src/transaction.ts +15 -13
  64. package/dist/chunk-6MLFYN32.js +0 -18
  65. package/dist/chunk-7DT3PVS3.js +0 -598
  66. package/dist/chunk-GVHKIJ3G.js +0 -329
  67. package/dist/chunk-O47EQUM6.js +0 -29
  68. package/dist/chunk-OAYGID5B.js +0 -27
  69. package/dist/chunk-PNIHPILQ.js +0 -8
  70. package/immortal/src/make-molecule.ts +0 -234
  71. package/immortal/src/molecule.ts +0 -167
  72. package/realtime-server/src/realtime-state-synchronizer.ts +0 -23
@@ -1,329 +0,0 @@
1
- import { getFromStore, assignTransactionToContinuity, subscribeToTransaction, setIntoStore, disposeAtom, setEpochNumberOfContinuity, getEpochNumberOfContinuity, isRootStore, ingestTransactionUpdate, getJsonToken, actUponStore } from 'atom.io/internal';
2
- import { optimisticUpdateQueue, confirmedUpdateQueue } from 'atom.io/realtime-client';
3
-
4
- // realtime-client/src/sync-continuity.ts
5
- function syncContinuity(continuity, socket, store) {
6
- const continuityKey = continuity.key;
7
- const optimisticUpdates = getFromStore(optimisticUpdateQueue, store);
8
- const confirmedUpdates = getFromStore(confirmedUpdateQueue, store);
9
- const initializeContinuity = (epoch, payload) => {
10
- socket.off(`continuity-init:${continuityKey}`, initializeContinuity);
11
- let i = 0;
12
- let k = ``;
13
- let v = null;
14
- for (const x of payload) {
15
- if (i % 2 === 0) {
16
- k = x;
17
- } else {
18
- v = x;
19
- if (`type` in k && k.type === `mutable_atom`) {
20
- k = getJsonToken(k, store);
21
- }
22
- setIntoStore(k, v, store);
23
- }
24
- i++;
25
- }
26
- setEpochNumberOfContinuity(continuityKey, epoch, store);
27
- };
28
- socket.off(`continuity-init:${continuityKey}`);
29
- socket.on(`continuity-init:${continuityKey}`, initializeContinuity);
30
- const registerAndAttemptConfirmedUpdate = (confirmed) => {
31
- function reconcileEpoch(optimisticUpdate, confirmedUpdate) {
32
- store.logger.info(
33
- `\u{1F9D1}\u200D\u2696\uFE0F`,
34
- `continuity`,
35
- continuityKey,
36
- `reconciling updates`
37
- );
38
- setIntoStore(
39
- optimisticUpdateQueue,
40
- (queue) => {
41
- queue.shift();
42
- return queue;
43
- },
44
- store
45
- );
46
- if (optimisticUpdate.id === confirmedUpdate.id) {
47
- const clientResult = JSON.stringify(optimisticUpdate.updates);
48
- const serverResult = JSON.stringify(confirmedUpdate.updates);
49
- if (clientResult === serverResult) {
50
- store.logger.info(
51
- `\u2705`,
52
- `continuity`,
53
- continuityKey,
54
- `results for ${optimisticUpdate.id} match between client and server`
55
- );
56
- socket.emit(`ack:${continuityKey}`, confirmedUpdate.epoch);
57
- return;
58
- }
59
- } else {
60
- store.logger.info(
61
- `\u274C`,
62
- `continuity`,
63
- continuityKey,
64
- `thought update #${confirmedUpdate.epoch} was ${optimisticUpdate.key}:${optimisticUpdate.id}, but it was actually ${confirmedUpdate.key}:${confirmedUpdate.id}`
65
- );
66
- }
67
- store.logger.info(
68
- `\u{1F9D1}\u200D\u2696\uFE0F`,
69
- `continuity`,
70
- continuityKey,
71
- `updates do not match`,
72
- optimisticUpdate,
73
- confirmedUpdate
74
- );
75
- const reversedOptimisticUpdates = optimisticUpdates.toReversed();
76
- for (const subsequentOptimistic of reversedOptimisticUpdates) {
77
- ingestTransactionUpdate(`oldValue`, subsequentOptimistic, store);
78
- }
79
- store.logger.info(
80
- `\u23EA`,
81
- `continuity`,
82
- continuityKey,
83
- `undid optimistic updates:`,
84
- reversedOptimisticUpdates
85
- );
86
- ingestTransactionUpdate(`oldValue`, optimisticUpdate, store);
87
- store.logger.info(
88
- `\u23EA`,
89
- `continuity`,
90
- continuityKey,
91
- `undid zeroth optimistic update`,
92
- optimisticUpdate
93
- );
94
- ingestTransactionUpdate(`newValue`, confirmedUpdate, store);
95
- store.logger.info(
96
- `\u23E9`,
97
- `continuity`,
98
- continuityKey,
99
- `applied confirmed update`,
100
- confirmedUpdate
101
- );
102
- socket.emit(`ack:${continuityKey}`, confirmedUpdate.epoch);
103
- for (const subsequentOptimistic of optimisticUpdates) {
104
- const token = {
105
- type: `transaction`,
106
- key: subsequentOptimistic.key
107
- };
108
- const { id, params } = subsequentOptimistic;
109
- actUponStore(token, id, store)(...params);
110
- }
111
- store.logger.info(
112
- `\u23E9`,
113
- `continuity`,
114
- continuityKey,
115
- `reapplied subsequent optimistic updates:`,
116
- optimisticUpdates
117
- );
118
- }
119
- store.logger.info(
120
- `\u{1F9D1}\u200D\u2696\uFE0F`,
121
- `continuity`,
122
- continuityKey,
123
- `integrating confirmed update`,
124
- { confirmedUpdate: confirmed, confirmedUpdates, optimisticUpdates }
125
- );
126
- const zerothOptimisticUpdate = optimisticUpdates[0];
127
- if (zerothOptimisticUpdate) {
128
- store.logger.info(
129
- `\u{1F9D1}\u200D\u2696\uFE0F`,
130
- `continuity`,
131
- continuityKey,
132
- `has optimistic updates to reconcile`
133
- );
134
- if (confirmed.epoch === zerothOptimisticUpdate.epoch) {
135
- store.logger.info(
136
- `\u{1F9D1}\u200D\u2696\uFE0F`,
137
- `continuity`,
138
- continuityKey,
139
- `epoch of confirmed update #${confirmed.epoch} matches zeroth optimistic update`
140
- );
141
- reconcileEpoch(zerothOptimisticUpdate, confirmed);
142
- for (const nextConfirmed of confirmedUpdates) {
143
- const nextOptimistic = optimisticUpdates[0];
144
- if (nextConfirmed.epoch === (nextOptimistic == null ? void 0 : nextOptimistic.epoch)) {
145
- reconcileEpoch(nextOptimistic, nextConfirmed);
146
- } else {
147
- break;
148
- }
149
- }
150
- } else {
151
- store.logger.info(
152
- `\u{1F9D1}\u200D\u2696\uFE0F`,
153
- `continuity`,
154
- continuityKey,
155
- `epoch of confirmed update #${confirmed.epoch} does not match zeroth optimistic update #${zerothOptimisticUpdate.epoch}`
156
- );
157
- const confirmedUpdateIsAlreadyEnqueued = confirmedUpdates.some(
158
- (update) => update.epoch === confirmed.epoch
159
- );
160
- if (!confirmedUpdateIsAlreadyEnqueued) {
161
- store.logger.info(
162
- `\u{1F448}`,
163
- `continuity`,
164
- continuityKey,
165
- `pushing confirmed update to queue`,
166
- confirmed
167
- );
168
- setIntoStore(
169
- confirmedUpdateQueue,
170
- (queue) => {
171
- queue.push(confirmed);
172
- queue.sort((a, b) => a.epoch - b.epoch);
173
- return queue;
174
- },
175
- store
176
- );
177
- }
178
- }
179
- } else {
180
- store.logger.info(
181
- `\u{1F9D1}\u200D\u2696\uFE0F`,
182
- `continuity`,
183
- continuityKey,
184
- `has no optimistic updates to deal with`
185
- );
186
- const continuityEpoch = getEpochNumberOfContinuity(continuityKey, store);
187
- const isRoot = isRootStore(store);
188
- if (isRoot && continuityEpoch === confirmed.epoch - 1) {
189
- store.logger.info(
190
- `\u2705`,
191
- `continuity`,
192
- continuityKey,
193
- `integrating update #${confirmed.epoch} (${confirmed.key} ${confirmed.id})`
194
- );
195
- ingestTransactionUpdate(`newValue`, confirmed, store);
196
- socket.emit(`ack:${continuityKey}`, confirmed.epoch);
197
- setEpochNumberOfContinuity(continuityKey, confirmed.epoch, store);
198
- } else if (isRoot && continuityEpoch !== void 0) {
199
- store.logger.info(
200
- `\u{1F9D1}\u200D\u2696\uFE0F`,
201
- `continuity`,
202
- continuityKey,
203
- `received update #${confirmed.epoch} but still waiting for update #${continuityEpoch + 1}`,
204
- {
205
- clientEpoch: continuityEpoch,
206
- serverEpoch: confirmed.epoch
207
- }
208
- );
209
- const confirmedUpdateIsAlreadyEnqueued = confirmedUpdates.some(
210
- (update) => update.epoch === confirmed.epoch
211
- );
212
- if (confirmedUpdateIsAlreadyEnqueued) {
213
- store.logger.info(
214
- `\u{1F44D}`,
215
- `continuity`,
216
- continuityKey,
217
- `confirmed update #${confirmed.epoch} is already enqueued`
218
- );
219
- } else {
220
- store.logger.info(
221
- `\u{1F448}`,
222
- `continuity`,
223
- continuityKey,
224
- `pushing confirmed update #${confirmed.epoch} to queue`
225
- );
226
- setIntoStore(
227
- confirmedUpdateQueue,
228
- (queue) => {
229
- queue.push(confirmed);
230
- queue.sort((a, b) => a.epoch - b.epoch);
231
- return queue;
232
- },
233
- store
234
- );
235
- }
236
- }
237
- }
238
- };
239
- socket.off(`tx-new:${continuityKey}`);
240
- socket.on(`tx-new:${continuityKey}`, registerAndAttemptConfirmedUpdate);
241
- const unsubscribeFunctions = continuity.actions.map((transaction) => {
242
- assignTransactionToContinuity(continuityKey, transaction.key, store);
243
- const unsubscribeFromTransactionUpdates = subscribeToTransaction(
244
- transaction,
245
- (clientUpdate) => {
246
- store.logger.info(
247
- `\u{1F91E}`,
248
- `continuity`,
249
- continuityKey,
250
- `enqueuing optimistic update`
251
- );
252
- const optimisticUpdateIndex = optimisticUpdates.findIndex(
253
- (update) => update.id === clientUpdate.id
254
- );
255
- if (optimisticUpdateIndex === -1) {
256
- store.logger.info(
257
- `\u{1F91E}`,
258
- `continuity`,
259
- continuityKey,
260
- `enqueuing new optimistic update`
261
- );
262
- setIntoStore(
263
- optimisticUpdateQueue,
264
- (queue) => {
265
- queue.push(clientUpdate);
266
- queue.sort((a, b) => a.epoch - b.epoch);
267
- return queue;
268
- },
269
- store
270
- );
271
- } else {
272
- store.logger.info(
273
- `\u{1F91E}`,
274
- `continuity`,
275
- continuityKey,
276
- `replacing existing optimistic update at index ${optimisticUpdateIndex}`
277
- );
278
- setIntoStore(
279
- optimisticUpdateQueue,
280
- (queue) => {
281
- queue[optimisticUpdateIndex] = clientUpdate;
282
- return queue;
283
- },
284
- store
285
- );
286
- }
287
- socket.emit(`tx-run:${continuityKey}`, {
288
- id: clientUpdate.id,
289
- key: transaction.key,
290
- params: clientUpdate.params
291
- });
292
- },
293
- `tx-run:${continuityKey}`,
294
- store
295
- );
296
- return unsubscribeFromTransactionUpdates;
297
- });
298
- socket.on(`reveal:${continuityKey}`, (revealed) => {
299
- let i = 0;
300
- let k = ``;
301
- let v = null;
302
- for (const x of revealed) {
303
- if (i % 2 === 0) {
304
- k = x;
305
- } else {
306
- v = x;
307
- setIntoStore(k, v, store);
308
- }
309
- i++;
310
- }
311
- });
312
- socket.on(
313
- `conceal:${continuityKey}`,
314
- (concealed) => {
315
- for (const token of concealed) {
316
- disposeAtom(token, store);
317
- }
318
- }
319
- );
320
- socket.emit(`get:${continuityKey}`);
321
- return () => {
322
- socket.off(`continuity-init:${continuityKey}`);
323
- socket.off(`tx-new:${continuityKey}`);
324
- for (const unsubscribe of unsubscribeFunctions)
325
- unsubscribe();
326
- };
327
- }
328
-
329
- export { syncContinuity };
@@ -1,29 +0,0 @@
1
- import { lazyLocalStorageEffect } from './chunk-BWWVY5O5.js';
2
- import * as AtomIO from 'atom.io';
3
-
4
- var myIdState__INTERNAL = AtomIO.atom({
5
- key: `mySocketId__INTERNAL`,
6
- default: void 0
7
- });
8
- var myIdState = AtomIO.selector({
9
- key: `mySocketId`,
10
- get: ({ get }) => get(myIdState__INTERNAL)
11
- });
12
- var usernameEffects = typeof window === `undefined` ? [] : [lazyLocalStorageEffect(`myUsername`)];
13
- var myUsernameState = AtomIO.atom({
14
- key: `myUsername`,
15
- default: null,
16
- effects: usernameEffects
17
- });
18
- var optimisticUpdateQueue = AtomIO.atom({
19
- key: `updateQueue`,
20
- default: []
21
- });
22
- var confirmedUpdateQueue = AtomIO.atom(
23
- {
24
- key: `serverConfirmedUpdateQueue`,
25
- default: []
26
- }
27
- );
28
-
29
- export { confirmedUpdateQueue, myIdState, myIdState__INTERNAL, myUsernameState, optimisticUpdateQueue };
@@ -1,27 +0,0 @@
1
- // ../anvl/src/json/json-interface.ts
2
- var stringSetJsonInterface = {
3
- toJson: (stringSet) => Array.from(stringSet),
4
- fromJson: (json) => new Set(json)
5
- };
6
-
7
- // ../anvl/src/json/index.ts
8
- var parseJson = (str) => JSON.parse(str);
9
- var stringifyJson = (json) => JSON.stringify(json);
10
- var JSON_TYPE_NAMES = [
11
- `array`,
12
- `boolean`,
13
- `null`,
14
- `number`,
15
- `object`,
16
- `string`
17
- ];
18
- var JSON_DEFAULTS = {
19
- array: [],
20
- boolean: false,
21
- null: null,
22
- number: 0,
23
- object: {},
24
- string: ``
25
- };
26
-
27
- export { JSON_DEFAULTS, JSON_TYPE_NAMES, parseJson, stringSetJsonInterface, stringifyJson };
@@ -1,8 +0,0 @@
1
- import * as Internal from 'atom.io/internal';
2
-
3
- // src/get-state.ts
4
- function getState(token) {
5
- return Internal.getFromStore(token, Internal.IMPLICIT.STORE);
6
- }
7
-
8
- export { getState };
@@ -1,234 +0,0 @@
1
- import type { Flat, MoleculeCreation, MoleculeDisposal } from "atom.io"
2
- import * as Internal from "atom.io/internal"
3
- import { type Json, stringifyJson } from "atom.io/json"
4
-
5
- import { Molecule } from "./molecule"
6
-
7
- export type MoleculeConstructor<
8
- Key extends Json.Serializable,
9
- Struct extends { [key: string]: any },
10
- Params extends any[],
11
- > = new (
12
- context: Molecule<any>[],
13
- token: MoleculeToken<Key, Struct, Params>,
14
- ...params: Params
15
- ) => Molecule<Key> & Struct
16
-
17
- export type MoleculeFamilyToken<
18
- Key extends Json.Serializable,
19
- Struct extends { [key: string]: any },
20
- Params extends any[],
21
- > = {
22
- key: string
23
- type: `molecule_family`
24
- __K?: Key
25
- __S?: Struct
26
- __P?: Params
27
- }
28
- export type MoleculeFamily<
29
- Key extends Json.Serializable,
30
- Struct extends { [key: string]: any },
31
- Params extends any[],
32
- > = Flat<
33
- MoleculeFamilyToken<Key, Struct, Params> & {
34
- subject: Internal.Subject<MoleculeCreation<Key> | MoleculeDisposal<Key>>
35
- }
36
- > &
37
- MoleculeConstructor<Key, Struct, Params>
38
-
39
- export type MoleculeToken<
40
- Key extends Json.Serializable,
41
- Struct extends { [key: string]: any },
42
- Params extends any[],
43
- > = {
44
- key: Key
45
- type: `molecule`
46
- family?: MoleculeFamilyToken<Key, Struct, Params>
47
- __S?: Struct
48
- __P?: Params
49
- }
50
-
51
- export type MoleculeFamilyOptions<
52
- Key extends Json.Serializable,
53
- Struct extends { [key: string]: any },
54
- Params extends any[],
55
- > = {
56
- key: string
57
- new: (store: Internal.Store) => MoleculeConstructor<Key, Struct, Params>
58
- }
59
-
60
- export function createMoleculeFamily<
61
- Key extends Json.Serializable,
62
- Struct extends { [key: string]: any },
63
- Params extends any[],
64
- >(
65
- options: MoleculeFamilyOptions<Key, Struct, Params>,
66
- store: Internal.Store,
67
- ): MoleculeFamilyToken<Key, Struct, Params> {
68
- const subject = new Internal.Subject<
69
- MoleculeCreation<Key> | MoleculeDisposal<Key>
70
- >()
71
- const token = {
72
- key: options.key,
73
- type: `molecule_family`,
74
- } as const satisfies MoleculeFamilyToken<Key, Struct, Params>
75
- const family = Object.assign(options.new(store), { ...token, subject })
76
- store.moleculeFamilies.set(options.key, family)
77
- return token
78
- }
79
-
80
- export function moleculeFamily<
81
- Key extends Json.Serializable,
82
- Struct extends { [key: string]: any },
83
- Params extends any[],
84
- >(
85
- options: MoleculeFamilyOptions<Key, Struct, Params>,
86
- ): MoleculeFamilyToken<Key, Struct, Params> {
87
- return createMoleculeFamily(options, Internal.IMPLICIT.STORE)
88
- }
89
-
90
- export function makeMoleculeInStore<
91
- Key extends Json.Serializable,
92
- Struct extends { [key: string]: any },
93
- Params extends any[],
94
- >(
95
- store: Internal.Store,
96
- context: MoleculeToken<any, any, any> | MoleculeToken<any, any, any>[],
97
- family: MoleculeFamilyToken<Key, Struct, Params>,
98
- key: Key,
99
- ...params: Params
100
- ): MoleculeToken<Key, Struct, Params> {
101
- const target = Internal.newest(store)
102
-
103
- const token = {
104
- type: `molecule`,
105
- key,
106
- family,
107
- } as const satisfies MoleculeToken<Key, Struct, Params>
108
-
109
- const contextArray = Array.isArray(context) ? context : [context]
110
- const owners = contextArray.map<Molecule<any>>((ctx) => {
111
- if (ctx instanceof Molecule) {
112
- return ctx
113
- }
114
- const stringKey = stringifyJson(ctx.key)
115
- const molecule = store.molecules.get(stringKey)
116
-
117
- if (!molecule) {
118
- throw new Error(
119
- `Molecule ${stringKey} not found in store "${store.config.name}"`,
120
- )
121
- }
122
- return molecule
123
- })
124
-
125
- const Formula = Internal.withdraw(family, store)
126
- const molecule = new Formula(owners, token, ...params)
127
- target.molecules.set(stringifyJson(key), molecule)
128
-
129
- const update = {
130
- type: `molecule_creation`,
131
- token,
132
- family,
133
- context: contextArray,
134
- params,
135
- } satisfies MoleculeCreation<Key>
136
-
137
- const isTransaction =
138
- Internal.isChildStore(target) && target.transactionMeta.phase === `building`
139
- if (isTransaction) {
140
- target.transactionMeta.update.updates.push(update)
141
- } else {
142
- Formula.subject.next(update)
143
- }
144
-
145
- return token
146
- }
147
- export function makeMolecule<
148
- Key extends Json.Serializable,
149
- Struct extends { [key: string]: any },
150
- Params extends any[],
151
- >(
152
- context: MoleculeToken<any, any, any> | MoleculeToken<any, any, any>[],
153
- family: MoleculeFamilyToken<Key, Struct, Params>,
154
- key: Key,
155
- ...params: Params
156
- ): MoleculeToken<Key, Struct, Params> {
157
- return makeMoleculeInStore(
158
- Internal.IMPLICIT.STORE,
159
- context,
160
- family,
161
- key,
162
- ...params,
163
- )
164
- }
165
-
166
- export function useMoleculeFromStore<
167
- Key extends Json.Serializable,
168
- Struct extends { [key: string]: any },
169
- Params extends any[],
170
- >(
171
- token: MoleculeToken<Key, Struct, Params>,
172
- store: Internal.Store,
173
- ): (Molecule<Key> & Struct) | undefined {
174
- const molecule = store.molecules.get(stringifyJson(token.key))
175
- return molecule as Molecule<Key> & Struct
176
- }
177
- export function useMolecule<
178
- Key extends Json.Serializable,
179
- Struct extends { [key: string]: any },
180
- Params extends any[],
181
- >(
182
- token: MoleculeToken<Key, Struct, Params>,
183
- ): (Molecule<Key> & Struct) | undefined {
184
- return useMoleculeFromStore(token, Internal.IMPLICIT.STORE)
185
- }
186
-
187
- export function disposeMolecule<
188
- Key extends Json.Serializable,
189
- Struct extends { [key: string]: any },
190
- Params extends any[],
191
- >(token: MoleculeToken<Key, Struct, Params>, store: Internal.Store): void {
192
- const mole = useMoleculeFromStore(token, store)
193
- if (!mole || !token.family) {
194
- return // add error log
195
- }
196
- const { family } = token
197
- const Formula = Internal.withdraw(family, store)
198
- const disposalEvent: MoleculeDisposal<Key> = {
199
- type: `molecule_disposal`,
200
- token,
201
- family,
202
- context: mole.above,
203
- familyKeys: mole.tokens
204
- .map((t) => t.family?.key)
205
- .filter((k): k is string => typeof k === `string`),
206
- }
207
- if (token.family) {
208
- disposalEvent.family = token.family
209
- }
210
- const isTransaction =
211
- Internal.isChildStore(store) && store.transactionMeta.phase === `building`
212
- if (isTransaction) {
213
- store.transactionMeta.update.updates.push(disposalEvent)
214
- } else {
215
- Formula.subject.next(disposalEvent)
216
- }
217
-
218
- mole.dispose()
219
- }
220
-
221
- export type MoleculeType<M extends MoleculeFamilyToken<any, any, any>> =
222
- M extends MoleculeFamilyToken<any, infer T, any> ? T : never
223
-
224
- export function makeRootMolecule(
225
- key: Json.Serializable,
226
- store: Internal.Store = Internal.IMPLICIT.STORE,
227
- ): MoleculeToken<Json.Serializable, { [key: string]: unknown }, []> {
228
- const molecule = new Molecule(store, undefined, { key, type: `molecule` })
229
- store.molecules.set(stringifyJson(key), molecule)
230
- return {
231
- key,
232
- type: `molecule`,
233
- } as const
234
- }