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
@@ -1,191 +0,0 @@
1
- import type {
2
- CtorToolkit,
3
- getState,
4
- MoleculeConstructor,
5
- MoleculeCreationClassic,
6
- MoleculeFamilyToken,
7
- MoleculeKey,
8
- MoleculeParams,
9
- MoleculeToken,
10
- ReadableFamilyToken,
11
- setState,
12
- } from "atom.io"
13
- import { findRelations, getJoin, type JoinToken } from "atom.io/data"
14
- import type { seekState } from "atom.io/immortal"
15
- import { stringifyJson } from "atom.io/json"
16
-
17
- import { arbitrary } from "../arbitrary"
18
- import { disposeFromStore, seekInStore } from "../families"
19
- import { getEnvironmentData } from "../get-environment-data"
20
- import { getFromStore } from "../get-state"
21
- import { eldest, newest } from "../lineage"
22
- import { getJsonToken } from "../mutable"
23
- import { setIntoStore } from "../set-state"
24
- import type { Store } from "../store"
25
- import { withdraw } from "../store"
26
- import { actUponStore, isChildStore, isRootStore } from "../transaction"
27
- import { growMoleculeInStore } from "./grow-molecule-in-store"
28
- import { Molecule } from "./molecule-internal"
29
-
30
- function capitalize<S extends string>(string: S): Capitalize<S> {
31
- return (string[0].toUpperCase() + string.slice(1)) as Capitalize<S>
32
- }
33
-
34
- export function makeMoleculeInStore<M extends MoleculeConstructor>(
35
- store: Store,
36
- context: MoleculeToken<M> | MoleculeToken<M>[],
37
- familyToken: MoleculeFamilyToken<M>,
38
- key: MoleculeKey<M>,
39
- ...params: MoleculeParams<M>
40
- ): MoleculeToken<M> {
41
- const rootStore = eldest(store)
42
- const target = newest(store)
43
- const stringKey = stringifyJson(key)
44
-
45
- target.moleculeInProgress = stringKey
46
-
47
- const contextArray = Array.isArray(context) ? context : [context]
48
- const owners = contextArray.map<Molecule<M>>((ctx) => {
49
- if (ctx instanceof Molecule) {
50
- return ctx
51
- }
52
- const ctxStringKey = stringifyJson(ctx.key)
53
- const molecule = store.molecules.get(ctxStringKey)
54
-
55
- if (!molecule) {
56
- throw new Error(
57
- `Molecule ${ctxStringKey} not found in store "${store.config.name}"`,
58
- )
59
- }
60
- return molecule
61
- })
62
-
63
- const molecule = new Molecule(owners, key, familyToken)
64
- target.molecules.set(stringKey, molecule)
65
- for (const owner of owners) {
66
- owner.below.set(molecule.stringKey, molecule)
67
- }
68
-
69
- const toolkit = {
70
- get: ((...ps: Parameters<typeof getState>) =>
71
- getFromStore(newest(rootStore), ...ps)) as typeof getState,
72
- set: ((...ps: Parameters<typeof setState>) => {
73
- setIntoStore(newest(rootStore), ...ps)
74
- }) as typeof setState,
75
- seek: ((t, k) => seekInStore(newest(rootStore), t, k)) as typeof seekState,
76
- json: (t) => getJsonToken(newest(rootStore), t),
77
- run: (t, i = arbitrary()) => actUponStore(t, i, newest(store)),
78
- make: (ctx, f, k, ...args) =>
79
- makeMoleculeInStore(newest(rootStore), ctx, f, k, ...args),
80
- dispose: (t) => {
81
- disposeFromStore(newest(rootStore), t)
82
- },
83
- env: () => getEnvironmentData(newest(rootStore)),
84
- bond: ((
85
- token:
86
- | JoinToken<any, any, any, any, any, any>
87
- | ReadableFamilyToken<any, any>,
88
- maybeRole,
89
- ) => {
90
- if (token.type === `join`) {
91
- const { as: role } = maybeRole
92
- const join = getJoin(token, rootStore)
93
- join.molecules.set(stringKey, molecule)
94
- molecule.joins.set(token.key, join)
95
- const unsubFromFamily = family.subject.subscribe(
96
- `join:${token.key}-${stringKey}`,
97
- (event) => {
98
- if (event.type === `molecule_disposal`) {
99
- if (stringifyJson(event.token.key) === stringKey) {
100
- unsubFromFamily()
101
- join.molecules.delete(stringKey)
102
- }
103
- }
104
- },
105
- )
106
-
107
- if (role === null) {
108
- return
109
- }
110
- const otherRole = token.a === role ? token.b : token.a
111
- const relations = findRelations(token, key)
112
- const relatedKeys =
113
- relations[
114
- `${otherRole}KeysOf${capitalize(role)}` as keyof typeof relations
115
- ]
116
- const relatedEntries =
117
- relations[
118
- `${otherRole}EntriesOf${capitalize(role)}` as keyof typeof relations
119
- ]
120
- let tokens = { relatedKeys }
121
- if (relatedEntries) {
122
- tokens = Object.assign(tokens, { relatedEntries })
123
- }
124
- return tokens
125
- }
126
- return growMoleculeInStore(
127
- molecule,
128
- withdraw(token, rootStore),
129
- newest(rootStore),
130
- )
131
- }) as CtorToolkit<MoleculeKey<M>>[`bond`],
132
- claim: (below, options) => {
133
- const { exclusive } = options
134
- const belowMolecule = newest(store).molecules.get(stringifyJson(below.key))
135
- if (belowMolecule) {
136
- if (exclusive) {
137
- for (const value of belowMolecule.above.values()) {
138
- value.below.delete(belowMolecule.stringKey)
139
- }
140
- belowMolecule.above.clear()
141
- belowMolecule.above.set(molecule.stringKey, molecule)
142
- molecule.below.set(belowMolecule.stringKey, belowMolecule)
143
- } else {
144
- belowMolecule.above.set(molecule.stringKey, molecule)
145
- molecule.below.set(belowMolecule.stringKey, belowMolecule)
146
- }
147
- }
148
- },
149
- spawn: (f: MoleculeFamilyToken<any>, k: any, ...p: any[]) =>
150
- makeMoleculeInStore(
151
- newest(store),
152
- [molecule],
153
- withdraw(f, store),
154
- k,
155
- ...p,
156
- ),
157
- } satisfies CtorToolkit<MoleculeKey<M>>
158
-
159
- const family = withdraw(familyToken, store)
160
- const Constructor = family.new
161
-
162
- molecule.instance = new Constructor(toolkit, key, ...params)
163
-
164
- const token = {
165
- type: `molecule`,
166
- key,
167
- family: familyToken,
168
- } as const satisfies MoleculeToken<M>
169
-
170
- const update = {
171
- type: `molecule_creation`,
172
- subType: `classic`,
173
- token,
174
- family: familyToken,
175
- context: contextArray,
176
- params,
177
- } satisfies MoleculeCreationClassic<M>
178
-
179
- if (isRootStore(target)) {
180
- family.subject.next(update)
181
- } else if (
182
- isChildStore(target) &&
183
- target.on.transactionApplying.state === null
184
- ) {
185
- target.transactionMeta.update.updates.push(update)
186
- }
187
-
188
- target.moleculeInProgress = null
189
-
190
- return token
191
- }
@@ -1,52 +0,0 @@
1
- import type {
2
- MoleculeConstructor,
3
- MoleculeFamilyToken,
4
- MoleculeKey,
5
- MoleculeToken,
6
- ReadableToken,
7
- StateCreation,
8
- StateDisposal,
9
- } from "atom.io"
10
- import type { Join } from "atom.io/data"
11
- import { stringifyJson } from "atom.io/json"
12
-
13
- import { Subject } from "../subject"
14
-
15
- export class Molecule<M extends MoleculeConstructor>
16
- implements MoleculeToken<M>
17
- {
18
- public readonly type = `molecule`
19
- public stringKey: string
20
- public family?: MoleculeFamilyToken<M>
21
- public _dependsOn: `all` | `any`
22
- public get dependsOn(): `all` | `any` {
23
- if (this.family) {
24
- return this.family.dependsOn
25
- }
26
- return this._dependsOn
27
- }
28
- public readonly subject = new Subject<
29
- StateCreation<any> | StateDisposal<any>
30
- >()
31
- public tokens = new Map<string, ReadableToken<any>>()
32
- public above = new Map<string, Molecule<any>>()
33
- public below = new Map<string, Molecule<any>>()
34
- public joins = new Map<string, Join<any, any, any, any, any, any>>()
35
- public instance: InstanceType<M>
36
- public constructor(
37
- ctx: Molecule<any>[] | undefined,
38
- public readonly key: MoleculeKey<M>,
39
- family?: MoleculeFamilyToken<M>,
40
- ) {
41
- this.stringKey = stringifyJson(key)
42
- if (family) {
43
- this.family = family
44
- this._dependsOn = family.dependsOn
45
- }
46
- if (ctx) {
47
- for (const molecule of ctx) {
48
- this.above.set(molecule.stringKey, molecule)
49
- }
50
- }
51
- }
52
- }
package/src/allocate.ts DELETED
@@ -1,277 +0,0 @@
1
- import type { Each, Store } from "atom.io/internal"
2
- import {
3
- disposeFromStore,
4
- isChildStore,
5
- Molecule,
6
- newest,
7
- } from "atom.io/internal"
8
- import type { Canonical } from "atom.io/json"
9
- import { stringifyJson } from "atom.io/json"
10
-
11
- import { makeRootMoleculeInStore } from "./molecule"
12
- import type {
13
- MoleculeCreationModern,
14
- MoleculeDisposalModern,
15
- } from "./transaction"
16
-
17
- export const $provenance = Symbol(`provenance`)
18
- export type Claim<
19
- H extends Hierarchy,
20
- V extends Vassal<H>,
21
- A extends Above<V, H>,
22
- > = V & {
23
- [$provenance]?: A
24
- }
25
-
26
- export function allocateIntoStore<
27
- H extends Hierarchy,
28
- V extends Vassal<H>,
29
- A extends Above<V, H>,
30
- >(store: Store, provenance: A, key: V): Claim<H, V, A> {
31
- const stringKey = stringifyJson(key)
32
- try {
33
- const above: Molecule<any>[] = []
34
-
35
- let allocationAttachmentStyle: `all` | `any`
36
- if (provenance === `root`) {
37
- // biome-ignore lint/style/noNonNullAssertion: let's assume we made the root molecule to get here
38
- above.push(store.molecules.get(`"root"`)!)
39
- allocationAttachmentStyle = `all`
40
- } else if (typeof provenance === `string` && provenance.startsWith(T$)) {
41
- allocationAttachmentStyle = `any`
42
- const provenanceKey = stringifyJson(provenance as Canonical)
43
- const provenanceMolecule = store.molecules.get(provenanceKey)
44
- if (!provenanceMolecule) {
45
- throw new Error(
46
- `Molecule ${provenanceKey} not found in store "${store.config.name}"`,
47
- )
48
- }
49
- above.push(provenanceMolecule)
50
- } else {
51
- const allocationIsCompound = key.startsWith(`T$--`)
52
- if (allocationIsCompound) {
53
- allocationAttachmentStyle = `all`
54
- for (const claim of provenance as SingularTypedKey[]) {
55
- const provenanceKey = stringifyJson(claim)
56
- const provenanceMolecule = store.molecules.get(provenanceKey)
57
- if (!provenanceMolecule) {
58
- throw new Error(
59
- `Molecule ${provenanceKey} not found in store "${store.config.name}"`,
60
- )
61
- }
62
- above.push(provenanceMolecule)
63
- }
64
- } else {
65
- allocationAttachmentStyle = `any`
66
- const provenanceKey = stringifyJson(provenance as Canonical)
67
- const provenanceMolecule = store.molecules.get(provenanceKey)
68
- if (!provenanceMolecule) {
69
- throw new Error(
70
- `Molecule ${provenanceKey} not found in store "${store.config.name}"`,
71
- )
72
- }
73
- above.push(provenanceMolecule)
74
- }
75
- }
76
-
77
- const molecule = new Molecule(above, key)
78
- molecule._dependsOn = allocationAttachmentStyle
79
-
80
- store.molecules.set(stringKey, molecule)
81
-
82
- for (const aboveMolecule of above) {
83
- aboveMolecule.below.set(molecule.stringKey, molecule)
84
- }
85
-
86
- const creationEvent: MoleculeCreationModern = {
87
- type: `molecule_creation`,
88
- subType: `modern`,
89
- key: molecule.key,
90
- provenance: provenance as Canonical,
91
- }
92
- const target = newest(store)
93
- const isTransaction =
94
- isChildStore(target) && target.transactionMeta.phase === `building`
95
- if (isTransaction) {
96
- target.transactionMeta.update.updates.push(creationEvent)
97
- } else {
98
- target.on.moleculeCreationStart.next(creationEvent)
99
- }
100
- } catch (thrown) {
101
- if (thrown instanceof Error) {
102
- store.logger.error(
103
- `❌`,
104
- `molecule`,
105
- stringKey,
106
- `allocation failed:`,
107
- thrown.message,
108
- )
109
- }
110
- }
111
-
112
- return key as Claim<H, V, A>
113
- }
114
-
115
- export function deallocateFromStore<
116
- H extends Hierarchy,
117
- V extends Vassal<H>,
118
- A extends Above<V, H>,
119
- >(store: Store, claim: Claim<H, V, A>): void {
120
- const stringKey = stringifyJson(claim)
121
- const molecule = store.molecules.get(stringKey)
122
- if (!molecule) {
123
- throw new Error(
124
- `Molecule ${stringKey} not found in store "${store.config.name}"`,
125
- )
126
- }
127
-
128
- for (const join of molecule.joins.values()) {
129
- join.relations.delete(molecule.key)
130
- join.molecules.delete(molecule.stringKey)
131
- }
132
-
133
- let provenance: Canonical
134
- if (molecule.above.size === 1) {
135
- const above = molecule.above.values().next().value
136
- provenance = above.key
137
- } else {
138
- provenance = [...molecule.above.values()].map(({ key }) => key)
139
- }
140
- const values: [string, any][] = []
141
- for (const stateToken of molecule.tokens.values()) {
142
- // biome-ignore lint/style/noNonNullAssertion: tokens of molecules must have a family
143
- const tokenFamily = stateToken.family!
144
- values.push([tokenFamily.key, store.valueMap.get(stateToken.key)])
145
- }
146
-
147
- for (const state of molecule.tokens.values()) {
148
- disposeFromStore(store, state)
149
- }
150
- for (const child of molecule.below.values()) {
151
- if (child.dependsOn === `all`) {
152
- deallocateFromStore<any, any, any>(store, child.key)
153
- } else {
154
- child.above.delete(molecule.stringKey)
155
- if (child.above.size === 0) {
156
- deallocateFromStore<any, any, any>(store, child.key)
157
- }
158
- }
159
- }
160
- molecule.below.clear()
161
-
162
- const disposalEvent: MoleculeDisposalModern = {
163
- type: `molecule_disposal`,
164
- subType: `modern`,
165
- key: molecule.key,
166
- values,
167
- provenance,
168
- }
169
- const target = newest(store)
170
- const isTransaction =
171
- isChildStore(target) && target.transactionMeta.phase === `building`
172
- if (isTransaction) {
173
- target.transactionMeta.update.updates.push(disposalEvent)
174
- } else {
175
- target.on.moleculeDisposal.next(disposalEvent)
176
- }
177
- target.molecules.delete(molecule.stringKey)
178
-
179
- for (const parent of molecule.above.values()) {
180
- parent.below.delete(molecule.stringKey)
181
- }
182
- }
183
-
184
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
185
- export function realm<H extends Hierarchy>(store: Store) {
186
- const root = makeRootMoleculeInStore(`root`, store)
187
- return {
188
- root,
189
- allocate: <V extends Vassal<H>, A extends Above<V, H>>(
190
- provenance: A,
191
- key: V,
192
- ): Claim<H, V, A> => {
193
- return allocateIntoStore(store, provenance, key)
194
- },
195
- deallocate: <V extends Vassal<H>, A extends Above<V, H>>(
196
- claim: Claim<H, V, A>,
197
- ): void => {
198
- deallocateFromStore(store, claim)
199
- },
200
- }
201
- }
202
-
203
- export const T$ = `T$`
204
- export type T$ = typeof T$
205
- export type TypeTag<T extends string> = `${T$}--${T}`
206
- export type SingularTypedKey<T extends string = string> = `${T}::${string}`
207
- export type CompoundTypedKey<
208
- A extends string = string,
209
- B extends string = string,
210
- C extends string = string,
211
- > = `${TypeTag<A>}==${SingularTypedKey<B>}++${SingularTypedKey<C>}`
212
- export type TypedKey<
213
- A extends string = string,
214
- B extends string = string,
215
- C extends string = string,
216
- > = CompoundTypedKey<A, B, C> | SingularTypedKey<A>
217
- type Scope = SingularTypedKey[]
218
- type MutualFealty = {
219
- above: Scope
220
- below: CompoundTypedKey
221
- style: `all` | `any`
222
- }
223
- type ExclusiveFealty = {
224
- above: TypedKey | `root`
225
- below: Scope
226
- }
227
- type Fealty = ExclusiveFealty | MutualFealty
228
-
229
- export type Hierarchy<F extends Fealty[] = Fealty[]> = Each<F>
230
-
231
- export type Vassal<H extends Hierarchy> = {
232
- [K in keyof H]: H[K] extends MutualFealty
233
- ? H[K][`below`]
234
- : H[K] extends { below: Array<infer V> }
235
- ? V extends TypedKey
236
- ? V
237
- : never
238
- : never
239
- }[keyof H]
240
-
241
- export type Above<TK extends TypedKey, H extends Hierarchy> = {
242
- [K in keyof H]: H[K] extends MutualFealty
243
- ? TK extends H[K][`below`]
244
- ? H[K][`above`]
245
- : never
246
- : H[K] extends { below: Array<infer V> }
247
- ? TK extends V
248
- ? H[K] extends ExclusiveFealty
249
- ? H[K][`above`]
250
- : never
251
- : never
252
- : never
253
- }[keyof H]
254
-
255
- export type Below<TK extends TypedKey | TypedKey[], H extends Hierarchy> = {
256
- [K in keyof H]: H[K] extends MutualFealty
257
- ? TK extends H[K][`above`]
258
- ? H[K][`below`]
259
- : TK extends H[K][`above`][number]
260
- ? H[K][`below`]
261
- : never
262
- : H[K] extends { above: infer V }
263
- ? TK extends V
264
- ? H[K] extends ExclusiveFealty
265
- ? H[K][`below`][number]
266
- : never
267
- : never
268
- : never
269
- }[keyof H]
270
-
271
- export type Mutuals<TK extends TypedKey | TypedKey[], H extends Hierarchy> = {
272
- [K in keyof H]: H[K] extends MutualFealty
273
- ? TK extends H[K][`above`][number]
274
- ? [mutual: Exclude<H[K][`above`][number], TK>, below: H[K][`below`]]
275
- : never
276
- : never
277
- }[keyof H]
package/src/molecule.ts DELETED
@@ -1,138 +0,0 @@
1
- import type {
2
- ActorToolkit,
3
- MoleculeCreation,
4
- MoleculeCreationClassic,
5
- MoleculeDisposal,
6
- MoleculeDisposalClassic,
7
- MutableAtomFamilyToken,
8
- MutableAtomToken,
9
- ReadableFamilyToken,
10
- ReadableToken,
11
- ReadonlySelectorFamilyToken,
12
- ReadonlySelectorToken,
13
- RegularAtomFamilyToken,
14
- RegularAtomToken,
15
- WritableFamilyToken,
16
- WritableSelectorFamilyToken,
17
- WritableSelectorToken,
18
- WritableToken,
19
- } from "atom.io"
20
- import type { JoinToken } from "atom.io/data"
21
- import type { Flat, Store, Subject, Transceiver } from "atom.io/internal"
22
- import {
23
- createMoleculeFamily,
24
- IMPLICIT,
25
- makeMoleculeInStore,
26
- Molecule,
27
- } from "atom.io/internal"
28
- import type { Canonical, Json } from "atom.io/json"
29
- import { stringifyJson } from "atom.io/json"
30
-
31
- export type CtorToolkit<K extends Canonical> = Flat<
32
- Omit<ActorToolkit, `find`> & {
33
- bond<T extends Transceiver<any>, J extends Json.Serializable>(
34
- family: MutableAtomFamilyToken<T, J, K>,
35
- ): MutableAtomToken<T, J>
36
- bond<T>(family: RegularAtomFamilyToken<T, K>): RegularAtomToken<T>
37
- bond<T>(family: WritableSelectorFamilyToken<T, K>): WritableSelectorToken<T>
38
- bond<T>(family: ReadonlySelectorFamilyToken<T, K>): ReadonlySelectorToken<T>
39
- bond<T>(family: WritableFamilyToken<T, K>): WritableToken<T>
40
- bond<T>(family: ReadableFamilyToken<T, K>): ReadableToken<T>
41
- bond<J extends JoinToken<any, any, any, any, any, any>>(
42
- joinToken: J,
43
- role: {
44
- as: J extends JoinToken<infer A, string, infer B, string, any, any>
45
- ? A | B
46
- : never
47
- },
48
- ): J extends JoinToken<any, any, any, any, any, infer Content>
49
- ? Content extends null
50
- ? { relatedKeys: ReadonlySelectorToken<string[]> }
51
- : {
52
- relatedKeys: ReadonlySelectorToken<string[]>
53
- relatedEntries: ReadonlySelectorToken<
54
- [key: string, value: Content][]
55
- >
56
- }
57
- : never
58
-
59
- claim(below: MoleculeToken<any>, options: { exclusive: boolean }): void
60
-
61
- spawn<Key extends Canonical, Ctor extends MoleculeConstructor>(
62
- family: MoleculeFamilyToken<Ctor>,
63
- key: Key,
64
- ...params: MoleculeParams<Ctor>
65
- ): MoleculeToken<Ctor>
66
- }
67
- >
68
- export type MoleculeConstructor = new (
69
- toolkit: CtorToolkit<any>,
70
- key: any,
71
- ...params: any
72
- ) => any
73
-
74
- type Tail<T extends any[]> = T extends [any, ...infer Rest] ? Rest : any[]
75
-
76
- export type MoleculeParams<M extends MoleculeConstructor> = Tail<
77
- Tail<ConstructorParameters<M>>
78
- >
79
-
80
- export type MoleculeFamilyOptions<M extends MoleculeConstructor> = {
81
- key: string
82
- dependsOn?: `all` | `any`
83
- new: M
84
- }
85
- export type MoleculeFamilyToken<M extends MoleculeConstructor> = {
86
- key: string
87
- type: `molecule_family`
88
- dependsOn: `all` | `any`
89
- __M?: M
90
- }
91
- export type MoleculeFamily<M extends MoleculeConstructor> = Flat<
92
- MoleculeFamilyToken<M> & {
93
- subject: Subject<MoleculeCreationClassic<M> | MoleculeDisposalClassic>
94
- dependsOn: `all` | `any`
95
- new: M
96
- }
97
- >
98
- export type MoleculeToken<M extends MoleculeConstructor> = {
99
- key: MoleculeKey<M>
100
- type: `molecule`
101
- family?: MoleculeFamilyToken<M>
102
- __M?: M
103
- }
104
-
105
- export function moleculeFamily<M extends MoleculeConstructor>(
106
- options: MoleculeFamilyOptions<M>,
107
- ): MoleculeFamilyToken<M> {
108
- return createMoleculeFamily(IMPLICIT.STORE, options)
109
- }
110
-
111
- export function makeMolecule<M extends MoleculeConstructor>(
112
- context: MoleculeToken<any> | MoleculeToken<any>[],
113
- family: MoleculeFamilyToken<M>,
114
- key: MoleculeKey<M>,
115
- ...params: MoleculeParams<M>
116
- ): MoleculeToken<M> {
117
- return makeMoleculeInStore(IMPLICIT.STORE, context, family, key, ...params)
118
- }
119
-
120
- export type MoleculeType<T extends MoleculeFamilyToken<any>> =
121
- T extends MoleculeFamilyToken<infer M>
122
- ? M
123
- : T extends MoleculeToken<infer M>
124
- ? M
125
- : never
126
- export type MoleculeKey<M extends MoleculeConstructor> = InstanceType<M>[`key`]
127
-
128
- export function makeRootMoleculeInStore(
129
- key: string,
130
- store: Store = IMPLICIT.STORE,
131
- ): MoleculeToken<ObjectConstructor> {
132
- const molecule = new Molecule(undefined, key)
133
- store.molecules.set(stringifyJson(key), molecule)
134
- return {
135
- key,
136
- type: `molecule`,
137
- } as const
138
- }