atom.io 0.6.8 → 0.7.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.
Files changed (214) hide show
  1. package/README.md +21 -2
  2. package/dist/index.d.mts +42 -461
  3. package/dist/index.d.ts +42 -461
  4. package/dist/index.js +128 -1792
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +75 -1742
  7. package/dist/index.mjs.map +1 -1
  8. package/internal/dist/index.d.mts +342 -0
  9. package/internal/dist/index.d.ts +342 -0
  10. package/internal/dist/index.js +1873 -0
  11. package/internal/dist/index.js.map +1 -0
  12. package/internal/dist/index.mjs +1798 -0
  13. package/internal/dist/index.mjs.map +1 -0
  14. package/internal/package.json +15 -0
  15. package/internal/src/atom/create-atom.ts +75 -0
  16. package/internal/src/atom/delete-atom.ts +10 -0
  17. package/internal/src/atom/index.ts +3 -0
  18. package/{src/internal → internal/src/atom}/is-default.ts +4 -2
  19. package/internal/src/caching.ts +21 -0
  20. package/internal/src/families/create-atom-family.ts +59 -0
  21. package/internal/src/families/create-readonly-selector-family.ts +45 -0
  22. package/internal/src/families/create-selector-family.ts +67 -0
  23. package/internal/src/families/index.ts +3 -0
  24. package/internal/src/get-state-internal.ts +23 -0
  25. package/internal/src/index.ts +13 -0
  26. package/internal/src/mutable/create-mutable-atom-family.ts +25 -0
  27. package/internal/src/mutable/create-mutable-atom.ts +49 -0
  28. package/internal/src/mutable/get-json-token.ts +22 -0
  29. package/internal/src/mutable/get-update-token.ts +20 -0
  30. package/internal/src/mutable/index.ts +17 -0
  31. package/internal/src/mutable/is-atom-token-mutable.ts +7 -0
  32. package/internal/src/mutable/tracker-family.ts +61 -0
  33. package/internal/src/mutable/tracker.ts +164 -0
  34. package/internal/src/mutable/transceiver.ts +110 -0
  35. package/internal/src/operation.ts +68 -0
  36. package/{src/internal → internal/src}/selector/create-read-write-selector.ts +10 -13
  37. package/{src/internal → internal/src}/selector/create-readonly-selector.ts +9 -8
  38. package/internal/src/selector/create-selector.ts +65 -0
  39. package/{src/internal → internal/src}/selector/index.ts +1 -0
  40. package/internal/src/selector/lookup-selector-sources.ts +20 -0
  41. package/{src/internal → internal/src}/selector/register-selector.ts +13 -9
  42. package/{src/internal → internal/src}/selector/trace-selector-atoms.ts +4 -2
  43. package/{src/internal → internal/src}/selector/update-selector-atoms.ts +4 -3
  44. package/internal/src/set-state/become.ts +10 -0
  45. package/internal/src/set-state/copy-mutable-if-needed.ts +23 -0
  46. package/internal/src/set-state/copy-mutable-in-transaction.ts +59 -0
  47. package/internal/src/set-state/copy-mutable-into-new-store.ts +34 -0
  48. package/internal/src/set-state/emit-update.ts +23 -0
  49. package/internal/src/set-state/evict-downstream.ts +39 -0
  50. package/internal/src/set-state/index.ts +2 -0
  51. package/internal/src/set-state/set-atom-state.ts +38 -0
  52. package/internal/src/set-state/set-selector-state.ts +19 -0
  53. package/internal/src/set-state/set-state-internal.ts +18 -0
  54. package/internal/src/set-state/stow-update.ts +42 -0
  55. package/internal/src/store/deposit.ts +43 -0
  56. package/internal/src/store/index.ts +5 -0
  57. package/internal/src/store/lookup.ts +26 -0
  58. package/internal/src/store/store.ts +154 -0
  59. package/internal/src/store/withdraw-new-family-member.ts +53 -0
  60. package/internal/src/store/withdraw.ts +113 -0
  61. package/internal/src/subject.ts +21 -0
  62. package/internal/src/subscribe/index.ts +1 -0
  63. package/internal/src/subscribe/recall-state.ts +19 -0
  64. package/internal/src/subscribe/subscribe-to-root-atoms.ts +47 -0
  65. package/{src/internal → internal/src}/timeline/add-atom-to-timeline.ts +50 -29
  66. package/internal/src/timeline/index.ts +3 -0
  67. package/{src/internal → internal/src/timeline}/time-travel-internal.ts +6 -6
  68. package/{src/internal → internal/src/timeline}/timeline-internal.ts +20 -12
  69. package/{src/internal → internal/src}/transaction/abort-transaction.ts +1 -1
  70. package/{src/internal → internal/src}/transaction/apply-transaction.ts +25 -18
  71. package/{src/internal → internal/src}/transaction/build-transaction.ts +12 -6
  72. package/{src/internal → internal/src}/transaction/index.ts +3 -2
  73. package/{src/internal → internal/src}/transaction/redo-transaction.ts +4 -5
  74. package/{src/internal → internal/src/transaction}/transaction-internal.ts +16 -13
  75. package/{src/internal → internal/src}/transaction/undo-transaction.ts +4 -5
  76. package/introspection/dist/index.d.mts +12 -260
  77. package/introspection/dist/index.d.ts +12 -260
  78. package/introspection/dist/index.js +125 -140
  79. package/introspection/dist/index.js.map +1 -1
  80. package/introspection/dist/index.mjs +103 -116
  81. package/introspection/dist/index.mjs.map +1 -1
  82. package/{src/introspection → introspection/src}/attach-atom-index.ts +41 -30
  83. package/{src/introspection → introspection/src}/attach-introspection-states.ts +6 -10
  84. package/introspection/src/attach-selector-index.ts +90 -0
  85. package/{src/introspection → introspection/src}/attach-timeline-family.ts +16 -16
  86. package/introspection/src/attach-timeline-index.ts +38 -0
  87. package/introspection/src/attach-transaction-index.ts +40 -0
  88. package/{src/introspection → introspection/src}/attach-transaction-logs.ts +11 -8
  89. package/json/dist/index.d.mts +41 -2
  90. package/json/dist/index.d.ts +41 -2
  91. package/json/dist/index.js +88 -48
  92. package/json/dist/index.js.map +1 -1
  93. package/json/dist/index.mjs +76 -13
  94. package/json/dist/index.mjs.map +1 -1
  95. package/json/src/index.ts +5 -0
  96. package/json/src/select-json-family.ts +35 -0
  97. package/json/src/select-json.ts +22 -0
  98. package/package.json +105 -57
  99. package/react/dist/index.d.mts +9 -17
  100. package/react/dist/index.d.ts +9 -17
  101. package/react/dist/index.js +45 -77
  102. package/react/dist/index.js.map +1 -1
  103. package/react/dist/index.mjs +18 -34
  104. package/react/dist/index.mjs.map +1 -1
  105. package/react/src/store-context.tsx +12 -0
  106. package/react/src/store-hooks.ts +36 -0
  107. package/react-devtools/dist/index.css +1 -1
  108. package/react-devtools/dist/index.css.map +1 -1
  109. package/react-devtools/dist/index.d.mts +199 -230
  110. package/react-devtools/dist/index.d.ts +199 -230
  111. package/react-devtools/dist/index.js +610 -2466
  112. package/react-devtools/dist/index.js.map +1 -1
  113. package/react-devtools/dist/index.mjs +543 -2401
  114. package/react-devtools/dist/index.mjs.map +1 -1
  115. package/{src/react-devtools → react-devtools/src}/AtomIODevtools.tsx +5 -3
  116. package/{src/react-devtools → react-devtools/src}/Button.tsx +2 -3
  117. package/{src/react-devtools → react-devtools/src}/StateEditor.tsx +3 -2
  118. package/{src/react-devtools → react-devtools/src}/StateIndex.tsx +7 -4
  119. package/{src/react-devtools → react-devtools/src}/TimelineIndex.tsx +7 -11
  120. package/{src/react-devtools → react-devtools/src}/TransactionIndex.tsx +4 -4
  121. package/{src/react-devtools → react-devtools/src}/Updates.tsx +9 -4
  122. package/{src/react-devtools → react-devtools/src}/index.ts +5 -5
  123. package/realtime-react/dist/index.d.mts +9 -25
  124. package/realtime-react/dist/index.d.ts +9 -25
  125. package/realtime-react/dist/index.js +75 -193
  126. package/realtime-react/dist/index.js.map +1 -1
  127. package/realtime-react/dist/index.mjs +44 -148
  128. package/realtime-react/dist/index.mjs.map +1 -1
  129. package/realtime-react/src/index.ts +7 -0
  130. package/{src/realtime-react → realtime-react/src}/realtime-context.tsx +3 -4
  131. package/realtime-react/src/use-pull-family-member.ts +15 -0
  132. package/realtime-react/src/use-pull-mutable-family-member.ts +20 -0
  133. package/realtime-react/src/use-pull-mutable.ts +17 -0
  134. package/realtime-react/src/use-pull.ts +15 -0
  135. package/realtime-react/src/use-push.ts +19 -0
  136. package/realtime-react/src/use-server-action.ts +18 -0
  137. package/realtime-testing/dist/index.d.mts +49 -0
  138. package/realtime-testing/dist/index.d.ts +49 -0
  139. package/realtime-testing/dist/index.js +147 -0
  140. package/realtime-testing/dist/index.js.map +1 -0
  141. package/realtime-testing/dist/index.mjs +116 -0
  142. package/realtime-testing/dist/index.mjs.map +1 -0
  143. package/{src/realtime-testing → realtime-testing/src}/setup-realtime-test.tsx +10 -8
  144. package/src/atom.ts +64 -8
  145. package/src/index.ts +36 -29
  146. package/src/logger.ts +7 -7
  147. package/src/selector.ts +5 -5
  148. package/src/silo.ts +49 -43
  149. package/src/subscribe.ts +27 -22
  150. package/src/timeline.ts +9 -4
  151. package/src/transaction.ts +3 -4
  152. package/transceivers/set-rtx/dist/index.d.mts +39 -0
  153. package/transceivers/set-rtx/dist/index.d.ts +39 -0
  154. package/transceivers/set-rtx/dist/index.js +213 -0
  155. package/transceivers/set-rtx/dist/index.js.map +1 -0
  156. package/transceivers/set-rtx/dist/index.mjs +211 -0
  157. package/transceivers/set-rtx/dist/index.mjs.map +1 -0
  158. package/{realtime → transceivers/set-rtx}/package.json +1 -1
  159. package/transceivers/set-rtx/src/index.ts +1 -0
  160. package/transceivers/set-rtx/src/set-rtx.ts +242 -0
  161. package/realtime/dist/index.d.mts +0 -25
  162. package/realtime/dist/index.d.ts +0 -25
  163. package/realtime/dist/index.js +0 -190
  164. package/realtime/dist/index.js.map +0 -1
  165. package/realtime/dist/index.mjs +0 -151
  166. package/realtime/dist/index.mjs.map +0 -1
  167. package/src/internal/atom-internal.ts +0 -54
  168. package/src/internal/families-internal.ts +0 -144
  169. package/src/internal/get.ts +0 -129
  170. package/src/internal/index.ts +0 -15
  171. package/src/internal/operation.ts +0 -139
  172. package/src/internal/selector/lookup-selector-sources.ts +0 -16
  173. package/src/internal/selector-internal.ts +0 -58
  174. package/src/internal/set.ts +0 -99
  175. package/src/internal/store.ts +0 -151
  176. package/src/internal/subscribe-internal.ts +0 -88
  177. package/src/internal/timeline/index.ts +0 -1
  178. package/src/introspection/attach-selector-index.ts +0 -77
  179. package/src/introspection/attach-timeline-index.ts +0 -36
  180. package/src/introspection/attach-transaction-index.ts +0 -38
  181. package/src/json/index.ts +0 -1
  182. package/src/json/select-json.ts +0 -18
  183. package/src/react/store-context.tsx +0 -13
  184. package/src/react/store-hooks.ts +0 -47
  185. package/src/react-explorer/AtomIOExplorer.tsx +0 -218
  186. package/src/react-explorer/explorer-effects.ts +0 -20
  187. package/src/react-explorer/explorer-states.ts +0 -217
  188. package/src/react-explorer/index.ts +0 -23
  189. package/src/react-explorer/space-states.ts +0 -72
  190. package/src/react-explorer/view-states.ts +0 -41
  191. package/src/realtime/README.md +0 -33
  192. package/src/realtime/hook-composition/expose-family.ts +0 -101
  193. package/src/realtime/hook-composition/expose-single.ts +0 -38
  194. package/src/realtime/hook-composition/expose-timeline.ts +0 -60
  195. package/src/realtime/hook-composition/index.ts +0 -12
  196. package/src/realtime/hook-composition/receive-state.ts +0 -29
  197. package/src/realtime/hook-composition/receive-transaction.ts +0 -18
  198. package/src/realtime/index.ts +0 -1
  199. package/src/realtime-react/index.ts +0 -3
  200. package/src/realtime-react/realtime-hooks.ts +0 -39
  201. package/src/realtime-react/realtime-state.ts +0 -10
  202. package/src/realtime-react/use-pull-family-member.ts +0 -26
  203. package/src/realtime-react/use-pull-family.ts +0 -24
  204. package/src/realtime-react/use-pull.ts +0 -24
  205. package/src/realtime-react/use-push.ts +0 -27
  206. package/src/realtime-react/use-server-action.ts +0 -33
  207. package/src/tracker/index.ts +0 -3
  208. package/src/tracker/tracker.ts +0 -61
  209. package/src/web-effects/index.ts +0 -1
  210. package/src/web-effects/storage.ts +0 -30
  211. /package/{src/introspection → introspection/src}/index.ts +0 -0
  212. /package/{src/react → react/src}/index.ts +0 -0
  213. /package/{src/react-devtools → react-devtools/src}/devtools.scss +0 -0
  214. /package/{src/realtime-testing → realtime-testing/src}/index.ts +0 -0
@@ -1,101 +0,0 @@
1
- import type { Json } from "anvl/json"
2
- import { parseJson } from "anvl/json"
3
- import * as AtomIO from "atom.io"
4
-
5
- import type { ServerConfig } from ".."
6
-
7
- const subscribeToTokenCreation = <T>(
8
- family: AtomIO.AtomFamily<T> | AtomIO.SelectorFamily<T>,
9
- handleTokenCreation: (token: AtomIO.StateToken<T>) => void,
10
- ): (() => void) => {
11
- const subscription =
12
- family.type === `atom_family`
13
- ? family.subject.subscribe(handleTokenCreation)
14
- : family.subject.subscribe(handleTokenCreation)
15
- return () => subscription.unsubscribe()
16
- }
17
-
18
- /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
19
- export const useExposeFamily = ({ socket, store }: ServerConfig) => {
20
- return function exposeFamily<J extends Json.Serializable>(
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
-
27
- const fillFamilyUnsubRequest = () => {
28
- unsubFamilyCallbacksByKey.forEach((unsub) => unsub())
29
- unsubFamilyCallbacksByKey.clear()
30
- socket.off(`unsub:${family.key}`, fillFamilyUnsubRequest)
31
- }
32
-
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
-
42
- const fillSubRequest = (subKey?: AtomIO.Json.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
-
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
-
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
-
91
- socket.on(`sub:${family.key}`, fillSubRequest)
92
-
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
- }
@@ -1,38 +0,0 @@
1
- import type { Json } from "anvl/json"
2
- import * as AtomIO from "atom.io"
3
-
4
- import type { ServerConfig } from ".."
5
-
6
- /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
7
- export const useExposeSingle = ({ socket, store }: ServerConfig) => {
8
- return function exposeSingle<J extends Json.Serializable>(
9
- token: AtomIO.StateToken<J>,
10
- ): () => void {
11
- let unsubscribeFromStateUpdates: (() => void) | null = null
12
-
13
- const fillUnsubRequest = () => {
14
- socket.off(`unsub:${token.key}`, fillUnsubRequest)
15
- unsubscribeFromStateUpdates?.()
16
- unsubscribeFromStateUpdates = null
17
- }
18
-
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
-
31
- socket.on(`sub:${token.key}`, fillSubRequest)
32
-
33
- return () => {
34
- socket.off(`sub:${token.key}`, fillSubRequest)
35
- unsubscribeFromStateUpdates?.()
36
- }
37
- }
38
- }
@@ -1,60 +0,0 @@
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
- }
@@ -1,12 +0,0 @@
1
- import type * as AtomIO from "atom.io"
2
- import type * as SocketIO from "socket.io"
3
-
4
- export * from "./expose-single"
5
- export * from "./expose-family"
6
- export * from "./receive-transaction"
7
- export * from "./receive-state"
8
-
9
- export type ServerConfig = {
10
- socket: SocketIO.Socket
11
- store?: AtomIO.__INTERNAL__.Store
12
- }
@@ -1,29 +0,0 @@
1
- import type { Json } from "anvl/json"
2
- import * as AtomIO from "atom.io"
3
-
4
- import type { ServerConfig } from ".."
5
-
6
- /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
7
- export const useReceiveState = ({ socket, store }: ServerConfig) => {
8
- return function receiveState<J extends Json.Serializable>(
9
- token: AtomIO.StateToken<J>,
10
- ): () => void {
11
- const publish = (newValue: J) => AtomIO.setState(token, newValue, store)
12
-
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
-
22
- socket.on(`claim:${token.key}`, fillPubClaim)
23
-
24
- return () => {
25
- socket.off(`claim:${token.key}`, fillPubClaim)
26
- socket.off(`pub:${token.key}`, publish)
27
- }
28
- }
29
- }
@@ -1,18 +0,0 @@
1
- import * as AtomIO from "atom.io"
2
-
3
- import type { ƒn } from "~/packages/anvl/src/function"
4
-
5
- import type { ServerConfig } from "."
6
-
7
- export const useReceiveTransaction = ({ socket, store }: ServerConfig) => {
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
-
14
- socket.on(`tx:${tx.key}`, fillTransactionRequest)
15
-
16
- return () => socket.off(`tx:${tx.key}`, fillTransactionRequest)
17
- }
18
- }
@@ -1 +0,0 @@
1
- export * from "./hook-composition"
@@ -1,3 +0,0 @@
1
- export * from "./realtime-context"
2
- export * from "./realtime-hooks"
3
- export * from "./realtime-state"
@@ -1,39 +0,0 @@
1
- import type * as AtomIO from "atom.io"
2
-
3
- import type { ƒn } from "~/packages/anvl/src/function"
4
- import type { Json } from "~/packages/anvl/src/json"
5
-
6
- import { usePull } from "./use-pull"
7
- import { usePullFamily } from "./use-pull-family"
8
- import { usePullFamilyMember } from "./use-pull-family-member"
9
- import { usePush } from "./use-push"
10
- import { useServerAction } from "./use-server-action"
11
-
12
- export type RealtimeHooks = {
13
- usePull: <J extends Json.Serializable>(token: AtomIO.StateToken<J>) => void
14
- usePullFamily: <J extends Json.Serializable>(
15
- family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
16
- ) => void
17
- usePullFamilyMember: <J extends Json.Serializable>(
18
- family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
19
- subKey: string,
20
- ) => void
21
- usePush: <J extends Json.Serializable>(token: AtomIO.StateToken<J>) => void
22
- useServerAction: <ƒ extends ƒn>(
23
- token: AtomIO.TransactionToken<ƒ>,
24
- ) => (...parameters: Parameters<ƒ>) => ReturnType<ƒ>
25
- }
26
-
27
- export const realtimeHooks: RealtimeHooks = {
28
- usePull,
29
- usePullFamily,
30
- usePullFamilyMember,
31
- usePush,
32
- useServerAction,
33
- }
34
-
35
- export * from "./use-pull"
36
- export * from "./use-pull-family"
37
- export * from "./use-pull-family-member"
38
- export * from "./use-push"
39
- export * from "./use-server-action"
@@ -1,10 +0,0 @@
1
- import * as AtomIO from "atom.io"
2
-
3
- export const myIdState__INTERNAL = AtomIO.atom<string | null>({
4
- key: `myId__INTERNAL`,
5
- default: null,
6
- })
7
- export const myIdState = AtomIO.selector<string | null>({
8
- key: `myId`,
9
- get: ({ get }) => get(myIdState__INTERNAL),
10
- })
@@ -1,26 +0,0 @@
1
- import * as AtomIO from "atom.io"
2
- import * as React from "react"
3
-
4
- import type { Json } from "~/packages/anvl/src/json"
5
-
6
- import { RealtimeContext } from "./realtime-context"
7
- import { StoreContext } from "../react"
8
-
9
- export function usePullFamilyMember<J extends Json.Serializable>(
10
- family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
11
- subKey: AtomIO.Json.Serializable,
12
- ): void {
13
- const token = family(subKey)
14
- const { socket } = React.useContext(RealtimeContext)
15
- const store = React.useContext(StoreContext)
16
- React.useEffect(() => {
17
- socket?.on(`serve:${token.key}`, (data: J) => {
18
- AtomIO.setState(family(subKey), data, store)
19
- })
20
- socket?.emit(`sub:${family.key}`, subKey)
21
- return () => {
22
- socket?.off(`serve:${token.key}`)
23
- socket?.emit(`unsub:${token.key}`)
24
- }
25
- }, [family.key])
26
- }
@@ -1,24 +0,0 @@
1
- import * as AtomIO from "atom.io"
2
- import * as React from "react"
3
-
4
- import type { Json } from "~/packages/anvl/src/json"
5
-
6
- import { RealtimeContext } from "./realtime-context"
7
- import { StoreContext } from "../react"
8
-
9
- export function usePullFamily<J extends Json.Serializable>(
10
- family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
11
- ): void {
12
- const { socket } = React.useContext(RealtimeContext)
13
- const store = React.useContext(StoreContext)
14
- React.useEffect(() => {
15
- socket.on(`serve:${family.key}`, (key: Json.Serializable, data: J) => {
16
- AtomIO.setState(family(key), data, store)
17
- })
18
- socket?.emit(`sub:${family.key}`)
19
- return () => {
20
- socket?.off(`serve:${family.key}`)
21
- socket?.emit(`unsub:${family.key}`)
22
- }
23
- }, [family.key])
24
- }
@@ -1,24 +0,0 @@
1
- import * as AtomIO from "atom.io"
2
- import * as React from "react"
3
-
4
- import type { Json } from "~/packages/anvl/src/json"
5
-
6
- import { RealtimeContext } from "./realtime-context"
7
- import { StoreContext } from "../react"
8
-
9
- export function usePull<J extends Json.Serializable>(
10
- token: AtomIO.StateToken<J>,
11
- ): void {
12
- const { socket } = React.useContext(RealtimeContext)
13
- const store = React.useContext(StoreContext)
14
- React.useEffect(() => {
15
- socket.on(`serve:${token.key}`, (data: J) => {
16
- AtomIO.setState(token, data, store)
17
- })
18
- socket.emit(`sub:${token.key}`)
19
- return () => {
20
- socket.off(`serve:${token.key}`)
21
- socket.emit(`unsub:${token.key}`)
22
- }
23
- }, [token.key])
24
- }
@@ -1,27 +0,0 @@
1
- import * as AtomIO from "atom.io"
2
- import * as React from "react"
3
-
4
- import type { Json } from "~/packages/anvl/src/json"
5
-
6
- import { RealtimeContext } from "./realtime-context"
7
- import { StoreContext } from "../react"
8
-
9
- export function usePush<J extends Json.Serializable>(
10
- token: AtomIO.StateToken<J>,
11
- ): void {
12
- const { socket } = React.useContext(RealtimeContext)
13
- const store = React.useContext(StoreContext)
14
- React.useEffect(() => {
15
- socket.emit(`claim:${token.key}`)
16
- AtomIO.subscribe(
17
- token,
18
- ({ newValue }) => {
19
- socket.emit(`pub:${token.key}`, newValue)
20
- },
21
- store,
22
- )
23
- return () => {
24
- socket.emit(`unclaim:${token.key}`)
25
- }
26
- }, [token.key])
27
- }
@@ -1,33 +0,0 @@
1
- import * as AtomIO from "atom.io"
2
- import { StoreContext } from "atom.io/react"
3
- import * as React from "react"
4
-
5
- import type { ƒn } from "~/packages/anvl/src/function"
6
-
7
- import { RealtimeContext } from "./realtime-context"
8
-
9
- const TX_SUBS = new Map<string, number>()
10
- export function useServerAction<ƒ extends ƒn>(
11
- token: AtomIO.TransactionToken<ƒ>,
12
- ): (...parameters: Parameters<ƒ>) => ReturnType<ƒ> {
13
- const store = React.useContext(StoreContext)
14
- const { socket } = React.useContext(RealtimeContext)
15
- React.useEffect(() => {
16
- const count = TX_SUBS.get(token.key) ?? 0
17
- TX_SUBS.set(token.key, count + 1)
18
- const unsubscribe =
19
- count === 0
20
- ? AtomIO.subscribeToTransaction(
21
- token,
22
- (update) => socket.emit(`tx:${token.key}`, update),
23
- store,
24
- )
25
- : () => null
26
- return () => {
27
- const newCount = TX_SUBS.get(token.key) ?? 0
28
- TX_SUBS.set(token.key, newCount - 1)
29
- unsubscribe()
30
- }
31
- }, [token.key])
32
- return AtomIO.runTransaction(token, store)
33
- }
@@ -1,3 +0,0 @@
1
- export * from "./tracker"
2
-
3
- export * from "~/packages/anvl/reactivity/transceiver"
@@ -1,61 +0,0 @@
1
- import * as AtomIO from "atom.io"
2
-
3
- import type { Transceiver } from "."
4
-
5
- export type CacheOptions<Core extends Transceiver<any>> = {
6
- key: string
7
- default: Core
8
- }
9
-
10
- export const tracker = <Core extends Transceiver<any>>(
11
- coreState: AtomIO.AtomToken<Core>,
12
- store: AtomIO.__INTERNAL__.Store = AtomIO.__INTERNAL__.IMPLICIT.STORE,
13
- ): AtomIO.AtomToken<
14
- (Core extends Transceiver<infer Signal> ? Signal : never) | null
15
- > => {
16
- const trackerKey = `${coreState.key}:signal`
17
- const trackerState = AtomIO.atom<
18
- (Core extends Transceiver<infer Signal> ? Signal : never) | null
19
- >({
20
- key: trackerKey,
21
- default: null,
22
- effects: [
23
- ({ setSelf }) => {
24
- AtomIO.getState(coreState).observe((update) => setSelf(update))
25
- },
26
- ({ onSet }) => {
27
- onSet(({ newValue, oldValue }) => {
28
- const timelineId = store.timelineAtoms.getRelatedId(trackerKey)
29
- if (timelineId) {
30
- const timelineData = store.timelines.get(timelineId)
31
- if (timelineData?.timeTraveling) {
32
- const unsubscribe = AtomIO.subscribeToTimeline(
33
- { key: timelineId, type: `timeline` },
34
- (update) => {
35
- unsubscribe()
36
- AtomIO.setState(coreState, (core) => {
37
- if (update === `redo` && newValue) {
38
- core.do(newValue)
39
- } else if (update === `undo` && oldValue) {
40
- core.undo(oldValue)
41
- }
42
- return core
43
- })
44
- },
45
- )
46
- return
47
- }
48
- }
49
-
50
- const { unsubscribe } = store.subject.operationStatus.subscribe(() => {
51
- unsubscribe()
52
- if (newValue) {
53
- AtomIO.setState(coreState, (core) => (core.do(newValue), core))
54
- }
55
- })
56
- })
57
- },
58
- ],
59
- })
60
- return trackerState
61
- }
@@ -1 +0,0 @@
1
- export * from "./storage"
@@ -1,30 +0,0 @@
1
- import type { Json } from "~/packages/anvl/src/json"
2
-
3
- import type { AtomEffect } from "../index"
4
-
5
- export type StringInterface<T> = {
6
- stringify: (t: T) => string
7
- parse: (s: string) => T
8
- }
9
-
10
- export const persistAtom =
11
- <T>(storage: Storage) =>
12
- ({ stringify, parse }: StringInterface<T>) =>
13
- (key: string): AtomEffect<T> =>
14
- ({ setSelf, onSet }) => {
15
- const savedValue = storage.getItem(key)
16
-
17
- if (savedValue != null) setSelf(parse(savedValue))
18
-
19
- onSet(({ newValue }) => {
20
- if (newValue == null) {
21
- storage.removeItem(key)
22
- return
23
- }
24
- storage.setItem(key, stringify(newValue))
25
- })
26
- }
27
-
28
- export const lazyLocalStorageEffect: <J extends Json.Serializable>(
29
- key: string,
30
- ) => AtomEffect<J> = persistAtom(localStorage)(JSON)
File without changes