atom.io 0.6.1 → 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 +3 -3
- package/dist/index.d.ts +3 -3
- 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.js.map +1 -1
- package/json/dist/index.mjs.map +1 -1
- package/package.json +13 -3
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs.map +1 -1
- package/react-devtools/dist/index.js +32 -18
- package/react-devtools/dist/index.js.map +1 -1
- package/react-devtools/dist/index.mjs +32 -18
- package/react-devtools/dist/index.mjs.map +1 -1
- package/realtime/dist/index.js.map +1 -1
- package/realtime/dist/index.mjs.map +1 -1
- package/realtime-react/dist/index.js.map +1 -1
- package/realtime-react/dist/index.mjs.map +1 -1
- 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
|
@@ -5,97 +5,97 @@ import * as AtomIO from "atom.io"
|
|
|
5
5
|
import type { ServerConfig } from ".."
|
|
6
6
|
|
|
7
7
|
const subscribeToTokenCreation = <T>(
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
family: AtomIO.AtomFamily<T> | AtomIO.SelectorFamily<T>,
|
|
9
|
+
handleTokenCreation: (token: AtomIO.StateToken<T>) => void,
|
|
10
10
|
): (() => void) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
const subscription =
|
|
12
|
+
family.type === `atom_family`
|
|
13
|
+
? family.subject.subscribe(handleTokenCreation)
|
|
14
|
+
: family.subject.subscribe(handleTokenCreation)
|
|
15
|
+
return () => subscription.unsubscribe()
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
19
19
|
export const useExposeFamily = ({ socket, store }: ServerConfig) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
return function exposeFamily<J extends Json>(
|
|
21
|
+
family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
|
|
22
|
+
index: AtomIO.StateToken<Set<string>>,
|
|
23
|
+
): () => void {
|
|
24
|
+
const unsubSingleCallbacksByKey = new Map<string, () => void>()
|
|
25
|
+
const unsubFamilyCallbacksByKey = new Map<string, () => void>()
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
const fillFamilyUnsubRequest = () => {
|
|
28
|
+
unsubFamilyCallbacksByKey.forEach((unsub) => unsub())
|
|
29
|
+
unsubFamilyCallbacksByKey.clear()
|
|
30
|
+
socket.off(`unsub:${family.key}`, fillFamilyUnsubRequest)
|
|
31
|
+
}
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
const fillSingleUnsubRequest = (key: string) => {
|
|
34
|
+
socket.off(`unsub:${key}`, fillSingleUnsubRequest)
|
|
35
|
+
const unsub = unsubSingleCallbacksByKey.get(key)
|
|
36
|
+
if (unsub) {
|
|
37
|
+
unsub()
|
|
38
|
+
unsubSingleCallbacksByKey.delete(key)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
42
|
+
const fillSubRequest = (subKey?: AtomIO.Serializable) => {
|
|
43
|
+
if (subKey === undefined) {
|
|
44
|
+
const keys = AtomIO.getState(index, store)
|
|
45
|
+
keys.forEach((key) => {
|
|
46
|
+
const token = family(key)
|
|
47
|
+
socket.emit(
|
|
48
|
+
`serve:${family.key}`,
|
|
49
|
+
parseJson(token.family?.subKey || `null`),
|
|
50
|
+
AtomIO.getState(token, store),
|
|
51
|
+
)
|
|
52
|
+
})
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
54
|
+
const unsubscribeFromTokenCreation = subscribeToTokenCreation(
|
|
55
|
+
family,
|
|
56
|
+
(token) => {
|
|
57
|
+
const unsub = AtomIO.subscribe(
|
|
58
|
+
token,
|
|
59
|
+
({ newValue }) => {
|
|
60
|
+
socket.emit(
|
|
61
|
+
`serve:${family.key}`,
|
|
62
|
+
parseJson(token.family?.subKey || `null`),
|
|
63
|
+
newValue,
|
|
64
|
+
)
|
|
65
|
+
},
|
|
66
|
+
store,
|
|
67
|
+
)
|
|
68
|
+
unsubFamilyCallbacksByKey.set(token.key, unsub)
|
|
69
|
+
},
|
|
70
|
+
)
|
|
71
|
+
unsubFamilyCallbacksByKey.set(family.key, unsubscribeFromTokenCreation)
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
73
|
+
socket.on(`unsub:${family.key}`, fillFamilyUnsubRequest)
|
|
74
|
+
} else {
|
|
75
|
+
const token = family(subKey)
|
|
76
|
+
socket.emit(`serve:${token.key}`, AtomIO.getState(token, store))
|
|
77
|
+
const unsubscribe = AtomIO.subscribe(
|
|
78
|
+
token,
|
|
79
|
+
({ newValue }) => {
|
|
80
|
+
socket.emit(`serve:${token.key}`, newValue)
|
|
81
|
+
},
|
|
82
|
+
store,
|
|
83
|
+
)
|
|
84
|
+
unsubSingleCallbacksByKey.set(token.key, unsubscribe)
|
|
85
|
+
socket.on(`unsub:${token.key}`, () => {
|
|
86
|
+
fillSingleUnsubRequest(token.key)
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
socket.on(`sub:${family.key}`, fillSubRequest)
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
93
|
+
return () => {
|
|
94
|
+
socket.off(`sub:${family.key}`, fillSubRequest)
|
|
95
|
+
unsubFamilyCallbacksByKey.forEach((unsub) => unsub())
|
|
96
|
+
unsubSingleCallbacksByKey.forEach((unsub) => unsub())
|
|
97
|
+
unsubFamilyCallbacksByKey.clear()
|
|
98
|
+
unsubSingleCallbacksByKey.clear()
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
101
|
}
|
|
@@ -5,34 +5,34 @@ import type { ServerConfig } from ".."
|
|
|
5
5
|
|
|
6
6
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
7
7
|
export const useExposeSingle = ({ socket, store }: ServerConfig) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
return function exposeSingle<J extends Json>(
|
|
9
|
+
token: AtomIO.StateToken<J>,
|
|
10
|
+
): () => void {
|
|
11
|
+
let unsubscribeFromStateUpdates: (() => void) | null = null
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
const fillUnsubRequest = () => {
|
|
14
|
+
socket.off(`unsub:${token.key}`, fillUnsubRequest)
|
|
15
|
+
unsubscribeFromStateUpdates?.()
|
|
16
|
+
unsubscribeFromStateUpdates = null
|
|
17
|
+
}
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
19
|
+
const fillSubRequest = () => {
|
|
20
|
+
socket.emit(`serve:${token.key}`, AtomIO.getState(token, store))
|
|
21
|
+
unsubscribeFromStateUpdates = AtomIO.subscribe(
|
|
22
|
+
token,
|
|
23
|
+
({ newValue }) => {
|
|
24
|
+
socket.emit(`serve:${token.key}`, newValue)
|
|
25
|
+
},
|
|
26
|
+
store,
|
|
27
|
+
)
|
|
28
|
+
socket.on(`unsub:${token.key}`, fillUnsubRequest)
|
|
29
|
+
}
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
socket.on(`sub:${token.key}`, fillSubRequest)
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
return () => {
|
|
34
|
+
socket.off(`sub:${token.key}`, fillSubRequest)
|
|
35
|
+
unsubscribeFromStateUpdates?.()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
38
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as AtomIO from "atom.io"
|
|
2
|
+
|
|
3
|
+
import { Join } from "~/packages/anvl/src/join"
|
|
4
|
+
|
|
5
|
+
import type { ServerConfig } from "."
|
|
6
|
+
import type { TimelineTransactionUpdate } from "../../internal"
|
|
7
|
+
|
|
8
|
+
export type TransactionRequest = {
|
|
9
|
+
key: string
|
|
10
|
+
params: unknown[]
|
|
11
|
+
transactionId: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const useExposeTimeline__UNSTABLE = ({
|
|
15
|
+
socket,
|
|
16
|
+
store,
|
|
17
|
+
}: ServerConfig): ((tl: AtomIO.TimelineToken) => () => void) => {
|
|
18
|
+
const timestampsOfTransactionsState = AtomIO.__INTERNAL__.atom__INTERNAL(
|
|
19
|
+
{
|
|
20
|
+
key: `timestampsOfTransactions`,
|
|
21
|
+
default: new Join<null, `transactionId`, `timestamp`>({
|
|
22
|
+
relationType: `1:1`,
|
|
23
|
+
})
|
|
24
|
+
.from(`transactionId`)
|
|
25
|
+
.to(`timestamp`),
|
|
26
|
+
},
|
|
27
|
+
undefined,
|
|
28
|
+
store,
|
|
29
|
+
)
|
|
30
|
+
return function exposeTimeline(tl: AtomIO.TimelineToken): () => void {
|
|
31
|
+
const handleTransactionRequest = (update: TransactionRequest) => {
|
|
32
|
+
AtomIO.runTransaction(
|
|
33
|
+
{ key: update.key, type: `transaction` },
|
|
34
|
+
store,
|
|
35
|
+
)(...update.params)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
socket.on(`txr:${tl.key}`, handleTransactionRequest)
|
|
39
|
+
|
|
40
|
+
const unsubscribeFromTimeline = AtomIO.subscribeToTimeline(
|
|
41
|
+
tl,
|
|
42
|
+
(update: TimelineTransactionUpdate) => {
|
|
43
|
+
// const timestamp = update.timestamp.toString()
|
|
44
|
+
// AtomIO.setState(
|
|
45
|
+
// timestampsOfTransactionsState,
|
|
46
|
+
// (j) => j.set(update),
|
|
47
|
+
// store
|
|
48
|
+
// )
|
|
49
|
+
if (update.type === `transaction_update`) {
|
|
50
|
+
socket.emit(`tl:${tl.key}`, update)
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
return () => {
|
|
56
|
+
socket.off(`tlu:${tl.key}`, handleTransactionRequest)
|
|
57
|
+
unsubscribeFromTimeline()
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -5,25 +5,25 @@ import type { ServerConfig } from ".."
|
|
|
5
5
|
|
|
6
6
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
7
7
|
export const useReceiveState = ({ socket, store }: ServerConfig) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
return function receiveState<J extends Json>(
|
|
9
|
+
token: AtomIO.StateToken<J>,
|
|
10
|
+
): () => void {
|
|
11
|
+
const publish = (newValue: J) => AtomIO.setState(token, newValue, store)
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
const fillPubUnclaim = () => {
|
|
14
|
+
socket.off(`pub:${token.key}`, publish)
|
|
15
|
+
socket.off(`unclaim:${token.key}`, fillPubUnclaim)
|
|
16
|
+
}
|
|
17
|
+
const fillPubClaim = () => {
|
|
18
|
+
socket.on(`pub:${token.key}`, publish)
|
|
19
|
+
socket.on(`unclaim:${token.key}`, fillPubUnclaim)
|
|
20
|
+
}
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
socket.on(`claim:${token.key}`, fillPubClaim)
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
return () => {
|
|
25
|
+
socket.off(`claim:${token.key}`, fillPubClaim)
|
|
26
|
+
socket.off(`pub:${token.key}`, publish)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
29
|
}
|
|
@@ -5,14 +5,14 @@ import type { ƒn } from "~/packages/anvl/src/function"
|
|
|
5
5
|
import type { ServerConfig } from "."
|
|
6
6
|
|
|
7
7
|
export const useReceiveTransaction = ({ socket, store }: ServerConfig) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
return function receiveTransaction<ƒ extends ƒn>(
|
|
9
|
+
tx: AtomIO.TransactionToken<ƒ>,
|
|
10
|
+
): () => void {
|
|
11
|
+
const fillTransactionRequest = (update: AtomIO.TransactionUpdate<ƒ>) =>
|
|
12
|
+
AtomIO.runTransaction<ƒ>(tx, store)(...update.params)
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
socket.on(`tx:${tx.key}`, fillTransactionRequest)
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
return () => socket.off(`tx:${tx.key}`, fillTransactionRequest)
|
|
17
|
+
}
|
|
18
18
|
}
|
|
@@ -7,25 +7,25 @@ import { io } from "socket.io-client"
|
|
|
7
7
|
import { myIdState__INTERNAL } from "./realtime-state"
|
|
8
8
|
|
|
9
9
|
export const RealtimeContext = React.createContext<{ socket: Socket }>({
|
|
10
|
-
|
|
10
|
+
socket: io(),
|
|
11
11
|
})
|
|
12
12
|
|
|
13
13
|
export const RealtimeProvider: React.FC<{
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
children: React.ReactNode
|
|
15
|
+
socket: Socket
|
|
16
16
|
}> = ({ children, socket }) => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
17
|
+
const setMyId = AR.useI(myIdState__INTERNAL)
|
|
18
|
+
React.useEffect(() => {
|
|
19
|
+
socket.on(`connect`, () => {
|
|
20
|
+
setMyId(socket.id)
|
|
21
|
+
})
|
|
22
|
+
socket.on(`disconnect`, () => {
|
|
23
|
+
setMyId(null)
|
|
24
|
+
})
|
|
25
|
+
}, [socket, setMyId])
|
|
26
|
+
return (
|
|
27
|
+
<RealtimeContext.Provider value={{ socket }}>
|
|
28
|
+
{children}
|
|
29
|
+
</RealtimeContext.Provider>
|
|
30
|
+
)
|
|
31
31
|
}
|
|
@@ -10,26 +10,26 @@ import { usePush } from "./use-push"
|
|
|
10
10
|
import { useServerAction } from "./use-server-action"
|
|
11
11
|
|
|
12
12
|
export type RealtimeHooks = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
13
|
+
usePull: <J extends Json>(token: AtomIO.StateToken<J>) => void
|
|
14
|
+
usePullFamily: <J extends Json>(
|
|
15
|
+
family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
|
|
16
|
+
) => void
|
|
17
|
+
usePullFamilyMember: <J extends Json>(
|
|
18
|
+
family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
|
|
19
|
+
subKey: string,
|
|
20
|
+
) => void
|
|
21
|
+
usePush: <J extends Json>(token: AtomIO.StateToken<J>) => void
|
|
22
|
+
useServerAction: <ƒ extends ƒn>(
|
|
23
|
+
token: AtomIO.TransactionToken<ƒ>,
|
|
24
|
+
) => (...parameters: Parameters<ƒ>) => ReturnType<ƒ>
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
export const realtimeHooks: RealtimeHooks = {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
usePull,
|
|
29
|
+
usePullFamily,
|
|
30
|
+
usePullFamilyMember,
|
|
31
|
+
usePush,
|
|
32
|
+
useServerAction,
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
export * from "./use-pull"
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as AtomIO from "atom.io"
|
|
2
2
|
|
|
3
3
|
export const myIdState__INTERNAL = AtomIO.atom<string | null>({
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
key: `myId__INTERNAL`,
|
|
5
|
+
default: null,
|
|
6
6
|
})
|
|
7
7
|
export const myIdState = AtomIO.selector<string | null>({
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
key: `myId`,
|
|
9
|
+
get: ({ get }) => get(myIdState__INTERNAL),
|
|
10
10
|
})
|
|
@@ -8,20 +8,20 @@ import { RealtimeContext } from "./realtime-context"
|
|
|
8
8
|
import { StoreContext } from "../react"
|
|
9
9
|
|
|
10
10
|
export function usePullFamilyMember<J extends Json>(
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
|
|
12
|
+
subKey: AtomIO.Serializable,
|
|
13
13
|
): void {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
14
|
+
const token = family(subKey)
|
|
15
|
+
const { socket } = React.useContext(RealtimeContext)
|
|
16
|
+
const store = React.useContext(StoreContext)
|
|
17
|
+
React.useEffect(() => {
|
|
18
|
+
socket?.on(`serve:${token.key}`, (data: J) => {
|
|
19
|
+
AtomIO.setState(family(subKey), data, store)
|
|
20
|
+
})
|
|
21
|
+
socket?.emit(`sub:${family.key}`, subKey)
|
|
22
|
+
return () => {
|
|
23
|
+
socket?.off(`serve:${token.key}`)
|
|
24
|
+
socket?.emit(`unsub:${token.key}`)
|
|
25
|
+
}
|
|
26
|
+
}, [family.key])
|
|
27
27
|
}
|
|
@@ -8,18 +8,18 @@ import { RealtimeContext } from "./realtime-context"
|
|
|
8
8
|
import { StoreContext } from "../react"
|
|
9
9
|
|
|
10
10
|
export function usePullFamily<J extends Json>(
|
|
11
|
-
|
|
11
|
+
family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
|
|
12
12
|
): void {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
13
|
+
const { socket } = React.useContext(RealtimeContext)
|
|
14
|
+
const store = React.useContext(StoreContext)
|
|
15
|
+
React.useEffect(() => {
|
|
16
|
+
socket.on(`serve:${family.key}`, (key: Json, data: J) => {
|
|
17
|
+
AtomIO.setState(family(key), data, store)
|
|
18
|
+
})
|
|
19
|
+
socket?.emit(`sub:${family.key}`)
|
|
20
|
+
return () => {
|
|
21
|
+
socket?.off(`serve:${family.key}`)
|
|
22
|
+
socket?.emit(`unsub:${family.key}`)
|
|
23
|
+
}
|
|
24
|
+
}, [family.key])
|
|
25
25
|
}
|
|
@@ -8,16 +8,16 @@ import { RealtimeContext } from "./realtime-context"
|
|
|
8
8
|
import { StoreContext } from "../react"
|
|
9
9
|
|
|
10
10
|
export function usePull<J extends Json>(token: AtomIO.StateToken<J>): void {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
11
|
+
const { socket } = React.useContext(RealtimeContext)
|
|
12
|
+
const store = React.useContext(StoreContext)
|
|
13
|
+
React.useEffect(() => {
|
|
14
|
+
socket.on(`serve:${token.key}`, (data: J) => {
|
|
15
|
+
AtomIO.setState(token, data, store)
|
|
16
|
+
})
|
|
17
|
+
socket.emit(`sub:${token.key}`)
|
|
18
|
+
return () => {
|
|
19
|
+
socket.off(`serve:${token.key}`)
|
|
20
|
+
socket.emit(`unsub:${token.key}`)
|
|
21
|
+
}
|
|
22
|
+
}, [token.key])
|
|
23
23
|
}
|
|
@@ -8,19 +8,19 @@ import { RealtimeContext } from "./realtime-context"
|
|
|
8
8
|
import { StoreContext } from "../react"
|
|
9
9
|
|
|
10
10
|
export function usePush<J extends Json>(token: AtomIO.StateToken<J>): void {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
11
|
+
const { socket } = React.useContext(RealtimeContext)
|
|
12
|
+
const store = React.useContext(StoreContext)
|
|
13
|
+
React.useEffect(() => {
|
|
14
|
+
socket.emit(`claim:${token.key}`)
|
|
15
|
+
AtomIO.subscribe(
|
|
16
|
+
token,
|
|
17
|
+
({ newValue }) => {
|
|
18
|
+
socket.emit(`pub:${token.key}`, newValue)
|
|
19
|
+
},
|
|
20
|
+
store,
|
|
21
|
+
)
|
|
22
|
+
return () => {
|
|
23
|
+
socket.emit(`unclaim:${token.key}`)
|
|
24
|
+
}
|
|
25
|
+
}, [token.key])
|
|
26
26
|
}
|