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,151 +0,0 @@
1
- // ../src/realtime/hook-composition/expose-single.ts
2
- import * as AtomIO from "atom.io";
3
- var useExposeSingle = ({ socket, store }) => {
4
- return function exposeSingle(token) {
5
- let unsubscribeFromStateUpdates = null;
6
- const fillUnsubRequest = () => {
7
- socket.off(`unsub:${token.key}`, fillUnsubRequest);
8
- unsubscribeFromStateUpdates == null ? void 0 : unsubscribeFromStateUpdates();
9
- unsubscribeFromStateUpdates = null;
10
- };
11
- const fillSubRequest = () => {
12
- socket.emit(`serve:${token.key}`, AtomIO.getState(token, store));
13
- unsubscribeFromStateUpdates = AtomIO.subscribe(
14
- token,
15
- ({ newValue }) => {
16
- socket.emit(`serve:${token.key}`, newValue);
17
- },
18
- store
19
- );
20
- socket.on(`unsub:${token.key}`, fillUnsubRequest);
21
- };
22
- socket.on(`sub:${token.key}`, fillSubRequest);
23
- return () => {
24
- socket.off(`sub:${token.key}`, fillSubRequest);
25
- unsubscribeFromStateUpdates == null ? void 0 : unsubscribeFromStateUpdates();
26
- };
27
- };
28
- };
29
-
30
- // ../../anvl/src/json/index.ts
31
- var parseJson = (str) => JSON.parse(str);
32
-
33
- // ../src/realtime/hook-composition/expose-family.ts
34
- import * as AtomIO2 from "atom.io";
35
- var subscribeToTokenCreation = (family, handleTokenCreation) => {
36
- const subscription = family.type === `atom_family` ? family.subject.subscribe(handleTokenCreation) : family.subject.subscribe(handleTokenCreation);
37
- return () => subscription.unsubscribe();
38
- };
39
- var useExposeFamily = ({ socket, store }) => {
40
- return function exposeFamily(family, index) {
41
- const unsubSingleCallbacksByKey = /* @__PURE__ */ new Map();
42
- const unsubFamilyCallbacksByKey = /* @__PURE__ */ new Map();
43
- const fillFamilyUnsubRequest = () => {
44
- unsubFamilyCallbacksByKey.forEach((unsub) => unsub());
45
- unsubFamilyCallbacksByKey.clear();
46
- socket.off(`unsub:${family.key}`, fillFamilyUnsubRequest);
47
- };
48
- const fillSingleUnsubRequest = (key) => {
49
- socket.off(`unsub:${key}`, fillSingleUnsubRequest);
50
- const unsub = unsubSingleCallbacksByKey.get(key);
51
- if (unsub) {
52
- unsub();
53
- unsubSingleCallbacksByKey.delete(key);
54
- }
55
- };
56
- const fillSubRequest = (subKey) => {
57
- if (subKey === void 0) {
58
- const keys = AtomIO2.getState(index, store);
59
- keys.forEach((key) => {
60
- var _a;
61
- const token = family(key);
62
- socket.emit(
63
- `serve:${family.key}`,
64
- parseJson(((_a = token.family) == null ? void 0 : _a.subKey) || `null`),
65
- AtomIO2.getState(token, store)
66
- );
67
- });
68
- const unsubscribeFromTokenCreation = subscribeToTokenCreation(
69
- family,
70
- (token) => {
71
- const unsub = AtomIO2.subscribe(
72
- token,
73
- ({ newValue }) => {
74
- var _a;
75
- socket.emit(
76
- `serve:${family.key}`,
77
- parseJson(((_a = token.family) == null ? void 0 : _a.subKey) || `null`),
78
- newValue
79
- );
80
- },
81
- store
82
- );
83
- unsubFamilyCallbacksByKey.set(token.key, unsub);
84
- }
85
- );
86
- unsubFamilyCallbacksByKey.set(family.key, unsubscribeFromTokenCreation);
87
- socket.on(`unsub:${family.key}`, fillFamilyUnsubRequest);
88
- } else {
89
- const token = family(subKey);
90
- socket.emit(`serve:${token.key}`, AtomIO2.getState(token, store));
91
- const unsubscribe = AtomIO2.subscribe(
92
- token,
93
- ({ newValue }) => {
94
- socket.emit(`serve:${token.key}`, newValue);
95
- },
96
- store
97
- );
98
- unsubSingleCallbacksByKey.set(token.key, unsubscribe);
99
- socket.on(`unsub:${token.key}`, () => {
100
- fillSingleUnsubRequest(token.key);
101
- });
102
- }
103
- };
104
- socket.on(`sub:${family.key}`, fillSubRequest);
105
- return () => {
106
- socket.off(`sub:${family.key}`, fillSubRequest);
107
- unsubFamilyCallbacksByKey.forEach((unsub) => unsub());
108
- unsubSingleCallbacksByKey.forEach((unsub) => unsub());
109
- unsubFamilyCallbacksByKey.clear();
110
- unsubSingleCallbacksByKey.clear();
111
- };
112
- };
113
- };
114
-
115
- // ../src/realtime/hook-composition/receive-transaction.ts
116
- import * as AtomIO3 from "atom.io";
117
- var useReceiveTransaction = ({ socket, store }) => {
118
- return function receiveTransaction(tx) {
119
- const fillTransactionRequest = (update) => AtomIO3.runTransaction(tx, store)(...update.params);
120
- socket.on(`tx:${tx.key}`, fillTransactionRequest);
121
- return () => socket.off(`tx:${tx.key}`, fillTransactionRequest);
122
- };
123
- };
124
-
125
- // ../src/realtime/hook-composition/receive-state.ts
126
- import * as AtomIO4 from "atom.io";
127
- var useReceiveState = ({ socket, store }) => {
128
- return function receiveState(token) {
129
- const publish = (newValue) => AtomIO4.setState(token, newValue, store);
130
- const fillPubUnclaim = () => {
131
- socket.off(`pub:${token.key}`, publish);
132
- socket.off(`unclaim:${token.key}`, fillPubUnclaim);
133
- };
134
- const fillPubClaim = () => {
135
- socket.on(`pub:${token.key}`, publish);
136
- socket.on(`unclaim:${token.key}`, fillPubUnclaim);
137
- };
138
- socket.on(`claim:${token.key}`, fillPubClaim);
139
- return () => {
140
- socket.off(`claim:${token.key}`, fillPubClaim);
141
- socket.off(`pub:${token.key}`, publish);
142
- };
143
- };
144
- };
145
- export {
146
- useExposeFamily,
147
- useExposeSingle,
148
- useReceiveState,
149
- useReceiveTransaction
150
- };
151
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/realtime/hook-composition/expose-single.ts","../../../anvl/src/json/index.ts","../../src/realtime/hook-composition/expose-family.ts","../../src/realtime/hook-composition/receive-transaction.ts","../../src/realtime/hook-composition/receive-state.ts"],"sourcesContent":["import type { Json } from \"anvl/json\"\nimport * as AtomIO from \"atom.io\"\n\nimport type { ServerConfig } from \"..\"\n\n/* eslint-disable @typescript-eslint/explicit-module-boundary-types */\nexport const useExposeSingle = ({ socket, store }: ServerConfig) => {\n\treturn function exposeSingle<J extends Json.Serializable>(\n\t\ttoken: AtomIO.StateToken<J>,\n\t): () => void {\n\t\tlet unsubscribeFromStateUpdates: (() => void) | null = null\n\n\t\tconst fillUnsubRequest = () => {\n\t\t\tsocket.off(`unsub:${token.key}`, fillUnsubRequest)\n\t\t\tunsubscribeFromStateUpdates?.()\n\t\t\tunsubscribeFromStateUpdates = null\n\t\t}\n\n\t\tconst fillSubRequest = () => {\n\t\t\tsocket.emit(`serve:${token.key}`, AtomIO.getState(token, store))\n\t\t\tunsubscribeFromStateUpdates = AtomIO.subscribe(\n\t\t\t\ttoken,\n\t\t\t\t({ newValue }) => {\n\t\t\t\t\tsocket.emit(`serve:${token.key}`, newValue)\n\t\t\t\t},\n\t\t\t\tstore,\n\t\t\t)\n\t\t\tsocket.on(`unsub:${token.key}`, fillUnsubRequest)\n\t\t}\n\n\t\tsocket.on(`sub:${token.key}`, fillSubRequest)\n\n\t\treturn () => {\n\t\t\tsocket.off(`sub:${token.key}`, fillSubRequest)\n\t\t\tunsubscribeFromStateUpdates?.()\n\t\t}\n\t}\n}\n","export * from \"./json-interface\"\nimport type * as Json from \"./json\"\nexport { Json }\n\nexport const parseJson = <S extends Stringified<Json.Serializable>>(\n\tstr: S | string,\n): S extends Stringified<infer J> ? J : Json.Serializable => JSON.parse(str)\n\nexport type Stringified<J extends Json.Serializable> = string & { __json: J }\n\nexport const stringifyJson = <J extends Json.Serializable>(\n\tjson: J,\n): Stringified<J> => JSON.stringify(json) as Stringified<J>\n\nexport type Empty = Record<string, never>\n\nexport const JSON_TYPE_NAMES = [\n\t`array`,\n\t`boolean`,\n\t`null`,\n\t`number`,\n\t`object`,\n\t`string`,\n] as const\n\nexport type JsonTypeName = typeof JSON_TYPE_NAMES[number]\n\nexport interface JsonTypes extends Record<JsonTypeName, Json.Serializable> {\n\tarray: Json.Array\n\tboolean: boolean\n\tnull: null\n\tnumber: number\n\tobject: Json.Object\n\tstring: string\n}\n\nexport const JSON_DEFAULTS: JsonTypes = {\n\tarray: [],\n\tboolean: false,\n\tnull: null,\n\tnumber: 0,\n\tobject: {},\n\tstring: ``,\n}\n","import type { Json } from \"anvl/json\"\nimport { parseJson } from \"anvl/json\"\nimport * as AtomIO from \"atom.io\"\n\nimport type { ServerConfig } from \"..\"\n\nconst subscribeToTokenCreation = <T>(\n\tfamily: AtomIO.AtomFamily<T> | AtomIO.SelectorFamily<T>,\n\thandleTokenCreation: (token: AtomIO.StateToken<T>) => void,\n): (() => void) => {\n\tconst subscription =\n\t\tfamily.type === `atom_family`\n\t\t\t? family.subject.subscribe(handleTokenCreation)\n\t\t\t: family.subject.subscribe(handleTokenCreation)\n\treturn () => subscription.unsubscribe()\n}\n\n/* eslint-disable @typescript-eslint/explicit-module-boundary-types */\nexport const useExposeFamily = ({ socket, store }: ServerConfig) => {\n\treturn function exposeFamily<J extends Json.Serializable>(\n\t\tfamily: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,\n\t\tindex: AtomIO.StateToken<Set<string>>,\n\t): () => void {\n\t\tconst unsubSingleCallbacksByKey = new Map<string, () => void>()\n\t\tconst unsubFamilyCallbacksByKey = new Map<string, () => void>()\n\n\t\tconst fillFamilyUnsubRequest = () => {\n\t\t\tunsubFamilyCallbacksByKey.forEach((unsub) => unsub())\n\t\t\tunsubFamilyCallbacksByKey.clear()\n\t\t\tsocket.off(`unsub:${family.key}`, fillFamilyUnsubRequest)\n\t\t}\n\n\t\tconst fillSingleUnsubRequest = (key: string) => {\n\t\t\tsocket.off(`unsub:${key}`, fillSingleUnsubRequest)\n\t\t\tconst unsub = unsubSingleCallbacksByKey.get(key)\n\t\t\tif (unsub) {\n\t\t\t\tunsub()\n\t\t\t\tunsubSingleCallbacksByKey.delete(key)\n\t\t\t}\n\t\t}\n\n\t\tconst fillSubRequest = (subKey?: AtomIO.Json.Serializable) => {\n\t\t\tif (subKey === undefined) {\n\t\t\t\tconst keys = AtomIO.getState(index, store)\n\t\t\t\tkeys.forEach((key) => {\n\t\t\t\t\tconst token = family(key)\n\t\t\t\t\tsocket.emit(\n\t\t\t\t\t\t`serve:${family.key}`,\n\t\t\t\t\t\tparseJson(token.family?.subKey || `null`),\n\t\t\t\t\t\tAtomIO.getState(token, store),\n\t\t\t\t\t)\n\t\t\t\t})\n\n\t\t\t\tconst unsubscribeFromTokenCreation = subscribeToTokenCreation(\n\t\t\t\t\tfamily,\n\t\t\t\t\t(token) => {\n\t\t\t\t\t\tconst unsub = AtomIO.subscribe(\n\t\t\t\t\t\t\ttoken,\n\t\t\t\t\t\t\t({ newValue }) => {\n\t\t\t\t\t\t\t\tsocket.emit(\n\t\t\t\t\t\t\t\t\t`serve:${family.key}`,\n\t\t\t\t\t\t\t\t\tparseJson(token.family?.subKey || `null`),\n\t\t\t\t\t\t\t\t\tnewValue,\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tstore,\n\t\t\t\t\t\t)\n\t\t\t\t\t\tunsubFamilyCallbacksByKey.set(token.key, unsub)\n\t\t\t\t\t},\n\t\t\t\t)\n\t\t\t\tunsubFamilyCallbacksByKey.set(family.key, unsubscribeFromTokenCreation)\n\n\t\t\t\tsocket.on(`unsub:${family.key}`, fillFamilyUnsubRequest)\n\t\t\t} else {\n\t\t\t\tconst token = family(subKey)\n\t\t\t\tsocket.emit(`serve:${token.key}`, AtomIO.getState(token, store))\n\t\t\t\tconst unsubscribe = AtomIO.subscribe(\n\t\t\t\t\ttoken,\n\t\t\t\t\t({ newValue }) => {\n\t\t\t\t\t\tsocket.emit(`serve:${token.key}`, newValue)\n\t\t\t\t\t},\n\t\t\t\t\tstore,\n\t\t\t\t)\n\t\t\t\tunsubSingleCallbacksByKey.set(token.key, unsubscribe)\n\t\t\t\tsocket.on(`unsub:${token.key}`, () => {\n\t\t\t\t\tfillSingleUnsubRequest(token.key)\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\n\t\tsocket.on(`sub:${family.key}`, fillSubRequest)\n\n\t\treturn () => {\n\t\t\tsocket.off(`sub:${family.key}`, fillSubRequest)\n\t\t\tunsubFamilyCallbacksByKey.forEach((unsub) => unsub())\n\t\t\tunsubSingleCallbacksByKey.forEach((unsub) => unsub())\n\t\t\tunsubFamilyCallbacksByKey.clear()\n\t\t\tunsubSingleCallbacksByKey.clear()\n\t\t}\n\t}\n}\n","import * as AtomIO from \"atom.io\"\n\nimport type { ƒn } from \"~/packages/anvl/src/function\"\n\nimport type { ServerConfig } from \".\"\n\nexport const useReceiveTransaction = ({ socket, store }: ServerConfig) => {\n\treturn function receiveTransaction<ƒ extends ƒn>(\n\t\ttx: AtomIO.TransactionToken<ƒ>,\n\t): () => void {\n\t\tconst fillTransactionRequest = (update: AtomIO.TransactionUpdate<ƒ>) =>\n\t\t\tAtomIO.runTransaction<ƒ>(tx, store)(...update.params)\n\n\t\tsocket.on(`tx:${tx.key}`, fillTransactionRequest)\n\n\t\treturn () => socket.off(`tx:${tx.key}`, fillTransactionRequest)\n\t}\n}\n","import type { Json } from \"anvl/json\"\nimport * as AtomIO from \"atom.io\"\n\nimport type { ServerConfig } from \"..\"\n\n/* eslint-disable @typescript-eslint/explicit-module-boundary-types */\nexport const useReceiveState = ({ socket, store }: ServerConfig) => {\n\treturn function receiveState<J extends Json.Serializable>(\n\t\ttoken: AtomIO.StateToken<J>,\n\t): () => void {\n\t\tconst publish = (newValue: J) => AtomIO.setState(token, newValue, store)\n\n\t\tconst fillPubUnclaim = () => {\n\t\t\tsocket.off(`pub:${token.key}`, publish)\n\t\t\tsocket.off(`unclaim:${token.key}`, fillPubUnclaim)\n\t\t}\n\t\tconst fillPubClaim = () => {\n\t\t\tsocket.on(`pub:${token.key}`, publish)\n\t\t\tsocket.on(`unclaim:${token.key}`, fillPubUnclaim)\n\t\t}\n\n\t\tsocket.on(`claim:${token.key}`, fillPubClaim)\n\n\t\treturn () => {\n\t\t\tsocket.off(`claim:${token.key}`, fillPubClaim)\n\t\t\tsocket.off(`pub:${token.key}`, publish)\n\t\t}\n\t}\n}\n"],"mappings":"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","names":["AtomIO","AtomIO","AtomIO"]}
@@ -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
- }
@@ -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
- }
@@ -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,139 +0,0 @@
1
- import type { Atom, ReadonlySelector, Selector } from "."
2
- import { target } from "."
3
- import type { Store } from "./store"
4
- import { IMPLICIT } from "./store"
5
- import type { StateToken } from ".."
6
-
7
- export type OperationProgress =
8
- | {
9
- open: false
10
- }
11
- | {
12
- open: true
13
- done: Set<string>
14
- prev: Map<string, any>
15
- time: number
16
- token: StateToken<any>
17
- }
18
-
19
- export const openOperation = (token: StateToken<any>, store: Store): void => {
20
- const core = target(store)
21
- if (core.operation.open) {
22
- store.config.logger?.error(
23
- `❌ failed to setState to "${token.key}" during a setState for "${core.operation.token.key}"`,
24
- )
25
- throw Symbol(`violation`)
26
- }
27
- core.operation = {
28
- open: true,
29
- done: new Set(),
30
- prev: new Map(store.valueMap),
31
- time: Date.now(),
32
- token,
33
- }
34
- store.config.logger?.info(
35
- `⭕ operation start from "${token.key}" in store "${store.config.name}"`,
36
- )
37
- }
38
- export const closeOperation = (store: Store): void => {
39
- const core = target(store)
40
- core.operation = { open: false }
41
- store.config.logger?.info(`🔴 operation done`)
42
- store.subject.operationStatus.next(core.operation)
43
- }
44
-
45
- export const isDone = (key: string, store: Store = IMPLICIT.STORE): boolean => {
46
- const core = target(store)
47
- if (!core.operation.open) {
48
- store.config.logger?.warn(
49
- `isDone called outside of an operation. This is probably a bug.`,
50
- )
51
- return true
52
- }
53
- return core.operation.done.has(key)
54
- }
55
- export const markDone = (key: string, store: Store = IMPLICIT.STORE): void => {
56
- const core = target(store)
57
- if (!core.operation.open) {
58
- store.config.logger?.warn(
59
- `markDone called outside of an operation. This is probably a bug.`,
60
- )
61
- return
62
- }
63
- core.operation.done.add(key)
64
- }
65
- export const recallState = <T>(
66
- state: Atom<T> | ReadonlySelector<T> | Selector<T>,
67
- store: Store = IMPLICIT.STORE,
68
- ): T => {
69
- const core = target(store)
70
- if (!core.operation.open) {
71
- store.config.logger?.warn(
72
- `recall called outside of an operation. This is probably a bug.`,
73
- )
74
- return core.valueMap.get(state.key)
75
- }
76
- return core.operation.prev.get(state.key)
77
- }
78
-
79
- export const cacheValue = (
80
- key: string,
81
- value: unknown,
82
- store: Store = IMPLICIT.STORE,
83
- ): void => {
84
- const core = target(store)
85
- core.valueMap.set(key, value)
86
- }
87
-
88
- export const evictCachedValue = (
89
- key: string,
90
- store: Store = IMPLICIT.STORE,
91
- ): void => {
92
- const core = target(store)
93
- core.valueMap.delete(key)
94
- }
95
- export const readCachedValue = <T>(
96
- key: string,
97
- store: Store = IMPLICIT.STORE,
98
- ): T => target(store).valueMap.get(key)
99
-
100
- export const isValueCached = (
101
- key: string,
102
- store: Store = IMPLICIT.STORE,
103
- ): boolean => target(store).valueMap.has(key)
104
-
105
- export const storeAtom = (
106
- atom: Atom<any>,
107
- store: Store = IMPLICIT.STORE,
108
- ): void => {
109
- const core = target(store)
110
- core.atoms.set(atom.key, atom)
111
- }
112
-
113
- export const storeSelector = (
114
- selector: Selector<any>,
115
- store: Store = IMPLICIT.STORE,
116
- ): void => {
117
- const core = target(store)
118
- core.selectors.set(selector.key, selector)
119
- }
120
-
121
- export const storeReadonlySelector = (
122
- selector: ReadonlySelector<any>,
123
- store: Store = IMPLICIT.STORE,
124
- ): void => {
125
- const core = target(store)
126
- core.readonlySelectors.set(selector.key, selector)
127
- }
128
-
129
- export const hasKeyBeenUsed = (
130
- key: string,
131
- store: Store = IMPLICIT.STORE,
132
- ): boolean => {
133
- const core = target(store)
134
- return (
135
- core.atoms.has(key) ||
136
- core.selectors.has(key) ||
137
- core.readonlySelectors.has(key)
138
- )
139
- }
@@ -1,16 +0,0 @@
1
- import type { Store } from ".."
2
- import { target, lookup } from ".."
3
- import type { AtomToken, ReadonlySelectorToken, SelectorToken } from "../.."
4
-
5
- export const lookupSelectorSources = (
6
- key: string,
7
- store: Store,
8
- ): (
9
- | AtomToken<unknown>
10
- | ReadonlySelectorToken<unknown>
11
- | SelectorToken<unknown>
12
- )[] =>
13
- target(store)
14
- .selectorGraph.getRelations(key)
15
- .filter(({ source }) => source !== key)
16
- .map(({ source }) => lookup(source, store))