atom.io 0.6.2 → 0.6.3
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 +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +7 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7 -2
- package/dist/index.mjs.map +1 -1
- package/json/dist/index.d.mts +18 -0
- package/json/dist/index.d.ts +18 -0
- package/json/dist/index.js +51 -0
- package/json/dist/index.js.map +1 -0
- package/json/dist/index.mjs +15 -0
- package/json/dist/index.mjs.map +1 -0
- package/package.json +13 -3
- package/react/dist/index.d.mts +24 -0
- package/react/dist/index.d.ts +24 -0
- package/react/dist/index.js +83 -0
- package/react/dist/index.js.map +1 -0
- package/react/dist/index.mjs +41 -0
- package/react/dist/index.mjs.map +1 -0
- package/react-devtools/dist/index.css +26 -0
- package/react-devtools/dist/index.css.map +1 -0
- package/react-devtools/dist/index.d.mts +15 -0
- package/react-devtools/dist/index.d.ts +15 -0
- package/react-devtools/dist/index.js +1596 -0
- package/react-devtools/dist/index.js.map +1 -0
- package/react-devtools/dist/index.mjs +1568 -0
- package/react-devtools/dist/index.mjs.map +1 -0
- package/realtime/dist/index.d.mts +27 -0
- package/realtime/dist/index.d.ts +27 -0
- package/realtime/dist/index.js +191 -0
- package/realtime/dist/index.js.map +1 -0
- package/realtime/dist/index.mjs +152 -0
- package/realtime/dist/index.mjs.map +1 -0
- package/realtime-react/dist/index.d.mts +45 -0
- package/realtime-react/dist/index.d.ts +45 -0
- package/realtime-react/dist/index.js +213 -0
- package/realtime-react/dist/index.js.map +1 -0
- package/realtime-react/dist/index.mjs +168 -0
- package/realtime-react/dist/index.mjs.map +1 -0
- package/realtime-testing/dist/index.d.mts +49 -0
- package/realtime-testing/dist/index.d.ts +49 -0
- package/realtime-testing/dist/index.js +153 -0
- package/realtime-testing/dist/index.js.map +1 -0
- package/realtime-testing/dist/index.mjs +117 -0
- package/realtime-testing/dist/index.mjs.map +1 -0
- package/realtime-testing/package.json +15 -0
- package/src/atom.ts +15 -15
- package/src/index.ts +59 -59
- package/src/internal/atom-internal.ts +36 -36
- package/src/internal/families-internal.ts +114 -114
- package/src/internal/get.ts +83 -83
- package/src/internal/is-default.ts +17 -17
- package/src/internal/meta/attach-meta.ts +7 -7
- package/src/internal/meta/meta-state.ts +115 -115
- package/src/internal/operation.ts +93 -93
- package/src/internal/selector/create-read-write-selector.ts +46 -46
- package/src/internal/selector/create-readonly-selector.ts +37 -37
- package/src/internal/selector/lookup-selector-sources.ts +9 -9
- package/src/internal/selector/register-selector.ts +44 -44
- package/src/internal/selector/trace-selector-atoms.ts +30 -30
- package/src/internal/selector/update-selector-atoms.ts +25 -25
- package/src/internal/selector-internal.ts +37 -37
- package/src/internal/set.ts +78 -78
- package/src/internal/store.ts +118 -118
- package/src/internal/subscribe-internal.ts +62 -62
- package/src/internal/time-travel-internal.ts +76 -76
- package/src/internal/timeline/add-atom-to-timeline.ts +158 -153
- package/src/internal/timeline-internal.ts +80 -80
- package/src/internal/transaction/abort-transaction.ts +8 -8
- package/src/internal/transaction/apply-transaction.ts +41 -41
- package/src/internal/transaction/build-transaction.ts +28 -28
- package/src/internal/transaction/index.ts +7 -7
- package/src/internal/transaction/redo-transaction.ts +13 -13
- package/src/internal/transaction/undo-transaction.ts +13 -13
- package/src/internal/transaction-internal.ts +48 -48
- package/src/json/select-json.ts +12 -12
- package/src/logger.ts +30 -30
- package/src/react/store-context.tsx +4 -4
- package/src/react/store-hooks.ts +18 -18
- package/src/react-devtools/AtomIODevtools.tsx +83 -82
- package/src/react-devtools/StateEditor.tsx +53 -53
- package/src/react-devtools/TokenList.tsx +47 -42
- package/src/react-explorer/AtomIOExplorer.tsx +197 -185
- package/src/react-explorer/explorer-effects.ts +11 -11
- package/src/react-explorer/explorer-states.ts +186 -193
- package/src/react-explorer/index.ts +11 -11
- package/src/react-explorer/space-states.ts +48 -50
- package/src/react-explorer/view-states.ts +25 -25
- package/src/realtime/hook-composition/expose-family.ts +81 -81
- package/src/realtime/hook-composition/expose-single.ts +26 -26
- package/src/realtime/hook-composition/expose-timeline.ts +60 -0
- package/src/realtime/hook-composition/index.ts +2 -2
- package/src/realtime/hook-composition/receive-state.ts +18 -18
- package/src/realtime/hook-composition/receive-transaction.ts +8 -8
- package/src/realtime-react/realtime-context.tsx +17 -17
- package/src/realtime-react/realtime-hooks.ts +17 -17
- package/src/realtime-react/realtime-state.ts +4 -4
- package/src/realtime-react/use-pull-family-member.ts +15 -15
- package/src/realtime-react/use-pull-family.ts +13 -13
- package/src/realtime-react/use-pull.ts +12 -12
- package/src/realtime-react/use-push.ts +15 -15
- package/src/realtime-react/use-server-action.ts +21 -21
- package/src/realtime-testing/index.ts +1 -0
- package/src/realtime-testing/setup-realtime-test.tsx +160 -0
- package/src/selector.ts +25 -25
- package/src/silo.ts +38 -38
- package/src/subscribe.ts +68 -68
- package/src/timeline.ts +13 -13
- package/src/transaction.ts +28 -28
- package/src/web-effects/storage.ts +17 -17
|
@@ -4,132 +4,132 @@ import type { Store } from "../store"
|
|
|
4
4
|
import { IMPLICIT } from "../store"
|
|
5
5
|
|
|
6
6
|
export type StateTokenIndex<
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
Token extends
|
|
8
|
+
| AtomToken<unknown>
|
|
9
|
+
| ReadonlySelectorToken<unknown>
|
|
10
|
+
| SelectorToken<unknown>,
|
|
11
11
|
> = Record<
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
string,
|
|
13
|
+
| Token
|
|
14
|
+
| {
|
|
15
|
+
key: string
|
|
16
|
+
familyMembers: Record<string, Token>
|
|
17
|
+
}
|
|
18
18
|
>
|
|
19
19
|
|
|
20
20
|
export type AtomTokenIndex = StateTokenIndex<AtomToken<unknown>>
|
|
21
21
|
export type SelectorTokenIndex = StateTokenIndex<
|
|
22
|
-
|
|
22
|
+
ReadonlySelectorToken<unknown> | SelectorToken<unknown>
|
|
23
23
|
>
|
|
24
24
|
|
|
25
25
|
export const attachMetaAtoms = (
|
|
26
|
-
|
|
26
|
+
store: Store = IMPLICIT.STORE,
|
|
27
27
|
): ReadonlySelectorToken<AtomTokenIndex> => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
28
|
+
const atomTokenIndexState__INTERNAL = atom<AtomTokenIndex>({
|
|
29
|
+
key: `👁🗨_atom_token_index__INTERNAL`,
|
|
30
|
+
default: () =>
|
|
31
|
+
[...store.atoms].reduce<AtomTokenIndex>((acc, [key]) => {
|
|
32
|
+
acc[key] = { key, type: `atom` }
|
|
33
|
+
return acc
|
|
34
|
+
}, {}),
|
|
35
|
+
effects: [
|
|
36
|
+
({ setSelf }) => {
|
|
37
|
+
store.subject.atomCreation.subscribe((atomToken) => {
|
|
38
|
+
if (store.operation.open) {
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
setSelf((state) => {
|
|
42
|
+
const { key, family } = atomToken
|
|
43
|
+
if (family) {
|
|
44
|
+
const { key: familyKey, subKey } = family
|
|
45
|
+
const current = state[familyKey]
|
|
46
|
+
if (current === undefined || `familyMembers` in current) {
|
|
47
|
+
const familyKeyState = current || {
|
|
48
|
+
key: familyKey,
|
|
49
|
+
familyMembers: {},
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
...state,
|
|
53
|
+
[familyKey]: {
|
|
54
|
+
...familyKeyState,
|
|
55
|
+
familyMembers: {
|
|
56
|
+
...familyKeyState.familyMembers,
|
|
57
|
+
[subKey]: atomToken,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
...state,
|
|
65
|
+
[key]: atomToken,
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
})
|
|
72
|
+
return selector({
|
|
73
|
+
key: `👁🗨_atom_token_index`,
|
|
74
|
+
get: ({ get }) => get(atomTokenIndexState__INTERNAL),
|
|
75
|
+
})
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
export const attachMetaSelectors = (
|
|
79
|
-
|
|
79
|
+
store: Store = IMPLICIT.STORE,
|
|
80
80
|
): ReadonlySelectorToken<SelectorTokenIndex> => {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
81
|
+
const readonlySelectorTokenIndexState__INTERNAL = atom<SelectorTokenIndex>({
|
|
82
|
+
key: `👁🗨_selector_token_index__INTERNAL`,
|
|
83
|
+
default: () =>
|
|
84
|
+
Object.assign(
|
|
85
|
+
[...store.readonlySelectors].reduce<SelectorTokenIndex>((acc, [key]) => {
|
|
86
|
+
acc[key] = { key, type: `readonly_selector` }
|
|
87
|
+
return acc
|
|
88
|
+
}, {}),
|
|
89
|
+
[...store.selectors].reduce<SelectorTokenIndex>((acc, [key]) => {
|
|
90
|
+
acc[key] = { key, type: `selector` }
|
|
91
|
+
return acc
|
|
92
|
+
}, {}),
|
|
93
|
+
),
|
|
94
|
+
effects: [
|
|
95
|
+
({ setSelf }) => {
|
|
96
|
+
store.subject.selectorCreation.subscribe((selectorToken) => {
|
|
97
|
+
if (store.operation.open) {
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
setSelf((state) => {
|
|
101
|
+
const { key, family } = selectorToken
|
|
102
|
+
if (family) {
|
|
103
|
+
const { key: familyKey, subKey } = family
|
|
104
|
+
const current = state[familyKey]
|
|
105
|
+
if (current === undefined || `familyMembers` in current) {
|
|
106
|
+
const familyKeyState = current || {
|
|
107
|
+
key: familyKey,
|
|
108
|
+
familyMembers: {},
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
...state,
|
|
112
|
+
[familyKey]: {
|
|
113
|
+
...familyKeyState,
|
|
114
|
+
familyMembers: {
|
|
115
|
+
...familyKeyState.familyMembers,
|
|
116
|
+
[subKey]: selectorToken,
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
...state,
|
|
124
|
+
[key]: selectorToken,
|
|
125
|
+
}
|
|
126
|
+
})
|
|
127
|
+
})
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
})
|
|
131
|
+
return selector({
|
|
132
|
+
key: `👁🗨_selector_token_index`,
|
|
133
|
+
get: ({ get }) => get(readonlySelectorTokenIndexState__INTERNAL),
|
|
134
|
+
})
|
|
135
135
|
}
|
|
@@ -8,138 +8,138 @@ import { IMPLICIT } from "./store"
|
|
|
8
8
|
import type { StateToken } from ".."
|
|
9
9
|
|
|
10
10
|
export type OperationProgress =
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
11
|
+
| {
|
|
12
|
+
open: false
|
|
13
|
+
}
|
|
14
|
+
| {
|
|
15
|
+
open: true
|
|
16
|
+
done: Set<string>
|
|
17
|
+
prev: Hamt<any, string>
|
|
18
|
+
time: number
|
|
19
|
+
token: StateToken<any>
|
|
20
|
+
}
|
|
21
21
|
|
|
22
22
|
export const openOperation = (token: StateToken<any>, store: Store): void => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
23
|
+
const core = target(store)
|
|
24
|
+
if (core.operation.open) {
|
|
25
|
+
store.config.logger?.error(
|
|
26
|
+
`❌ failed to setState to "${token.key}" during a setState for "${core.operation.token.key}"`,
|
|
27
|
+
)
|
|
28
|
+
throw Symbol(`violation`)
|
|
29
|
+
}
|
|
30
|
+
core.operation = {
|
|
31
|
+
open: true,
|
|
32
|
+
done: new Set(),
|
|
33
|
+
prev: store.valueMap,
|
|
34
|
+
time: Date.now(),
|
|
35
|
+
token,
|
|
36
|
+
}
|
|
37
|
+
store.config.logger?.info(
|
|
38
|
+
`⭕ operation start from "${token.key}" in store "${store.config.name}"`,
|
|
39
|
+
)
|
|
40
40
|
}
|
|
41
41
|
export const closeOperation = (store: Store): void => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
const core = target(store)
|
|
43
|
+
core.operation = { open: false }
|
|
44
|
+
store.config.logger?.info(`🔴 operation done`)
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
export const isDone = (key: string, store: Store = IMPLICIT.STORE): boolean => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
48
|
+
const core = target(store)
|
|
49
|
+
if (!core.operation.open) {
|
|
50
|
+
store.config.logger?.warn(
|
|
51
|
+
`isDone called outside of an operation. This is probably a bug.`,
|
|
52
|
+
)
|
|
53
|
+
return true
|
|
54
|
+
}
|
|
55
|
+
return core.operation.done.has(key)
|
|
56
56
|
}
|
|
57
57
|
export const markDone = (key: string, store: Store = IMPLICIT.STORE): void => {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
const core = target(store)
|
|
59
|
+
if (!core.operation.open) {
|
|
60
|
+
store.config.logger?.warn(
|
|
61
|
+
`markDone called outside of an operation. This is probably a bug.`,
|
|
62
|
+
)
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
core.operation.done.add(key)
|
|
66
66
|
}
|
|
67
67
|
export const recallState = <T>(
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
state: Atom<T> | ReadonlySelector<T> | Selector<T>,
|
|
69
|
+
store: Store = IMPLICIT.STORE,
|
|
70
70
|
): T => {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
const core = target(store)
|
|
72
|
+
if (!core.operation.open) {
|
|
73
|
+
store.config.logger?.warn(
|
|
74
|
+
`recall called outside of an operation. This is probably a bug.`,
|
|
75
|
+
)
|
|
76
|
+
return HAMT.get(state.key, core.valueMap)
|
|
77
|
+
}
|
|
78
|
+
return HAMT.get(state.key, core.operation.prev)
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
export const cacheValue = (
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
key: string,
|
|
83
|
+
value: unknown,
|
|
84
|
+
store: Store = IMPLICIT.STORE,
|
|
85
85
|
): void => {
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
const core = target(store)
|
|
87
|
+
core.valueMap = HAMT.set(key, value, core.valueMap)
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
export const evictCachedValue = (
|
|
91
|
-
|
|
92
|
-
|
|
91
|
+
key: string,
|
|
92
|
+
store: Store = IMPLICIT.STORE,
|
|
93
93
|
): void => {
|
|
94
|
-
|
|
95
|
-
|
|
94
|
+
const core = target(store)
|
|
95
|
+
core.valueMap = HAMT.remove(key, core.valueMap)
|
|
96
96
|
}
|
|
97
97
|
export const readCachedValue = <T>(
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
key: string,
|
|
99
|
+
store: Store = IMPLICIT.STORE,
|
|
100
100
|
): T => HAMT.get(key, target(store).valueMap)
|
|
101
101
|
|
|
102
102
|
export const isValueCached = (
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
key: string,
|
|
104
|
+
store: Store = IMPLICIT.STORE,
|
|
105
105
|
): boolean => HAMT.has(key, target(store).valueMap)
|
|
106
106
|
|
|
107
107
|
export const storeAtom = (
|
|
108
|
-
|
|
109
|
-
|
|
108
|
+
atom: Atom<any>,
|
|
109
|
+
store: Store = IMPLICIT.STORE,
|
|
110
110
|
): void => {
|
|
111
|
-
|
|
112
|
-
|
|
111
|
+
const core = target(store)
|
|
112
|
+
core.atoms = HAMT.set(atom.key, atom, core.atoms)
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
export const storeSelector = (
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
selector: Selector<any>,
|
|
117
|
+
store: Store = IMPLICIT.STORE,
|
|
118
118
|
): void => {
|
|
119
|
-
|
|
120
|
-
|
|
119
|
+
const core = target(store)
|
|
120
|
+
core.selectors = HAMT.set(selector.key, selector, core.selectors)
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
export const storeReadonlySelector = (
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
selector: ReadonlySelector<any>,
|
|
125
|
+
store: Store = IMPLICIT.STORE,
|
|
126
126
|
): void => {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
127
|
+
const core = target(store)
|
|
128
|
+
core.readonlySelectors = HAMT.set(
|
|
129
|
+
selector.key,
|
|
130
|
+
selector,
|
|
131
|
+
core.readonlySelectors,
|
|
132
|
+
)
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
export const hasKeyBeenUsed = (
|
|
136
|
-
|
|
137
|
-
|
|
136
|
+
key: string,
|
|
137
|
+
store: Store = IMPLICIT.STORE,
|
|
138
138
|
): boolean => {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
139
|
+
const core = target(store)
|
|
140
|
+
return (
|
|
141
|
+
HAMT.has(key, core.atoms) ||
|
|
142
|
+
HAMT.has(key, core.selectors) ||
|
|
143
|
+
HAMT.has(key, core.readonlySelectors)
|
|
144
|
+
)
|
|
145
145
|
}
|
|
@@ -4,59 +4,59 @@ import * as Rx from "rxjs"
|
|
|
4
4
|
import { become } from "~/packages/anvl/src/function"
|
|
5
5
|
|
|
6
6
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
type Store,
|
|
8
|
+
type Selector,
|
|
9
|
+
type StoreCore,
|
|
10
|
+
registerSelector,
|
|
11
|
+
selector__INTERNAL,
|
|
12
12
|
} from ".."
|
|
13
13
|
import type { FamilyMetadata, SelectorToken } from "../.."
|
|
14
14
|
import type { SelectorOptions } from "../../selector"
|
|
15
15
|
import { cacheValue, markDone } from "../operation"
|
|
16
16
|
|
|
17
17
|
export const createReadWriteSelector = <T>(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
options: SelectorOptions<T>,
|
|
19
|
+
family: FamilyMetadata | undefined,
|
|
20
|
+
store: Store,
|
|
21
|
+
core: StoreCore,
|
|
22
22
|
): SelectorToken<T> => {
|
|
23
|
-
|
|
23
|
+
const subject = new Rx.Subject<{ newValue: T; oldValue: T }>()
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
const { get, set } = registerSelector(options.key, store)
|
|
26
|
+
const getSelf = () => {
|
|
27
|
+
const value = options.get({ get })
|
|
28
|
+
cacheValue(options.key, value, store)
|
|
29
|
+
return value
|
|
30
|
+
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
32
|
+
const setSelf = (next: T | ((oldValue: T) => T)): void => {
|
|
33
|
+
store.config.logger?.info(` <- "${options.key}" became`, next)
|
|
34
|
+
const oldValue = getSelf()
|
|
35
|
+
const newValue = become(next)(oldValue)
|
|
36
|
+
cacheValue(options.key, newValue, store)
|
|
37
|
+
markDone(options.key, store)
|
|
38
|
+
if (store.transactionStatus.phase === `idle`) {
|
|
39
|
+
subject.next({ newValue, oldValue })
|
|
40
|
+
}
|
|
41
|
+
options.set({ get, set }, newValue)
|
|
42
|
+
}
|
|
43
|
+
const mySelector: Selector<T> = {
|
|
44
|
+
...options,
|
|
45
|
+
subject,
|
|
46
|
+
install: (s: Store) => selector__INTERNAL(options, family, s),
|
|
47
|
+
get: getSelf,
|
|
48
|
+
set: setSelf,
|
|
49
|
+
type: `selector`,
|
|
50
|
+
...(family && { family }),
|
|
51
|
+
}
|
|
52
|
+
core.selectors = HAMT.set(options.key, mySelector, core.selectors)
|
|
53
|
+
const initialValue = getSelf()
|
|
54
|
+
store.config.logger?.info(` ✨ "${options.key}" =`, initialValue)
|
|
55
|
+
const token: SelectorToken<T> = {
|
|
56
|
+
key: options.key,
|
|
57
|
+
type: `selector`,
|
|
58
|
+
family,
|
|
59
|
+
}
|
|
60
|
+
store.subject.selectorCreation.next(token)
|
|
61
|
+
return token
|
|
62
62
|
}
|