atom.io 0.42.0 → 0.42.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.
- package/dist/internal/index.d.ts +9 -9
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +46 -36
- package/dist/internal/index.js.map +1 -1
- package/dist/main/index.d.ts +34 -20
- package/dist/main/index.d.ts.map +1 -1
- package/dist/main/index.js +32 -6
- package/dist/main/index.js.map +1 -1
- package/dist/transceivers/u-list/index.d.ts +5 -1
- package/dist/transceivers/u-list/index.d.ts.map +1 -1
- package/dist/transceivers/u-list/index.js +33 -2
- package/dist/transceivers/u-list/index.js.map +1 -1
- package/package.json +15 -15
- package/src/internal/atom/create-regular-atom.ts +3 -1
- package/src/internal/join/join-internal.ts +18 -47
- package/src/internal/molecule.ts +44 -25
- package/src/internal/mutable/create-mutable-atom.ts +15 -11
- package/src/internal/store/store.ts +15 -14
- package/src/internal/transaction/build-transaction.ts +1 -1
- package/src/main/atom.ts +15 -6
- package/src/main/realm.ts +54 -13
- package/src/transceivers/u-list/index.ts +1 -0
- package/src/transceivers/u-list/u-list-disposed-key-cleanup-effect.ts +47 -0
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
findState,
|
|
3
|
-
getState,
|
|
4
|
-
JoinOptions,
|
|
5
|
-
MutableAtomFamilyToken,
|
|
6
|
-
ReadonlyPureSelectorFamilyToken,
|
|
7
|
-
setState,
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import {
|
|
2
|
+
type findState,
|
|
3
|
+
type getState,
|
|
4
|
+
type JoinOptions,
|
|
5
|
+
type MutableAtomFamilyToken,
|
|
6
|
+
type ReadonlyPureSelectorFamilyToken,
|
|
7
|
+
type setState,
|
|
8
|
+
simpleCompound,
|
|
9
|
+
type Write,
|
|
10
|
+
type WriterToolkit,
|
|
10
11
|
} from "atom.io"
|
|
11
|
-
import { Anarchy } from "atom.io"
|
|
12
|
-
import { stringifyJson } from "atom.io/json"
|
|
13
12
|
import { UList } from "atom.io/transceivers/u-list"
|
|
14
13
|
|
|
15
14
|
import { capitalize } from "../capitalize"
|
|
@@ -86,7 +85,6 @@ export class Join<
|
|
|
86
85
|
}
|
|
87
86
|
|
|
88
87
|
public store: Store
|
|
89
|
-
public realm: Anarchy
|
|
90
88
|
|
|
91
89
|
public [Symbol.dispose](): void {}
|
|
92
90
|
|
|
@@ -97,13 +95,10 @@ export class Join<
|
|
|
97
95
|
type AB = A & B
|
|
98
96
|
|
|
99
97
|
this.store = store
|
|
100
|
-
this.realm = new Anarchy(store)
|
|
101
98
|
this.options = options
|
|
102
99
|
|
|
103
100
|
this.store.miscResources.set(`join:${options.key}`, this)
|
|
104
101
|
|
|
105
|
-
this.realm.allocate(`root`, options.key)
|
|
106
|
-
|
|
107
102
|
this.toolkit = {
|
|
108
103
|
get: ((...ps: Parameters<typeof getState>) =>
|
|
109
104
|
getFromStore(store, ...ps)) as typeof getState,
|
|
@@ -179,9 +174,9 @@ export class Join<
|
|
|
179
174
|
})
|
|
180
175
|
}
|
|
181
176
|
for (const previousOwner of previousOwnersToDispose) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
177
|
+
store.keyRefsInJoins.delete(
|
|
178
|
+
simpleCompound(newRelationB, previousOwner),
|
|
179
|
+
)
|
|
185
180
|
}
|
|
186
181
|
}
|
|
187
182
|
if (!newRelationBIsAlreadyRelated) {
|
|
@@ -222,14 +217,9 @@ export class Join<
|
|
|
222
217
|
getRelatedKeys: (key) =>
|
|
223
218
|
this.toolkit.get(relatedKeysAtoms, key) as UList<A> | UList<B>,
|
|
224
219
|
addRelation: (a, b) => {
|
|
225
|
-
this.store.
|
|
226
|
-
this.store.
|
|
227
|
-
|
|
228
|
-
this.realm.allocate(options.key, a)
|
|
229
|
-
}
|
|
230
|
-
if (!this.store.molecules.has(stringifyJson(b))) {
|
|
231
|
-
this.realm.allocate(options.key, b)
|
|
232
|
-
}
|
|
220
|
+
this.store.keyRefsInJoins.set(`"${a}"`, options.key)
|
|
221
|
+
this.store.keyRefsInJoins.set(`"${b}"`, options.key)
|
|
222
|
+
this.store.keyRefsInJoins.set(simpleCompound(a, b), options.key)
|
|
233
223
|
this.toolkit.set(relatedKeysAtoms, a, (aKeys) => aKeys.add(b))
|
|
234
224
|
this.toolkit.set(relatedKeysAtoms, b, (bKeys) => bKeys.add(a))
|
|
235
225
|
},
|
|
@@ -242,9 +232,8 @@ export class Join<
|
|
|
242
232
|
bKeys.delete(a)
|
|
243
233
|
return bKeys
|
|
244
234
|
})
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
this.store.moleculeJoins.delete(compositeKey)
|
|
235
|
+
const compositeKey = simpleCompound(a, b)
|
|
236
|
+
this.store.keyRefsInJoins.delete(compositeKey)
|
|
248
237
|
},
|
|
249
238
|
replaceRelationsSafely: (a, bs) => {
|
|
250
239
|
replaceRelationsSafely(this.toolkit, a, bs)
|
|
@@ -262,24 +251,6 @@ export class Join<
|
|
|
262
251
|
externalStore,
|
|
263
252
|
isAType: options.isAType,
|
|
264
253
|
isBType: options.isBType,
|
|
265
|
-
// makeContentKey: (...args) => {
|
|
266
|
-
// const [a, b] = args
|
|
267
|
-
// const [x, y] = args.sort()
|
|
268
|
-
// const compositeKey = `${x}:${y}`
|
|
269
|
-
// const aMolecule = store.molecules.get(stringifyJson(a))
|
|
270
|
-
// const bMolecule = store.molecules.get(stringifyJson(b))
|
|
271
|
-
// if (!aMolecule) {
|
|
272
|
-
// this.realm.allocate(options.key, a)
|
|
273
|
-
// }
|
|
274
|
-
// if (!bMolecule) {
|
|
275
|
-
// this.realm.allocate(options.key, b)
|
|
276
|
-
// }
|
|
277
|
-
|
|
278
|
-
// this.realm.allocate(a, compositeKey, `all`)
|
|
279
|
-
// this.realm.claim(b, compositeKey)
|
|
280
|
-
// this.store.moleculeJoins.set(compositeKey, options.key)
|
|
281
|
-
// return compositeKey
|
|
282
|
-
// },
|
|
283
254
|
})
|
|
284
255
|
|
|
285
256
|
const createSingleKeySelectorFamily = () =>
|
package/src/internal/molecule.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
Above,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
Hierarchy,
|
|
7
|
-
MoleculeCreationEvent,
|
|
8
|
-
MoleculeDisposalEvent,
|
|
9
|
-
MoleculeTransferEvent,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import {
|
|
2
|
+
type Above,
|
|
3
|
+
type CompoundFrom,
|
|
4
|
+
type CompoundTypedKey,
|
|
5
|
+
decomposeCompound,
|
|
6
|
+
type Hierarchy,
|
|
7
|
+
type MoleculeCreationEvent,
|
|
8
|
+
type MoleculeDisposalEvent,
|
|
9
|
+
type MoleculeTransferEvent,
|
|
10
|
+
simpleCompound,
|
|
11
|
+
type SingularTypedKey,
|
|
12
|
+
type TransactionToken,
|
|
13
|
+
type ValidKey,
|
|
14
|
+
type Vassal,
|
|
13
15
|
} from "atom.io"
|
|
14
16
|
import type { Canonical, stringified } from "atom.io/json"
|
|
15
17
|
import { parseJson, stringifyJson } from "atom.io/json"
|
|
@@ -19,6 +21,7 @@ import { getFromStore } from "./get-state"
|
|
|
19
21
|
import { getTrace } from "./get-trace"
|
|
20
22
|
import { newest } from "./lineage"
|
|
21
23
|
import type { Store } from "./store"
|
|
24
|
+
import { Subject } from "./subject"
|
|
22
25
|
import type { RootStore } from "./transaction"
|
|
23
26
|
import { createTransaction, isChildStore } from "./transaction"
|
|
24
27
|
|
|
@@ -26,6 +29,7 @@ export type Molecule<K extends Canonical> = {
|
|
|
26
29
|
readonly key: K
|
|
27
30
|
readonly stringKey: stringified<K>
|
|
28
31
|
readonly dependsOn: `all` | `any`
|
|
32
|
+
readonly subject: Subject<void>
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
export function makeRootMoleculeInStore<S extends string>(
|
|
@@ -36,6 +40,7 @@ export function makeRootMoleculeInStore<S extends string>(
|
|
|
36
40
|
key,
|
|
37
41
|
stringKey: stringifyJson(key),
|
|
38
42
|
dependsOn: `any`,
|
|
43
|
+
subject: new Subject(),
|
|
39
44
|
} satisfies Molecule<S>
|
|
40
45
|
store.molecules.set(stringifyJson(key), molecule)
|
|
41
46
|
return key
|
|
@@ -50,7 +55,7 @@ export function allocateIntoStore<
|
|
|
50
55
|
provenance: A,
|
|
51
56
|
key: V,
|
|
52
57
|
dependsOn: `all` | `any` = `any`,
|
|
53
|
-
):
|
|
58
|
+
): ValidKey<V> {
|
|
54
59
|
const origin = provenance as Canonical | [Canonical, Canonical]
|
|
55
60
|
const stringKey = stringifyJson(key)
|
|
56
61
|
const invalidKeys: stringified<Canonical>[] = []
|
|
@@ -75,8 +80,9 @@ export function allocateIntoStore<
|
|
|
75
80
|
invalidKeys.push(claimString)
|
|
76
81
|
}
|
|
77
82
|
}
|
|
83
|
+
const subject = new Subject<void>()
|
|
78
84
|
if (invalidKeys.length === 0) {
|
|
79
|
-
target.molecules.set(stringKey, { key, stringKey, dependsOn })
|
|
85
|
+
target.molecules.set(stringKey, { key, stringKey, dependsOn, subject })
|
|
80
86
|
}
|
|
81
87
|
|
|
82
88
|
const creationEvent: MoleculeCreationEvent = {
|
|
@@ -109,7 +115,7 @@ export function allocateIntoStore<
|
|
|
109
115
|
)
|
|
110
116
|
}
|
|
111
117
|
|
|
112
|
-
return key as
|
|
118
|
+
return key as ValidKey<V>
|
|
113
119
|
}
|
|
114
120
|
|
|
115
121
|
export function fuseWithinStore<
|
|
@@ -123,7 +129,7 @@ export function fuseWithinStore<
|
|
|
123
129
|
type: T,
|
|
124
130
|
sideA: SingularTypedKey<A>,
|
|
125
131
|
sideB: SingularTypedKey<B>,
|
|
126
|
-
):
|
|
132
|
+
): ValidKey<CompoundTypedKey<T, A, B>> {
|
|
127
133
|
const compoundKey: CompoundTypedKey<T, A, B> =
|
|
128
134
|
`T$--${type}==${sideA}++${sideB}`
|
|
129
135
|
const above = [sideA, sideB] as Above<Vassal<H>, H>
|
|
@@ -139,10 +145,10 @@ export function fuseWithinStore<
|
|
|
139
145
|
export function createDeallocateTX<
|
|
140
146
|
H extends Hierarchy,
|
|
141
147
|
V extends Exclude<Vassal<H>, CompoundTypedKey>,
|
|
142
|
-
>(store: RootStore): TransactionToken<(claim:
|
|
148
|
+
>(store: RootStore): TransactionToken<(claim: ValidKey<V>) => void> {
|
|
143
149
|
return createTransaction(store, {
|
|
144
150
|
key: `[Internal] deallocate`,
|
|
145
|
-
do: (_, claim:
|
|
151
|
+
do: (_, claim: ValidKey<V>): void => {
|
|
146
152
|
deallocateFromStore<H, V>(newest(store), claim)
|
|
147
153
|
},
|
|
148
154
|
})
|
|
@@ -150,7 +156,7 @@ export function createDeallocateTX<
|
|
|
150
156
|
|
|
151
157
|
export function deallocateFromStore<H extends Hierarchy, V extends Vassal<H>>(
|
|
152
158
|
target: Store,
|
|
153
|
-
claim:
|
|
159
|
+
claim: ValidKey<V>,
|
|
154
160
|
): void {
|
|
155
161
|
const stringKey = stringifyJson(claim)
|
|
156
162
|
|
|
@@ -171,8 +177,9 @@ export function deallocateFromStore<H extends Hierarchy, V extends Vassal<H>>(
|
|
|
171
177
|
)
|
|
172
178
|
return
|
|
173
179
|
}
|
|
180
|
+
molecule.subject.next()
|
|
174
181
|
|
|
175
|
-
const joinKeys = target.
|
|
182
|
+
const joinKeys = target.keyRefsInJoins.getRelatedKeys(stringKey)
|
|
176
183
|
if (joinKeys) {
|
|
177
184
|
for (const joinKey of joinKeys) {
|
|
178
185
|
const join = target.joins.get(joinKey)
|
|
@@ -180,8 +187,20 @@ export function deallocateFromStore<H extends Hierarchy, V extends Vassal<H>>(
|
|
|
180
187
|
join.relations.delete(claim)
|
|
181
188
|
}
|
|
182
189
|
}
|
|
190
|
+
} else {
|
|
191
|
+
const compound = decomposeCompound(claim)
|
|
192
|
+
if (compound) {
|
|
193
|
+
const [, a, b] = compound
|
|
194
|
+
const joinKey = target.keyRefsInJoins.getRelatedKey(simpleCompound(a, b))
|
|
195
|
+
if (joinKey) {
|
|
196
|
+
const join = target.joins.get(joinKey)
|
|
197
|
+
if (join) {
|
|
198
|
+
join.relations.delete(a, b)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
183
202
|
}
|
|
184
|
-
target.
|
|
203
|
+
target.keyRefsInJoins.delete(stringKey)
|
|
185
204
|
|
|
186
205
|
const provenance: stringified<Canonical>[] = []
|
|
187
206
|
|
|
@@ -225,7 +244,7 @@ export function deallocateFromStore<H extends Hierarchy, V extends Vassal<H>>(
|
|
|
225
244
|
}
|
|
226
245
|
|
|
227
246
|
target.moleculeGraph.delete(molecule.stringKey)
|
|
228
|
-
target.
|
|
247
|
+
target.keyRefsInJoins.delete(molecule.stringKey)
|
|
229
248
|
target.moleculeData.delete(molecule.stringKey)
|
|
230
249
|
|
|
231
250
|
if (!isTransaction) {
|
|
@@ -244,7 +263,7 @@ export function createClaimTX<
|
|
|
244
263
|
>(
|
|
245
264
|
store: RootStore,
|
|
246
265
|
): TransactionToken<
|
|
247
|
-
(newProvenance: A, claim:
|
|
266
|
+
(newProvenance: A, claim: ValidKey<V>, exclusive?: `exclusive`) => void
|
|
248
267
|
> {
|
|
249
268
|
return createTransaction(store, {
|
|
250
269
|
key: `[Internal] claim`,
|
|
@@ -261,9 +280,9 @@ export function claimWithinStore<
|
|
|
261
280
|
>(
|
|
262
281
|
store: Store,
|
|
263
282
|
newProvenance: A,
|
|
264
|
-
claim:
|
|
283
|
+
claim: ValidKey<V>,
|
|
265
284
|
exclusive?: `exclusive`,
|
|
266
|
-
):
|
|
285
|
+
): ValidKey<V> {
|
|
267
286
|
const stringKey = stringifyJson(claim)
|
|
268
287
|
const target = newest(store)
|
|
269
288
|
const molecule = target.molecules.get(stringKey)
|
|
@@ -5,7 +5,7 @@ import type {
|
|
|
5
5
|
UpdateHandler,
|
|
6
6
|
} from "atom.io"
|
|
7
7
|
|
|
8
|
-
import { newest } from "../lineage"
|
|
8
|
+
import { eldest, newest } from "../lineage"
|
|
9
9
|
import { createStandaloneSelector } from "../selector"
|
|
10
10
|
import { resetInStore, setIntoStore } from "../set-state"
|
|
11
11
|
import type { MutableAtom } from "../state-types"
|
|
@@ -58,6 +58,18 @@ export function createMutableAtom<T extends Transceiver<any, any, any>>(
|
|
|
58
58
|
}
|
|
59
59
|
target.atoms.set(newAtom.key, newAtom)
|
|
60
60
|
const token = deposit(newAtom)
|
|
61
|
+
|
|
62
|
+
new Tracker(token, store)
|
|
63
|
+
if (!family) {
|
|
64
|
+
createStandaloneSelector(store, {
|
|
65
|
+
key: `${key}:JSON`,
|
|
66
|
+
get: ({ get }) => get(token).toJSON(),
|
|
67
|
+
set: ({ set }, newValue) => {
|
|
68
|
+
set(token, options.class.fromJSON(newValue))
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
61
73
|
if (options.effects) {
|
|
62
74
|
let effectIndex = 0
|
|
63
75
|
const cleanupFunctions: (() => void)[] = []
|
|
@@ -71,6 +83,8 @@ export function createMutableAtom<T extends Transceiver<any, any, any>>(
|
|
|
71
83
|
},
|
|
72
84
|
onSet: (handle: UpdateHandler<T>) =>
|
|
73
85
|
subscribeToState(store, token, `effect[${effectIndex}]`, handle),
|
|
86
|
+
token: token as any,
|
|
87
|
+
store: eldest(store),
|
|
74
88
|
})
|
|
75
89
|
if (cleanup) {
|
|
76
90
|
cleanupFunctions.push(cleanup)
|
|
@@ -84,16 +98,6 @@ export function createMutableAtom<T extends Transceiver<any, any, any>>(
|
|
|
84
98
|
}
|
|
85
99
|
}
|
|
86
100
|
|
|
87
|
-
new Tracker(token, store)
|
|
88
|
-
if (!family) {
|
|
89
|
-
createStandaloneSelector(store, {
|
|
90
|
-
key: `${key}:JSON`,
|
|
91
|
-
get: ({ get }) => get(token).toJSON(),
|
|
92
|
-
set: ({ set }, newValue) => {
|
|
93
|
-
set(token, options.class.fromJSON(newValue))
|
|
94
|
-
},
|
|
95
|
-
})
|
|
96
|
-
}
|
|
97
101
|
store.on.atomCreation.next(token)
|
|
98
102
|
return token
|
|
99
103
|
}
|
|
@@ -107,20 +107,7 @@ export class Store implements Lineage {
|
|
|
107
107
|
new CircularBuffer(100)
|
|
108
108
|
|
|
109
109
|
public molecules: Map<string, Molecule<Canonical>> = new Map()
|
|
110
|
-
|
|
111
|
-
`moleculeKey`,
|
|
112
|
-
stringified<Canonical>,
|
|
113
|
-
`joinKey`,
|
|
114
|
-
string
|
|
115
|
-
> = new Junction(
|
|
116
|
-
{
|
|
117
|
-
between: [`moleculeKey`, `joinKey`],
|
|
118
|
-
cardinality: `n:n`,
|
|
119
|
-
},
|
|
120
|
-
{
|
|
121
|
-
makeContentKey: (...keys) => keys.sort().join(`:`),
|
|
122
|
-
},
|
|
123
|
-
)
|
|
110
|
+
|
|
124
111
|
public moleculeGraph: Junction<
|
|
125
112
|
`upstreamMoleculeKey`,
|
|
126
113
|
stringified<Canonical> | `"root"`,
|
|
@@ -150,6 +137,20 @@ export class Store implements Lineage {
|
|
|
150
137
|
makeContentKey: (...keys) => keys.sort().join(`:`),
|
|
151
138
|
},
|
|
152
139
|
)
|
|
140
|
+
public keyRefsInJoins: Junction<
|
|
141
|
+
`moleculeKey`,
|
|
142
|
+
stringified<Canonical>,
|
|
143
|
+
`joinKey`,
|
|
144
|
+
string
|
|
145
|
+
> = new Junction(
|
|
146
|
+
{
|
|
147
|
+
between: [`moleculeKey`, `joinKey`],
|
|
148
|
+
cardinality: `n:n`,
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
makeContentKey: (...keys) => keys.sort().join(`:`),
|
|
152
|
+
},
|
|
153
|
+
)
|
|
153
154
|
public miscResources: Map<string, Disposable> = new Map()
|
|
154
155
|
|
|
155
156
|
public on: StoreEventCarrier = {
|
|
@@ -54,7 +54,7 @@ export const buildTransaction = (
|
|
|
54
54
|
molecules: new MapOverlay(parent.molecules),
|
|
55
55
|
moleculeGraph: parent.moleculeGraph.overlay(),
|
|
56
56
|
moleculeData: parent.moleculeData.overlay(),
|
|
57
|
-
|
|
57
|
+
keyRefsInJoins: parent.keyRefsInJoins.overlay(),
|
|
58
58
|
miscResources: new MapOverlay(parent.miscResources),
|
|
59
59
|
}
|
|
60
60
|
const epoch = getEpochNumberOfAction(store, token.key)
|
package/src/main/atom.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ConstructorOf, Ctor, Transceiver } from "atom.io/internal"
|
|
1
|
+
import type { ConstructorOf, Ctor, Store, Transceiver } from "atom.io/internal"
|
|
2
2
|
import {
|
|
3
3
|
createMutableAtom,
|
|
4
4
|
createMutableAtomFamily,
|
|
@@ -10,6 +10,7 @@ import type { Canonical } from "atom.io/json"
|
|
|
10
10
|
|
|
11
11
|
import type { StateUpdate } from "./events"
|
|
12
12
|
import type {
|
|
13
|
+
AtomToken,
|
|
13
14
|
MutableAtomFamilyToken,
|
|
14
15
|
MutableAtomToken,
|
|
15
16
|
RegularAtomFamilyToken,
|
|
@@ -22,7 +23,7 @@ export type RegularAtomOptions<T, E = never> = {
|
|
|
22
23
|
/** The starting value of the atom */
|
|
23
24
|
default: T | (() => T)
|
|
24
25
|
/** Hooks used to run side effects when the atom is set */
|
|
25
|
-
effects?: readonly AtomEffect<T>[]
|
|
26
|
+
effects?: readonly AtomEffect<T, E>[]
|
|
26
27
|
/** The classes of errors that might be thrown when deriving the atom's default value */
|
|
27
28
|
catch?: readonly Ctor<E>[]
|
|
28
29
|
}
|
|
@@ -67,8 +68,10 @@ export function mutableAtom<T extends Transceiver<any, any, any>>(
|
|
|
67
68
|
* @returns
|
|
68
69
|
* Optionally, a cleanup function that will be called when the atom is disposed
|
|
69
70
|
*/
|
|
70
|
-
export type AtomEffect<T
|
|
71
|
-
|
|
71
|
+
export type AtomEffect<T, E = never> = (
|
|
72
|
+
tools: Effectors<T, E>,
|
|
73
|
+
) => (() => void) | void
|
|
74
|
+
export type Effectors<T, E = never> = {
|
|
72
75
|
/**
|
|
73
76
|
* Reset the value of the atom to its default
|
|
74
77
|
*/
|
|
@@ -79,7 +82,13 @@ export type Effectors<T> = {
|
|
|
79
82
|
*/
|
|
80
83
|
setSelf: <New extends T>(next: New | ((old: T) => New)) => void
|
|
81
84
|
/** Subscribe to changes to the atom */
|
|
82
|
-
onSet: (callback: (options: StateUpdate<T>) => void) => void
|
|
85
|
+
onSet: (callback: (options: StateUpdate<E | T>) => void) => void
|
|
86
|
+
/** The token of the atom */
|
|
87
|
+
token: T extends Transceiver<any, any, any>
|
|
88
|
+
? MutableAtomToken<T>
|
|
89
|
+
: AtomToken<T, any, E>
|
|
90
|
+
/** The store in which the atom exists */
|
|
91
|
+
store: Store
|
|
83
92
|
}
|
|
84
93
|
|
|
85
94
|
export type RegularAtomFamilyOptions<T, K extends Canonical, E = never> = {
|
|
@@ -88,7 +97,7 @@ export type RegularAtomFamilyOptions<T, K extends Canonical, E = never> = {
|
|
|
88
97
|
/** The starting value of the atom family */
|
|
89
98
|
default: T | ((key: K) => T)
|
|
90
99
|
/** Hooks used to run side effects when an atom in the family is set */
|
|
91
|
-
effects?: (key: K) => AtomEffect<T>[]
|
|
100
|
+
effects?: (key: K) => AtomEffect<T, E>[]
|
|
92
101
|
/** The classes of errors that might be thrown when deriving the atom's default value */
|
|
93
102
|
catch?: readonly Ctor<E>[]
|
|
94
103
|
}
|
package/src/main/realm.ts
CHANGED
|
@@ -14,16 +14,20 @@ import type { Canonical } from "atom.io/json"
|
|
|
14
14
|
|
|
15
15
|
import type { TransactionToken } from "./tokens"
|
|
16
16
|
|
|
17
|
-
export const $
|
|
18
|
-
export type
|
|
17
|
+
export const $validatedKey: unique symbol = Symbol.for(`claim`)
|
|
18
|
+
export type ValidKey<K extends Canonical> = K & { [$validatedKey]?: true }
|
|
19
|
+
|
|
20
|
+
export function simpleCompound(a: string, b: string): string {
|
|
21
|
+
return [a, b].sort().join(`\u001F`)
|
|
22
|
+
}
|
|
19
23
|
|
|
20
24
|
export class Realm<H extends Hierarchy> {
|
|
21
25
|
public store: RootStore
|
|
22
|
-
public deallocateTX: TransactionToken<(claim:
|
|
26
|
+
public deallocateTX: TransactionToken<(claim: ValidKey<Vassal<H>>) => void>
|
|
23
27
|
public claimTX: TransactionToken<
|
|
24
28
|
<V extends Exclude<Vassal<H>, CompoundTypedKey>, A extends Above<V, H>>(
|
|
25
29
|
newProvenance: A,
|
|
26
|
-
claim:
|
|
30
|
+
claim: ValidKey<V>,
|
|
27
31
|
exclusive?: `exclusive`,
|
|
28
32
|
) => void
|
|
29
33
|
>
|
|
@@ -42,13 +46,13 @@ export class Realm<H extends Hierarchy> {
|
|
|
42
46
|
* @param key - A unique identifier for the new subject
|
|
43
47
|
* @param attachmentStyle - The attachment style of new subject to its owner(s). `any` means that if any owners remain, the subject will be retained. `all` means that the subject be retained only if all owners remain .
|
|
44
48
|
* @returns
|
|
45
|
-
* The subject's key, given status as a true {@link
|
|
49
|
+
* The subject's key, given status as a true {@link ValidKey}
|
|
46
50
|
*/
|
|
47
51
|
public allocate<V extends Vassal<H>, A extends Above<V, H>>(
|
|
48
52
|
provenance: A,
|
|
49
53
|
key: V,
|
|
50
54
|
attachmentStyle?: `all` | `any`,
|
|
51
|
-
):
|
|
55
|
+
): ValidKey<V> {
|
|
52
56
|
return allocateIntoStore<H, V, A>(
|
|
53
57
|
this.store,
|
|
54
58
|
provenance,
|
|
@@ -62,7 +66,7 @@ export class Realm<H extends Hierarchy> {
|
|
|
62
66
|
* @param reagentA - the left reagent of the compound
|
|
63
67
|
* @param reagentB - the right reagent of the compound
|
|
64
68
|
* @returns
|
|
65
|
-
* The compound's key, given status as a
|
|
69
|
+
* The compound's key, given status as a {@link ValidKey}
|
|
66
70
|
*/
|
|
67
71
|
public fuse<
|
|
68
72
|
C extends CompoundFrom<H>,
|
|
@@ -73,14 +77,14 @@ export class Realm<H extends Hierarchy> {
|
|
|
73
77
|
type: T,
|
|
74
78
|
reagentA: SingularTypedKey<A>,
|
|
75
79
|
reagentB: SingularTypedKey<B>,
|
|
76
|
-
):
|
|
80
|
+
): ValidKey<CompoundTypedKey<T, A, B>> {
|
|
77
81
|
return fuseWithinStore<H, C, T, A, B>(this.store, type, reagentA, reagentB)
|
|
78
82
|
}
|
|
79
83
|
/**
|
|
80
84
|
* Remove a subject from the realm
|
|
81
85
|
* @param claim - The subject to be deallocated
|
|
82
86
|
*/
|
|
83
|
-
public deallocate<V extends Vassal<H>>(claim:
|
|
87
|
+
public deallocate<V extends Vassal<H>>(claim: ValidKey<V>): void {
|
|
84
88
|
actUponStore(this.store, this.deallocateTX, arbitrary())(claim)
|
|
85
89
|
}
|
|
86
90
|
/**
|
|
@@ -89,12 +93,12 @@ export class Realm<H extends Hierarchy> {
|
|
|
89
93
|
* @param claim - The subject to be claimed
|
|
90
94
|
* @param exclusive - Whether the subjects previous owners should be detached from it
|
|
91
95
|
* @returns
|
|
92
|
-
* The subject's key, given status as a true {@link
|
|
96
|
+
* The subject's key, given status as a true {@link ValidKey}
|
|
93
97
|
*/
|
|
94
98
|
public claim<
|
|
95
99
|
V extends Exclude<Vassal<H>, CompoundTypedKey>,
|
|
96
100
|
A extends Above<V, H>,
|
|
97
|
-
>(newProvenance: A, claim:
|
|
101
|
+
>(newProvenance: A, claim: ValidKey<V>, exclusive?: `exclusive`): void {
|
|
98
102
|
actUponStore(this.store, this.claimTX, arbitrary())(
|
|
99
103
|
newProvenance,
|
|
100
104
|
claim,
|
|
@@ -157,10 +161,47 @@ export class Anarchy {
|
|
|
157
161
|
): void {
|
|
158
162
|
claimWithinStore<any, any, any>(this.store, newProvenance, key, exclusive)
|
|
159
163
|
}
|
|
164
|
+
/**
|
|
165
|
+
* Fuse two reagents into a compound
|
|
166
|
+
* @param type - the name of the compound that is being fused
|
|
167
|
+
* @param reagentA - the left reagent of the compound
|
|
168
|
+
* @param reagentB - the right reagent of the compound
|
|
169
|
+
* @returns
|
|
170
|
+
* The compound's key, given status as a {@link ValidKey}
|
|
171
|
+
*/
|
|
172
|
+
public fuse<T extends string, A extends string, B extends string>(
|
|
173
|
+
type: T,
|
|
174
|
+
reagentA: SingularTypedKey<A>,
|
|
175
|
+
reagentB: SingularTypedKey<B>,
|
|
176
|
+
): ValidKey<CompoundTypedKey<T, A, B>> {
|
|
177
|
+
return fuseWithinStore<any, any, T, A, B>(
|
|
178
|
+
this.store,
|
|
179
|
+
type,
|
|
180
|
+
reagentA,
|
|
181
|
+
reagentB,
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function decomposeCompound(
|
|
187
|
+
compound: Canonical,
|
|
188
|
+
): [type: string, a: string, b: string] | null {
|
|
189
|
+
if ((typeof compound === `string`) === false) {
|
|
190
|
+
return null
|
|
191
|
+
}
|
|
192
|
+
const [typeTag, components] = compound.split(`==`)
|
|
193
|
+
if (!components) {
|
|
194
|
+
return null
|
|
195
|
+
}
|
|
196
|
+
const type = typeTag.slice(4)
|
|
197
|
+
const [a, b] = components.split(`++`)
|
|
198
|
+
if (type && a && b) {
|
|
199
|
+
return [type, a, b]
|
|
200
|
+
}
|
|
201
|
+
return null
|
|
160
202
|
}
|
|
161
203
|
|
|
162
|
-
export
|
|
163
|
-
export type T$ = typeof T$
|
|
204
|
+
export type T$ = `T$`
|
|
164
205
|
export type TypeTag<T extends string> = `${T$}--${T}`
|
|
165
206
|
export type SingularTypedKey<T extends string = string> = `${T}::${string}`
|
|
166
207
|
export type CompoundTypedKey<
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { AtomEffect } from "atom.io"
|
|
2
|
+
import { getUpdateToken, subscribeInStore } from "atom.io/internal"
|
|
3
|
+
import { type primitive, stringifyJson } from "atom.io/json"
|
|
4
|
+
|
|
5
|
+
import { UList } from "./u-list"
|
|
6
|
+
|
|
7
|
+
export const uListDisposedKeyCleanupEffect: AtomEffect<UList<primitive>> = ({
|
|
8
|
+
token,
|
|
9
|
+
setSelf,
|
|
10
|
+
store,
|
|
11
|
+
}) => {
|
|
12
|
+
const disposalSubscriptions = new Map<primitive, () => void>()
|
|
13
|
+
const updateToken = getUpdateToken(token)
|
|
14
|
+
subscribeInStore(
|
|
15
|
+
store,
|
|
16
|
+
updateToken,
|
|
17
|
+
function setAutoDeletionTriggers({ newValue }) {
|
|
18
|
+
const unpacked = UList.unpackUpdate(newValue)
|
|
19
|
+
switch (unpacked.type) {
|
|
20
|
+
case `add`:
|
|
21
|
+
{
|
|
22
|
+
const molecule = store.molecules.get(stringifyJson(unpacked.value))
|
|
23
|
+
if (molecule) {
|
|
24
|
+
disposalSubscriptions.set(
|
|
25
|
+
unpacked.value,
|
|
26
|
+
molecule.subject.subscribe(token.key, () => {
|
|
27
|
+
setSelf((self) => {
|
|
28
|
+
self.delete(unpacked.value)
|
|
29
|
+
return self
|
|
30
|
+
})
|
|
31
|
+
}),
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
break
|
|
36
|
+
case `delete`:
|
|
37
|
+
disposalSubscriptions.get(unpacked.value)?.()
|
|
38
|
+
disposalSubscriptions.delete(unpacked.value)
|
|
39
|
+
break
|
|
40
|
+
case `clear`:
|
|
41
|
+
for (const unsub of disposalSubscriptions.values()) unsub()
|
|
42
|
+
disposalSubscriptions.clear()
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
`set-auto-deletion-triggers`,
|
|
46
|
+
)
|
|
47
|
+
}
|