atom.io 0.4.1 → 0.6.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 (101) hide show
  1. package/README.md +38 -10
  2. package/dist/index.d.mts +614 -0
  3. package/dist/index.d.ts +130 -77
  4. package/dist/index.js +584 -347
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +582 -347
  7. package/dist/index.mjs.map +1 -1
  8. package/json/dist/index.d.mts +18 -0
  9. package/json/dist/index.d.ts +18 -0
  10. package/json/dist/index.js +51 -0
  11. package/json/dist/index.js.map +1 -0
  12. package/json/dist/index.mjs +15 -0
  13. package/json/dist/index.mjs.map +1 -0
  14. package/json/package.json +15 -0
  15. package/package.json +43 -9
  16. package/react/dist/index.d.mts +24 -0
  17. package/react/dist/index.d.ts +18 -11
  18. package/react/dist/index.js +45 -21
  19. package/react/dist/index.js.map +1 -1
  20. package/react/dist/index.mjs +31 -21
  21. package/react/dist/index.mjs.map +1 -1
  22. package/react-devtools/dist/index.d.mts +15 -0
  23. package/react-devtools/dist/index.d.ts +4 -4
  24. package/react-devtools/dist/index.js +1 -1
  25. package/react-devtools/dist/index.js.map +1 -1
  26. package/react-devtools/dist/index.mjs +1 -1
  27. package/react-devtools/dist/index.mjs.map +1 -1
  28. package/realtime/dist/index.d.mts +27 -0
  29. package/realtime/dist/index.d.ts +27 -0
  30. package/realtime/dist/index.js +191 -0
  31. package/realtime/dist/index.js.map +1 -0
  32. package/realtime/dist/index.mjs +152 -0
  33. package/realtime/dist/index.mjs.map +1 -0
  34. package/realtime/package.json +15 -0
  35. package/realtime-react/dist/index.d.mts +45 -0
  36. package/realtime-react/dist/index.d.ts +45 -0
  37. package/realtime-react/dist/index.js +213 -0
  38. package/realtime-react/dist/index.js.map +1 -0
  39. package/realtime-react/dist/index.mjs +168 -0
  40. package/realtime-react/dist/index.mjs.map +1 -0
  41. package/realtime-react/package.json +15 -0
  42. package/src/index.ts +21 -5
  43. package/src/internal/atom-internal.ts +1 -1
  44. package/src/internal/families-internal.ts +3 -3
  45. package/src/internal/get.ts +39 -15
  46. package/src/internal/index.ts +2 -0
  47. package/src/internal/meta/meta-state.ts +1 -1
  48. package/src/internal/operation.ts +3 -1
  49. package/src/internal/selector/create-read-write-selector.ts +62 -0
  50. package/src/internal/selector/create-readonly-selector.ts +52 -0
  51. package/src/internal/selector/index.ts +4 -0
  52. package/src/internal/selector/lookup-selector-sources.ts +16 -0
  53. package/src/internal/selector/register-selector.ts +57 -0
  54. package/src/internal/selector/trace-selector-atoms.ts +43 -0
  55. package/src/internal/selector/update-selector-atoms.ts +33 -0
  56. package/src/internal/selector-internal.ts +9 -197
  57. package/src/internal/set.ts +1 -1
  58. package/src/internal/store.ts +44 -17
  59. package/src/internal/subscribe-internal.ts +6 -1
  60. package/src/internal/time-travel-internal.ts +7 -7
  61. package/src/internal/timeline/add-atom-to-timeline.ts +164 -0
  62. package/src/internal/timeline/index.ts +1 -0
  63. package/src/internal/timeline-internal.ts +39 -146
  64. package/src/internal/transaction/abort-transaction.ts +12 -0
  65. package/src/internal/transaction/apply-transaction.ts +54 -0
  66. package/src/internal/transaction/build-transaction.ts +33 -0
  67. package/src/internal/transaction/index.ts +25 -0
  68. package/src/internal/transaction/redo-transaction.ts +23 -0
  69. package/src/internal/transaction/undo-transaction.ts +23 -0
  70. package/src/internal/transaction-internal.ts +16 -133
  71. package/src/json/index.ts +1 -0
  72. package/src/json/select-json.ts +18 -0
  73. package/src/react/index.ts +2 -46
  74. package/src/react/store-context.tsx +14 -0
  75. package/src/react/store-hooks.ts +48 -0
  76. package/src/react-devtools/AtomIODevtools.tsx +1 -1
  77. package/src/react-explorer/AtomIOExplorer.tsx +2 -2
  78. package/src/react-explorer/explorer-states.ts +5 -5
  79. package/src/react-explorer/index.ts +1 -1
  80. package/src/react-explorer/space-states.ts +8 -9
  81. package/src/realtime/README.md +33 -0
  82. package/src/realtime/hook-composition/expose-family.ts +101 -0
  83. package/src/realtime/hook-composition/expose-single.ts +38 -0
  84. package/src/realtime/hook-composition/index.ts +12 -0
  85. package/src/realtime/hook-composition/receive-state.ts +29 -0
  86. package/src/realtime/hook-composition/receive-transaction.ts +18 -0
  87. package/src/realtime/index.ts +1 -0
  88. package/src/realtime-react/index.ts +3 -0
  89. package/src/realtime-react/realtime-context.tsx +31 -0
  90. package/src/realtime-react/realtime-hooks.ts +39 -0
  91. package/src/realtime-react/realtime-state.ts +10 -0
  92. package/src/realtime-react/use-pull-family-member.ts +27 -0
  93. package/src/realtime-react/use-pull-family.ts +25 -0
  94. package/src/realtime-react/use-pull.ts +23 -0
  95. package/src/realtime-react/use-push.ts +26 -0
  96. package/src/realtime-react/use-server-action.ts +34 -0
  97. package/src/selector.ts +9 -6
  98. package/src/silo.ts +53 -0
  99. package/src/subscribe.ts +42 -2
  100. package/src/timeline.ts +10 -0
  101. package/src/transaction.ts +24 -12
@@ -0,0 +1,213 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // ../src/realtime-react/index.ts
30
+ var realtime_react_exports = {};
31
+ __export(realtime_react_exports, {
32
+ RealtimeContext: () => RealtimeContext,
33
+ RealtimeProvider: () => RealtimeProvider,
34
+ myIdState: () => myIdState,
35
+ myIdState__INTERNAL: () => myIdState__INTERNAL,
36
+ realtimeHooks: () => realtimeHooks,
37
+ usePull: () => usePull,
38
+ usePullFamily: () => usePullFamily,
39
+ usePullFamilyMember: () => usePullFamilyMember,
40
+ usePush: () => usePush,
41
+ useServerAction: () => useServerAction
42
+ });
43
+ module.exports = __toCommonJS(realtime_react_exports);
44
+
45
+ // ../src/realtime-react/realtime-context.tsx
46
+ var React = __toESM(require("react"));
47
+ var AR = __toESM(require("atom.io/react"));
48
+ var import_socket = require("socket.io-client");
49
+
50
+ // ../src/realtime-react/realtime-state.ts
51
+ var AtomIO = __toESM(require("atom.io"));
52
+ var myIdState__INTERNAL = AtomIO.atom({
53
+ key: `myId__INTERNAL`,
54
+ default: null
55
+ });
56
+ var myIdState = AtomIO.selector({
57
+ key: `myId`,
58
+ get: ({ get }) => get(myIdState__INTERNAL)
59
+ });
60
+
61
+ // ../src/realtime-react/realtime-context.tsx
62
+ var import_jsx_runtime = require("@emotion/react/jsx-runtime");
63
+ var RealtimeContext = React.createContext({
64
+ socket: (0, import_socket.io)()
65
+ });
66
+ var RealtimeProvider = ({ children, socket }) => {
67
+ const setMyId = AR.useI(myIdState__INTERNAL);
68
+ React.useEffect(() => {
69
+ socket.on(`connect`, () => {
70
+ setMyId(socket.id);
71
+ });
72
+ socket.on(`disconnect`, () => {
73
+ setMyId(null);
74
+ });
75
+ }, [socket, setMyId]);
76
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RealtimeContext.Provider, { value: { socket }, children });
77
+ };
78
+
79
+ // ../src/realtime-react/use-pull.ts
80
+ var React3 = __toESM(require("react"));
81
+ var AtomIO3 = __toESM(require("atom.io"));
82
+
83
+ // ../src/react/store-context.tsx
84
+ var React2 = __toESM(require("react"));
85
+ var AtomIO2 = __toESM(require("atom.io"));
86
+ var import_jsx_runtime2 = require("@emotion/react/jsx-runtime");
87
+ var StoreContext = React2.createContext(
88
+ AtomIO2.__INTERNAL__.IMPLICIT.STORE
89
+ );
90
+
91
+ // ../src/realtime-react/use-pull.ts
92
+ function usePull(token) {
93
+ const { socket } = React3.useContext(RealtimeContext);
94
+ const store = React3.useContext(StoreContext);
95
+ React3.useEffect(() => {
96
+ socket.on(`serve:${token.key}`, (data) => {
97
+ AtomIO3.setState(token, data, store);
98
+ });
99
+ socket.emit(`sub:${token.key}`);
100
+ return () => {
101
+ socket.off(`serve:${token.key}`);
102
+ socket.emit(`unsub:${token.key}`);
103
+ };
104
+ }, [token.key]);
105
+ }
106
+
107
+ // ../src/realtime-react/use-pull-family.ts
108
+ var React4 = __toESM(require("react"));
109
+ var AtomIO4 = __toESM(require("atom.io"));
110
+ function usePullFamily(family) {
111
+ const { socket } = React4.useContext(RealtimeContext);
112
+ const store = React4.useContext(StoreContext);
113
+ React4.useEffect(() => {
114
+ socket.on(`serve:${family.key}`, (key, data) => {
115
+ AtomIO4.setState(family(key), data, store);
116
+ });
117
+ socket == null ? void 0 : socket.emit(`sub:${family.key}`);
118
+ return () => {
119
+ socket == null ? void 0 : socket.off(`serve:${family.key}`);
120
+ socket == null ? void 0 : socket.emit(`unsub:${family.key}`);
121
+ };
122
+ }, [family.key]);
123
+ }
124
+
125
+ // ../src/realtime-react/use-pull-family-member.ts
126
+ var React5 = __toESM(require("react"));
127
+ var AtomIO5 = __toESM(require("atom.io"));
128
+ function usePullFamilyMember(family, subKey) {
129
+ const token = family(subKey);
130
+ const { socket } = React5.useContext(RealtimeContext);
131
+ const store = React5.useContext(StoreContext);
132
+ React5.useEffect(() => {
133
+ socket == null ? void 0 : socket.on(`serve:${token.key}`, (data) => {
134
+ AtomIO5.setState(family(subKey), data, store);
135
+ });
136
+ socket == null ? void 0 : socket.emit(`sub:${family.key}`, subKey);
137
+ return () => {
138
+ socket == null ? void 0 : socket.off(`serve:${token.key}`);
139
+ socket == null ? void 0 : socket.emit(`unsub:${token.key}`);
140
+ };
141
+ }, [family.key]);
142
+ }
143
+
144
+ // ../src/realtime-react/use-push.ts
145
+ var React6 = __toESM(require("react"));
146
+ var AtomIO6 = __toESM(require("atom.io"));
147
+ function usePush(token) {
148
+ const { socket } = React6.useContext(RealtimeContext);
149
+ const store = React6.useContext(StoreContext);
150
+ React6.useEffect(() => {
151
+ socket.emit(`claim:${token.key}`);
152
+ AtomIO6.subscribe(
153
+ token,
154
+ ({ newValue }) => {
155
+ socket.emit(`pub:${token.key}`, newValue);
156
+ },
157
+ store
158
+ );
159
+ return () => {
160
+ socket.emit(`unclaim:${token.key}`);
161
+ };
162
+ }, [token.key]);
163
+ }
164
+
165
+ // ../src/realtime-react/use-server-action.ts
166
+ var React7 = __toESM(require("react"));
167
+ var AtomIO7 = __toESM(require("atom.io"));
168
+ var import_react5 = require("atom.io/react");
169
+ var TX_SUBS = /* @__PURE__ */ new Map();
170
+ function useServerAction(token) {
171
+ const store = React7.useContext(import_react5.StoreContext);
172
+ const { socket } = React7.useContext(RealtimeContext);
173
+ React7.useEffect(() => {
174
+ var _a;
175
+ const count = (_a = TX_SUBS.get(token.key)) != null ? _a : 0;
176
+ TX_SUBS.set(token.key, count + 1);
177
+ const unsubscribe = count === 0 ? AtomIO7.subscribeToTransaction(
178
+ token,
179
+ (update) => socket.emit(`tx:${token.key}`, update),
180
+ store
181
+ ) : () => null;
182
+ return () => {
183
+ var _a2;
184
+ const newCount = (_a2 = TX_SUBS.get(token.key)) != null ? _a2 : 0;
185
+ TX_SUBS.set(token.key, newCount - 1);
186
+ unsubscribe();
187
+ };
188
+ }, [token.key]);
189
+ return AtomIO7.runTransaction(token, store);
190
+ }
191
+
192
+ // ../src/realtime-react/realtime-hooks.ts
193
+ var realtimeHooks = {
194
+ usePull,
195
+ usePullFamily,
196
+ usePullFamilyMember,
197
+ usePush,
198
+ useServerAction
199
+ };
200
+ // Annotate the CommonJS export names for ESM import in node:
201
+ 0 && (module.exports = {
202
+ RealtimeContext,
203
+ RealtimeProvider,
204
+ myIdState,
205
+ myIdState__INTERNAL,
206
+ realtimeHooks,
207
+ usePull,
208
+ usePullFamily,
209
+ usePullFamilyMember,
210
+ usePush,
211
+ useServerAction
212
+ });
213
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/realtime-react/index.ts","../../src/realtime-react/realtime-context.tsx","../../src/realtime-react/realtime-state.ts","../../src/realtime-react/use-pull.ts","../../src/react/store-context.tsx","../../src/realtime-react/use-pull-family.ts","../../src/realtime-react/use-pull-family-member.ts","../../src/realtime-react/use-push.ts","../../src/realtime-react/use-server-action.ts","../../src/realtime-react/realtime-hooks.ts"],"sourcesContent":["export * from \"./realtime-context\"\nexport * from \"./realtime-hooks\"\nexport * from \"./realtime-state\"\n","import * as React from \"react\"\n\nimport * as AR from \"atom.io/react\"\nimport type { Socket } from \"socket.io-client\"\nimport { io } from \"socket.io-client\"\n\nimport { myIdState__INTERNAL } from \"./realtime-state\"\n\nexport const RealtimeContext = React.createContext<{ socket: Socket }>({\n socket: io(),\n})\n\nexport const RealtimeProvider: React.FC<{\n children: React.ReactNode\n socket: Socket\n}> = ({ children, socket }) => {\n const setMyId = AR.useI(myIdState__INTERNAL)\n React.useEffect(() => {\n socket.on(`connect`, () => {\n setMyId(socket.id)\n })\n socket.on(`disconnect`, () => {\n setMyId(null)\n })\n }, [socket, setMyId])\n return (\n <RealtimeContext.Provider value={{ socket }}>\n {children}\n </RealtimeContext.Provider>\n )\n}\n","import * as AtomIO from \"atom.io\"\n\nexport const myIdState__INTERNAL = AtomIO.atom<string | null>({\n key: `myId__INTERNAL`,\n default: null,\n})\nexport const myIdState = AtomIO.selector<string | null>({\n key: `myId`,\n get: ({ get }) => get(myIdState__INTERNAL),\n})\n","import * as React from \"react\"\n\nimport * as AtomIO from \"atom.io\"\n\nimport type { Json } from \"~/packages/anvl/src/json\"\n\nimport { RealtimeContext } from \"./realtime-context\"\nimport { StoreContext } from \"../react\"\n\nexport function usePull<J extends Json>(token: AtomIO.StateToken<J>): void {\n const { socket } = React.useContext(RealtimeContext)\n const store = React.useContext(StoreContext)\n React.useEffect(() => {\n socket.on(`serve:${token.key}`, (data: J) => {\n AtomIO.setState(token, data, store)\n })\n socket.emit(`sub:${token.key}`)\n return () => {\n socket.off(`serve:${token.key}`)\n socket.emit(`unsub:${token.key}`)\n }\n }, [token.key])\n}\n","import * as React from \"react\"\n\nimport * as AtomIO from \"atom.io\"\n\nexport const StoreContext = React.createContext<AtomIO.Store>(\n AtomIO.__INTERNAL__.IMPLICIT.STORE\n)\n\nexport const StoreProvider: React.FC<{\n children: React.ReactNode\n store?: AtomIO.Store\n}> = ({ children, store = AtomIO.__INTERNAL__.IMPLICIT.STORE }) => (\n <StoreContext.Provider value={store}>{children}</StoreContext.Provider>\n)\n","import * as React from \"react\"\n\nimport * as AtomIO from \"atom.io\"\n\nimport type { Json } from \"~/packages/anvl/src/json\"\n\nimport { RealtimeContext } from \"./realtime-context\"\nimport { StoreContext } from \"../react\"\n\nexport function usePullFamily<J extends Json>(\n family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>\n): void {\n const { socket } = React.useContext(RealtimeContext)\n const store = React.useContext(StoreContext)\n React.useEffect(() => {\n socket.on(`serve:${family.key}`, (key: Json, data: J) => {\n AtomIO.setState(family(key), data, store)\n })\n socket?.emit(`sub:${family.key}`)\n return () => {\n socket?.off(`serve:${family.key}`)\n socket?.emit(`unsub:${family.key}`)\n }\n }, [family.key])\n}\n","import * as React from \"react\"\n\nimport * as AtomIO from \"atom.io\"\n\nimport type { Json } from \"~/packages/anvl/src/json\"\n\nimport { RealtimeContext } from \"./realtime-context\"\nimport { StoreContext } from \"../react\"\n\nexport function usePullFamilyMember<J extends Json>(\n family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,\n subKey: AtomIO.Serializable\n): void {\n const token = family(subKey)\n const { socket } = React.useContext(RealtimeContext)\n const store = React.useContext(StoreContext)\n React.useEffect(() => {\n socket?.on(`serve:${token.key}`, (data: J) => {\n AtomIO.setState(family(subKey), data, store)\n })\n socket?.emit(`sub:${family.key}`, subKey)\n return () => {\n socket?.off(`serve:${token.key}`)\n socket?.emit(`unsub:${token.key}`)\n }\n }, [family.key])\n}\n","import * as React from \"react\"\n\nimport * as AtomIO from \"atom.io\"\n\nimport type { Json } from \"~/packages/anvl/src/json\"\n\nimport { RealtimeContext } from \"./realtime-context\"\nimport { StoreContext } from \"../react\"\n\nexport function usePush<J extends Json>(token: AtomIO.StateToken<J>): void {\n const { socket } = React.useContext(RealtimeContext)\n const store = React.useContext(StoreContext)\n React.useEffect(() => {\n socket.emit(`claim:${token.key}`)\n AtomIO.subscribe(\n token,\n ({ newValue }) => {\n socket.emit(`pub:${token.key}`, newValue)\n },\n store\n )\n return () => {\n socket.emit(`unclaim:${token.key}`)\n }\n }, [token.key])\n}\n","import * as React from \"react\"\n\nimport * as AtomIO from \"atom.io\"\nimport { StoreContext } from \"atom.io/react\"\n\nimport type { ƒn } from \"~/packages/anvl/src/function\"\n\nimport { RealtimeContext } from \"./realtime-context\"\n\nconst TX_SUBS = new Map<string, number>()\nexport function useServerAction<ƒ extends ƒn>(\n token: AtomIO.TransactionToken<ƒ>\n): (...parameters: Parameters<ƒ>) => ReturnType<ƒ> {\n const store = React.useContext(StoreContext)\n const { socket } = React.useContext(RealtimeContext)\n React.useEffect(() => {\n const count = TX_SUBS.get(token.key) ?? 0\n TX_SUBS.set(token.key, count + 1)\n const unsubscribe =\n count === 0\n ? AtomIO.subscribeToTransaction(\n token,\n (update) => socket.emit(`tx:${token.key}`, update),\n store\n )\n : () => null\n return () => {\n const newCount = TX_SUBS.get(token.key) ?? 0\n TX_SUBS.set(token.key, newCount - 1)\n unsubscribe()\n }\n }, [token.key])\n return AtomIO.runTransaction(token, store)\n}\n","import type * as AtomIO from \"atom.io\"\n\nimport type { ƒn } from \"~/packages/anvl/src/function\"\nimport type { Json } from \"~/packages/anvl/src/json\"\n\nimport { usePull } from \"./use-pull\"\nimport { usePullFamily } from \"./use-pull-family\"\nimport { usePullFamilyMember } from \"./use-pull-family-member\"\nimport { usePush } from \"./use-push\"\nimport { useServerAction } from \"./use-server-action\"\n\nexport type RealtimeHooks = {\n usePull: <J extends Json>(token: AtomIO.StateToken<J>) => void\n usePullFamily: <J extends Json>(\n family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>\n ) => void\n usePullFamilyMember: <J extends Json>(\n family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,\n subKey: string\n ) => void\n usePush: <J extends Json>(token: AtomIO.StateToken<J>) => void\n useServerAction: <ƒ extends ƒn>(\n token: AtomIO.TransactionToken<ƒ>\n ) => (...parameters: Parameters<ƒ>) => ReturnType<ƒ>\n}\n\nexport const realtimeHooks: RealtimeHooks = {\n usePull,\n usePullFamily,\n usePullFamilyMember,\n usePush,\n useServerAction,\n}\n\nexport * from \"./use-pull\"\nexport * from \"./use-pull-family\"\nexport * from \"./use-pull-family-member\"\nexport * from \"./use-push\"\nexport * from \"./use-server-action\"\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AAEvB,SAAoB;AAEpB,oBAAmB;;;ACJnB,aAAwB;AAEjB,IAAM,sBAA6B,YAAoB;AAAA,EAC5D,KAAK;AAAA,EACL,SAAS;AACX,CAAC;AACM,IAAM,YAAmB,gBAAwB;AAAA,EACtD,KAAK;AAAA,EACL,KAAK,CAAC,EAAE,IAAI,MAAM,IAA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M;AACX,aAAO,KAAK,WAAW,MAAM,GAAG,EAAE;AAAA,IACpC;AAAA,EACF,GAAG,CAAC,MAAM,GAAG,CAAC;AAChB;;;ACzBA,IAAAC,SAAuB;AAEvB,IAAAC,UAAwB;AACxB,IAAAC,gBAA6B;AAM7B,IAAM,UAAU,oBAAI,IAAoB;AACjC,SAAS,gBACd,OACiD;AACjD,QAAM,QAAc,kBAAW,0BAAY;AAC3C,QAAM,EAAE,OAAO,IAAU,kBAAW,eAAe;AACnD,EAAM,iBAAU,MAAM;AAfxB;AAgBI,UAAM,SAAQ,aAAQ,IAAI,MAAM,GAAG,MAArB,YAA0B;AACxC,YAAQ,IAAI,MAAM,KAAK,QAAQ,CAAC;AAChC,UAAM,cACJ,UAAU,IACC;AAAA,MACL;AAAA,MACA,CAAC,WAAW,OAAO,KAAK,MAAM,MAAM,GAAG,IAAI,MAAM;AAAA,MACjD;AAAA,IACF,IACA,MAAM;AACZ,WAAO,MAAM;AA1BjB,UAAAC;AA2BM,YAAM,YAAWA,MAAA,QAAQ,IAAI,MAAM,GAAG,MAArB,OAAAA,MAA0B;AAC3C,cAAQ,IAAI,MAAM,KAAK,WAAW,CAAC;AACnC,kBAAY;AAAA,IACd;AAAA,EACF,GAAG,CAAC,MAAM,GAAG,CAAC;AACd,SAAc,uBAAe,OAAO,KAAK;AAC3C;;;ACPO,IAAM,gBAA+B;AAAA,EAC1C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;","names":["React","AtomIO","React","AtomIO","import_jsx_runtime","React","AtomIO","React","AtomIO","React","AtomIO","React","AtomIO","import_react","_a"]}
@@ -0,0 +1,168 @@
1
+ // ../src/realtime-react/realtime-context.tsx
2
+ import * as React from "react";
3
+ import * as AR from "atom.io/react";
4
+ import { io } from "socket.io-client";
5
+
6
+ // ../src/realtime-react/realtime-state.ts
7
+ import * as AtomIO from "atom.io";
8
+ var myIdState__INTERNAL = AtomIO.atom({
9
+ key: `myId__INTERNAL`,
10
+ default: null
11
+ });
12
+ var myIdState = AtomIO.selector({
13
+ key: `myId`,
14
+ get: ({ get }) => get(myIdState__INTERNAL)
15
+ });
16
+
17
+ // ../src/realtime-react/realtime-context.tsx
18
+ import { jsx } from "@emotion/react/jsx-runtime";
19
+ var RealtimeContext = React.createContext({
20
+ socket: io()
21
+ });
22
+ var RealtimeProvider = ({ children, socket }) => {
23
+ const setMyId = AR.useI(myIdState__INTERNAL);
24
+ React.useEffect(() => {
25
+ socket.on(`connect`, () => {
26
+ setMyId(socket.id);
27
+ });
28
+ socket.on(`disconnect`, () => {
29
+ setMyId(null);
30
+ });
31
+ }, [socket, setMyId]);
32
+ return /* @__PURE__ */ jsx(RealtimeContext.Provider, { value: { socket }, children });
33
+ };
34
+
35
+ // ../src/realtime-react/use-pull.ts
36
+ import * as React3 from "react";
37
+ import * as AtomIO3 from "atom.io";
38
+
39
+ // ../src/react/store-context.tsx
40
+ import * as React2 from "react";
41
+ import * as AtomIO2 from "atom.io";
42
+ import { jsx as jsx2 } from "@emotion/react/jsx-runtime";
43
+ var StoreContext = React2.createContext(
44
+ AtomIO2.__INTERNAL__.IMPLICIT.STORE
45
+ );
46
+
47
+ // ../src/realtime-react/use-pull.ts
48
+ function usePull(token) {
49
+ const { socket } = React3.useContext(RealtimeContext);
50
+ const store = React3.useContext(StoreContext);
51
+ React3.useEffect(() => {
52
+ socket.on(`serve:${token.key}`, (data) => {
53
+ AtomIO3.setState(token, data, store);
54
+ });
55
+ socket.emit(`sub:${token.key}`);
56
+ return () => {
57
+ socket.off(`serve:${token.key}`);
58
+ socket.emit(`unsub:${token.key}`);
59
+ };
60
+ }, [token.key]);
61
+ }
62
+
63
+ // ../src/realtime-react/use-pull-family.ts
64
+ import * as React4 from "react";
65
+ import * as AtomIO4 from "atom.io";
66
+ function usePullFamily(family) {
67
+ const { socket } = React4.useContext(RealtimeContext);
68
+ const store = React4.useContext(StoreContext);
69
+ React4.useEffect(() => {
70
+ socket.on(`serve:${family.key}`, (key, data) => {
71
+ AtomIO4.setState(family(key), data, store);
72
+ });
73
+ socket == null ? void 0 : socket.emit(`sub:${family.key}`);
74
+ return () => {
75
+ socket == null ? void 0 : socket.off(`serve:${family.key}`);
76
+ socket == null ? void 0 : socket.emit(`unsub:${family.key}`);
77
+ };
78
+ }, [family.key]);
79
+ }
80
+
81
+ // ../src/realtime-react/use-pull-family-member.ts
82
+ import * as React5 from "react";
83
+ import * as AtomIO5 from "atom.io";
84
+ function usePullFamilyMember(family, subKey) {
85
+ const token = family(subKey);
86
+ const { socket } = React5.useContext(RealtimeContext);
87
+ const store = React5.useContext(StoreContext);
88
+ React5.useEffect(() => {
89
+ socket == null ? void 0 : socket.on(`serve:${token.key}`, (data) => {
90
+ AtomIO5.setState(family(subKey), data, store);
91
+ });
92
+ socket == null ? void 0 : socket.emit(`sub:${family.key}`, subKey);
93
+ return () => {
94
+ socket == null ? void 0 : socket.off(`serve:${token.key}`);
95
+ socket == null ? void 0 : socket.emit(`unsub:${token.key}`);
96
+ };
97
+ }, [family.key]);
98
+ }
99
+
100
+ // ../src/realtime-react/use-push.ts
101
+ import * as React6 from "react";
102
+ import * as AtomIO6 from "atom.io";
103
+ function usePush(token) {
104
+ const { socket } = React6.useContext(RealtimeContext);
105
+ const store = React6.useContext(StoreContext);
106
+ React6.useEffect(() => {
107
+ socket.emit(`claim:${token.key}`);
108
+ AtomIO6.subscribe(
109
+ token,
110
+ ({ newValue }) => {
111
+ socket.emit(`pub:${token.key}`, newValue);
112
+ },
113
+ store
114
+ );
115
+ return () => {
116
+ socket.emit(`unclaim:${token.key}`);
117
+ };
118
+ }, [token.key]);
119
+ }
120
+
121
+ // ../src/realtime-react/use-server-action.ts
122
+ import * as React7 from "react";
123
+ import * as AtomIO7 from "atom.io";
124
+ import { StoreContext as StoreContext2 } from "atom.io/react";
125
+ var TX_SUBS = /* @__PURE__ */ new Map();
126
+ function useServerAction(token) {
127
+ const store = React7.useContext(StoreContext2);
128
+ const { socket } = React7.useContext(RealtimeContext);
129
+ React7.useEffect(() => {
130
+ var _a;
131
+ const count = (_a = TX_SUBS.get(token.key)) != null ? _a : 0;
132
+ TX_SUBS.set(token.key, count + 1);
133
+ const unsubscribe = count === 0 ? AtomIO7.subscribeToTransaction(
134
+ token,
135
+ (update) => socket.emit(`tx:${token.key}`, update),
136
+ store
137
+ ) : () => null;
138
+ return () => {
139
+ var _a2;
140
+ const newCount = (_a2 = TX_SUBS.get(token.key)) != null ? _a2 : 0;
141
+ TX_SUBS.set(token.key, newCount - 1);
142
+ unsubscribe();
143
+ };
144
+ }, [token.key]);
145
+ return AtomIO7.runTransaction(token, store);
146
+ }
147
+
148
+ // ../src/realtime-react/realtime-hooks.ts
149
+ var realtimeHooks = {
150
+ usePull,
151
+ usePullFamily,
152
+ usePullFamilyMember,
153
+ usePush,
154
+ useServerAction
155
+ };
156
+ export {
157
+ RealtimeContext,
158
+ RealtimeProvider,
159
+ myIdState,
160
+ myIdState__INTERNAL,
161
+ realtimeHooks,
162
+ usePull,
163
+ usePullFamily,
164
+ usePullFamilyMember,
165
+ usePush,
166
+ useServerAction
167
+ };
168
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/realtime-react/realtime-context.tsx","../../src/realtime-react/realtime-state.ts","../../src/realtime-react/use-pull.ts","../../src/react/store-context.tsx","../../src/realtime-react/use-pull-family.ts","../../src/realtime-react/use-pull-family-member.ts","../../src/realtime-react/use-push.ts","../../src/realtime-react/use-server-action.ts","../../src/realtime-react/realtime-hooks.ts"],"sourcesContent":["import * as React from \"react\"\n\nimport * as AR from \"atom.io/react\"\nimport type { Socket } from \"socket.io-client\"\nimport { io } from \"socket.io-client\"\n\nimport { myIdState__INTERNAL } from \"./realtime-state\"\n\nexport const RealtimeContext = React.createContext<{ socket: Socket }>({\n socket: io(),\n})\n\nexport const RealtimeProvider: React.FC<{\n children: React.ReactNode\n socket: Socket\n}> = ({ children, socket }) => {\n const setMyId = AR.useI(myIdState__INTERNAL)\n React.useEffect(() => {\n socket.on(`connect`, () => {\n setMyId(socket.id)\n })\n socket.on(`disconnect`, () => {\n setMyId(null)\n })\n }, [socket, setMyId])\n return (\n <RealtimeContext.Provider value={{ socket }}>\n {children}\n </RealtimeContext.Provider>\n )\n}\n","import * as AtomIO from \"atom.io\"\n\nexport const myIdState__INTERNAL = AtomIO.atom<string | null>({\n key: `myId__INTERNAL`,\n default: null,\n})\nexport const myIdState = AtomIO.selector<string | null>({\n key: `myId`,\n get: ({ get }) => get(myIdState__INTERNAL),\n})\n","import * as React from \"react\"\n\nimport * as AtomIO from \"atom.io\"\n\nimport type { Json } from \"~/packages/anvl/src/json\"\n\nimport { RealtimeContext } from \"./realtime-context\"\nimport { StoreContext } from \"../react\"\n\nexport function usePull<J extends Json>(token: AtomIO.StateToken<J>): void {\n const { socket } = React.useContext(RealtimeContext)\n const store = React.useContext(StoreContext)\n React.useEffect(() => {\n socket.on(`serve:${token.key}`, (data: J) => {\n AtomIO.setState(token, data, store)\n })\n socket.emit(`sub:${token.key}`)\n return () => {\n socket.off(`serve:${token.key}`)\n socket.emit(`unsub:${token.key}`)\n }\n }, [token.key])\n}\n","import * as React from \"react\"\n\nimport * as AtomIO from \"atom.io\"\n\nexport const StoreContext = React.createContext<AtomIO.Store>(\n AtomIO.__INTERNAL__.IMPLICIT.STORE\n)\n\nexport const StoreProvider: React.FC<{\n children: React.ReactNode\n store?: AtomIO.Store\n}> = ({ children, store = AtomIO.__INTERNAL__.IMPLICIT.STORE }) => (\n <StoreContext.Provider value={store}>{children}</StoreContext.Provider>\n)\n","import * as React from \"react\"\n\nimport * as AtomIO from \"atom.io\"\n\nimport type { Json } from \"~/packages/anvl/src/json\"\n\nimport { RealtimeContext } from \"./realtime-context\"\nimport { StoreContext } from \"../react\"\n\nexport function usePullFamily<J extends Json>(\n family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>\n): void {\n const { socket } = React.useContext(RealtimeContext)\n const store = React.useContext(StoreContext)\n React.useEffect(() => {\n socket.on(`serve:${family.key}`, (key: Json, data: J) => {\n AtomIO.setState(family(key), data, store)\n })\n socket?.emit(`sub:${family.key}`)\n return () => {\n socket?.off(`serve:${family.key}`)\n socket?.emit(`unsub:${family.key}`)\n }\n }, [family.key])\n}\n","import * as React from \"react\"\n\nimport * as AtomIO from \"atom.io\"\n\nimport type { Json } from \"~/packages/anvl/src/json\"\n\nimport { RealtimeContext } from \"./realtime-context\"\nimport { StoreContext } from \"../react\"\n\nexport function usePullFamilyMember<J extends Json>(\n family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,\n subKey: AtomIO.Serializable\n): void {\n const token = family(subKey)\n const { socket } = React.useContext(RealtimeContext)\n const store = React.useContext(StoreContext)\n React.useEffect(() => {\n socket?.on(`serve:${token.key}`, (data: J) => {\n AtomIO.setState(family(subKey), data, store)\n })\n socket?.emit(`sub:${family.key}`, subKey)\n return () => {\n socket?.off(`serve:${token.key}`)\n socket?.emit(`unsub:${token.key}`)\n }\n }, [family.key])\n}\n","import * as React from \"react\"\n\nimport * as AtomIO from \"atom.io\"\n\nimport type { Json } from \"~/packages/anvl/src/json\"\n\nimport { RealtimeContext } from \"./realtime-context\"\nimport { StoreContext } from \"../react\"\n\nexport function usePush<J extends Json>(token: AtomIO.StateToken<J>): void {\n const { socket } = React.useContext(RealtimeContext)\n const store = React.useContext(StoreContext)\n React.useEffect(() => {\n socket.emit(`claim:${token.key}`)\n AtomIO.subscribe(\n token,\n ({ newValue }) => {\n socket.emit(`pub:${token.key}`, newValue)\n },\n store\n )\n return () => {\n socket.emit(`unclaim:${token.key}`)\n }\n }, [token.key])\n}\n","import * as React from \"react\"\n\nimport * as AtomIO from \"atom.io\"\nimport { StoreContext } from \"atom.io/react\"\n\nimport type { ƒn } from \"~/packages/anvl/src/function\"\n\nimport { RealtimeContext } from \"./realtime-context\"\n\nconst TX_SUBS = new Map<string, number>()\nexport function useServerAction<ƒ extends ƒn>(\n token: AtomIO.TransactionToken<ƒ>\n): (...parameters: Parameters<ƒ>) => ReturnType<ƒ> {\n const store = React.useContext(StoreContext)\n const { socket } = React.useContext(RealtimeContext)\n React.useEffect(() => {\n const count = TX_SUBS.get(token.key) ?? 0\n TX_SUBS.set(token.key, count + 1)\n const unsubscribe =\n count === 0\n ? AtomIO.subscribeToTransaction(\n token,\n (update) => socket.emit(`tx:${token.key}`, update),\n store\n )\n : () => null\n return () => {\n const newCount = TX_SUBS.get(token.key) ?? 0\n TX_SUBS.set(token.key, newCount - 1)\n unsubscribe()\n }\n }, [token.key])\n return AtomIO.runTransaction(token, store)\n}\n","import type * as AtomIO from \"atom.io\"\n\nimport type { ƒn } from \"~/packages/anvl/src/function\"\nimport type { Json } from \"~/packages/anvl/src/json\"\n\nimport { usePull } from \"./use-pull\"\nimport { usePullFamily } from \"./use-pull-family\"\nimport { usePullFamilyMember } from \"./use-pull-family-member\"\nimport { usePush } from \"./use-push\"\nimport { useServerAction } from \"./use-server-action\"\n\nexport type RealtimeHooks = {\n usePull: <J extends Json>(token: AtomIO.StateToken<J>) => void\n usePullFamily: <J extends Json>(\n family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>\n ) => void\n usePullFamilyMember: <J extends Json>(\n family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,\n subKey: string\n ) => void\n usePush: <J extends Json>(token: AtomIO.StateToken<J>) => void\n useServerAction: <ƒ extends ƒn>(\n token: AtomIO.TransactionToken<ƒ>\n ) => (...parameters: Parameters<ƒ>) => ReturnType<ƒ>\n}\n\nexport const realtimeHooks: RealtimeHooks = {\n usePull,\n usePullFamily,\n usePullFamilyMember,\n usePush,\n useServerAction,\n}\n\nexport * from \"./use-pull\"\nexport * from \"./use-pull-family\"\nexport * from \"./use-pull-family-member\"\nexport * from \"./use-push\"\nexport * from \"./use-server-action\"\n"],"mappings":";AAAA,YAAY,WAAW;AAEvB,YAAY,QAAQ;AAEpB,SAAS,UAAU;;;ACJnB,YAAY,YAAY;AAEjB,IAAM,sBAA6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AAA,MACA,CAAC,EAAE,SAAS,MAAM;AAChB,eAAO,KAAK,OAAO,MAAM,GAAG,IAAI,QAAQ;AAAA,MAC1C;AAAA,MACA;AAAA,IACF;AACA,WAAO,MAAM;AACX,aAAO,KAAK,WAAW,MAAM,GAAG,EAAE;AAAA,IACpC;AAAA,EACF,GAAG,CAAC,MAAM,GAAG,CAAC;AAChB;;;ACzBA,YAAYC,YAAW;AAEvB,YAAYC,aAAY;AACxB,SAAS,gBAAAC,qBAAoB;AAM7B,IAAM,UAAU,oBAAI,IAAoB;AACjC,SAAS,gBACd,OACiD;AACjD,QAAM,QAAc,kBAAWC,aAAY;AAC3C,QAAM,EAAE,OAAO,IAAU,kBAAW,eAAe;AACnD,EAAM,iBAAU,MAAM;AAfxB;AAgBI,UAAM,SAAQ,aAAQ,IAAI,MAAM,GAAG,MAArB,YAA0B;AACxC,YAAQ,IAAI,MAAM,KAAK,QAAQ,CAAC;AAChC,UAAM,cACJ,UAAU,IACC;AAAA,MACL;AAAA,MACA,CAAC,WAAW,OAAO,KAAK,MAAM,MAAM,GAAG,IAAI,MAAM;AAAA,MACjD;AAAA,IACF,IACA,MAAM;AACZ,WAAO,MAAM;AA1BjB,UAAAC;AA2BM,YAAM,YAAWA,MAAA,QAAQ,IAAI,MAAM,GAAG,MAArB,OAAAA,MAA0B;AAC3C,cAAQ,IAAI,MAAM,KAAK,WAAW,CAAC;AACnC,kBAAY;AAAA,IACd;AAAA,EACF,GAAG,CAAC,MAAM,GAAG,CAAC;AACd,SAAc,uBAAe,OAAO,KAAK;AAC3C;;;ACPO,IAAM,gBAA+B;AAAA,EAC1C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;","names":["React","AtomIO","React","AtomIO","jsx","React","AtomIO","React","AtomIO","React","AtomIO","React","AtomIO","StoreContext","StoreContext","_a"]}
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "atom.io-realtime-react",
3
+ "private": true,
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "module": "dist/index.mjs",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "browser": "./dist/index.mjs",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ }
15
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { capitalize } from "~/packages/anvl/src/string/capitalize"
2
+
1
3
  import {
2
4
  IMPLICIT,
3
5
  closeOperation,
@@ -14,21 +16,25 @@ import type { Store } from "./internal/store"
14
16
  export * from "./atom"
15
17
  export * from "./logger"
16
18
  export * from "./selector"
19
+ export * from "./silo"
17
20
  export * from "./subscribe"
18
21
  export * from "./timeline"
19
22
  export * from "./transaction"
20
23
  export { __INTERNAL__ }
24
+ export type { Store } from "./internal/store"
21
25
  export type { Serializable } from "~/packages/anvl/src/json"
22
26
 
23
27
  export type AtomToken<_> = {
24
28
  key: string
25
29
  type: `atom`
26
30
  family?: FamilyMetadata
31
+ __brand?: _
27
32
  }
28
33
  export type SelectorToken<_> = {
29
34
  key: string
30
35
  type: `selector`
31
36
  family?: FamilyMetadata
37
+ __brand?: _
32
38
  }
33
39
  export type StateToken<T> = AtomToken<T> | SelectorToken<T>
34
40
 
@@ -36,6 +42,7 @@ export type ReadonlySelectorToken<_> = {
36
42
  key: string
37
43
  type: `readonly_selector`
38
44
  family?: FamilyMetadata
45
+ __brand?: _
39
46
  }
40
47
 
41
48
  export type FamilyMetadata = {
@@ -43,16 +50,18 @@ export type FamilyMetadata = {
43
50
  subKey: string
44
51
  }
45
52
 
46
- export type TransactionToken<_> = {
47
- key: string
48
- type: `transaction`
49
- }
50
-
51
53
  export const getState = <T>(
52
54
  token: ReadonlySelectorToken<T> | StateToken<T>,
53
55
  store: Store = IMPLICIT.STORE
54
56
  ): T => {
55
57
  const state = withdraw<T>(token, store)
58
+ if (state === null) {
59
+ throw new Error(
60
+ `${capitalize(token.type)} "${token.key}" not found in store "${
61
+ store.config.name
62
+ }".`
63
+ )
64
+ }
56
65
  return getState__INTERNAL(state, store)
57
66
  }
58
67
 
@@ -70,6 +79,13 @@ export const setState = <T, New extends T>(
70
79
  return
71
80
  }
72
81
  const state = withdraw(token, store)
82
+ if (state === null) {
83
+ throw new Error(
84
+ `${capitalize(token.type)} "${token.key}" not found in store "${
85
+ store.config.name
86
+ }".`
87
+ )
88
+ }
73
89
  setState__INTERNAL(state, value, store)
74
90
  closeOperation(store)
75
91
  }
@@ -51,5 +51,5 @@ export function atom__INTERNAL<T>(
51
51
  })
52
52
  )
53
53
  store.subject.atomCreation.next(token)
54
- return token
54
+ return token as AtomToken<T>
55
55
  }
@@ -36,7 +36,7 @@ export function atomFamily__INTERNAL<T, K extends Serializable>(
36
36
  const family: FamilyMetadata = { key: options.key, subKey }
37
37
  const fullKey = `${options.key}(${subKey})`
38
38
  const existing = withdraw({ key: fullKey, type: `atom` }, store)
39
- const token = existing
39
+ const token: AtomToken<any> = existing
40
40
  ? deposit(existing)
41
41
  : atom__INTERNAL<T>(
42
42
  {
@@ -62,7 +62,7 @@ export function atomFamily__INTERNAL<T, K extends Serializable>(
62
62
  }
63
63
 
64
64
  export function readonlySelectorFamily__INTERNAL<T, K extends Serializable>(
65
- options: SelectorFamilyOptions<T, K>,
65
+ options: ReadonlySelectorFamilyOptions<T, K>,
66
66
  store?: Store
67
67
  ): ReadonlySelectorFamily<T, K> {
68
68
  const core = target(store)
@@ -108,7 +108,7 @@ export function selectorFamily__INTERNAL<T, K extends Serializable>(
108
108
  const isReadonly = !(`set` in options)
109
109
 
110
110
  if (isReadonly) {
111
- return readonlySelectorFamily__INTERNAL(options as any, store)
111
+ return readonlySelectorFamily__INTERNAL(options, store)
112
112
  }
113
113
  const core = target(store)
114
114
  const subject = new Rx.Subject<SelectorToken<T>>()
@@ -1,15 +1,23 @@
1
1
  import HAMT from "hamt_plus"
2
2
 
3
- import type { Atom, ReadonlySelector, Selector, Store } from "."
3
+ import type { ƒn } from "~/packages/anvl/src/function"
4
+
5
+ import type {
6
+ Atom,
7
+ ReadonlySelector,
8
+ Selector,
9
+ Store,
10
+ Timeline,
11
+ Transaction,
12
+ } from "."
4
13
  import { target, isValueCached, readCachedValue, IMPLICIT } from "."
5
14
  import type {
6
15
  AtomToken,
7
16
  ReadonlySelectorToken,
8
17
  SelectorToken,
9
18
  StateToken,
10
- Transaction,
19
+ TimelineToken,
11
20
  TransactionToken,
12
- ƒn,
13
21
  } from ".."
14
22
 
15
23
  export const computeSelectorState = <T>(
@@ -26,41 +34,53 @@ export function lookup(
26
34
  : HAMT.has(key, core.selectors)
27
35
  ? `selector`
28
36
  : `readonly_selector`
29
- return { key, type }
37
+ return { key, type } as any
30
38
  }
31
39
 
32
- export function withdraw<T>(token: AtomToken<T>, store: Store): Atom<T>
33
- export function withdraw<T>(token: SelectorToken<T>, store: Store): Selector<T>
40
+ export function withdraw<T>(token: AtomToken<T>, store: Store): Atom<T> | null
41
+ export function withdraw<T>(
42
+ token: SelectorToken<T>,
43
+ store: Store
44
+ ): Selector<T> | null
34
45
  export function withdraw<T>(
35
46
  token: StateToken<T>,
36
47
  store: Store
37
- ): Atom<T> | Selector<T>
48
+ ): Atom<T> | Selector<T> | null
38
49
  export function withdraw<T>(
39
50
  token: ReadonlySelectorToken<T>,
40
51
  store: Store
41
- ): ReadonlySelector<T>
52
+ ): ReadonlySelector<T> | null
42
53
  export function withdraw<T>(
43
54
  token: TransactionToken<T>,
44
55
  store: Store
45
- ): Transaction<T extends ƒn ? T : never>
56
+ ): Transaction<T extends ƒn ? T : never> | null
46
57
  export function withdraw<T>(
47
58
  token: ReadonlySelectorToken<T> | StateToken<T>,
48
59
  store: Store
49
- ): Atom<T> | ReadonlySelector<T> | Selector<T>
60
+ ): Atom<T> | ReadonlySelector<T> | Selector<T> | null
61
+ export function withdraw<T>(token: TimelineToken, store: Store): Timeline | null
50
62
  export function withdraw<T>(
51
- token: ReadonlySelectorToken<T> | StateToken<T> | TransactionToken<T>,
63
+ token:
64
+ | ReadonlySelectorToken<T>
65
+ | StateToken<T>
66
+ | TimelineToken
67
+ | TransactionToken<T>,
52
68
  store: Store
53
69
  ):
54
70
  | Atom<T>
55
71
  | ReadonlySelector<T>
56
72
  | Selector<T>
57
- | Transaction<T extends ƒn ? T : never> {
73
+ | Timeline
74
+ | Transaction<T extends ƒn ? T : never>
75
+ | null {
58
76
  const core = target(store)
59
77
  return (
60
78
  HAMT.get(token.key, core.atoms) ??
61
79
  HAMT.get(token.key, core.selectors) ??
62
80
  HAMT.get(token.key, core.readonlySelectors) ??
63
- HAMT.get(token.key, core.transactions)
81
+ HAMT.get(token.key, core.transactions) ??
82
+ HAMT.get(token.key, core.timelines) ??
83
+ null
64
84
  )
65
85
  }
66
86
 
@@ -80,12 +100,16 @@ export function deposit<T>(
80
100
  | ReadonlySelector<T>
81
101
  | Selector<T>
82
102
  | Transaction<T extends ƒn ? T : never>
83
- ): ReadonlySelectorToken<T> | StateToken<T> | TransactionToken<T> {
103
+ ):
104
+ | AtomToken<T>
105
+ | ReadonlySelectorToken<T>
106
+ | SelectorToken<T>
107
+ | TransactionToken<T> {
84
108
  return {
85
109
  key: state.key,
86
110
  type: state.type,
87
111
  ...(`family` in state && { family: state.family }),
88
- }
112
+ } as any
89
113
  }
90
114
 
91
115
  export const getState__INTERNAL = <T>(