atom.io 0.30.7 → 0.31.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 (92) hide show
  1. package/data/dist/index.d.ts +20 -18
  2. package/data/dist/index.js +104 -172
  3. package/data/src/join.ts +138 -210
  4. package/dist/{chunk-UDHCFTYT.js → chunk-Y5MBNTVU.js} +582 -762
  5. package/dist/index.d.ts +254 -173
  6. package/dist/index.js +91 -14
  7. package/ephemeral/dist/index.d.ts +35 -25
  8. package/ephemeral/src/find-state.ts +35 -25
  9. package/eslint-plugin/dist/index.d.ts +22 -3
  10. package/eslint-plugin/dist/index.js +7 -7
  11. package/immortal/dist/index.d.ts +1 -2
  12. package/immortal/dist/index.js +0 -3
  13. package/immortal/src/seek-state.ts +2 -14
  14. package/internal/dist/index.d.ts +57 -88
  15. package/internal/dist/index.js +1 -2
  16. package/internal/src/atom/dispose-atom.ts +31 -15
  17. package/internal/src/families/dispose-from-store.ts +15 -44
  18. package/internal/src/families/find-in-store.ts +9 -7
  19. package/internal/src/families/init-family-member.ts +1 -1
  20. package/internal/src/families/seek-in-store.ts +2 -14
  21. package/internal/src/get-state/get-from-store.ts +13 -79
  22. package/internal/src/get-trace.ts +7 -0
  23. package/internal/src/index.ts +17 -8
  24. package/internal/src/ingest-updates/ingest-creation-disposal.ts +65 -73
  25. package/internal/src/ingest-updates/ingest-transaction-update.ts +4 -0
  26. package/internal/src/install-into-store.ts +48 -0
  27. package/internal/src/junction.ts +52 -12
  28. package/internal/src/lineage.ts +0 -7
  29. package/internal/src/molecule.ts +306 -0
  30. package/internal/src/mutable/transceiver.ts +5 -5
  31. package/internal/src/not-found-error.ts +8 -30
  32. package/internal/src/pretty-print.ts +1 -16
  33. package/internal/src/selector/dispose-selector.ts +3 -5
  34. package/internal/src/selector/register-selector.ts +3 -34
  35. package/internal/src/set-state/set-into-store.ts +3 -2
  36. package/internal/src/store/counterfeit.ts +11 -25
  37. package/internal/src/store/deposit.ts +10 -42
  38. package/internal/src/store/store.ts +51 -12
  39. package/internal/src/store/withdraw.ts +18 -60
  40. package/internal/src/timeline/create-timeline.ts +133 -237
  41. package/internal/src/timeline/time-travel.ts +1 -8
  42. package/internal/src/transaction/build-transaction.ts +10 -5
  43. package/internal/src/transaction/index.ts +1 -1
  44. package/internal/src/utility-types.ts +2 -0
  45. package/introspection/dist/index.d.ts +2 -3
  46. package/introspection/dist/index.js +9 -9
  47. package/introspection/src/refinery.ts +1 -3
  48. package/json/dist/index.js +8 -40
  49. package/json/src/index.ts +2 -0
  50. package/json/src/select-json-family.ts +7 -44
  51. package/package.json +33 -28
  52. package/react/dist/index.js +2 -10
  53. package/react/src/parse-state-overloads.ts +3 -11
  54. package/react-devtools/dist/index.js +13 -13
  55. package/react-devtools/src/Updates.tsx +2 -0
  56. package/realtime-client/dist/index.d.ts +20 -12
  57. package/realtime-client/dist/index.js +241 -244
  58. package/realtime-client/src/continuity/index.ts +3 -0
  59. package/realtime-client/src/continuity/register-and-attempt-confirmed-update.ts +231 -0
  60. package/realtime-client/src/continuity/use-conceal-state.ts +11 -0
  61. package/realtime-client/src/continuity/use-reveal-state.ts +19 -0
  62. package/realtime-client/src/index.ts +1 -0
  63. package/realtime-client/src/sync-continuity.ts +18 -262
  64. package/realtime-react/dist/index.js +2 -2
  65. package/realtime-server/dist/index.d.ts +1 -1
  66. package/realtime-server/dist/index.js +2 -2
  67. package/realtime-server/src/index.ts +1 -1
  68. package/realtime-server/src/ipc-sockets/child-socket.ts +0 -1
  69. package/realtime-server/src/realtime-server-stores/server-room-external-actions.ts +1 -1
  70. package/realtime-testing/dist/index.js +5 -7
  71. package/realtime-testing/src/setup-realtime-test.tsx +5 -6
  72. package/src/atom.ts +53 -29
  73. package/src/dispose-state.ts +17 -23
  74. package/src/get-state.ts +19 -21
  75. package/src/index.ts +73 -3
  76. package/src/realm.ts +169 -0
  77. package/src/selector.ts +20 -0
  78. package/src/set-state.ts +16 -8
  79. package/src/silo.ts +9 -14
  80. package/src/timeline.ts +2 -3
  81. package/src/transaction.ts +25 -38
  82. package/transceivers/set-rtx/dist/index.js +4 -1
  83. package/transceivers/set-rtx/src/set-rtx.ts +4 -1
  84. package/dist/chunk-ADMEAXYU.js +0 -167
  85. package/internal/src/molecule/create-molecule-family.ts +0 -30
  86. package/internal/src/molecule/dispose-molecule.ts +0 -79
  87. package/internal/src/molecule/grow-molecule-in-store.ts +0 -95
  88. package/internal/src/molecule/index.ts +0 -5
  89. package/internal/src/molecule/make-molecule-in-store.ts +0 -191
  90. package/internal/src/molecule/molecule-internal.ts +0 -52
  91. package/src/allocate.ts +0 -277
  92. package/src/molecule.ts +0 -138
package/data/src/join.ts CHANGED
@@ -1,19 +1,18 @@
1
1
  import type {
2
- CtorToolkit,
3
- disposeState,
2
+ CompoundTypedKey,
4
3
  getState,
5
- MoleculeFamilyToken,
4
+ Hierarchy,
6
5
  MutableAtomFamilyToken,
7
6
  Read,
8
- ReadableFamilyToken,
9
- ReadableToken,
10
7
  ReadonlySelectorFamilyToken,
11
8
  ReadonlySelectorToken,
12
9
  RegularAtomFamilyToken,
13
10
  setState,
14
11
  SetterToolkit,
12
+ SingularTypedKey,
15
13
  Write,
16
14
  } from "atom.io"
15
+ import { Anarchy, Realm } from "atom.io"
17
16
  import type { findState } from "atom.io/ephemeral"
18
17
  import type { seekState } from "atom.io/immortal"
19
18
  import type {
@@ -22,31 +21,25 @@ import type {
22
21
  JunctionEntriesBase,
23
22
  JunctionSchemaBase,
24
23
  Molecule,
24
+ Refinement,
25
25
  Store,
26
26
  } from "atom.io/internal"
27
27
  import {
28
- createMoleculeFamily,
29
28
  createMutableAtomFamily,
30
29
  createReadonlySelectorFamily,
31
30
  createRegularAtomFamily,
32
- disposeFromStore,
31
+ findInStore,
33
32
  getFromStore,
34
33
  getJsonFamily,
35
34
  getJsonToken,
36
- growMoleculeInStore,
37
35
  IMPLICIT,
38
- initFamilyMemberInStore,
39
36
  isChildStore,
40
37
  Junction,
41
- makeMoleculeInStore,
42
38
  newest,
43
- NotFoundError,
44
39
  seekInStore,
45
40
  setIntoStore,
46
- withdraw,
47
41
  } from "atom.io/internal"
48
- import type { Refinement } from "atom.io/introspection"
49
- import type { Json } from "atom.io/json"
42
+ import type { Canonical, Json, stringified } from "atom.io/json"
50
43
  import { stringifyJson } from "atom.io/json"
51
44
  import type { SetRTXJson } from "atom.io/transceivers/set-rtx"
52
45
  import { SetRTX } from "atom.io/transceivers/set-rtx"
@@ -151,6 +144,23 @@ export type JoinStateFamilies<
151
144
  }
152
145
  : never
153
146
 
147
+ export type JoinHierarchy<
148
+ AType extends SingularTypedKey,
149
+ BType extends SingularTypedKey,
150
+ > = Hierarchy<
151
+ [
152
+ {
153
+ above: `root`
154
+ below: [AType, BType]
155
+ },
156
+ {
157
+ above: [AType, BType]
158
+ style: `all`
159
+ below: CompoundTypedKey<`content`, AType, BType>
160
+ },
161
+ ]
162
+ >
163
+
154
164
  export class Join<
155
165
  const ASide extends string,
156
166
  const AType extends string,
@@ -158,11 +168,15 @@ export class Join<
158
168
  const BType extends string,
159
169
  const Cardinality extends `1:1` | `1:n` | `n:n`,
160
170
  const Content extends Json.Object | null = null,
171
+ const ContentKey extends CompoundTypedKey<
172
+ `content`,
173
+ ASide,
174
+ BSide
175
+ > = CompoundTypedKey<`content`, ASide, BSide>,
161
176
  > {
162
- private options: JoinOptions<ASide, AType, BSide, BType, Cardinality, Content>
163
- private defaultContent: Content | undefined
164
- private toolkit: SetterToolkit & { dispose: typeof disposeState }
165
- public retrieve: typeof findState
177
+ private toolkit: SetterToolkit
178
+ public options: JoinOptions<ASide, AType, BSide, BType, Cardinality, Content>
179
+ public defaultContent: Content | undefined
166
180
  public molecules: Map<string, Molecule<any>> = new Map()
167
181
  public relations: Junction<ASide, AType, BSide, BType, Content>
168
182
  public states: JoinStateFamilies<
@@ -174,14 +188,14 @@ export class Join<
174
188
  Content
175
189
  >
176
190
  public core: {
177
- findRelatedKeysState: MutableAtomFamilyToken<
191
+ relatedKeysAtoms: MutableAtomFamilyToken<
178
192
  SetRTX<string>,
179
193
  SetRTXJson<string>,
180
194
  string
181
195
  >
182
196
  }
183
197
  public transact(
184
- toolkit: SetterToolkit & { dispose: typeof disposeState },
198
+ toolkit: SetterToolkit,
185
199
  run: (join: Join<ASide, AType, BSide, BType, Cardinality, Content>) => void,
186
200
  ): void {
187
201
  const originalToolkit = this.toolkit
@@ -191,27 +205,9 @@ export class Join<
191
205
  }
192
206
 
193
207
  public store: Store
194
- public alternates: Map<
195
- string,
196
- Join<ASide, AType, BSide, BType, Cardinality, Content>
197
- >
198
- public [Symbol.dispose](): void {
199
- this.alternates.delete(this.store.config.name)
200
- }
208
+ public realm: Anarchy
201
209
 
202
- public in(
203
- store: Store,
204
- ): Join<ASide, AType, BSide, BType, Cardinality, Content> {
205
- const key = store.config.name
206
- const alternate = this.alternates.get(key)
207
- if (alternate) {
208
- return alternate
209
- }
210
- const join = new Join(this.options, this.defaultContent, store)
211
- this.alternates.set(key, join)
212
- join.alternates = this.alternates
213
- return join
214
- }
210
+ public [Symbol.dispose](): void {}
215
211
 
216
212
  public constructor(
217
213
  options: JoinOptions<ASide, AType, BSide, BType, Cardinality, Content>,
@@ -221,44 +217,25 @@ export class Join<
221
217
  type AnyKey = AType & BType
222
218
 
223
219
  this.store = store
220
+ this.realm = new Anarchy(store)
224
221
  this.options = options
225
222
  this.defaultContent = defaultContent
226
- this.alternates = new Map()
227
- this.alternates.set(store.config.name, this)
228
223
 
229
224
  this.store.miscResources.set(`join:${options.key}`, this)
230
225
 
231
- this.retrieve = ((
232
- token: ReadableFamilyToken<any, any>,
233
- key: Json.Serializable,
234
- ): ReadableToken<any> => {
235
- const maybeToken = this.toolkit.seek(token, key)
236
- if (maybeToken) {
237
- return maybeToken
238
- }
239
- const molecule = this.store.molecules.get(stringifyJson(key))
240
- if (molecule) {
241
- const family = withdraw(token, store)
242
- return growMoleculeInStore(molecule, family, store)
243
- }
244
- if (store.config.lifespan === `immortal`) {
245
- throw new NotFoundError(token, key, store)
246
- }
247
- return initFamilyMemberInStore(store, token, key)
248
- }) as typeof findState
226
+ this.realm.allocate(`root`, options.key)
227
+
249
228
  this.toolkit = {
250
229
  get: ((...ps: Parameters<typeof getState>) =>
251
230
  getFromStore(store, ...ps)) as typeof getState,
252
231
  set: ((...ps: Parameters<typeof setState>) => {
253
232
  setIntoStore(store, ...ps)
254
233
  }) as typeof setState,
255
- find: this.retrieve,
234
+ find: ((...ps: Parameters<typeof findState>) =>
235
+ findInStore(store, ...ps)) as typeof findState,
256
236
  seek: ((...ps: Parameters<typeof seekState>) =>
257
237
  seekInStore(store, ...ps)) as typeof seekState,
258
238
  json: (token) => getJsonToken(store, token),
259
- dispose: ((...ps: Parameters<typeof disposeState>) => {
260
- disposeFromStore(store, ...ps)
261
- }) as typeof disposeState,
262
239
  }
263
240
 
264
241
  const aSide: ASide = options.between[0]
@@ -278,62 +255,47 @@ export class Join<
278
255
  },
279
256
  [`join`, `relations`],
280
257
  )
281
- this.core = { findRelatedKeysState: relatedKeysAtoms }
258
+ this.core = { relatedKeysAtoms }
282
259
  const getRelatedKeys: Read<
283
260
  (key: string) => SetRTX<AType> | SetRTX<BType>
284
- > = ({ get }, key) => get(this.retrieve(relatedKeysAtoms, key) as any)
261
+ > = ({ get }, key) => get(relatedKeysAtoms, key) as any
285
262
  const addRelation: Write<(a: string, b: string) => void> = (
286
- toolkit,
263
+ { set },
287
264
  a,
288
265
  b,
289
266
  ) => {
290
- const { set } = toolkit
291
- const aKeysState = this.retrieve(relatedKeysAtoms, a)
292
- const bKeysState = this.retrieve(relatedKeysAtoms, b)
293
- set(aKeysState, (aKeys) => aKeys.add(b))
294
- set(bKeysState, (bKeys) => bKeys.add(a))
267
+ if (!this.store.molecules.has(stringifyJson(a))) {
268
+ this.realm.allocate(options.key, a)
269
+ }
270
+ set(relatedKeysAtoms, a, (aKeys) => aKeys.add(b))
271
+ set(relatedKeysAtoms, b, (bKeys) => bKeys.add(a))
295
272
  }
296
273
  const deleteRelation: Write<(a: string, b: string) => void> = (
297
- toolkit,
274
+ { set },
298
275
  a,
299
276
  b,
300
277
  ) => {
301
- const { set } = toolkit
302
- const aKeysState = this.retrieve(relatedKeysAtoms, a)
303
- const bKeysState = this.retrieve(relatedKeysAtoms, b)
304
- let stringA: string | undefined
305
- let stringB: string | undefined
306
- set(aKeysState, (aKeys) => {
278
+ set(relatedKeysAtoms, a, (aKeys) => {
307
279
  aKeys.delete(b)
308
- if (aKeys.size === 0) {
309
- stringA = `"${a}"`
310
- }
311
280
  return aKeys
312
281
  })
313
- set(bKeysState, (bKeys) => {
282
+ set(relatedKeysAtoms, b, (bKeys) => {
314
283
  bKeys.delete(a)
315
- if (bKeys.size === 0) {
316
- stringB = `"${b}"`
317
- }
318
284
  return bKeys
319
285
  })
320
286
  }
321
287
  const replaceRelationsSafely: Write<
322
288
  (a: string, newRelationsOfA: string[]) => void
323
289
  > = (toolkit, a, newRelationsOfA) => {
324
- const { get, set } = toolkit
325
- const relationsOfAState = this.retrieve(relatedKeysAtoms, a)
290
+ const { find, get, set } = toolkit
291
+ const relationsOfAState = find(relatedKeysAtoms, a)
326
292
  const currentRelationsOfA = get(relationsOfAState)
327
293
  for (const currentRelationB of currentRelationsOfA) {
328
294
  const remainsRelated = newRelationsOfA.includes(currentRelationB)
329
295
  if (remainsRelated) {
330
296
  continue
331
297
  }
332
- const relationsOfBState = this.retrieve(
333
- relatedKeysAtoms,
334
- currentRelationB,
335
- )
336
- set(relationsOfBState, (relationsOfB) => {
298
+ set(relatedKeysAtoms, currentRelationB, (relationsOfB) => {
337
299
  relationsOfB.delete(a)
338
300
  return relationsOfB
339
301
  })
@@ -382,8 +344,7 @@ export class Join<
382
344
  (a: string, newRelationsOfA: string[]) => void
383
345
  > = (toolkit, a, newRelationsOfA) => {
384
346
  const { set } = toolkit
385
- const relationsOfAState = this.retrieve(relatedKeysAtoms, a)
386
- set(relationsOfAState, (relationsOfA) => {
347
+ set(relatedKeysAtoms, a, (relationsOfA) => {
387
348
  relationsOfA.transaction((nextRelationsOfA) => {
388
349
  for (const newRelationB of newRelationsOfA) {
389
350
  nextRelationsOfA.add(newRelationB)
@@ -393,8 +354,7 @@ export class Join<
393
354
  return relationsOfA
394
355
  })
395
356
  for (const newRelationB of newRelationsOfA) {
396
- const newRelationsBState = this.retrieve(relatedKeysAtoms, newRelationB)
397
- set(newRelationsBState, (newRelationsB) => {
357
+ set(relatedKeysAtoms, newRelationB, (newRelationsB) => {
398
358
  newRelationsB.add(a)
399
359
  return newRelationsB
400
360
  })
@@ -408,6 +368,14 @@ export class Join<
408
368
  const baseExternalStoreConfiguration: BaseExternalStoreConfiguration = {
409
369
  getRelatedKeys: (key) => getRelatedKeys(this.toolkit, key),
410
370
  addRelation: (a, b) => {
371
+ this.store.moleculeJoins.set(
372
+ a as stringified<Canonical> /* 💥 RECONCILE */,
373
+ options.key,
374
+ )
375
+ this.store.moleculeJoins.set(
376
+ b as stringified<Canonical> /* 💥 RECONCILE */,
377
+ options.key,
378
+ )
411
379
  addRelation(this.toolkit, a, b)
412
380
  },
413
381
  deleteRelation: (a, b) => {
@@ -423,13 +391,9 @@ export class Join<
423
391
  }
424
392
  let externalStore: ExternalStoreConfiguration<Content>
425
393
  let contentAtoms: RegularAtomFamilyToken<Content, string>
426
- let contentMolecules: MoleculeFamilyToken<
427
- new (
428
- ..._: any[]
429
- ) => { key: string }
430
- >
394
+
431
395
  if (defaultContent) {
432
- contentAtoms = createRegularAtomFamily<Content, string>(
396
+ contentAtoms = createRegularAtomFamily<Content, ContentKey>(
433
397
  store,
434
398
  {
435
399
  key: `${options.key}/content`,
@@ -437,44 +401,27 @@ export class Join<
437
401
  },
438
402
  [`join`, `content`],
439
403
  )
440
- contentMolecules = createMoleculeFamily(store, {
441
- key: `${options.key}/content-molecules`,
442
- new: class ContentMolecule {
443
- public constructor(
444
- _: CtorToolkit<string>,
445
- public key: string,
446
- ) {}
447
- },
448
- })
404
+
449
405
  const getContent: Read<(key: string) => Content | null> = ({ get }, key) =>
450
- get(this.retrieve(contentAtoms, key))
406
+ get(contentAtoms, key)
451
407
  const setContent: Write<(key: string, content: Content) => void> = (
452
408
  { set },
453
409
  key,
454
410
  content,
455
411
  ) => {
456
- set(this.retrieve(contentAtoms, key), content)
457
- }
458
- const deleteContent: Write<(compositeKey: string) => void> = (
459
- _,
460
- compositeKey,
461
- ) => {
462
- const contentMolecule = store.molecules.get(`"${compositeKey}"`)
463
- if (contentMolecule) {
464
- this.toolkit.dispose(contentMolecule)
465
- this.molecules.delete(`"${compositeKey}"`)
466
- }
412
+ set(contentAtoms, key, content)
467
413
  }
414
+
468
415
  const externalStoreWithContentConfiguration = {
469
- getContent: (contentKey: string) => {
416
+ getContent: (contentKey: ContentKey) => {
470
417
  const content = getContent(this.toolkit, contentKey)
471
418
  return content
472
419
  },
473
- setContent: (contentKey: string, content: Content) => {
420
+ setContent: (contentKey: ContentKey, content: Content) => {
474
421
  setContent(this.toolkit, contentKey, content)
475
422
  },
476
- deleteContent: (contentKey: string) => {
477
- deleteContent(this.toolkit, contentKey)
423
+ deleteContent: (contentKey: ContentKey) => {
424
+ this.realm.deallocate(contentKey)
478
425
  },
479
426
  }
480
427
  externalStore = Object.assign(
@@ -492,30 +439,26 @@ export class Join<
492
439
  isAType: options.isAType,
493
440
  isBType: options.isBType,
494
441
  makeContentKey: (...args) => {
442
+ const [a, b] = args
495
443
  const sorted = args.sort()
496
444
  const compositeKey = `${sorted[0]}:${sorted[1]}`
497
- const [m0, m1] = sorted.map((key) =>
498
- this.molecules.get(stringifyJson(key)),
499
- )
500
- if (store.config.lifespan === `immortal` && m0 && m1) {
501
- const target = newest(store)
502
- const moleculeToken = makeMoleculeInStore(
503
- target,
504
- [m0, m1],
505
- contentMolecules,
506
- compositeKey,
507
- )
508
- this.molecules.set(
509
- `"${compositeKey}"`,
510
- withdraw(moleculeToken, target),
511
- )
445
+ const aMolecule = store.molecules.get(stringifyJson(a))
446
+ const bMolecule = store.molecules.get(stringifyJson(b))
447
+ if (!aMolecule) {
448
+ this.realm.allocate(options.key, a)
512
449
  }
450
+ if (!bMolecule) {
451
+ this.realm.allocate(options.key, b)
452
+ }
453
+ this.realm.allocate(a, compositeKey, `all`)
454
+ this.realm.claim(b, compositeKey)
455
+ this.store.moleculeJoins.set(compositeKey, options.key)
513
456
  return compositeKey
514
457
  },
515
458
  },
516
459
  )
517
460
 
518
- const createSingleKeyStateFamily = () =>
461
+ const createSingleKeySelectorFamily = () =>
519
462
  createReadonlySelectorFamily<string | null, string>(
520
463
  store,
521
464
  {
@@ -523,8 +466,7 @@ export class Join<
523
466
  get:
524
467
  (key) =>
525
468
  ({ get }) => {
526
- const relatedKeysState = this.retrieve(relatedKeysAtoms, key)
527
- const relatedKeys = get(relatedKeysState)
469
+ const relatedKeys = get(relatedKeysAtoms, key)
528
470
  for (const relatedKey of relatedKeys) {
529
471
  return relatedKey
530
472
  }
@@ -533,7 +475,7 @@ export class Join<
533
475
  },
534
476
  [`join`, `keys`],
535
477
  )
536
- const getMultipleKeyStateFamily = () => {
478
+ const getMultipleKeySelectorFamily = () => {
537
479
  return createReadonlySelectorFamily<string[], string>(
538
480
  store,
539
481
  {
@@ -542,15 +484,14 @@ export class Join<
542
484
  (key) =>
543
485
  ({ get }) => {
544
486
  const jsonFamily = getJsonFamily(relatedKeysAtoms, store)
545
- const jsonState = this.retrieve(jsonFamily, key)
546
- const json = get(jsonState)
487
+ const json = get(jsonFamily, key)
547
488
  return json.members
548
489
  },
549
490
  },
550
491
  [`join`, `keys`],
551
492
  )
552
493
  }
553
- const createSingleEntryStateFamily = () =>
494
+ const createSingleEntrySelectorFamily = () =>
554
495
  createReadonlySelectorFamily<[string, Content] | null, string>(
555
496
  store,
556
497
  {
@@ -558,16 +499,14 @@ export class Join<
558
499
  get:
559
500
  (x) =>
560
501
  ({ get }) => {
561
- const relatedKeysState = this.retrieve(relatedKeysAtoms, x)
562
- const relatedKeys = get(relatedKeysState)
502
+ const relatedKeys = get(relatedKeysAtoms, x)
563
503
  for (const y of relatedKeys) {
564
504
  let a = relations.isAType?.(x) ? x : undefined
565
505
  let b = a === undefined ? (x as BType) : undefined
566
506
  a ??= y as AType
567
507
  b ??= y as BType
568
508
  const contentKey = relations.makeContentKey(a, b)
569
- const contentState = this.retrieve(contentAtoms, contentKey)
570
- const content = get(contentState)
509
+ const content = get(contentAtoms, contentKey)
571
510
  return [y, content]
572
511
  }
573
512
  return null
@@ -575,7 +514,7 @@ export class Join<
575
514
  },
576
515
  [`join`, `entries`],
577
516
  )
578
- const getMultipleEntryStateFamily = () =>
517
+ const getMultipleEntrySelectorFamily = () =>
579
518
  createReadonlySelectorFamily<[string, Content][], string>(
580
519
  store,
581
520
  {
@@ -584,16 +523,14 @@ export class Join<
584
523
  (x) =>
585
524
  ({ get }) => {
586
525
  const jsonFamily = getJsonFamily(relatedKeysAtoms, store)
587
- const jsonState = this.retrieve(jsonFamily, x)
588
- const json = get(jsonState)
526
+ const json = get(jsonFamily, x)
589
527
  return json.members.map((y) => {
590
528
  let a = relations.isAType?.(x) ? x : undefined
591
529
  let b = a === undefined ? (x as BType) : undefined
592
530
  a ??= y as AType
593
531
  b ??= y as BType
594
532
  const contentKey = relations.makeContentKey(a, b)
595
- const contentState = this.retrieve(contentAtoms, contentKey)
596
- const content = get(contentState)
533
+ const content = get(contentAtoms, contentKey)
597
534
  return [y, content]
598
535
  })
599
536
  },
@@ -603,12 +540,12 @@ export class Join<
603
540
 
604
541
  switch (options.cardinality) {
605
542
  case `1:1`: {
606
- const findSingleRelatedKeyState = createSingleKeyStateFamily()
543
+ const singleRelatedKeySelectors = createSingleKeySelectorFamily()
607
544
  const stateKeyA = `${aSide}KeyOf${capitalize(bSide)}` as const
608
545
  const stateKeyB = `${bSide}KeyOf${capitalize(aSide)}` as const
609
546
  const baseStates = {
610
- [stateKeyA]: findSingleRelatedKeyState,
611
- [stateKeyB]: findSingleRelatedKeyState,
547
+ [stateKeyA]: singleRelatedKeySelectors,
548
+ [stateKeyB]: singleRelatedKeySelectors,
612
549
  } as JoinStateFamilies<ASide, AType, BSide, BType, Cardinality, Content>
613
550
  let states: JoinStateFamilies<
614
551
  ASide,
@@ -619,12 +556,12 @@ export class Join<
619
556
  Content
620
557
  >
621
558
  if (defaultContent) {
622
- const findSingleRelatedEntryState = createSingleEntryStateFamily()
559
+ const singleEntrySelectors = createSingleEntrySelectorFamily()
623
560
  const entriesStateKeyA = `${aSide}EntryOf${capitalize(bSide)}` as const
624
561
  const entriesStateKeyB = `${bSide}EntryOf${capitalize(aSide)}` as const
625
562
  const contentStates = {
626
- [entriesStateKeyA]: findSingleRelatedEntryState,
627
- [entriesStateKeyB]: findSingleRelatedEntryState,
563
+ [entriesStateKeyA]: singleEntrySelectors,
564
+ [entriesStateKeyB]: singleEntrySelectors,
628
565
  }
629
566
  states = Object.assign(baseStates, contentStates)
630
567
  } else {
@@ -635,13 +572,13 @@ export class Join<
635
572
  break
636
573
  }
637
574
  case `1:n`: {
638
- const findSingleRelatedKeyState = createSingleKeyStateFamily()
639
- const findMultipleRelatedKeysState = getMultipleKeyStateFamily()
575
+ const singleRelatedKeySelectors = createSingleKeySelectorFamily()
576
+ const multipleRelatedKeysSelectors = getMultipleKeySelectorFamily()
640
577
  const stateKeyA = `${aSide}KeyOf${capitalize(bSide)}` as const
641
578
  const stateKeyB = `${bSide}KeysOf${capitalize(aSide)}` as const
642
579
  const baseStates = {
643
- [stateKeyA]: findSingleRelatedKeyState,
644
- [stateKeyB]: findMultipleRelatedKeysState,
580
+ [stateKeyA]: singleRelatedKeySelectors,
581
+ [stateKeyB]: multipleRelatedKeysSelectors,
645
582
  } as JoinStateFamilies<ASide, AType, BSide, BType, Cardinality, Content>
646
583
  let states: JoinStateFamilies<
647
584
  ASide,
@@ -652,15 +589,16 @@ export class Join<
652
589
  Content
653
590
  >
654
591
  if (defaultContent) {
655
- const findSingleRelatedEntryState = createSingleEntryStateFamily()
656
- const findMultipleRelatedEntriesState = getMultipleEntryStateFamily()
592
+ const singleRelatedEntrySelectors = createSingleEntrySelectorFamily()
593
+ const multipleRelatedEntriesSelectors =
594
+ getMultipleEntrySelectorFamily()
657
595
  const entriesStateKeyA = `${aSide}EntryOf${capitalize(bSide)}` as const
658
596
  const entriesStateKeyB = `${bSide}EntriesOf${capitalize(
659
597
  aSide,
660
598
  )}` as const
661
599
  const contentStates = {
662
- [entriesStateKeyA]: findSingleRelatedEntryState,
663
- [entriesStateKeyB]: findMultipleRelatedEntriesState,
600
+ [entriesStateKeyA]: singleRelatedEntrySelectors,
601
+ [entriesStateKeyB]: multipleRelatedEntriesSelectors,
664
602
  }
665
603
  states = Object.assign(baseStates, contentStates)
666
604
  } else {
@@ -671,12 +609,12 @@ export class Join<
671
609
  break
672
610
  }
673
611
  default: {
674
- const findMultipleRelatedKeysState = getMultipleKeyStateFamily()
612
+ const multipleRelatedKeysSelectors = getMultipleKeySelectorFamily()
675
613
  const stateKeyA = `${aSide}KeysOf${capitalize(bSide)}` as const
676
614
  const stateKeyB = `${bSide}KeysOf${capitalize(aSide)}` as const
677
615
  const baseStates = {
678
- [stateKeyA]: findMultipleRelatedKeysState,
679
- [stateKeyB]: findMultipleRelatedKeysState,
616
+ [stateKeyA]: multipleRelatedKeysSelectors,
617
+ [stateKeyB]: multipleRelatedKeysSelectors,
680
618
  } as JoinStateFamilies<ASide, AType, BSide, BType, Cardinality, Content>
681
619
  let states: JoinStateFamilies<
682
620
  ASide,
@@ -687,7 +625,8 @@ export class Join<
687
625
  Content
688
626
  >
689
627
  if (defaultContent) {
690
- const findMultipleRelatedEntriesState = getMultipleEntryStateFamily()
628
+ const multipleRelatedEntriesSelectors =
629
+ getMultipleEntrySelectorFamily()
691
630
  const entriesStateKeyA = `${aSide}EntriesOf${capitalize(
692
631
  bSide,
693
632
  )}` as const
@@ -695,8 +634,8 @@ export class Join<
695
634
  aSide,
696
635
  )}` as const
697
636
  const contentStates = {
698
- [entriesStateKeyA]: findMultipleRelatedEntriesState,
699
- [entriesStateKeyB]: findMultipleRelatedEntriesState,
637
+ [entriesStateKeyA]: multipleRelatedEntriesSelectors,
638
+ [entriesStateKeyB]: multipleRelatedEntriesSelectors,
700
639
  }
701
640
  states = Object.assign(baseStates, contentStates)
702
641
  } else {
@@ -762,8 +701,7 @@ export function join<
762
701
  defaultContent: Content | undefined,
763
702
  store: Store = IMPLICIT.STORE,
764
703
  ): JoinToken<ASide, AType, BSide, BType, Cardinality, Content> {
765
- const joins = getJoinMap(store)
766
- joins.set(options.key, new Join(options, defaultContent, store))
704
+ store.joins.set(options.key, new Join(options, defaultContent, store))
767
705
  const token: JoinToken<ASide, AType, BSide, BType, Cardinality, Content> = {
768
706
  key: options.key,
769
707
  type: `join`,
@@ -774,16 +712,6 @@ export function join<
774
712
  return token
775
713
  }
776
714
 
777
- export function getJoinMap(
778
- store: Store & { joins?: Map<string, Join<any, any, any, any, any, any>> },
779
- ): Map<string, Join<any, any, any, any, any, any>> {
780
- if (`joins` in store && store.joins instanceof Map) {
781
- return store.joins
782
- }
783
- const joins = new Map<string, Join<any, any, any, any, any, any>>()
784
- store.joins = joins
785
- return joins
786
- }
787
715
  export function getJoin<
788
716
  ASide extends string,
789
717
  AType extends string,
@@ -795,17 +723,17 @@ export function getJoin<
795
723
  token: JoinToken<ASide, AType, BSide, BType, Cardinality, Content>,
796
724
  store: Store,
797
725
  ): Join<ASide, AType, BSide, BType, Cardinality, Content> {
798
- const joinMap = getJoinMap(store)
799
- let myJoin = joinMap.get(token.key)
726
+ let myJoin = store.joins.get(token.key)
800
727
  if (myJoin === undefined) {
801
- const rootJoinMap = getJoinMap(IMPLICIT.STORE)
802
- myJoin = rootJoinMap.get(token.key)?.in(store)
803
- if (myJoin === undefined) {
728
+ const rootJoinMap = IMPLICIT.STORE.joins
729
+ const rootJoin = rootJoinMap.get(token.key)
730
+ if (rootJoin === undefined) {
804
731
  throw new Error(
805
732
  `Join "${token.key}" not found in store "${store.config.name}"`,
806
733
  )
807
734
  }
808
- joinMap.set(token.key, myJoin)
735
+ myJoin = new Join(rootJoin.options, rootJoin.defaultContent, store)
736
+ store.joins.set(token.key, myJoin)
809
737
  }
810
738
  return myJoin
811
739
  }
@@ -912,12 +840,12 @@ export function findRelationsInStore<
912
840
  relations = {
913
841
  get [keyAB]() {
914
842
  const familyAB = myJoin.states[keyAB as any]
915
- const state = myJoin.retrieve(familyAB, key)
843
+ const state = findInStore(store, familyAB, key)
916
844
  return state
917
845
  },
918
846
  get [keyBA]() {
919
847
  const familyBA = myJoin.states[keyBA as any]
920
- const state = myJoin.retrieve(familyBA, key)
848
+ const state = findInStore(store, familyBA, key)
921
849
  return state
922
850
  },
923
851
  } as JoinStates<ASide, AType, BSide, BType, Cardinality, Content>
@@ -927,12 +855,12 @@ export function findRelationsInStore<
927
855
  Object.assign(relations, {
928
856
  get [entryAB]() {
929
857
  const familyAB = myJoin.states[entryAB as any]
930
- const state = myJoin.retrieve(familyAB, key)
858
+ const state = findInStore(store, familyAB, key)
931
859
  return state
932
860
  },
933
861
  get [entryBA]() {
934
862
  const familyBA = myJoin.states[entryBA as any]
935
- const state = myJoin.retrieve(familyBA, key)
863
+ const state = findInStore(store, familyBA, key)
936
864
  return state
937
865
  },
938
866
  })
@@ -945,12 +873,12 @@ export function findRelationsInStore<
945
873
  relations = {
946
874
  get [keyAB]() {
947
875
  const familyAB = myJoin.states[keyAB as any]
948
- const state = myJoin.retrieve(familyAB, key)
876
+ const state = findInStore(store, familyAB, key)
949
877
  return state
950
878
  },
951
879
  get [keysBA]() {
952
880
  const familyBA = myJoin.states[keysBA as any]
953
- const state = myJoin.retrieve(familyBA, key)
881
+ const state = findInStore(store, familyBA, key)
954
882
  return state
955
883
  },
956
884
  } as JoinStates<ASide, AType, BSide, BType, Cardinality, Content>
@@ -960,12 +888,12 @@ export function findRelationsInStore<
960
888
  Object.assign(relations, {
961
889
  get [entryAB]() {
962
890
  const familyAB = myJoin.states[entryAB as any]
963
- const state = myJoin.retrieve(familyAB, key)
891
+ const state = findInStore(store, familyAB, key)
964
892
  return state
965
893
  },
966
894
  get [entriesBA]() {
967
895
  const familyBA = myJoin.states[entriesBA as any]
968
- const state = myJoin.retrieve(familyBA, key)
896
+ const state = findInStore(store, familyBA, key)
969
897
  return state
970
898
  },
971
899
  })
@@ -978,12 +906,12 @@ export function findRelationsInStore<
978
906
  relations = {
979
907
  get [keysAB]() {
980
908
  const familyAB = myJoin.states[keysAB as any]
981
- const state = myJoin.retrieve(familyAB, key)
909
+ const state = findInStore(store, familyAB, key)
982
910
  return state
983
911
  },
984
912
  get [keysBA]() {
985
913
  const familyBA = myJoin.states[keysBA as any]
986
- const state = myJoin.retrieve(familyBA, key)
914
+ const state = findInStore(store, familyBA, key)
987
915
  return state
988
916
  },
989
917
  } as JoinStates<ASide, AType, BSide, BType, Cardinality, Content>
@@ -993,12 +921,12 @@ export function findRelationsInStore<
993
921
  Object.assign(relations, {
994
922
  get [entriesAB]() {
995
923
  const familyAB = myJoin.states[entriesAB as any]
996
- const state = myJoin.retrieve(familyAB, key)
924
+ const state = findInStore(store, familyAB, key)
997
925
  return state
998
926
  },
999
927
  get [entriesBA]() {
1000
928
  const familyBA = myJoin.states[entriesBA as any]
1001
- const state = myJoin.retrieve(familyBA, key)
929
+ const state = findInStore(store, familyBA, key)
1002
930
  return state
1003
931
  },
1004
932
  })
@@ -1065,7 +993,7 @@ export function getInternalRelationsFromStore(
1065
993
  store: Store,
1066
994
  ): MutableAtomFamilyToken<SetRTX<string>, SetRTXJson<string>, string> {
1067
995
  const myJoin = getJoin(token, store)
1068
- const family = myJoin.core.findRelatedKeysState
996
+ const family = myJoin.core.relatedKeysAtoms
1069
997
  return family
1070
998
  }
1071
999