atom.io 0.6.8 → 0.6.9
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/index.d.mts +226 -258
- package/dist/index.d.ts +226 -258
- package/dist/index.js +28 -1917
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -1877
- package/dist/index.mjs.map +1 -1
- package/introspection/dist/index.d.mts +117 -171
- package/introspection/dist/index.d.ts +117 -171
- package/introspection/dist/index.js +6 -346
- package/introspection/dist/index.js.map +1 -1
- package/introspection/dist/index.mjs +5 -324
- package/introspection/dist/index.mjs.map +1 -1
- package/json/dist/index.d.mts +32 -1
- package/json/dist/index.d.ts +32 -1
- package/json/dist/index.js +31 -48
- package/json/dist/index.js.map +1 -1
- package/json/dist/index.mjs +6 -14
- package/json/dist/index.mjs.map +1 -1
- package/package.json +22 -14
- package/react/dist/index.js +34 -83
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +7 -43
- package/react/dist/index.mjs.map +1 -1
- package/react-devtools/dist/index.css +1 -50
- package/react-devtools/dist/index.css.map +1 -1
- package/react-devtools/dist/index.d.mts +124 -188
- package/react-devtools/dist/index.d.ts +124 -188
- package/react-devtools/dist/index.js +56 -4674
- package/react-devtools/dist/index.js.map +1 -1
- package/react-devtools/dist/index.mjs +19 -4642
- package/react-devtools/dist/index.mjs.map +1 -1
- package/realtime/dist/index.d.mts +1 -3
- package/realtime/dist/index.d.ts +1 -3
- package/realtime/dist/index.js +26 -184
- package/realtime/dist/index.js.map +1 -1
- package/realtime/dist/index.mjs +4 -148
- package/realtime/dist/index.mjs.map +1 -1
- package/realtime-react/dist/index.d.mts +2 -4
- package/realtime-react/dist/index.d.ts +2 -4
- package/realtime-react/dist/index.js +41 -214
- package/realtime-react/dist/index.js.map +1 -1
- package/realtime-react/dist/index.mjs +9 -169
- package/realtime-react/dist/index.mjs.map +1 -1
- package/src/atom.ts +4 -3
- package/src/index.ts +12 -9
- package/src/logger.ts +5 -5
- package/src/selector.ts +3 -3
- package/src/silo.ts +36 -39
- package/src/subscribe.ts +24 -19
- package/src/timeline.ts +9 -4
- package/src/transaction.ts +3 -4
- package/src/internal/atom-internal.ts +0 -54
- package/src/internal/families-internal.ts +0 -144
- package/src/internal/get.ts +0 -129
- package/src/internal/index.ts +0 -15
- package/src/internal/is-default.ts +0 -35
- package/src/internal/operation.ts +0 -139
- package/src/internal/selector/create-read-write-selector.ts +0 -68
- package/src/internal/selector/create-readonly-selector.ts +0 -48
- package/src/internal/selector/index.ts +0 -4
- package/src/internal/selector/lookup-selector-sources.ts +0 -16
- package/src/internal/selector/register-selector.ts +0 -57
- package/src/internal/selector/trace-selector-atoms.ts +0 -43
- package/src/internal/selector/update-selector-atoms.ts +0 -33
- package/src/internal/selector-internal.ts +0 -58
- package/src/internal/set.ts +0 -99
- package/src/internal/store.ts +0 -151
- package/src/internal/subscribe-internal.ts +0 -88
- package/src/internal/time-travel-internal.ts +0 -91
- package/src/internal/timeline/add-atom-to-timeline.ts +0 -168
- package/src/internal/timeline/index.ts +0 -1
- package/src/internal/timeline-internal.ts +0 -107
- package/src/internal/transaction/abort-transaction.ts +0 -12
- package/src/internal/transaction/apply-transaction.ts +0 -57
- package/src/internal/transaction/build-transaction.ts +0 -33
- package/src/internal/transaction/index.ts +0 -25
- package/src/internal/transaction/redo-transaction.ts +0 -23
- package/src/internal/transaction/undo-transaction.ts +0 -23
- package/src/internal/transaction-internal.ts +0 -61
- package/src/introspection/attach-atom-index.ts +0 -73
- package/src/introspection/attach-introspection-states.ts +0 -42
- package/src/introspection/attach-selector-index.ts +0 -77
- package/src/introspection/attach-timeline-family.ts +0 -59
- package/src/introspection/attach-timeline-index.ts +0 -36
- package/src/introspection/attach-transaction-index.ts +0 -38
- package/src/introspection/attach-transaction-logs.ts +0 -40
- package/src/introspection/index.ts +0 -20
- package/src/json/index.ts +0 -1
- package/src/json/select-json.ts +0 -18
- package/src/react/index.ts +0 -2
- package/src/react/store-context.tsx +0 -13
- package/src/react/store-hooks.ts +0 -47
- package/src/react-devtools/AtomIODevtools.tsx +0 -107
- package/src/react-devtools/Button.tsx +0 -24
- package/src/react-devtools/StateEditor.tsx +0 -74
- package/src/react-devtools/StateIndex.tsx +0 -156
- package/src/react-devtools/TimelineIndex.tsx +0 -92
- package/src/react-devtools/TransactionIndex.tsx +0 -70
- package/src/react-devtools/Updates.tsx +0 -145
- package/src/react-devtools/devtools.scss +0 -310
- package/src/react-devtools/index.ts +0 -72
- package/src/react-explorer/AtomIOExplorer.tsx +0 -218
- package/src/react-explorer/explorer-effects.ts +0 -20
- package/src/react-explorer/explorer-states.ts +0 -217
- package/src/react-explorer/index.ts +0 -23
- package/src/react-explorer/space-states.ts +0 -72
- package/src/react-explorer/view-states.ts +0 -41
- package/src/realtime/README.md +0 -33
- package/src/realtime/hook-composition/expose-family.ts +0 -101
- package/src/realtime/hook-composition/expose-single.ts +0 -38
- package/src/realtime/hook-composition/expose-timeline.ts +0 -60
- package/src/realtime/hook-composition/index.ts +0 -12
- package/src/realtime/hook-composition/receive-state.ts +0 -29
- package/src/realtime/hook-composition/receive-transaction.ts +0 -18
- package/src/realtime/index.ts +0 -1
- package/src/realtime-react/index.ts +0 -3
- package/src/realtime-react/realtime-context.tsx +0 -30
- package/src/realtime-react/realtime-hooks.ts +0 -39
- package/src/realtime-react/realtime-state.ts +0 -10
- package/src/realtime-react/use-pull-family-member.ts +0 -26
- package/src/realtime-react/use-pull-family.ts +0 -24
- package/src/realtime-react/use-pull.ts +0 -24
- package/src/realtime-react/use-push.ts +0 -27
- package/src/realtime-react/use-server-action.ts +0 -33
- package/src/realtime-testing/index.ts +0 -1
- package/src/realtime-testing/setup-realtime-test.tsx +0 -159
- package/src/tracker/index.ts +0 -3
- package/src/tracker/tracker.ts +0 -61
- package/src/web-effects/index.ts +0 -1
- package/src/web-effects/storage.ts +0 -30
package/src/silo.ts
CHANGED
|
@@ -1,53 +1,50 @@
|
|
|
1
|
-
import type { redo, timeline, undo } from "."
|
|
2
|
-
import { getState, setState, subscribe } from "."
|
|
3
|
-
import type { atom, atomFamily } from "./atom"
|
|
4
|
-
import type { Store } from "./internal"
|
|
5
1
|
import {
|
|
2
|
+
Store,
|
|
6
3
|
atomFamily__INTERNAL,
|
|
7
4
|
atom__INTERNAL,
|
|
8
|
-
createStore,
|
|
9
5
|
redo__INTERNAL,
|
|
10
6
|
selectorFamily__INTERNAL,
|
|
11
7
|
selector__INTERNAL,
|
|
12
8
|
timeline__INTERNAL,
|
|
13
9
|
transaction__INTERNAL,
|
|
14
10
|
undo__INTERNAL,
|
|
15
|
-
} from "
|
|
11
|
+
} from "atom.io/internal"
|
|
12
|
+
|
|
13
|
+
import type { redo, timeline, undo } from "."
|
|
14
|
+
import { getState, setState, subscribe } from "."
|
|
15
|
+
import type { atom, atomFamily } from "./atom"
|
|
16
16
|
import type { selector, selectorFamily } from "./selector"
|
|
17
17
|
import type { transaction } from "./transaction"
|
|
18
18
|
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
subscribe: (token, handler) => subscribe(token, handler, store),
|
|
50
|
-
undo: (token) => undo__INTERNAL(token, store),
|
|
51
|
-
redo: (token) => redo__INTERNAL(token, store),
|
|
19
|
+
export class Silo {
|
|
20
|
+
public store: Store
|
|
21
|
+
public atom: typeof atom
|
|
22
|
+
public atomFamily: typeof atomFamily
|
|
23
|
+
public selector: typeof selector
|
|
24
|
+
public selectorFamily: typeof selectorFamily
|
|
25
|
+
public transaction: typeof transaction
|
|
26
|
+
public timeline: typeof timeline
|
|
27
|
+
public getState: typeof getState
|
|
28
|
+
public setState: typeof setState
|
|
29
|
+
public subscribe: typeof subscribe
|
|
30
|
+
public undo: typeof undo
|
|
31
|
+
public redo: typeof redo
|
|
32
|
+
public constructor(name: string, fromStore: Store | null = null) {
|
|
33
|
+
const store = new Store(name, fromStore)
|
|
34
|
+
this.store = store
|
|
35
|
+
this.atom = (options) => atom__INTERNAL(options, undefined, store)
|
|
36
|
+
this.atomFamily = (options) => atomFamily__INTERNAL(options, store)
|
|
37
|
+
this.selector = (options) =>
|
|
38
|
+
selector__INTERNAL(options, undefined, store) as any
|
|
39
|
+
this.selectorFamily = (options) =>
|
|
40
|
+
selectorFamily__INTERNAL(options, store) as any
|
|
41
|
+
this.transaction = (options) => transaction__INTERNAL(options, store)
|
|
42
|
+
this.timeline = (options) => timeline__INTERNAL(options, store)
|
|
43
|
+
this.getState = (token) => getState(token, store)
|
|
44
|
+
this.setState = (token, newValue) => setState(token, newValue, store)
|
|
45
|
+
;(this.subscribe = (token, handler, key) =>
|
|
46
|
+
subscribe(token, handler, key, store)),
|
|
47
|
+
(this.undo = (token) => undo__INTERNAL(token, store))
|
|
48
|
+
this.redo = (token) => redo__INTERNAL(token, store)
|
|
52
49
|
}
|
|
53
50
|
}
|
package/src/subscribe.ts
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Store } from "atom.io/internal"
|
|
2
|
+
import { IMPLICIT, subscribeToRootAtoms, withdraw } from "atom.io/internal"
|
|
2
3
|
|
|
3
4
|
import type {
|
|
5
|
+
FamilyMetadata,
|
|
4
6
|
ReadonlySelectorToken,
|
|
5
7
|
StateToken,
|
|
6
8
|
TimelineToken,
|
|
7
9
|
TimelineUpdate,
|
|
8
10
|
TransactionToken,
|
|
9
11
|
TransactionUpdate,
|
|
12
|
+
ƒn,
|
|
10
13
|
} from "."
|
|
11
|
-
import type { Store } from "./internal"
|
|
12
|
-
import { IMPLICIT, subscribeToRootAtoms, withdraw } from "./internal"
|
|
13
14
|
|
|
14
15
|
export type StateUpdate<T> = { newValue: T; oldValue: T }
|
|
15
|
-
export type KeyedStateUpdate<T> = StateUpdate<T> & {
|
|
16
|
+
export type KeyedStateUpdate<T> = StateUpdate<T> & {
|
|
17
|
+
key: string
|
|
18
|
+
family?: FamilyMetadata
|
|
19
|
+
}
|
|
16
20
|
export type UpdateHandler<T> = (update: StateUpdate<T>) => void
|
|
17
21
|
|
|
18
22
|
export const subscribe = <T>(
|
|
19
23
|
token: ReadonlySelectorToken<T> | StateToken<T>,
|
|
20
24
|
handleUpdate: UpdateHandler<T>,
|
|
25
|
+
key: string = Math.random().toString(36).slice(2),
|
|
21
26
|
store: Store = IMPLICIT.STORE,
|
|
22
27
|
): (() => void) => {
|
|
23
28
|
const state = withdraw<T>(token, store)
|
|
@@ -26,24 +31,24 @@ export const subscribe = <T>(
|
|
|
26
31
|
`State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`,
|
|
27
32
|
)
|
|
28
33
|
}
|
|
29
|
-
const
|
|
34
|
+
const unsubFunction = state.subject.subscribe(key, handleUpdate)
|
|
30
35
|
store.config.logger?.info(`👀 subscribe to "${state.key}"`)
|
|
31
|
-
const
|
|
36
|
+
const dependencyUnsubFunctions =
|
|
32
37
|
state.type !== `atom` ? subscribeToRootAtoms(state, store) : null
|
|
33
38
|
|
|
34
39
|
const unsubscribe =
|
|
35
|
-
|
|
40
|
+
dependencyUnsubFunctions === null
|
|
36
41
|
? () => {
|
|
37
42
|
store.config.logger?.info(`🙈 unsubscribe from "${state.key}"`)
|
|
38
|
-
|
|
43
|
+
unsubFunction()
|
|
39
44
|
}
|
|
40
45
|
: () => {
|
|
41
46
|
store.config.logger?.info(
|
|
42
47
|
`🙈 unsubscribe from "${state.key}" and its dependencies`,
|
|
43
48
|
)
|
|
44
|
-
|
|
45
|
-
for (const
|
|
46
|
-
|
|
49
|
+
unsubFunction()
|
|
50
|
+
for (const unsubFromDependency of dependencyUnsubFunctions) {
|
|
51
|
+
unsubFromDependency()
|
|
47
52
|
}
|
|
48
53
|
}
|
|
49
54
|
|
|
@@ -57,6 +62,7 @@ export type TransactionUpdateHandler<ƒ extends ƒn> = (
|
|
|
57
62
|
export const subscribeToTransaction = <ƒ extends ƒn>(
|
|
58
63
|
token: TransactionToken<ƒ>,
|
|
59
64
|
handleUpdate: TransactionUpdateHandler<ƒ>,
|
|
65
|
+
key: string = Math.random().toString(36).slice(2),
|
|
60
66
|
store = IMPLICIT.STORE,
|
|
61
67
|
): (() => void) => {
|
|
62
68
|
const tx = withdraw(token, store)
|
|
@@ -66,17 +72,17 @@ export const subscribeToTransaction = <ƒ extends ƒn>(
|
|
|
66
72
|
)
|
|
67
73
|
}
|
|
68
74
|
store.config.logger?.info(`👀 subscribe to transaction "${token.key}"`)
|
|
69
|
-
const
|
|
70
|
-
|
|
75
|
+
const unsubscribe = tx.subject.subscribe(key, handleUpdate)
|
|
76
|
+
return () => {
|
|
71
77
|
store.config.logger?.info(`🙈 unsubscribe from transaction "${token.key}"`)
|
|
72
|
-
|
|
78
|
+
unsubscribe()
|
|
73
79
|
}
|
|
74
|
-
return unsubscribe
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
export const subscribeToTimeline = (
|
|
78
83
|
token: TimelineToken,
|
|
79
84
|
handleUpdate: (update: TimelineUpdate | `redo` | `undo`) => void,
|
|
85
|
+
key: string = Math.random().toString(36).slice(2),
|
|
80
86
|
store = IMPLICIT.STORE,
|
|
81
87
|
): (() => void) => {
|
|
82
88
|
const tl = withdraw(token, store)
|
|
@@ -86,10 +92,9 @@ export const subscribeToTimeline = (
|
|
|
86
92
|
)
|
|
87
93
|
}
|
|
88
94
|
store.config.logger?.info(`👀 subscribe to timeline "${token.key}"`)
|
|
89
|
-
const
|
|
90
|
-
|
|
95
|
+
const unsubscribe = tl.subject.subscribe(key, handleUpdate)
|
|
96
|
+
return () => {
|
|
91
97
|
store.config.logger?.info(`🙈 unsubscribe from timeline "${token.key}"`)
|
|
92
|
-
|
|
98
|
+
unsubscribe()
|
|
93
99
|
}
|
|
94
|
-
return unsubscribe
|
|
95
100
|
}
|
package/src/timeline.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import type { AtomFamily, AtomToken } from "."
|
|
2
1
|
import type {
|
|
3
2
|
TimelineAtomUpdate,
|
|
4
3
|
TimelineSelectorUpdate,
|
|
5
4
|
TimelineTransactionUpdate,
|
|
6
|
-
} from "
|
|
7
|
-
import { IMPLICIT } from "
|
|
8
|
-
import {
|
|
5
|
+
} from "atom.io/internal"
|
|
6
|
+
import { IMPLICIT } from "atom.io/internal"
|
|
7
|
+
import {
|
|
8
|
+
redo__INTERNAL,
|
|
9
|
+
timeline__INTERNAL,
|
|
10
|
+
undo__INTERNAL,
|
|
11
|
+
} from "atom.io/internal/"
|
|
12
|
+
|
|
13
|
+
import type { AtomFamily, AtomToken } from "."
|
|
9
14
|
|
|
10
15
|
export type TimelineToken = {
|
|
11
16
|
key: string
|
package/src/transaction.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Store } from "atom.io/internal"
|
|
2
|
+
import { IMPLICIT, transaction__INTERNAL, withdraw } from "atom.io/internal"
|
|
2
3
|
|
|
3
|
-
import type { KeyedStateUpdate, ReadonlySelectorToken, StateToken } from "."
|
|
4
|
-
import type { Store } from "./internal"
|
|
5
|
-
import { IMPLICIT, transaction__INTERNAL, withdraw } from "./internal"
|
|
4
|
+
import type { KeyedStateUpdate, ReadonlySelectorToken, StateToken, ƒn } from "."
|
|
6
5
|
|
|
7
6
|
export type TransactionToken<_> = {
|
|
8
7
|
key: string
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { Subject } from "."
|
|
2
|
-
import { deposit } from "./get"
|
|
3
|
-
import { markAtomAsDefault } from "./is-default"
|
|
4
|
-
import { cacheValue } from "./operation"
|
|
5
|
-
import type { Store } from "./store"
|
|
6
|
-
import { IMPLICIT } from "./store"
|
|
7
|
-
import { target } from "./transaction-internal"
|
|
8
|
-
import type { AtomToken, FamilyMetadata, UpdateHandler } from ".."
|
|
9
|
-
import { setState, subscribe } from ".."
|
|
10
|
-
import type { AtomOptions } from "../atom"
|
|
11
|
-
|
|
12
|
-
export type Atom<T> = {
|
|
13
|
-
key: string
|
|
14
|
-
type: `atom`
|
|
15
|
-
family?: FamilyMetadata
|
|
16
|
-
subject: Subject<{ newValue: T; oldValue: T }>
|
|
17
|
-
default: T
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function atom__INTERNAL<T>(
|
|
21
|
-
options: AtomOptions<T>,
|
|
22
|
-
family?: FamilyMetadata,
|
|
23
|
-
store: Store = IMPLICIT.STORE,
|
|
24
|
-
): AtomToken<T> {
|
|
25
|
-
const core = target(store)
|
|
26
|
-
const existing = core.atoms.get(options.key)
|
|
27
|
-
if (existing) {
|
|
28
|
-
store.config.logger?.error?.(
|
|
29
|
-
`Key "${options.key}" already exists in the store.`,
|
|
30
|
-
)
|
|
31
|
-
return deposit(existing)
|
|
32
|
-
}
|
|
33
|
-
const subject = new Subject<{ newValue: T; oldValue: T }>()
|
|
34
|
-
const newAtom = {
|
|
35
|
-
...options,
|
|
36
|
-
subject,
|
|
37
|
-
type: `atom`,
|
|
38
|
-
...(family && { family }),
|
|
39
|
-
} as const
|
|
40
|
-
const initialValue =
|
|
41
|
-
options.default instanceof Function ? options.default() : options.default
|
|
42
|
-
core.atoms.set(newAtom.key, newAtom)
|
|
43
|
-
markAtomAsDefault(options.key, store)
|
|
44
|
-
cacheValue(options.key, initialValue, store)
|
|
45
|
-
const token = deposit(newAtom)
|
|
46
|
-
options.effects?.forEach((effect) =>
|
|
47
|
-
effect({
|
|
48
|
-
setSelf: (next) => setState(token, next, store),
|
|
49
|
-
onSet: (handle: UpdateHandler<T>) => subscribe(token, handle, store),
|
|
50
|
-
}),
|
|
51
|
-
)
|
|
52
|
-
store.subject.atomCreation.next(token)
|
|
53
|
-
return token as AtomToken<T>
|
|
54
|
-
}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import type { Json } from "~/packages/anvl/src/json"
|
|
2
|
-
import { stringifyJson } from "~/packages/anvl/src/json"
|
|
3
|
-
|
|
4
|
-
import type { Store } from "."
|
|
5
|
-
import {
|
|
6
|
-
Subject,
|
|
7
|
-
atom__INTERNAL,
|
|
8
|
-
withdraw,
|
|
9
|
-
selector__INTERNAL,
|
|
10
|
-
target,
|
|
11
|
-
deposit,
|
|
12
|
-
IMPLICIT,
|
|
13
|
-
} from "."
|
|
14
|
-
import type {
|
|
15
|
-
AtomFamily,
|
|
16
|
-
AtomFamilyOptions,
|
|
17
|
-
AtomOptions,
|
|
18
|
-
AtomToken,
|
|
19
|
-
FamilyMetadata,
|
|
20
|
-
ReadonlySelectorFamily,
|
|
21
|
-
ReadonlySelectorFamilyOptions,
|
|
22
|
-
ReadonlySelectorToken,
|
|
23
|
-
SelectorFamily,
|
|
24
|
-
SelectorFamilyOptions,
|
|
25
|
-
SelectorToken,
|
|
26
|
-
} from ".."
|
|
27
|
-
|
|
28
|
-
export function atomFamily__INTERNAL<T, K extends Json.Serializable>(
|
|
29
|
-
options: AtomFamilyOptions<T, K>,
|
|
30
|
-
store: Store = IMPLICIT.STORE,
|
|
31
|
-
): AtomFamily<T, K> {
|
|
32
|
-
const subject = new Subject<AtomToken<T>>()
|
|
33
|
-
return Object.assign(
|
|
34
|
-
(key: K): AtomToken<T> => {
|
|
35
|
-
const subKey = stringifyJson(key)
|
|
36
|
-
const family: FamilyMetadata = { key: options.key, subKey }
|
|
37
|
-
const fullKey = `${options.key}(${subKey})`
|
|
38
|
-
const existing = withdraw({ key: fullKey, type: `atom` }, store)
|
|
39
|
-
let token: AtomToken<any>
|
|
40
|
-
if (existing) {
|
|
41
|
-
token = deposit(existing)
|
|
42
|
-
} else {
|
|
43
|
-
const individualOptions: AtomOptions<any> = {
|
|
44
|
-
key: fullKey,
|
|
45
|
-
default:
|
|
46
|
-
options.default instanceof Function
|
|
47
|
-
? options.default(key)
|
|
48
|
-
: options.default,
|
|
49
|
-
}
|
|
50
|
-
if (options.effects) {
|
|
51
|
-
individualOptions.effects = options.effects(key)
|
|
52
|
-
}
|
|
53
|
-
token = atom__INTERNAL<T>(individualOptions, family, store)
|
|
54
|
-
}
|
|
55
|
-
subject.next(token)
|
|
56
|
-
return token
|
|
57
|
-
},
|
|
58
|
-
{
|
|
59
|
-
key: options.key,
|
|
60
|
-
type: `atom_family`,
|
|
61
|
-
subject,
|
|
62
|
-
} as const,
|
|
63
|
-
)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function readonlySelectorFamily__INTERNAL<T, K extends Json.Serializable>(
|
|
67
|
-
options: ReadonlySelectorFamilyOptions<T, K>,
|
|
68
|
-
store?: Store,
|
|
69
|
-
): ReadonlySelectorFamily<T, K> {
|
|
70
|
-
const core = target(store)
|
|
71
|
-
const subject = new Subject<ReadonlySelectorToken<T>>()
|
|
72
|
-
return Object.assign(
|
|
73
|
-
(key: K): ReadonlySelectorToken<T> => {
|
|
74
|
-
const subKey = stringifyJson(key)
|
|
75
|
-
const family: FamilyMetadata = { key: options.key, subKey }
|
|
76
|
-
const fullKey = `${options.key}(${subKey})`
|
|
77
|
-
const existing = core.readonlySelectors.get(fullKey)
|
|
78
|
-
if (existing) {
|
|
79
|
-
return deposit(existing)
|
|
80
|
-
}
|
|
81
|
-
return selector__INTERNAL<T>(
|
|
82
|
-
{
|
|
83
|
-
key: fullKey,
|
|
84
|
-
get: options.get(key),
|
|
85
|
-
},
|
|
86
|
-
family,
|
|
87
|
-
store,
|
|
88
|
-
) as ReadonlySelectorToken<T>
|
|
89
|
-
},
|
|
90
|
-
{
|
|
91
|
-
key: options.key,
|
|
92
|
-
type: `readonly_selector_family`,
|
|
93
|
-
subject,
|
|
94
|
-
} as const,
|
|
95
|
-
) as ReadonlySelectorFamily<T, K>
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function selectorFamily__INTERNAL<T, K extends Json.Serializable>(
|
|
99
|
-
options: SelectorFamilyOptions<T, K>,
|
|
100
|
-
store?: Store,
|
|
101
|
-
): SelectorFamily<T, K>
|
|
102
|
-
export function selectorFamily__INTERNAL<T, K extends Json.Serializable>(
|
|
103
|
-
options: ReadonlySelectorFamilyOptions<T, K>,
|
|
104
|
-
store?: Store,
|
|
105
|
-
): ReadonlySelectorFamily<T, K>
|
|
106
|
-
export function selectorFamily__INTERNAL<T, K extends Json.Serializable>(
|
|
107
|
-
options: ReadonlySelectorFamilyOptions<T, K> | SelectorFamilyOptions<T, K>,
|
|
108
|
-
store: Store = IMPLICIT.STORE,
|
|
109
|
-
): ReadonlySelectorFamily<T, K> | SelectorFamily<T, K> {
|
|
110
|
-
const isReadonly = !(`set` in options)
|
|
111
|
-
|
|
112
|
-
if (isReadonly) {
|
|
113
|
-
return readonlySelectorFamily__INTERNAL(options, store)
|
|
114
|
-
}
|
|
115
|
-
const core = target(store)
|
|
116
|
-
const subject = new Subject<SelectorToken<T>>()
|
|
117
|
-
|
|
118
|
-
return Object.assign(
|
|
119
|
-
(key: K): SelectorToken<T> => {
|
|
120
|
-
const subKey = stringifyJson(key)
|
|
121
|
-
const family: FamilyMetadata = { key: options.key, subKey }
|
|
122
|
-
const fullKey = `${options.key}(${subKey})`
|
|
123
|
-
const existing = core.selectors.get(fullKey)
|
|
124
|
-
if (existing) {
|
|
125
|
-
return deposit(existing)
|
|
126
|
-
}
|
|
127
|
-
const token = selector__INTERNAL<T>(
|
|
128
|
-
{
|
|
129
|
-
key: fullKey,
|
|
130
|
-
get: options.get(key),
|
|
131
|
-
set: options.set(key),
|
|
132
|
-
},
|
|
133
|
-
family,
|
|
134
|
-
store,
|
|
135
|
-
)
|
|
136
|
-
subject.next(token)
|
|
137
|
-
return token
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
key: options.key,
|
|
141
|
-
type: `selector_family`,
|
|
142
|
-
} as const,
|
|
143
|
-
) as SelectorFamily<T, K>
|
|
144
|
-
}
|
package/src/internal/get.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import type { ƒn } from "~/packages/anvl/src/function"
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
Atom,
|
|
5
|
-
ReadonlySelector,
|
|
6
|
-
Selector,
|
|
7
|
-
Store,
|
|
8
|
-
Timeline,
|
|
9
|
-
Transaction,
|
|
10
|
-
} from "."
|
|
11
|
-
import { target, isValueCached, readCachedValue, IMPLICIT } from "."
|
|
12
|
-
import type {
|
|
13
|
-
AtomToken,
|
|
14
|
-
ReadonlySelectorToken,
|
|
15
|
-
SelectorToken,
|
|
16
|
-
StateToken,
|
|
17
|
-
TimelineToken,
|
|
18
|
-
TransactionToken,
|
|
19
|
-
} from ".."
|
|
20
|
-
|
|
21
|
-
export const computeSelectorState = <T>(
|
|
22
|
-
selector: ReadonlySelector<T> | Selector<T>,
|
|
23
|
-
): T => selector.get()
|
|
24
|
-
|
|
25
|
-
export function lookup(
|
|
26
|
-
key: string,
|
|
27
|
-
store: Store,
|
|
28
|
-
): AtomToken<unknown> | ReadonlySelectorToken<unknown> | SelectorToken<unknown> {
|
|
29
|
-
const core = target(store)
|
|
30
|
-
const type = core.atoms.has(key)
|
|
31
|
-
? `atom`
|
|
32
|
-
: core.selectors.has(key)
|
|
33
|
-
? `selector`
|
|
34
|
-
: `readonly_selector`
|
|
35
|
-
return { key, type } as any
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function withdraw<T>(token: AtomToken<T>, store: Store): Atom<T> | null
|
|
39
|
-
export function withdraw<T>(
|
|
40
|
-
token: SelectorToken<T>,
|
|
41
|
-
store: Store,
|
|
42
|
-
): Selector<T> | null
|
|
43
|
-
export function withdraw<T>(
|
|
44
|
-
token: StateToken<T>,
|
|
45
|
-
store: Store,
|
|
46
|
-
): Atom<T> | Selector<T> | null
|
|
47
|
-
export function withdraw<T>(
|
|
48
|
-
token: ReadonlySelectorToken<T>,
|
|
49
|
-
store: Store,
|
|
50
|
-
): ReadonlySelector<T> | null
|
|
51
|
-
export function withdraw<T>(
|
|
52
|
-
token: TransactionToken<T>,
|
|
53
|
-
store: Store,
|
|
54
|
-
): Transaction<T extends ƒn ? T : never> | null
|
|
55
|
-
export function withdraw<T>(
|
|
56
|
-
token: ReadonlySelectorToken<T> | StateToken<T>,
|
|
57
|
-
store: Store,
|
|
58
|
-
): Atom<T> | ReadonlySelector<T> | Selector<T> | null
|
|
59
|
-
export function withdraw<T>(token: TimelineToken, store: Store): Timeline | null
|
|
60
|
-
export function withdraw<T>(
|
|
61
|
-
token:
|
|
62
|
-
| ReadonlySelectorToken<T>
|
|
63
|
-
| StateToken<T>
|
|
64
|
-
| TimelineToken
|
|
65
|
-
| TransactionToken<T>,
|
|
66
|
-
store: Store,
|
|
67
|
-
):
|
|
68
|
-
| Atom<T>
|
|
69
|
-
| ReadonlySelector<T>
|
|
70
|
-
| Selector<T>
|
|
71
|
-
| Timeline
|
|
72
|
-
| Transaction<T extends ƒn ? T : never>
|
|
73
|
-
| null {
|
|
74
|
-
const core = target(store)
|
|
75
|
-
return (
|
|
76
|
-
core.atoms.get(token.key) ??
|
|
77
|
-
core.selectors.get(token.key) ??
|
|
78
|
-
core.readonlySelectors.get(token.key) ??
|
|
79
|
-
core.transactions.get(token.key) ??
|
|
80
|
-
core.timelines.get(token.key) ??
|
|
81
|
-
null
|
|
82
|
-
)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function deposit<T>(state: Atom<T>): AtomToken<T>
|
|
86
|
-
export function deposit<T>(state: Selector<T>): SelectorToken<T>
|
|
87
|
-
export function deposit<T>(state: Atom<T> | Selector<T>): StateToken<T>
|
|
88
|
-
export function deposit<T>(state: ReadonlySelector<T>): ReadonlySelectorToken<T>
|
|
89
|
-
export function deposit<T>(
|
|
90
|
-
state: Transaction<T extends ƒn ? T : never>,
|
|
91
|
-
): TransactionToken<T>
|
|
92
|
-
export function deposit<T>(
|
|
93
|
-
state: Atom<T> | ReadonlySelector<T> | Selector<T>,
|
|
94
|
-
): ReadonlySelectorToken<T> | StateToken<T>
|
|
95
|
-
export function deposit<T>(
|
|
96
|
-
state:
|
|
97
|
-
| Atom<T>
|
|
98
|
-
| ReadonlySelector<T>
|
|
99
|
-
| Selector<T>
|
|
100
|
-
| Transaction<T extends ƒn ? T : never>,
|
|
101
|
-
):
|
|
102
|
-
| AtomToken<T>
|
|
103
|
-
| ReadonlySelectorToken<T>
|
|
104
|
-
| SelectorToken<T>
|
|
105
|
-
| TransactionToken<T> {
|
|
106
|
-
return {
|
|
107
|
-
key: state.key,
|
|
108
|
-
type: state.type,
|
|
109
|
-
...(`family` in state && { family: state.family }),
|
|
110
|
-
} as any
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export const getState__INTERNAL = <T>(
|
|
114
|
-
state: Atom<T> | ReadonlySelector<T> | Selector<T>,
|
|
115
|
-
store: Store = IMPLICIT.STORE,
|
|
116
|
-
): T => {
|
|
117
|
-
if (isValueCached(state.key, store)) {
|
|
118
|
-
store.config.logger?.info(`>> read "${state.key}"`)
|
|
119
|
-
return readCachedValue(state.key, store)
|
|
120
|
-
}
|
|
121
|
-
if (state.type !== `atom`) {
|
|
122
|
-
store.config.logger?.info(`-> calc "${state.key}"`)
|
|
123
|
-
return computeSelectorState(state)
|
|
124
|
-
}
|
|
125
|
-
store.config.logger?.error(
|
|
126
|
-
`Attempted to get atom "${state.key}", which was never initialized in store "${store.config.name}".`,
|
|
127
|
-
)
|
|
128
|
-
return state.default
|
|
129
|
-
}
|
package/src/internal/index.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export * from "./atom-internal"
|
|
2
|
-
export * from "./families-internal"
|
|
3
|
-
export * from "./get"
|
|
4
|
-
export * from "./is-default"
|
|
5
|
-
export * from "./operation"
|
|
6
|
-
export * from "./selector-internal"
|
|
7
|
-
export * from "./selector"
|
|
8
|
-
export * from "./set"
|
|
9
|
-
export * from "./store"
|
|
10
|
-
export * from "../../../anvl/reactivity/subject"
|
|
11
|
-
export * from "./subscribe-internal"
|
|
12
|
-
export * from "./time-travel-internal"
|
|
13
|
-
export * from "./timeline-internal"
|
|
14
|
-
export * from "./transaction-internal"
|
|
15
|
-
export * from "./transaction"
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { Store } from "."
|
|
2
|
-
import { target, IMPLICIT, traceAllSelectorAtoms } from "."
|
|
3
|
-
|
|
4
|
-
export const isAtomDefault = (
|
|
5
|
-
key: string,
|
|
6
|
-
store: Store = IMPLICIT.STORE,
|
|
7
|
-
): boolean => {
|
|
8
|
-
const core = target(store)
|
|
9
|
-
return core.atomsThatAreDefault.has(key)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const markAtomAsDefault = (
|
|
13
|
-
key: string,
|
|
14
|
-
store: Store = IMPLICIT.STORE,
|
|
15
|
-
): void => {
|
|
16
|
-
const core = target(store)
|
|
17
|
-
core.atomsThatAreDefault = new Set(core.atomsThatAreDefault).add(key)
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const markAtomAsNotDefault = (
|
|
21
|
-
key: string,
|
|
22
|
-
store: Store = IMPLICIT.STORE,
|
|
23
|
-
): void => {
|
|
24
|
-
const core = target(store)
|
|
25
|
-
core.atomsThatAreDefault = new Set(target(store).atomsThatAreDefault)
|
|
26
|
-
core.atomsThatAreDefault.delete(key)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export const isSelectorDefault = (
|
|
30
|
-
key: string,
|
|
31
|
-
store: Store = IMPLICIT.STORE,
|
|
32
|
-
): boolean => {
|
|
33
|
-
const roots = traceAllSelectorAtoms(key, store)
|
|
34
|
-
return roots.every((root) => isAtomDefault(root.key, store))
|
|
35
|
-
}
|