atom.io 0.24.7 → 0.25.0
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/data/dist/index.cjs +40 -62
- package/data/dist/index.d.ts +3 -3
- package/data/dist/index.js +41 -63
- package/data/src/join.ts +47 -64
- package/dist/index.cjs +26 -8
- package/dist/index.d.ts +37 -15
- package/dist/index.js +27 -9
- package/internal/dist/index.cjs +224 -117
- package/internal/dist/index.d.ts +12 -7
- package/internal/dist/index.js +225 -118
- package/internal/src/families/dispose-from-store.ts +56 -3
- package/internal/src/get-state/get-from-store.ts +58 -25
- package/internal/src/molecule/make-molecule-in-store.ts +59 -23
- package/internal/src/not-found-error.ts +29 -6
- package/internal/src/selector/create-readonly-selector.ts +7 -2
- package/internal/src/selector/create-writable-selector.ts +7 -5
- package/internal/src/selector/register-selector.ts +59 -9
- package/internal/src/selector/trace-selector-atoms.ts +6 -8
- package/internal/src/selector/update-selector-atoms.ts +3 -1
- package/internal/src/set-state/set-into-store.ts +48 -1
- package/internal/src/store/store.ts +4 -4
- package/internal/src/transaction/build-transaction.ts +10 -9
- package/internal/src/transaction/create-transaction.ts +2 -2
- package/internal/src/transaction/index.ts +2 -2
- package/package.json +6 -6
- package/react/dist/index.cjs +4 -1
- package/react/dist/index.js +5 -2
- package/react/src/use-o.ts +11 -3
- package/realtime/dist/index.cjs +0 -1
- package/realtime/dist/index.js +0 -1
- package/realtime/src/realtime-continuity.ts +0 -1
- package/realtime-server/dist/index.cjs +7 -7
- package/realtime-server/dist/index.js +7 -7
- package/realtime-server/src/realtime-server-stores/server-room-external-actions.ts +7 -7
- package/src/dispose-state.ts +32 -3
- package/src/get-state.ts +37 -4
- package/src/molecule.ts +20 -7
- package/src/set-state.ts +19 -2
- package/src/silo.ts +11 -4
- package/src/transaction.ts +9 -17
|
@@ -1,39 +1,72 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
MoleculeConstructor,
|
|
3
|
+
MoleculeFamilyToken,
|
|
4
|
+
MoleculeKey,
|
|
5
|
+
MoleculeToken,
|
|
6
|
+
ReadableFamilyToken,
|
|
7
|
+
ReadableToken,
|
|
8
|
+
} from "atom.io"
|
|
9
|
+
import type { Json } from "atom.io/json"
|
|
2
10
|
|
|
11
|
+
import { findInStore, seekInStore } from "../families"
|
|
12
|
+
import { NotFoundError } from "../not-found-error"
|
|
3
13
|
import type { Store } from "../store"
|
|
4
14
|
import { withdraw } from "../store"
|
|
5
15
|
import { readOrComputeValue } from "./read-or-compute-value"
|
|
6
16
|
|
|
7
17
|
export function getFromStore<T>(token: ReadableToken<T>, store: Store): T
|
|
18
|
+
|
|
8
19
|
export function getFromStore<M extends MoleculeConstructor>(
|
|
9
20
|
token: MoleculeToken<M>,
|
|
10
21
|
store: Store,
|
|
11
22
|
): InstanceType<M> | undefined
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
>(token: Token, store: Store): InstanceType<M> | T | undefined
|
|
17
|
-
export function getFromStore<
|
|
18
|
-
Token extends MoleculeToken<any> | ReadableToken<any>,
|
|
19
|
-
>(
|
|
20
|
-
token: Token,
|
|
23
|
+
|
|
24
|
+
export function getFromStore<T, K extends Json.Serializable>(
|
|
25
|
+
token: ReadableFamilyToken<T, K>,
|
|
26
|
+
key: K,
|
|
21
27
|
store: Store,
|
|
22
|
-
):
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
28
|
+
): T
|
|
29
|
+
|
|
30
|
+
export function getFromStore<M extends MoleculeConstructor>(
|
|
31
|
+
token: MoleculeFamilyToken<M>,
|
|
32
|
+
key: MoleculeKey<M>,
|
|
33
|
+
store: Store,
|
|
34
|
+
): InstanceType<M>
|
|
35
|
+
|
|
36
|
+
export function getFromStore<T>(
|
|
37
|
+
...params:
|
|
38
|
+
| [token: MoleculeFamilyToken<any>, key: MoleculeKey<any>, store: Store]
|
|
39
|
+
| [token: MoleculeToken<any>, store: Store]
|
|
40
|
+
| [token: ReadableFamilyToken<T, any>, key: Json.Serializable, store: Store]
|
|
41
|
+
| [token: ReadableToken<T>, store: Store]
|
|
42
|
+
): any {
|
|
43
|
+
let token: MoleculeToken<any> | ReadableToken<T>
|
|
44
|
+
let store: Store
|
|
45
|
+
if (params.length === 2) {
|
|
46
|
+
token = params[0]
|
|
47
|
+
store = params[1]
|
|
48
|
+
} else {
|
|
49
|
+
const family = params[0]
|
|
50
|
+
const key = params[1]
|
|
51
|
+
store = params[2]
|
|
52
|
+
const maybeToken =
|
|
53
|
+
family.type === `molecule_family`
|
|
54
|
+
? seekInStore(family, key, store)
|
|
55
|
+
: store.config.lifespan === `immortal`
|
|
56
|
+
? seekInStore(family, key, store)
|
|
57
|
+
: findInStore(family, key, store)
|
|
58
|
+
if (!maybeToken) {
|
|
59
|
+
throw new NotFoundError(family, key, store)
|
|
35
60
|
}
|
|
61
|
+
token = maybeToken
|
|
62
|
+
}
|
|
63
|
+
switch (token.type) {
|
|
64
|
+
case `atom`:
|
|
65
|
+
case `mutable_atom`:
|
|
66
|
+
case `selector`:
|
|
67
|
+
case `readonly_selector`:
|
|
68
|
+
return readOrComputeValue(withdraw(token, store), store)
|
|
69
|
+
case `molecule`:
|
|
70
|
+
return withdraw(token, store).instance
|
|
36
71
|
}
|
|
37
|
-
const state = withdraw(token, store)
|
|
38
|
-
return readOrComputeValue(state, store)
|
|
39
72
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
CtorToolkit,
|
|
2
3
|
MK,
|
|
3
4
|
MoleculeConstructor,
|
|
4
5
|
MoleculeCreation,
|
|
@@ -6,10 +7,9 @@ import type {
|
|
|
6
7
|
MoleculeKey,
|
|
7
8
|
MoleculeParams,
|
|
8
9
|
MoleculeToken,
|
|
9
|
-
MoleculeTransactors,
|
|
10
10
|
ReadableFamilyToken,
|
|
11
11
|
} from "atom.io"
|
|
12
|
-
import { getJoin, type JoinToken } from "atom.io/data"
|
|
12
|
+
import { findRelations, getJoin, type JoinToken } from "atom.io/data"
|
|
13
13
|
import type { seekState } from "atom.io/immortal"
|
|
14
14
|
import { stringifyJson } from "atom.io/json"
|
|
15
15
|
|
|
@@ -26,6 +26,10 @@ import { actUponStore, isChildStore, isRootStore } from "../transaction"
|
|
|
26
26
|
import { growMoleculeInStore } from "./grow-molecule-in-store"
|
|
27
27
|
import { Molecule } from "./molecule-internal"
|
|
28
28
|
|
|
29
|
+
function capitalize<S extends string>(string: S): Capitalize<S> {
|
|
30
|
+
return (string[0].toUpperCase() + string.slice(1)) as Capitalize<S>
|
|
31
|
+
}
|
|
32
|
+
|
|
29
33
|
export function makeMoleculeInStore<M extends MoleculeConstructor>(
|
|
30
34
|
store: Store,
|
|
31
35
|
context: MoleculeToken<M> | MoleculeToken<M>[],
|
|
@@ -34,33 +38,34 @@ export function makeMoleculeInStore<M extends MoleculeConstructor>(
|
|
|
34
38
|
...params: MoleculeParams<M>
|
|
35
39
|
): MoleculeToken<M> {
|
|
36
40
|
const target = newest(store)
|
|
41
|
+
const stringKey = stringifyJson(key)
|
|
37
42
|
|
|
38
|
-
target.moleculeInProgress =
|
|
43
|
+
target.moleculeInProgress = stringKey
|
|
39
44
|
|
|
40
45
|
const contextArray = Array.isArray(context) ? context : [context]
|
|
41
46
|
const owners = contextArray.map<Molecule<M>>((ctx) => {
|
|
42
47
|
if (ctx instanceof Molecule) {
|
|
43
48
|
return ctx
|
|
44
49
|
}
|
|
45
|
-
const
|
|
46
|
-
const molecule = store.molecules.get(
|
|
50
|
+
const ctxStringKey = stringifyJson(ctx.key)
|
|
51
|
+
const molecule = store.molecules.get(ctxStringKey)
|
|
47
52
|
|
|
48
53
|
if (!molecule) {
|
|
49
54
|
throw new Error(
|
|
50
|
-
`Molecule ${
|
|
55
|
+
`Molecule ${ctxStringKey} not found in store "${store.config.name}"`,
|
|
51
56
|
)
|
|
52
57
|
}
|
|
53
58
|
return molecule
|
|
54
59
|
})
|
|
55
60
|
|
|
56
61
|
const molecule = new Molecule(owners, key, familyToken)
|
|
57
|
-
target.molecules.set(
|
|
62
|
+
target.molecules.set(stringKey, molecule)
|
|
58
63
|
for (const owner of owners) {
|
|
59
64
|
owner.below.set(molecule.stringKey, molecule)
|
|
60
65
|
}
|
|
61
66
|
|
|
62
|
-
const
|
|
63
|
-
get: (t) => getFromStore(t, newest(store)),
|
|
67
|
+
const toolkit = {
|
|
68
|
+
get: (t) => getFromStore(t, undefined, newest(store)),
|
|
64
69
|
set: (t, newValue) => {
|
|
65
70
|
setIntoStore(t, newValue, newest(store))
|
|
66
71
|
},
|
|
@@ -73,12 +78,49 @@ export function makeMoleculeInStore<M extends MoleculeConstructor>(
|
|
|
73
78
|
disposeFromStore(t, newest(store))
|
|
74
79
|
},
|
|
75
80
|
env: () => getEnvironmentData(newest(store)),
|
|
76
|
-
bond: ((
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
bond: ((
|
|
82
|
+
token: JoinToken<any, any, any, any> | ReadableFamilyToken<any, any>,
|
|
83
|
+
maybeRole,
|
|
84
|
+
) => {
|
|
85
|
+
if (token.type === `join`) {
|
|
86
|
+
const { as: role } = maybeRole
|
|
87
|
+
const join = getJoin(token, store)
|
|
88
|
+
join.molecules.set(stringKey, molecule)
|
|
89
|
+
molecule.joins.set(token.key, join)
|
|
90
|
+
const unsubFromFamily = family.subject.subscribe(
|
|
91
|
+
`join:${token.key}-${stringKey}`,
|
|
92
|
+
(event) => {
|
|
93
|
+
if (
|
|
94
|
+
event.type === `molecule_disposal` &&
|
|
95
|
+
stringifyJson(event.token.key) === stringKey
|
|
96
|
+
) {
|
|
97
|
+
unsubFromFamily()
|
|
98
|
+
join.molecules.delete(stringKey)
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
if (role === null) {
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
const otherRole = token.a === role ? token.b : token.a
|
|
107
|
+
const relations = findRelations(token, key)
|
|
108
|
+
const relatedKeys =
|
|
109
|
+
relations[
|
|
110
|
+
`${otherRole}KeysOf${capitalize(role)}` as keyof typeof relations
|
|
111
|
+
]
|
|
112
|
+
const relatedEntries =
|
|
113
|
+
relations[
|
|
114
|
+
`${otherRole}EntriesOf${capitalize(role)}` as keyof typeof relations
|
|
115
|
+
]
|
|
116
|
+
let tokens = { relatedKeys }
|
|
117
|
+
if (relatedEntries) {
|
|
118
|
+
tokens = Object.assign(tokens, { relatedEntries })
|
|
119
|
+
}
|
|
120
|
+
return tokens
|
|
121
|
+
}
|
|
122
|
+
return growMoleculeInStore(molecule, withdraw(token, store), newest(store))
|
|
123
|
+
}) as CtorToolkit<MK<M>>[`bond`],
|
|
82
124
|
claim: (below, options) => {
|
|
83
125
|
const { exclusive } = options
|
|
84
126
|
const belowMolecule = newest(store).molecules.get(stringifyJson(below.key))
|
|
@@ -96,12 +138,6 @@ export function makeMoleculeInStore<M extends MoleculeConstructor>(
|
|
|
96
138
|
}
|
|
97
139
|
}
|
|
98
140
|
},
|
|
99
|
-
join: <J extends JoinToken<any, any, any, any>>(joinToken: J) => {
|
|
100
|
-
const join = getJoin(joinToken, store)
|
|
101
|
-
join.molecules.set(stringifyJson(key), molecule)
|
|
102
|
-
molecule.joins.set(joinToken.key, join)
|
|
103
|
-
return joinToken
|
|
104
|
-
},
|
|
105
141
|
spawn: (f: MoleculeFamilyToken<any>, k: any, ...p: any[]) =>
|
|
106
142
|
makeMoleculeInStore(
|
|
107
143
|
newest(store),
|
|
@@ -110,12 +146,12 @@ export function makeMoleculeInStore<M extends MoleculeConstructor>(
|
|
|
110
146
|
k,
|
|
111
147
|
...p,
|
|
112
148
|
),
|
|
113
|
-
} satisfies
|
|
149
|
+
} satisfies CtorToolkit<MK<M>>
|
|
114
150
|
|
|
115
151
|
const family = withdraw(familyToken, store)
|
|
116
152
|
const Constructor = family.new
|
|
117
153
|
|
|
118
|
-
molecule.instance = new Constructor(
|
|
154
|
+
molecule.instance = new Constructor(toolkit, key, ...params)
|
|
119
155
|
|
|
120
156
|
const token = {
|
|
121
157
|
type: `molecule`,
|
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
TimelineToken,
|
|
7
7
|
TransactionToken,
|
|
8
8
|
} from "atom.io"
|
|
9
|
+
import { type Json, stringifyJson } from "atom.io/json"
|
|
9
10
|
|
|
10
11
|
import type { Store } from "./store"
|
|
11
12
|
|
|
@@ -37,11 +38,33 @@ function prettyPrintTokenType(token: AtomIOToken) {
|
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
export class NotFoundError extends Error {
|
|
40
|
-
public constructor(token: AtomIOToken, store: Store)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
public constructor(token: AtomIOToken, store: Store)
|
|
42
|
+
public constructor(
|
|
43
|
+
familyToken: AtomIOToken,
|
|
44
|
+
key: Json.Serializable,
|
|
45
|
+
store: Store,
|
|
46
|
+
)
|
|
47
|
+
public constructor(
|
|
48
|
+
...params:
|
|
49
|
+
| [token: AtomIOToken, key: Json.Serializable, store: Store]
|
|
50
|
+
| [token: AtomIOToken, store: Store]
|
|
51
|
+
) {
|
|
52
|
+
const token: AtomIOToken = params[0]
|
|
53
|
+
const store: Store = params.length === 2 ? params[1] : params[2]
|
|
54
|
+
|
|
55
|
+
if (params.length === 2) {
|
|
56
|
+
super(
|
|
57
|
+
`${prettyPrintTokenType(token)} "${token.key}" not found in store "${
|
|
58
|
+
store.config.name
|
|
59
|
+
}".`,
|
|
60
|
+
)
|
|
61
|
+
} else {
|
|
62
|
+
const key = params[1]
|
|
63
|
+
super(
|
|
64
|
+
`${prettyPrintTokenType(token)} "${token.key}" member ${stringifyJson(key)} not found in store "${
|
|
65
|
+
store.config.name
|
|
66
|
+
}".`,
|
|
67
|
+
)
|
|
68
|
+
}
|
|
46
69
|
}
|
|
47
70
|
}
|
|
@@ -18,11 +18,16 @@ export const createReadonlySelector = <T>(
|
|
|
18
18
|
): ReadonlySelectorToken<T> => {
|
|
19
19
|
const target = newest(store)
|
|
20
20
|
const subject = new Subject<{ newValue: T; oldValue: T }>()
|
|
21
|
-
|
|
22
|
-
const { get, find, seek, json } = registerSelector(
|
|
21
|
+
const covered = new Set<string>()
|
|
22
|
+
const { get, find, seek, json } = registerSelector(
|
|
23
|
+
options.key,
|
|
24
|
+
covered,
|
|
25
|
+
target,
|
|
26
|
+
)
|
|
23
27
|
const getSelf = () => {
|
|
24
28
|
const value = options.get({ get, find, seek, json })
|
|
25
29
|
cacheValue(options.key, value, subject, newest(store))
|
|
30
|
+
covered.clear()
|
|
26
31
|
return value
|
|
27
32
|
}
|
|
28
33
|
|
|
@@ -21,13 +21,15 @@ export const createWritableSelector = <T>(
|
|
|
21
21
|
): WritableSelectorToken<T> => {
|
|
22
22
|
const target = newest(store)
|
|
23
23
|
const subject = new Subject<{ newValue: T; oldValue: T }>()
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
24
|
+
const covered = new Set<string>()
|
|
25
|
+
const toolkit = registerSelector(options.key, covered, target)
|
|
26
|
+
const { find, get, seek, json } = toolkit
|
|
27
|
+
const getterToolkit = { find, get, seek, json }
|
|
27
28
|
|
|
28
29
|
const getSelf = (innerTarget = newest(store)): T => {
|
|
29
|
-
const value = options.get(
|
|
30
|
+
const value = options.get(getterToolkit)
|
|
30
31
|
cacheValue(options.key, value, subject, innerTarget)
|
|
32
|
+
covered.clear()
|
|
31
33
|
return value
|
|
32
34
|
}
|
|
33
35
|
|
|
@@ -50,7 +52,7 @@ export const createWritableSelector = <T>(
|
|
|
50
52
|
if (isRootStore(innerTarget)) {
|
|
51
53
|
subject.next({ newValue, oldValue })
|
|
52
54
|
}
|
|
53
|
-
options.set(
|
|
55
|
+
options.set(toolkit, newValue)
|
|
54
56
|
}
|
|
55
57
|
const mySelector: WritableSelector<T> = {
|
|
56
58
|
...options,
|
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
MoleculeConstructor,
|
|
3
|
+
MoleculeFamilyToken,
|
|
3
4
|
MoleculeToken,
|
|
5
|
+
ReadableFamilyToken,
|
|
4
6
|
ReadableToken,
|
|
5
|
-
|
|
7
|
+
setState,
|
|
8
|
+
SetterToolkit,
|
|
9
|
+
WritableSelectorFamilyToken,
|
|
10
|
+
WritableToken,
|
|
6
11
|
} from "atom.io"
|
|
7
12
|
import type { findState } from "atom.io/ephemeral"
|
|
8
13
|
import type { seekState } from "atom.io/immortal"
|
|
14
|
+
import type { Json } from "atom.io/json"
|
|
9
15
|
|
|
10
16
|
import { findInStore, seekInStore } from "../families"
|
|
11
17
|
import { getFromStore } from "../get-state"
|
|
12
18
|
import { readOrComputeValue } from "../get-state/read-or-compute-value"
|
|
13
19
|
import { newest } from "../lineage"
|
|
14
20
|
import { getJsonToken } from "../mutable"
|
|
21
|
+
import { NotFoundError } from "../not-found-error"
|
|
15
22
|
import { setAtomOrSelector } from "../set-state"
|
|
16
23
|
import type { Store } from "../store"
|
|
17
24
|
import { withdraw } from "../store"
|
|
@@ -19,16 +26,33 @@ import { updateSelectorAtoms } from "./update-selector-atoms"
|
|
|
19
26
|
|
|
20
27
|
export const registerSelector = (
|
|
21
28
|
selectorKey: string,
|
|
29
|
+
covered: Set<string>,
|
|
22
30
|
store: Store,
|
|
23
|
-
):
|
|
24
|
-
get: (
|
|
31
|
+
): SetterToolkit => ({
|
|
32
|
+
get: (
|
|
33
|
+
dependency:
|
|
34
|
+
| MoleculeFamilyToken<any>
|
|
35
|
+
| MoleculeToken<MoleculeConstructor>
|
|
36
|
+
| ReadableFamilyToken<any, any>
|
|
37
|
+
| ReadableToken<any>,
|
|
38
|
+
key?: Json.Serializable,
|
|
39
|
+
) => {
|
|
25
40
|
const target = newest(store)
|
|
26
41
|
|
|
42
|
+
if (key) {
|
|
43
|
+
switch (dependency.type) {
|
|
44
|
+
case `molecule_family`:
|
|
45
|
+
return getFromStore(dependency, key, store)
|
|
46
|
+
case `atom_family`:
|
|
47
|
+
dependency = seekInStore(dependency, key, store) as any
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
27
51
|
if (dependency.type === `molecule`) {
|
|
28
52
|
return getFromStore(dependency, store)
|
|
29
53
|
}
|
|
30
54
|
|
|
31
|
-
const dependencyState = withdraw(dependency
|
|
55
|
+
const dependencyState = withdraw(dependency as ReadableToken<any>, store)
|
|
32
56
|
const dependencyValue = readOrComputeValue(dependencyState, store)
|
|
33
57
|
|
|
34
58
|
store.logger.info(
|
|
@@ -49,14 +73,40 @@ export const registerSelector = (
|
|
|
49
73
|
source: dependency.key,
|
|
50
74
|
},
|
|
51
75
|
)
|
|
52
|
-
updateSelectorAtoms(selectorKey, dependency, store)
|
|
76
|
+
updateSelectorAtoms(selectorKey, dependency as any, covered, store)
|
|
53
77
|
return dependencyValue
|
|
54
78
|
},
|
|
55
|
-
set: (
|
|
79
|
+
set: (<T, New extends T>(
|
|
80
|
+
...params:
|
|
81
|
+
| [
|
|
82
|
+
token: WritableSelectorFamilyToken<T, any>,
|
|
83
|
+
key: Json.Serializable,
|
|
84
|
+
value: New | ((oldValue: any) => New),
|
|
85
|
+
]
|
|
86
|
+
| [token: WritableToken<T>, value: New | ((oldValue: T) => New)]
|
|
87
|
+
) => {
|
|
88
|
+
let token: WritableToken<T>
|
|
89
|
+
let value: New | ((oldValue: T) => New)
|
|
90
|
+
if (params.length === 2) {
|
|
91
|
+
token = params[0]
|
|
92
|
+
value = params[1]
|
|
93
|
+
} else {
|
|
94
|
+
const family = params[0]
|
|
95
|
+
const key = params[1]
|
|
96
|
+
value = params[2]
|
|
97
|
+
const maybeToken =
|
|
98
|
+
store.config.lifespan === `ephemeral`
|
|
99
|
+
? findInStore(family, key, store)
|
|
100
|
+
: seekInStore(family, key, store)
|
|
101
|
+
if (!maybeToken) {
|
|
102
|
+
throw new NotFoundError(family, key, store)
|
|
103
|
+
}
|
|
104
|
+
token = maybeToken
|
|
105
|
+
}
|
|
56
106
|
const target = newest(store)
|
|
57
|
-
const state = withdraw(
|
|
58
|
-
setAtomOrSelector(state,
|
|
59
|
-
},
|
|
107
|
+
const state = withdraw(token, target)
|
|
108
|
+
setAtomOrSelector(state, value, target)
|
|
109
|
+
}) as typeof setState,
|
|
60
110
|
find: ((token, key) => findInStore(token, key, store)) as typeof findState,
|
|
61
111
|
seek: ((token, key) => seekInStore(token, key, store)) as typeof seekState,
|
|
62
112
|
json: (token) => getJsonToken(token, store),
|
|
@@ -4,8 +4,8 @@ import { isAtomKey } from "../keys"
|
|
|
4
4
|
import { getSelectorDependencyKeys } from "./get-selector-dependency-keys"
|
|
5
5
|
|
|
6
6
|
export const traceSelectorAtoms = (
|
|
7
|
-
selectorKey: string,
|
|
8
7
|
directDependencyKey: StateKey<unknown>,
|
|
8
|
+
covered: Set<string>,
|
|
9
9
|
store: Store,
|
|
10
10
|
): AtomKey<unknown>[] => {
|
|
11
11
|
const rootKeys: AtomKey<unknown>[] = []
|
|
@@ -14,16 +14,13 @@ export const traceSelectorAtoms = (
|
|
|
14
14
|
directDependencyKey,
|
|
15
15
|
store,
|
|
16
16
|
)
|
|
17
|
-
let depth = 0
|
|
18
17
|
while (indirectDependencyKeys.length > 0) {
|
|
19
18
|
// biome-ignore lint/style/noNonNullAssertion: just checked length ^^^
|
|
20
19
|
const indirectDependencyKey = indirectDependencyKeys.shift()!
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
throw new Error(
|
|
24
|
-
`Maximum selector dependency depth exceeded (> 99999) in selector "${selectorKey}". This is likely due to a circular dependency.`,
|
|
25
|
-
)
|
|
20
|
+
if (covered.has(indirectDependencyKey)) {
|
|
21
|
+
continue
|
|
26
22
|
}
|
|
23
|
+
covered.add(indirectDependencyKey)
|
|
27
24
|
|
|
28
25
|
if (!isAtomKey(indirectDependencyKey, store)) {
|
|
29
26
|
indirectDependencyKeys.push(
|
|
@@ -43,9 +40,10 @@ export const traceAllSelectorAtoms = (
|
|
|
43
40
|
): AtomKey<unknown>[] => {
|
|
44
41
|
const selectorKey = selector.key
|
|
45
42
|
const directDependencyKeys = getSelectorDependencyKeys(selectorKey, store)
|
|
43
|
+
const covered = new Set<string>()
|
|
46
44
|
return directDependencyKeys.flatMap((depKey) =>
|
|
47
45
|
isAtomKey(depKey, store)
|
|
48
46
|
? depKey
|
|
49
|
-
: traceSelectorAtoms(
|
|
47
|
+
: traceSelectorAtoms(depKey, covered, store),
|
|
50
48
|
)
|
|
51
49
|
}
|
|
@@ -7,6 +7,7 @@ import { traceSelectorAtoms } from "./trace-selector-atoms"
|
|
|
7
7
|
export const updateSelectorAtoms = (
|
|
8
8
|
selectorKey: string,
|
|
9
9
|
dependency: ReadonlySelectorToken<unknown> | WritableToken<unknown>,
|
|
10
|
+
covered: Set<string>,
|
|
10
11
|
store: Store,
|
|
11
12
|
): void => {
|
|
12
13
|
const target = newest(store)
|
|
@@ -22,7 +23,7 @@ export const updateSelectorAtoms = (
|
|
|
22
23
|
`discovers root atom "${dependency.key}"`,
|
|
23
24
|
)
|
|
24
25
|
} else {
|
|
25
|
-
const rootKeys = traceSelectorAtoms(
|
|
26
|
+
const rootKeys = traceSelectorAtoms(dependency.key, covered, store)
|
|
26
27
|
store.logger.info(
|
|
27
28
|
`🔍`,
|
|
28
29
|
`selector`,
|
|
@@ -38,4 +39,5 @@ export const updateSelectorAtoms = (
|
|
|
38
39
|
})
|
|
39
40
|
}
|
|
40
41
|
}
|
|
42
|
+
covered.add(dependency.key)
|
|
41
43
|
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import type { WritableToken } from "atom.io"
|
|
1
|
+
import type { WritableFamilyToken, WritableToken } from "atom.io"
|
|
2
|
+
import type { Json } from "atom.io/json"
|
|
2
3
|
|
|
4
|
+
import { findInStore, seekInStore } from "../families"
|
|
5
|
+
import { NotFoundError } from "../not-found-error"
|
|
3
6
|
import { closeOperation, openOperation } from "../operation"
|
|
4
7
|
import type { Store } from "../store"
|
|
5
8
|
import { withdraw } from "../store"
|
|
@@ -9,7 +12,51 @@ export function setIntoStore<T, New extends T>(
|
|
|
9
12
|
token: WritableToken<T>,
|
|
10
13
|
value: New | ((oldValue: T) => New),
|
|
11
14
|
store: Store,
|
|
15
|
+
): void
|
|
16
|
+
|
|
17
|
+
export function setIntoStore<T, K extends Json.Serializable, New extends T>(
|
|
18
|
+
token: WritableFamilyToken<T, K>,
|
|
19
|
+
key: K,
|
|
20
|
+
value: New | ((oldValue: T) => New),
|
|
21
|
+
store: Store,
|
|
22
|
+
): void
|
|
23
|
+
|
|
24
|
+
export function setIntoStore<T, New extends T>(
|
|
25
|
+
...params:
|
|
26
|
+
| [
|
|
27
|
+
token: WritableFamilyToken<T, Json.Serializable>,
|
|
28
|
+
key: Json.Serializable,
|
|
29
|
+
value: New | ((oldValue: T) => New),
|
|
30
|
+
store: Store,
|
|
31
|
+
]
|
|
32
|
+
| [
|
|
33
|
+
token: WritableToken<T>,
|
|
34
|
+
value: New | ((oldValue: T) => New),
|
|
35
|
+
store: Store,
|
|
36
|
+
]
|
|
12
37
|
): void {
|
|
38
|
+
let token: WritableToken<T>
|
|
39
|
+
let value: New | ((oldValue: T) => New)
|
|
40
|
+
let store: Store
|
|
41
|
+
if (params.length === 3) {
|
|
42
|
+
token = params[0]
|
|
43
|
+
value = params[1]
|
|
44
|
+
store = params[2]
|
|
45
|
+
} else {
|
|
46
|
+
const family = params[0]
|
|
47
|
+
const key = params[1]
|
|
48
|
+
value = params[2]
|
|
49
|
+
store = params[3]
|
|
50
|
+
const maybeToken =
|
|
51
|
+
store.config.lifespan === `ephemeral`
|
|
52
|
+
? findInStore(family, key, store)
|
|
53
|
+
: seekInStore(family, key, store)
|
|
54
|
+
if (!maybeToken) {
|
|
55
|
+
throw new NotFoundError(family, key, store)
|
|
56
|
+
}
|
|
57
|
+
token = maybeToken
|
|
58
|
+
}
|
|
59
|
+
|
|
13
60
|
const rejectionTime = openOperation(token, store)
|
|
14
61
|
if (rejectionTime) {
|
|
15
62
|
const unsubscribe = store.on.operationClose.subscribe(
|
|
@@ -143,6 +143,10 @@ export class Store implements Lineage {
|
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
public constructor(config: Store[`config`], store: Store | null = null) {
|
|
146
|
+
this.config = {
|
|
147
|
+
...store?.config,
|
|
148
|
+
...config,
|
|
149
|
+
}
|
|
146
150
|
if (store !== null) {
|
|
147
151
|
this.valueMap = new Map(store?.valueMap)
|
|
148
152
|
this.operation = { ...store?.operation }
|
|
@@ -155,10 +159,6 @@ export class Store implements Lineage {
|
|
|
155
159
|
}
|
|
156
160
|
}
|
|
157
161
|
|
|
158
|
-
this.config = {
|
|
159
|
-
...store?.config,
|
|
160
|
-
...config,
|
|
161
|
-
}
|
|
162
162
|
for (const [, family] of store.families) {
|
|
163
163
|
family.install(this)
|
|
164
164
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Func } from "atom.io"
|
|
1
|
+
import type { disposeState, Func, getState, setState } from "atom.io"
|
|
2
2
|
import type { findState } from "atom.io/ephemeral"
|
|
3
3
|
import type { seekState } from "atom.io/immortal"
|
|
4
4
|
|
|
@@ -64,11 +64,12 @@ export const buildTransaction = (
|
|
|
64
64
|
params,
|
|
65
65
|
output: undefined,
|
|
66
66
|
},
|
|
67
|
-
|
|
68
|
-
get: (
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
67
|
+
toolkit: {
|
|
68
|
+
get: ((...ps: Parameters<typeof getState>) =>
|
|
69
|
+
getFromStore(...ps, child)) as typeof getState,
|
|
70
|
+
set: ((...ps: Parameters<typeof setState>) => {
|
|
71
|
+
setIntoStore(...ps, child)
|
|
72
|
+
}) as typeof setState,
|
|
72
73
|
run: (token, identifier = arbitrary()) =>
|
|
73
74
|
actUponStore(token, identifier, child),
|
|
74
75
|
find: ((token, k) => findInStore(token, k, child)) as typeof findState,
|
|
@@ -76,9 +77,9 @@ export const buildTransaction = (
|
|
|
76
77
|
json: (token) => getJsonToken(token, child),
|
|
77
78
|
make: (context, family, k, ...args) =>
|
|
78
79
|
makeMoleculeInStore(child, context, family, k, ...args),
|
|
79
|
-
dispose: (
|
|
80
|
-
disposeFromStore(
|
|
81
|
-
},
|
|
80
|
+
dispose: ((...ps: Parameters<typeof disposeState>) => {
|
|
81
|
+
disposeFromStore(...ps, child)
|
|
82
|
+
}) as typeof disposeState,
|
|
82
83
|
env: () => getEnvironmentData(child),
|
|
83
84
|
},
|
|
84
85
|
}
|
|
@@ -32,8 +32,8 @@ export function createTransaction<F extends Func>(
|
|
|
32
32
|
const childStore = buildTransaction(options.key, params, store, id)
|
|
33
33
|
try {
|
|
34
34
|
const target = newest(store)
|
|
35
|
-
const {
|
|
36
|
-
const output = options.do(
|
|
35
|
+
const { toolkit } = childStore.transactionMeta
|
|
36
|
+
const output = options.do(toolkit, ...params)
|
|
37
37
|
applyTransaction(output, target)
|
|
38
38
|
return output
|
|
39
39
|
} catch (thrown) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Func, TransactionUpdate
|
|
1
|
+
import type { ActorToolkit, Func, TransactionUpdate } from "atom.io"
|
|
2
2
|
import type { Junction } from "rel8/junction"
|
|
3
3
|
|
|
4
4
|
export * from "./abort-transaction"
|
|
@@ -17,7 +17,7 @@ export type TransactionPhase = (typeof TRANSACTION_PHASES)[number]
|
|
|
17
17
|
export type TransactionProgress<F extends Func> = {
|
|
18
18
|
phase: `applying` | `building`
|
|
19
19
|
update: TransactionUpdate<F>
|
|
20
|
-
|
|
20
|
+
toolkit: ActorToolkit
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export type TransactionEpoch = {
|