atom.io 0.21.1 → 0.23.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 +152 -63
- package/data/dist/index.d.ts +6 -0
- package/data/dist/index.js +3 -3
- package/data/src/join.ts +164 -51
- package/data/src/struct-family.ts +2 -2
- package/dist/chunk-6MLFYN32.js +18 -0
- package/dist/{chunk-HITX3MO4.js → chunk-7DT3PVS3.js} +151 -62
- package/dist/{chunk-RT43TVKP.js → chunk-GVHKIJ3G.js} +1 -1
- package/dist/chunk-OAYGID5B.js +27 -0
- package/dist/index.cjs +4 -18
- package/dist/index.d.ts +71 -28
- package/dist/index.js +6 -19
- package/ephemeral/dist/index.cjs +11 -0
- package/ephemeral/dist/index.js +9 -0
- package/ephemeral/package.json +16 -0
- package/ephemeral/src/index.ts +1 -0
- package/eslint-plugin/dist/index.cjs +155 -1
- package/eslint-plugin/dist/index.js +155 -1
- package/eslint-plugin/src/rules/index.ts +1 -0
- package/eslint-plugin/src/rules/lifespan.ts +203 -0
- package/eslint-plugin/src/rules/synchronous-selector-dependencies.ts +1 -65
- package/eslint-plugin/src/walk.ts +73 -0
- package/immortal/dist/index.cjs +260 -0
- package/immortal/dist/index.js +212 -0
- package/immortal/package.json +16 -0
- package/immortal/src/index.ts +3 -0
- package/immortal/src/make-molecule.ts +222 -0
- package/immortal/src/molecule.ts +167 -0
- package/immortal/src/seek-state.ts +73 -0
- package/internal/dist/index.cjs +1242 -837
- package/internal/dist/index.d.ts +135 -22
- package/internal/dist/index.js +1215 -838
- package/internal/src/atom/create-regular-atom.ts +0 -2
- package/internal/src/atom/create-standalone-atom.ts +6 -2
- package/internal/src/atom/dispose-atom.ts +26 -3
- package/internal/src/families/create-readonly-selector-family.ts +15 -10
- package/internal/src/families/create-regular-atom-family.ts +20 -21
- package/internal/src/families/create-writable-selector-family.ts +13 -9
- package/{src/dispose.ts → internal/src/families/dispose-from-store.ts} +7 -4
- package/internal/src/families/find-in-store.ts +11 -6
- package/internal/src/families/index.ts +3 -0
- package/internal/src/families/init-family-member.ts +112 -0
- package/internal/src/families/seek-in-store.ts +123 -0
- package/internal/src/get-state/get-from-store.ts +2 -2
- package/internal/src/ingest-updates/index.ts +1 -0
- package/internal/src/ingest-updates/ingest-creation-disposal.ts +104 -0
- package/internal/src/ingest-updates/ingest-transaction-update.ts +26 -4
- package/internal/src/mutable/create-mutable-atom-family.ts +22 -24
- package/internal/src/mutable/create-mutable-atom.ts +3 -3
- package/internal/src/mutable/get-json-family.ts +2 -2
- package/internal/src/mutable/get-json-token.ts +26 -12
- package/internal/src/mutable/tracker-family.ts +21 -19
- package/internal/src/not-found-error.ts +16 -3
- package/internal/src/selector/create-readonly-selector.ts +2 -3
- package/internal/src/selector/create-standalone-selector.ts +6 -2
- package/internal/src/selector/create-writable-selector.ts +2 -3
- package/internal/src/selector/dispose-selector.ts +68 -24
- package/internal/src/selector/register-selector.ts +10 -5
- package/internal/src/set-state/set-into-store.ts +2 -2
- package/internal/src/set-state/stow-update.ts +5 -1
- package/internal/src/store/deposit.ts +41 -7
- package/internal/src/store/index.ts +0 -1
- package/internal/src/store/store.ts +29 -5
- package/internal/src/store/withdraw.ts +28 -1
- package/internal/src/subscribe/subscribe-to-state.ts +2 -2
- package/internal/src/timeline/add-atom-to-timeline.ts +206 -182
- package/internal/src/timeline/create-timeline.ts +181 -60
- package/internal/src/timeline/time-travel.ts +20 -0
- package/internal/src/transaction/apply-transaction.ts +2 -12
- package/internal/src/transaction/build-transaction.ts +16 -2
- package/introspection/dist/index.cjs +40 -53
- package/introspection/dist/index.js +40 -53
- package/introspection/src/attach-atom-index.ts +38 -48
- package/introspection/src/attach-selector-index.ts +45 -50
- package/introspection/src/attach-timeline-family.ts +1 -0
- package/json/dist/index.cjs +40 -6
- package/json/dist/index.js +44 -9
- package/json/src/select-json-family.ts +47 -9
- package/package.json +30 -10
- package/react/dist/index.cjs +1 -1
- package/react/dist/index.js +1 -1
- package/react/src/use-json.ts +1 -1
- package/react-devtools/dist/index.cjs +69 -57
- package/react-devtools/dist/index.js +62 -49
- package/react-devtools/src/StateIndex.tsx +2 -1
- package/react-devtools/src/TimelineIndex.tsx +17 -14
- package/react-devtools/src/TransactionIndex.tsx +7 -7
- package/react-devtools/src/Updates.tsx +41 -32
- package/realtime-client/dist/index.cjs +3 -3
- package/realtime-client/dist/index.js +3 -3
- package/realtime-client/src/pull-mutable-atom-family-member.ts +1 -1
- package/realtime-client/src/pull-mutable-atom.ts +1 -1
- package/realtime-client/src/sync-continuity.ts +1 -2
- package/realtime-react/dist/index.cjs +1 -1
- package/realtime-react/dist/index.js +1 -1
- package/realtime-server/dist/index.cjs +39 -27
- package/realtime-server/dist/index.d.ts +1 -1
- package/realtime-server/dist/index.js +27 -16
- package/realtime-server/src/realtime-continuity-synchronizer.ts +5 -3
- package/realtime-server/src/realtime-mutable-family-provider.ts +2 -1
- package/realtime-server/src/realtime-mutable-provider.ts +1 -1
- package/realtime-server/src/realtime-server-stores/server-sync-store.ts +21 -11
- package/realtime-testing/dist/index.cjs +7 -2
- package/realtime-testing/dist/index.js +8 -5
- package/realtime-testing/src/setup-realtime-test.tsx +5 -2
- package/src/atom.ts +19 -7
- package/src/dispose-state.ts +10 -0
- package/src/index.ts +5 -2
- package/src/selector.ts +13 -7
- package/src/silo.ts +3 -3
- package/src/subscribe.ts +8 -4
- package/src/timeline.ts +18 -1
- package/src/transaction.ts +59 -4
- package/dist/chunk-BF4MVQF6.js +0 -44
- package/internal/src/store/withdraw-new-family-member.ts +0 -69
- /package/{src → ephemeral/src}/find-state.ts +0 -0
|
@@ -27,7 +27,11 @@ export function createStandaloneAtom<T>(
|
|
|
27
27
|
const isMutable = `mutable` in options
|
|
28
28
|
|
|
29
29
|
if (isMutable) {
|
|
30
|
-
|
|
30
|
+
const state = createMutableAtom(options, undefined, store)
|
|
31
|
+
store.on.atomCreation.next(state)
|
|
32
|
+
return state
|
|
31
33
|
}
|
|
32
|
-
|
|
34
|
+
const state = createRegularAtom(options, undefined, store)
|
|
35
|
+
store.on.atomCreation.next(state)
|
|
36
|
+
return state
|
|
33
37
|
}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import type { AtomToken } from "atom.io"
|
|
2
2
|
|
|
3
3
|
import type { Store } from ".."
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
disposeSelector,
|
|
6
|
+
getUpdateToken,
|
|
7
|
+
isChildStore,
|
|
8
|
+
newest,
|
|
9
|
+
withdraw,
|
|
10
|
+
} from ".."
|
|
5
11
|
|
|
6
12
|
export function disposeAtom(atomToken: AtomToken<unknown>, store: Store): void {
|
|
7
13
|
const target = newest(store)
|
|
@@ -12,10 +18,19 @@ export function disposeAtom(atomToken: AtomToken<unknown>, store: Store): void {
|
|
|
12
18
|
`❌`,
|
|
13
19
|
`atom`,
|
|
14
20
|
key,
|
|
15
|
-
`Tried to
|
|
21
|
+
`Tried to dispose atom, but it does not exist in the store.`,
|
|
16
22
|
)
|
|
23
|
+
} else if (!atom.family) {
|
|
24
|
+
store.logger.error(`❌`, `atom`, key, `Standalone atoms cannot be disposed.`)
|
|
17
25
|
} else {
|
|
18
26
|
atom.cleanup?.()
|
|
27
|
+
const lastValue = store.valueMap.get(atom.key)
|
|
28
|
+
const family = withdraw({ key: atom.family.key, type: `atom_family` }, store)
|
|
29
|
+
family.subject.next({
|
|
30
|
+
type: `state_disposal`,
|
|
31
|
+
token: atomToken,
|
|
32
|
+
value: lastValue,
|
|
33
|
+
})
|
|
19
34
|
target.atoms.delete(key)
|
|
20
35
|
target.valueMap.delete(key)
|
|
21
36
|
const selectorKeys = target.selectorAtoms.getRelatedKeys(key)
|
|
@@ -35,8 +50,16 @@ export function disposeAtom(atomToken: AtomToken<unknown>, store: Store): void {
|
|
|
35
50
|
if (atomToken.type === `mutable_atom`) {
|
|
36
51
|
const updateToken = getUpdateToken(atomToken)
|
|
37
52
|
disposeAtom(updateToken, store)
|
|
53
|
+
store.trackers.delete(key)
|
|
38
54
|
}
|
|
39
55
|
store.logger.info(`🔥`, `atom`, key, `deleted`)
|
|
40
|
-
|
|
56
|
+
if (isChildStore(target) && target.transactionMeta.phase === `building`) {
|
|
57
|
+
target.transactionMeta.update.updates.push({
|
|
58
|
+
type: `state_disposal`,
|
|
59
|
+
token: atomToken,
|
|
60
|
+
})
|
|
61
|
+
} else {
|
|
62
|
+
store.on.atomDisposal.next(atomToken)
|
|
63
|
+
}
|
|
41
64
|
}
|
|
42
65
|
}
|
|
@@ -3,6 +3,8 @@ import type {
|
|
|
3
3
|
ReadonlySelectorFamily,
|
|
4
4
|
ReadonlySelectorFamilyOptions,
|
|
5
5
|
ReadonlySelectorToken,
|
|
6
|
+
StateCreation,
|
|
7
|
+
StateDisposal,
|
|
6
8
|
} from "atom.io"
|
|
7
9
|
import type { Json } from "atom.io/json"
|
|
8
10
|
import { stringifyJson } from "atom.io/json"
|
|
@@ -10,32 +12,35 @@ import { stringifyJson } from "atom.io/json"
|
|
|
10
12
|
import { newest } from "../lineage"
|
|
11
13
|
import { createReadonlySelector } from "../selector"
|
|
12
14
|
import type { Store } from "../store"
|
|
13
|
-
import { deposit } from "../store"
|
|
14
15
|
import { Subject } from "../subject"
|
|
15
16
|
|
|
16
17
|
export function createReadonlySelectorFamily<T, K extends Json.Serializable>(
|
|
17
18
|
options: ReadonlySelectorFamilyOptions<T, K>,
|
|
18
19
|
store: Store,
|
|
19
20
|
): ReadonlySelectorFamily<T, K> {
|
|
20
|
-
const subject = new Subject<
|
|
21
|
+
const subject = new Subject<
|
|
22
|
+
| StateCreation<ReadonlySelectorToken<T>>
|
|
23
|
+
| StateDisposal<ReadonlySelectorToken<T>>
|
|
24
|
+
>()
|
|
25
|
+
|
|
21
26
|
const readonlySelectorFamily = Object.assign(
|
|
22
27
|
(key: K): ReadonlySelectorToken<T> => {
|
|
23
|
-
const target = newest(store)
|
|
24
28
|
const subKey = stringifyJson(key)
|
|
25
29
|
const family: FamilyMetadata = { key: options.key, subKey }
|
|
26
30
|
const fullKey = `${options.key}(${subKey})`
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
return createReadonlySelector(
|
|
31
|
+
const target = newest(store)
|
|
32
|
+
|
|
33
|
+
const token = createReadonlySelector(
|
|
32
34
|
{
|
|
33
35
|
key: fullKey,
|
|
34
36
|
get: options.get(key),
|
|
35
37
|
},
|
|
36
38
|
family,
|
|
37
|
-
|
|
39
|
+
target,
|
|
38
40
|
)
|
|
41
|
+
|
|
42
|
+
subject.next({ type: `state_creation`, token })
|
|
43
|
+
return token
|
|
39
44
|
},
|
|
40
45
|
{
|
|
41
46
|
key: options.key,
|
|
@@ -43,7 +48,7 @@ export function createReadonlySelectorFamily<T, K extends Json.Serializable>(
|
|
|
43
48
|
subject,
|
|
44
49
|
install: (s: Store) => createReadonlySelectorFamily(options, s),
|
|
45
50
|
} as const,
|
|
46
|
-
)
|
|
51
|
+
) satisfies ReadonlySelectorFamily<T, K>
|
|
47
52
|
store.families.set(options.key, readonlySelectorFamily)
|
|
48
53
|
return readonlySelectorFamily
|
|
49
54
|
}
|
|
@@ -4,6 +4,8 @@ import type {
|
|
|
4
4
|
RegularAtomFamilyOptions,
|
|
5
5
|
RegularAtomOptions,
|
|
6
6
|
RegularAtomToken,
|
|
7
|
+
StateCreation,
|
|
8
|
+
StateDisposal,
|
|
7
9
|
} from "atom.io"
|
|
8
10
|
import type { Json } from "atom.io/json"
|
|
9
11
|
import { stringifyJson } from "atom.io/json"
|
|
@@ -17,31 +19,29 @@ export function createRegularAtomFamily<T, K extends Json.Serializable>(
|
|
|
17
19
|
options: RegularAtomFamilyOptions<T, K>,
|
|
18
20
|
store: Store,
|
|
19
21
|
): RegularAtomFamily<T, K> {
|
|
20
|
-
const subject = new Subject<
|
|
22
|
+
const subject = new Subject<
|
|
23
|
+
StateCreation<RegularAtomToken<T>> | StateDisposal<RegularAtomToken<T>>
|
|
24
|
+
>()
|
|
25
|
+
|
|
21
26
|
const atomFamily: RegularAtomFamily<T, K> = Object.assign(
|
|
22
27
|
(key: K): RegularAtomToken<any> => {
|
|
23
28
|
const subKey = stringifyJson(key)
|
|
24
29
|
const family: FamilyMetadata = { key: options.key, subKey }
|
|
25
30
|
const fullKey = `${options.key}(${subKey})`
|
|
26
31
|
const target = newest(store)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
options.default instanceof Function
|
|
36
|
-
? options.default(key)
|
|
37
|
-
: options.default,
|
|
38
|
-
}
|
|
39
|
-
if (options.effects) {
|
|
40
|
-
individualOptions.effects = options.effects(key)
|
|
41
|
-
}
|
|
42
|
-
token = createRegularAtom(individualOptions, family, store)
|
|
43
|
-
subject.next(token)
|
|
32
|
+
|
|
33
|
+
const def = options.default
|
|
34
|
+
const individualOptions: RegularAtomOptions<T> = {
|
|
35
|
+
key: fullKey,
|
|
36
|
+
default: def instanceof Function ? def(key) : def,
|
|
37
|
+
}
|
|
38
|
+
if (options.effects) {
|
|
39
|
+
individualOptions.effects = options.effects(key)
|
|
44
40
|
}
|
|
41
|
+
|
|
42
|
+
const token = createRegularAtom(individualOptions, family, target)
|
|
43
|
+
|
|
44
|
+
subject.next({ type: `state_creation`, token })
|
|
45
45
|
return token
|
|
46
46
|
},
|
|
47
47
|
{
|
|
@@ -50,8 +50,7 @@ export function createRegularAtomFamily<T, K extends Json.Serializable>(
|
|
|
50
50
|
subject,
|
|
51
51
|
install: (s: Store) => createRegularAtomFamily(options, s),
|
|
52
52
|
} as const,
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
target.families.set(options.key, atomFamily)
|
|
53
|
+
) satisfies RegularAtomFamily<T, K>
|
|
54
|
+
store.families.set(options.key, atomFamily)
|
|
56
55
|
return atomFamily
|
|
57
56
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
FamilyMetadata,
|
|
3
|
+
StateCreation,
|
|
4
|
+
StateDisposal,
|
|
3
5
|
WritableSelectorFamily,
|
|
4
6
|
WritableSelectorFamilyOptions,
|
|
5
7
|
WritableSelectorToken,
|
|
@@ -7,26 +9,27 @@ import type {
|
|
|
7
9
|
import type { Json } from "atom.io/json"
|
|
8
10
|
import { stringifyJson } from "atom.io/json"
|
|
9
11
|
|
|
12
|
+
import { newest } from "../lineage"
|
|
10
13
|
import { createWritableSelector } from "../selector"
|
|
11
14
|
import type { Store } from "../store"
|
|
12
|
-
import { deposit } from "../store"
|
|
13
15
|
import { Subject } from "../subject"
|
|
14
16
|
|
|
15
17
|
export function createWritableSelectorFamily<T, K extends Json.Serializable>(
|
|
16
18
|
options: WritableSelectorFamilyOptions<T, K>,
|
|
17
19
|
store: Store,
|
|
18
20
|
): WritableSelectorFamily<T, K> {
|
|
19
|
-
const subject = new Subject<
|
|
21
|
+
const subject = new Subject<
|
|
22
|
+
| StateCreation<WritableSelectorToken<T>>
|
|
23
|
+
| StateDisposal<WritableSelectorToken<T>>
|
|
24
|
+
>()
|
|
20
25
|
|
|
21
26
|
const selectorFamily = Object.assign(
|
|
22
27
|
(key: K): WritableSelectorToken<T> => {
|
|
23
28
|
const subKey = stringifyJson(key)
|
|
24
29
|
const family: FamilyMetadata = { key: options.key, subKey }
|
|
25
30
|
const fullKey = `${options.key}(${subKey})`
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
return deposit(existing)
|
|
29
|
-
}
|
|
31
|
+
const target = newest(store)
|
|
32
|
+
|
|
30
33
|
const token = createWritableSelector(
|
|
31
34
|
{
|
|
32
35
|
key: fullKey,
|
|
@@ -34,9 +37,10 @@ export function createWritableSelectorFamily<T, K extends Json.Serializable>(
|
|
|
34
37
|
set: options.set(key),
|
|
35
38
|
},
|
|
36
39
|
family,
|
|
37
|
-
|
|
40
|
+
target,
|
|
38
41
|
)
|
|
39
|
-
|
|
42
|
+
|
|
43
|
+
subject.next({ type: `state_creation`, token })
|
|
40
44
|
return token
|
|
41
45
|
},
|
|
42
46
|
{
|
|
@@ -45,7 +49,7 @@ export function createWritableSelectorFamily<T, K extends Json.Serializable>(
|
|
|
45
49
|
subject,
|
|
46
50
|
install: (s: Store) => createWritableSelectorFamily(options, s),
|
|
47
51
|
} as const,
|
|
48
|
-
)
|
|
52
|
+
) satisfies WritableSelectorFamily<T, K>
|
|
49
53
|
store.families.set(options.key, selectorFamily)
|
|
50
54
|
return selectorFamily
|
|
51
55
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import type { ReadableToken } from "atom.io"
|
|
2
|
+
import { disposeMolecule, type MoleculeToken } from "atom.io/immortal"
|
|
1
3
|
import * as Internal from "atom.io/internal"
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export function disposeState(
|
|
6
|
-
token: ReadableToken<any>,
|
|
5
|
+
export function disposeFromStore(
|
|
6
|
+
token: MoleculeToken<any, any, any> | ReadableToken<any>,
|
|
7
7
|
store: Internal.Store = Internal.IMPLICIT.STORE,
|
|
8
8
|
): void {
|
|
9
9
|
switch (token.type) {
|
|
@@ -15,5 +15,8 @@ export function disposeState(
|
|
|
15
15
|
case `readonly_selector`:
|
|
16
16
|
Internal.disposeSelector(token, store)
|
|
17
17
|
break
|
|
18
|
+
case `molecule`:
|
|
19
|
+
disposeMolecule(token, store)
|
|
20
|
+
break
|
|
18
21
|
}
|
|
19
22
|
}
|
|
@@ -19,8 +19,9 @@ import type {
|
|
|
19
19
|
import type { Json } from "atom.io/json"
|
|
20
20
|
|
|
21
21
|
import type { Transceiver } from "../mutable"
|
|
22
|
-
import { NotFoundError } from "../not-found-error"
|
|
23
22
|
import type { Store } from "../store"
|
|
23
|
+
import { initFamilyMember } from "./init-family-member"
|
|
24
|
+
import { seekInStore } from "./seek-in-store"
|
|
24
25
|
|
|
25
26
|
export function findInStore<
|
|
26
27
|
T extends Transceiver<any>,
|
|
@@ -80,11 +81,15 @@ export function findInStore(
|
|
|
80
81
|
key: Json.Serializable,
|
|
81
82
|
store: Store,
|
|
82
83
|
): ReadableToken<any> {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
if (store.config.lifespan === `immortal`) {
|
|
85
|
+
throw new Error(
|
|
86
|
+
`Do not use \`find\` or \`findState\` in an immortal store. Prefer \`seek\` or \`seekState\`.`,
|
|
87
|
+
)
|
|
87
88
|
}
|
|
88
|
-
|
|
89
|
+
let state = seekInStore(token, key, store)
|
|
90
|
+
if (state) {
|
|
91
|
+
return state
|
|
92
|
+
}
|
|
93
|
+
state = initFamilyMember(token, key, store)
|
|
89
94
|
return state
|
|
90
95
|
}
|
|
@@ -2,4 +2,7 @@ export * from "./create-atom-family"
|
|
|
2
2
|
export * from "./create-readonly-selector-family"
|
|
3
3
|
export * from "./create-regular-atom-family"
|
|
4
4
|
export * from "./create-selector-family"
|
|
5
|
+
export * from "./dispose-from-store"
|
|
5
6
|
export * from "./find-in-store"
|
|
7
|
+
export * from "./init-family-member"
|
|
8
|
+
export * from "./seek-in-store"
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AtomFamilyToken,
|
|
3
|
+
AtomToken,
|
|
4
|
+
MutableAtomFamilyToken,
|
|
5
|
+
MutableAtomToken,
|
|
6
|
+
ReadableFamilyToken,
|
|
7
|
+
ReadableToken,
|
|
8
|
+
ReadonlySelectorFamilyToken,
|
|
9
|
+
ReadonlySelectorToken,
|
|
10
|
+
RegularAtomFamilyToken,
|
|
11
|
+
RegularAtomToken,
|
|
12
|
+
SelectorFamilyToken,
|
|
13
|
+
SelectorToken,
|
|
14
|
+
WritableFamilyToken,
|
|
15
|
+
WritableSelectorFamilyToken,
|
|
16
|
+
WritableSelectorToken,
|
|
17
|
+
WritableToken,
|
|
18
|
+
} from "atom.io"
|
|
19
|
+
import type { Json } from "atom.io/json"
|
|
20
|
+
|
|
21
|
+
import { newest } from "../lineage"
|
|
22
|
+
import type { Transceiver } from "../mutable"
|
|
23
|
+
import { NotFoundError } from "../not-found-error"
|
|
24
|
+
import type { Store } from "../store"
|
|
25
|
+
import { isChildStore } from "../transaction"
|
|
26
|
+
|
|
27
|
+
export function initFamilyMember<
|
|
28
|
+
T extends Transceiver<any>,
|
|
29
|
+
J extends Json.Serializable,
|
|
30
|
+
K extends Json.Serializable,
|
|
31
|
+
Key extends K,
|
|
32
|
+
>(
|
|
33
|
+
token: MutableAtomFamilyToken<T, J, K>,
|
|
34
|
+
key: Key,
|
|
35
|
+
store: Store,
|
|
36
|
+
): MutableAtomToken<T, J>
|
|
37
|
+
|
|
38
|
+
export function initFamilyMember<T, K extends Json.Serializable, Key extends K>(
|
|
39
|
+
token: RegularAtomFamilyToken<T, K>,
|
|
40
|
+
key: Key,
|
|
41
|
+
store: Store,
|
|
42
|
+
): RegularAtomToken<T>
|
|
43
|
+
|
|
44
|
+
export function initFamilyMember<T, K extends Json.Serializable, Key extends K>(
|
|
45
|
+
token: AtomFamilyToken<T, K>,
|
|
46
|
+
key: Key,
|
|
47
|
+
store: Store,
|
|
48
|
+
): AtomToken<T>
|
|
49
|
+
|
|
50
|
+
export function initFamilyMember<T, K extends Json.Serializable, Key extends K>(
|
|
51
|
+
token: WritableSelectorFamilyToken<T, K>,
|
|
52
|
+
key: Key,
|
|
53
|
+
store: Store,
|
|
54
|
+
): WritableSelectorToken<T>
|
|
55
|
+
|
|
56
|
+
export function initFamilyMember<T, K extends Json.Serializable, Key extends K>(
|
|
57
|
+
token: ReadonlySelectorFamilyToken<T, K>,
|
|
58
|
+
key: Key,
|
|
59
|
+
store: Store,
|
|
60
|
+
): ReadonlySelectorToken<T>
|
|
61
|
+
|
|
62
|
+
export function initFamilyMember<T, K extends Json.Serializable, Key extends K>(
|
|
63
|
+
token: SelectorFamilyToken<T, K>,
|
|
64
|
+
key: Key,
|
|
65
|
+
store: Store,
|
|
66
|
+
): SelectorToken<T>
|
|
67
|
+
|
|
68
|
+
export function initFamilyMember<T, K extends Json.Serializable, Key extends K>(
|
|
69
|
+
token: WritableFamilyToken<T, K>,
|
|
70
|
+
key: Key,
|
|
71
|
+
store: Store,
|
|
72
|
+
): WritableToken<T>
|
|
73
|
+
|
|
74
|
+
export function initFamilyMember<T, K extends Json.Serializable, Key extends K>(
|
|
75
|
+
token: ReadableFamilyToken<T, K>,
|
|
76
|
+
key: Key,
|
|
77
|
+
store: Store,
|
|
78
|
+
): ReadableToken<T>
|
|
79
|
+
|
|
80
|
+
export function initFamilyMember(
|
|
81
|
+
token: ReadableFamilyToken<any, any>,
|
|
82
|
+
key: Json.Serializable,
|
|
83
|
+
store: Store,
|
|
84
|
+
): ReadableToken<any> {
|
|
85
|
+
const familyKey = token.key
|
|
86
|
+
const family = store.families.get(familyKey)
|
|
87
|
+
if (family === undefined) {
|
|
88
|
+
throw new NotFoundError(token, store)
|
|
89
|
+
}
|
|
90
|
+
const state = family(key)
|
|
91
|
+
const target = newest(store)
|
|
92
|
+
if (state.family) {
|
|
93
|
+
if (isChildStore(target) && target.transactionMeta.phase === `building`) {
|
|
94
|
+
target.transactionMeta.update.updates.push({
|
|
95
|
+
type: `state_creation`,
|
|
96
|
+
token: state,
|
|
97
|
+
})
|
|
98
|
+
} else {
|
|
99
|
+
switch (state.type) {
|
|
100
|
+
case `atom`:
|
|
101
|
+
case `mutable_atom`:
|
|
102
|
+
store.on.atomCreation.next(state)
|
|
103
|
+
break
|
|
104
|
+
case `selector`:
|
|
105
|
+
case `readonly_selector`:
|
|
106
|
+
store.on.selectorCreation.next(state)
|
|
107
|
+
break
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return state
|
|
112
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AtomFamilyToken,
|
|
3
|
+
AtomToken,
|
|
4
|
+
MutableAtomFamilyToken,
|
|
5
|
+
MutableAtomToken,
|
|
6
|
+
ReadableFamilyToken,
|
|
7
|
+
ReadableToken,
|
|
8
|
+
ReadonlySelectorFamilyToken,
|
|
9
|
+
ReadonlySelectorToken,
|
|
10
|
+
RegularAtomFamilyToken,
|
|
11
|
+
RegularAtomToken,
|
|
12
|
+
SelectorFamilyToken,
|
|
13
|
+
SelectorToken,
|
|
14
|
+
WritableFamilyToken,
|
|
15
|
+
WritableSelectorFamilyToken,
|
|
16
|
+
WritableSelectorToken,
|
|
17
|
+
WritableToken,
|
|
18
|
+
} from "atom.io"
|
|
19
|
+
import type {
|
|
20
|
+
Molecule,
|
|
21
|
+
MoleculeFamilyToken,
|
|
22
|
+
MoleculeToken,
|
|
23
|
+
} from "atom.io/immortal"
|
|
24
|
+
import { type Json, stringifyJson } from "atom.io/json"
|
|
25
|
+
|
|
26
|
+
import type { ReadableState } from ".."
|
|
27
|
+
import { newest } from "../lineage"
|
|
28
|
+
import type { Transceiver } from "../mutable"
|
|
29
|
+
import { deposit, type Store } from "../store"
|
|
30
|
+
|
|
31
|
+
export function seekInStore<
|
|
32
|
+
T extends Transceiver<any>,
|
|
33
|
+
J extends Json.Serializable,
|
|
34
|
+
K extends Json.Serializable,
|
|
35
|
+
Key extends K,
|
|
36
|
+
>(
|
|
37
|
+
token: MutableAtomFamilyToken<T, J, K>,
|
|
38
|
+
key: Key,
|
|
39
|
+
store: Store,
|
|
40
|
+
): MutableAtomToken<T, J> | undefined
|
|
41
|
+
|
|
42
|
+
export function seekInStore<T, K extends Json.Serializable, Key extends K>(
|
|
43
|
+
token: RegularAtomFamilyToken<T, K>,
|
|
44
|
+
key: Key,
|
|
45
|
+
store: Store,
|
|
46
|
+
): RegularAtomToken<T> | undefined
|
|
47
|
+
|
|
48
|
+
export function seekInStore<T, K extends Json.Serializable, Key extends K>(
|
|
49
|
+
token: AtomFamilyToken<T, K>,
|
|
50
|
+
key: Key,
|
|
51
|
+
store: Store,
|
|
52
|
+
): AtomToken<T> | undefined
|
|
53
|
+
|
|
54
|
+
export function seekInStore<T, K extends Json.Serializable, Key extends K>(
|
|
55
|
+
token: WritableSelectorFamilyToken<T, K>,
|
|
56
|
+
key: Key,
|
|
57
|
+
store: Store,
|
|
58
|
+
): WritableSelectorToken<T> | undefined
|
|
59
|
+
|
|
60
|
+
export function seekInStore<T, K extends Json.Serializable, Key extends K>(
|
|
61
|
+
token: ReadonlySelectorFamilyToken<T, K>,
|
|
62
|
+
key: Key,
|
|
63
|
+
store: Store,
|
|
64
|
+
): ReadonlySelectorToken<T> | undefined
|
|
65
|
+
|
|
66
|
+
export function seekInStore<T, K extends Json.Serializable, Key extends K>(
|
|
67
|
+
token: SelectorFamilyToken<T, K>,
|
|
68
|
+
key: Key,
|
|
69
|
+
store: Store,
|
|
70
|
+
): SelectorToken<T> | undefined
|
|
71
|
+
|
|
72
|
+
export function seekInStore<T, K extends Json.Serializable, Key extends K>(
|
|
73
|
+
token: WritableFamilyToken<T, K>,
|
|
74
|
+
key: Key,
|
|
75
|
+
store: Store,
|
|
76
|
+
): WritableToken<T> | undefined
|
|
77
|
+
|
|
78
|
+
export function seekInStore<T, K extends Json.Serializable, Key extends K>(
|
|
79
|
+
token: ReadableFamilyToken<T, K>,
|
|
80
|
+
key: Key,
|
|
81
|
+
store: Store,
|
|
82
|
+
): ReadableToken<T> | undefined
|
|
83
|
+
|
|
84
|
+
export function seekInStore<
|
|
85
|
+
K extends Json.Serializable,
|
|
86
|
+
S extends { [key: string]: any },
|
|
87
|
+
>(
|
|
88
|
+
token: MoleculeFamilyToken<K, S, any[]>,
|
|
89
|
+
key: K,
|
|
90
|
+
store: Store,
|
|
91
|
+
): MoleculeToken<K, S, any[]> | undefined
|
|
92
|
+
|
|
93
|
+
export function seekInStore(
|
|
94
|
+
token: MoleculeFamilyToken<any, any, any> | ReadableFamilyToken<any, any>,
|
|
95
|
+
key: Json.Serializable,
|
|
96
|
+
store: Store,
|
|
97
|
+
): MoleculeToken<any, any, any> | ReadableToken<any> | undefined {
|
|
98
|
+
const subKey = stringifyJson(key)
|
|
99
|
+
const fullKey = `${token.key}(${subKey})`
|
|
100
|
+
const target = newest(store)
|
|
101
|
+
let state: Molecule<any> | ReadableState<any> | undefined
|
|
102
|
+
switch (token.type) {
|
|
103
|
+
case `atom_family`:
|
|
104
|
+
case `mutable_atom_family`:
|
|
105
|
+
state = target.atoms.get(fullKey)
|
|
106
|
+
break
|
|
107
|
+
case `selector_family`:
|
|
108
|
+
state = target.selectors.get(fullKey)
|
|
109
|
+
break
|
|
110
|
+
case `readonly_selector_family`:
|
|
111
|
+
state = target.readonlySelectors.get(fullKey)
|
|
112
|
+
break
|
|
113
|
+
case `molecule_family`:
|
|
114
|
+
state = target.molecules.get(stringifyJson(key))
|
|
115
|
+
if (state) {
|
|
116
|
+
return deposit(state)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (state) {
|
|
120
|
+
return deposit(state)
|
|
121
|
+
}
|
|
122
|
+
return state
|
|
123
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { ReadableToken } from "atom.io"
|
|
2
2
|
|
|
3
3
|
import type { Store } from "../store"
|
|
4
|
-
import {
|
|
4
|
+
import { withdraw } from "../store"
|
|
5
5
|
import { readOrComputeValue } from "./read-or-compute-value"
|
|
6
6
|
|
|
7
7
|
export function getFromStore<T>(token: ReadableToken<T>, store: Store): T {
|
|
8
|
-
const state =
|
|
8
|
+
const state = withdraw(token, store)
|
|
9
9
|
return readOrComputeValue(state, store)
|
|
10
10
|
}
|