atom.io 0.16.1 → 0.16.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/data/dist/index.cjs +31 -14
- package/data/dist/index.cjs.map +1 -1
- package/data/dist/index.js +31 -14
- package/data/dist/index.js.map +1 -1
- package/data/src/join.ts +31 -14
- package/dist/chunk-H4Q5FTPZ.js +11 -0
- package/dist/chunk-H4Q5FTPZ.js.map +1 -0
- package/dist/index.cjs +7 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -7
- package/dist/index.js +8 -14
- package/dist/index.js.map +1 -1
- package/internal/dist/index.cjs +240 -193
- package/internal/dist/index.cjs.map +1 -1
- package/internal/dist/index.d.ts +30 -9
- package/internal/dist/index.js +233 -194
- package/internal/dist/index.js.map +1 -1
- package/internal/src/families/find-in-store.ts +74 -0
- package/internal/src/families/index.ts +1 -0
- package/internal/src/ingest-updates/ingest-transaction-update.ts +1 -0
- package/internal/src/mutable/tracker.ts +37 -32
- package/internal/src/mutable/transceiver.ts +1 -1
- package/internal/src/not-found-error.ts +14 -3
- package/internal/src/operation.ts +2 -1
- package/internal/src/selector/create-writable-selector.ts +2 -1
- package/internal/src/selector/register-selector.ts +5 -4
- package/internal/src/set-state/set-atom.ts +23 -6
- package/internal/src/set-state/stow-update.ts +2 -4
- package/internal/src/store/store.ts +13 -4
- package/internal/src/timeline/add-atom-to-timeline.ts +5 -5
- package/internal/src/transaction/abort-transaction.ts +2 -1
- package/internal/src/transaction/apply-transaction.ts +5 -3
- package/internal/src/transaction/build-transaction.ts +17 -10
- package/internal/src/transaction/create-transaction.ts +2 -3
- package/internal/src/transaction/index.ts +3 -2
- package/internal/src/transaction/is-root-store.ts +23 -0
- package/package.json +10 -10
- package/react/dist/index.cjs +27 -21
- package/react/dist/index.cjs.map +1 -1
- package/react/dist/index.d.ts +8 -2
- package/react/dist/index.js +27 -21
- package/react/dist/index.js.map +1 -1
- package/react/src/index.ts +4 -1
- package/react/src/use-i.ts +36 -0
- package/react/src/use-json.ts +38 -0
- package/react/src/use-o.ts +34 -0
- package/react/src/use-tl.ts +45 -0
- package/realtime-client/dist/index.cjs +163 -62
- package/realtime-client/dist/index.cjs.map +1 -1
- package/realtime-client/dist/index.d.ts +10 -6
- package/realtime-client/dist/index.js +153 -60
- package/realtime-client/dist/index.js.map +1 -1
- package/realtime-client/src/index.ts +2 -1
- package/realtime-client/src/pull-state.ts +4 -3
- package/realtime-client/src/{realtime-client-store.ts → realtime-client-stores/client-main-store.ts} +0 -8
- package/realtime-client/src/realtime-client-stores/client-sync-store.ts +15 -0
- package/realtime-client/src/realtime-client-stores/index.ts +2 -0
- package/realtime-client/src/sync-server-action.ts +131 -39
- package/realtime-client/src/sync-state.ts +19 -0
- package/realtime-react/dist/index.cjs +43 -26
- package/realtime-react/dist/index.cjs.map +1 -1
- package/realtime-react/dist/index.d.ts +3 -1
- package/realtime-react/dist/index.js +41 -25
- package/realtime-react/dist/index.js.map +1 -1
- package/realtime-react/src/index.ts +1 -0
- package/realtime-react/src/on-mount.ts +3 -21
- package/realtime-react/src/use-realtime-service.ts +1 -1
- package/realtime-react/src/use-server-action.ts +1 -1
- package/realtime-react/src/use-single-effect.ts +29 -0
- package/realtime-react/src/use-sync-server-action.ts +5 -8
- package/realtime-react/src/use-sync.ts +17 -0
- package/realtime-server/dist/index.cjs +242 -48
- package/realtime-server/dist/index.cjs.map +1 -1
- package/realtime-server/dist/index.d.ts +147 -9
- package/realtime-server/dist/index.js +232 -51
- package/realtime-server/dist/index.js.map +1 -1
- package/realtime-server/src/index.ts +2 -0
- package/realtime-server/src/realtime-action-receiver.ts +4 -3
- package/realtime-server/src/realtime-action-synchronizer.ts +100 -13
- package/realtime-server/src/realtime-family-provider.ts +10 -6
- package/realtime-server/src/realtime-mutable-family-provider.ts +15 -18
- package/realtime-server/src/realtime-mutable-provider.ts +1 -0
- package/realtime-server/src/realtime-server-stores/index.ts +2 -0
- package/realtime-server/src/realtime-server-stores/server-sync-store.ts +115 -0
- package/realtime-server/src/realtime-server-stores/server-user-store.ts +45 -0
- package/realtime-server/src/realtime-state-provider.ts +16 -8
- package/realtime-server/src/realtime-state-receiver.ts +1 -0
- package/realtime-server/src/realtime-state-synchronizer.ts +23 -0
- package/realtime-testing/dist/index.cjs +65 -26
- package/realtime-testing/dist/index.cjs.map +1 -1
- package/realtime-testing/dist/index.d.ts +11 -7
- package/realtime-testing/dist/index.js +64 -26
- package/realtime-testing/dist/index.js.map +1 -1
- package/realtime-testing/src/setup-realtime-test.tsx +83 -43
- package/src/find-state.ts +8 -16
- package/src/logger.ts +16 -11
- package/src/transaction.ts +4 -4
- package/react/src/store-hooks.ts +0 -87
- package/realtime-server/src/realtime-server-store.ts +0 -39
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "atom.io",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.3",
|
|
4
4
|
"description": "Composable and testable reactive data library.",
|
|
5
5
|
"homepage": "https://atom.io.fyi",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -51,31 +51,31 @@
|
|
|
51
51
|
"@testing-library/react": "14.1.2",
|
|
52
52
|
"@types/http-proxy": "1.17.14",
|
|
53
53
|
"@types/npmlog": "7.0.0",
|
|
54
|
-
"@types/react": "18.2.
|
|
54
|
+
"@types/react": "18.2.48",
|
|
55
55
|
"@types/tmp": "0.2.6",
|
|
56
|
-
"@vitest/coverage-v8": "1.2.
|
|
57
|
-
"@vitest/ui": "1.2.
|
|
56
|
+
"@vitest/coverage-v8": "1.2.1",
|
|
57
|
+
"@vitest/ui": "1.2.1",
|
|
58
58
|
"concurrently": "8.2.2",
|
|
59
|
-
"drizzle-kit": "0.20.
|
|
59
|
+
"drizzle-kit": "0.20.13",
|
|
60
60
|
"drizzle-orm": "0.29.3",
|
|
61
61
|
"eslint": "8.56.0",
|
|
62
62
|
"framer-motion": "10.18.0",
|
|
63
|
-
"happy-dom": "13.0
|
|
63
|
+
"happy-dom": "13.2.0",
|
|
64
64
|
"http-proxy": "1.18.1",
|
|
65
65
|
"npmlog": "7.0.1",
|
|
66
66
|
"postgres": "3.4.3",
|
|
67
67
|
"preact": "10.19.3",
|
|
68
68
|
"react": "18.2.0",
|
|
69
69
|
"react-dom": "18.2.0",
|
|
70
|
-
"react-router-dom": "6.21.
|
|
70
|
+
"react-router-dom": "6.21.3",
|
|
71
71
|
"socket.io": "4.7.4",
|
|
72
72
|
"socket.io-client": "4.7.4",
|
|
73
73
|
"tmp": "0.2.1",
|
|
74
74
|
"tsup": "8.0.1",
|
|
75
75
|
"typescript": "5.3.3",
|
|
76
|
-
"vite": "5.0.
|
|
77
|
-
"vite-tsconfig-paths": "4.
|
|
78
|
-
"vitest": "1.2.
|
|
76
|
+
"vite": "5.0.12",
|
|
77
|
+
"vite-tsconfig-paths": "4.3.1",
|
|
78
|
+
"vitest": "1.2.1"
|
|
79
79
|
},
|
|
80
80
|
"main": "dist/index.js",
|
|
81
81
|
"types": "dist/index.d.ts",
|
package/react/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var internal = require('atom.io/internal');
|
|
4
|
-
var
|
|
4
|
+
var React5 = require('react');
|
|
5
5
|
var jsxRuntime = require('react/jsx-runtime');
|
|
6
6
|
var atom_io = require('atom.io');
|
|
7
7
|
|
|
@@ -23,37 +23,43 @@ function _interopNamespace(e) {
|
|
|
23
23
|
return Object.freeze(n);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
var
|
|
26
|
+
var React5__namespace = /*#__PURE__*/_interopNamespace(React5);
|
|
27
27
|
|
|
28
28
|
// react/src/store-context.tsx
|
|
29
|
-
var StoreContext =
|
|
29
|
+
var StoreContext = React5__namespace.createContext(internal.IMPLICIT.STORE);
|
|
30
30
|
var StoreProvider = ({ children, store = internal.IMPLICIT.STORE }) => /* @__PURE__ */ jsxRuntime.jsx(StoreContext.Provider, { value: store, children });
|
|
31
|
-
function useI(token) {
|
|
32
|
-
const store =
|
|
33
|
-
const
|
|
31
|
+
function useI(token, key) {
|
|
32
|
+
const store = React5__namespace.useContext(StoreContext);
|
|
33
|
+
const stateToken = token.type === `atom_family` || token.type === `mutable_atom_family` || token.type === `selector_family` ? internal.findInStore(token, key, store) : token;
|
|
34
|
+
const setter = React5__namespace.useRef(null);
|
|
34
35
|
if (setter.current === null) {
|
|
35
|
-
setter.current = (next) => atom_io.setState(
|
|
36
|
+
setter.current = (next) => atom_io.setState(stateToken, next, store);
|
|
36
37
|
}
|
|
37
38
|
return setter.current;
|
|
38
39
|
}
|
|
39
|
-
function useO(token) {
|
|
40
|
-
const store =
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
() =>
|
|
45
|
-
() => atom_io.getState(
|
|
40
|
+
function useO(token, key) {
|
|
41
|
+
const store = React5__namespace.useContext(StoreContext);
|
|
42
|
+
const stateToken = token.type === `atom_family` || token.type === `mutable_atom_family` || token.type === `selector_family` || token.type === `readonly_selector_family` ? internal.findInStore(token, key, store) : token;
|
|
43
|
+
const id = React5__namespace.useId();
|
|
44
|
+
return React5__namespace.useSyncExternalStore(
|
|
45
|
+
(dispatch) => internal.subscribeToState(stateToken, dispatch, `use-o:${id}`, store),
|
|
46
|
+
() => atom_io.getState(stateToken, store),
|
|
47
|
+
() => atom_io.getState(stateToken, store)
|
|
46
48
|
);
|
|
47
49
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
|
|
51
|
+
// react/src/use-json.ts
|
|
52
|
+
function useJSON(token, key) {
|
|
53
|
+
const store = React5__namespace.useContext(StoreContext);
|
|
54
|
+
const stateToken = token.type === `mutable_atom_family` ? internal.findInStore(token, key, store) : token;
|
|
55
|
+
const jsonToken = internal.getJsonToken(stateToken);
|
|
50
56
|
return useO(jsonToken);
|
|
51
57
|
}
|
|
52
58
|
function useTL(token) {
|
|
53
|
-
const store =
|
|
54
|
-
const id =
|
|
59
|
+
const store = React5__namespace.useContext(StoreContext);
|
|
60
|
+
const id = React5__namespace.useId();
|
|
55
61
|
const timeline = internal.withdraw(token, store);
|
|
56
|
-
const tokenRef =
|
|
62
|
+
const tokenRef = React5__namespace.useRef(token);
|
|
57
63
|
const rebuildMeta = () => {
|
|
58
64
|
var _a, _b;
|
|
59
65
|
return {
|
|
@@ -63,7 +69,7 @@ function useTL(token) {
|
|
|
63
69
|
redo: () => atom_io.redo(token)
|
|
64
70
|
};
|
|
65
71
|
};
|
|
66
|
-
const meta =
|
|
72
|
+
const meta = React5__namespace.useRef(rebuildMeta());
|
|
67
73
|
const retrieve = () => {
|
|
68
74
|
if (meta.current.at !== (timeline == null ? void 0 : timeline.at) || meta.current.length !== (timeline == null ? void 0 : timeline.history.length) || tokenRef.current !== token) {
|
|
69
75
|
tokenRef.current = token;
|
|
@@ -71,7 +77,7 @@ function useTL(token) {
|
|
|
71
77
|
}
|
|
72
78
|
return meta.current;
|
|
73
79
|
};
|
|
74
|
-
return
|
|
80
|
+
return React5__namespace.useSyncExternalStore(
|
|
75
81
|
(dispatch) => internal.subscribeToTimeline(token, dispatch, `use-tl:${id}`, store),
|
|
76
82
|
retrieve,
|
|
77
83
|
retrieve
|
package/react/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/store-context.tsx","../src/
|
|
1
|
+
{"version":3,"sources":["../src/store-context.tsx","../src/use-i.ts","../src/use-json.ts","../src/use-o.ts","../src/use-tl.ts"],"names":["React","findInStore"],"mappings":";AACA,SAAS,gBAAgB;AACzB,YAAY,WAAW;AAQtB;AANM,IAAM,eAAqB,oBAAqB,SAAS,KAAK;AAE9D,IAAM,gBAGR,CAAC,EAAE,UAAU,QAAQ,SAAS,MAAM,MACxC,oBAAC,aAAa,UAAb,EAAsB,OAAO,OAAQ,UAAS;;;ACVhD,SAAS,gBAAgB;AAEzB,SAAS,mBAAmB;AAE5B,YAAYA,YAAW;AAahB,SAAS,KACf,OACA,KACyD;AACzD,QAAM,QAAc,kBAAW,YAAY;AAC3C,QAAM,aACL,MAAM,SAAS,iBACf,MAAM,SAAS,yBACf,MAAM,SAAS,oBACZ,YAAY,OAAO,KAAU,KAAK,IAClC;AACJ,QAAM,SAEI,cAAO,IAAI;AACrB,MAAI,OAAO,YAAY,MAAM;AAC5B,WAAO,UAAU,CAAC,SAAS,SAAS,YAAY,MAAM,KAAK;AAAA,EAC5D;AACA,SAAO,OAAO;AACf;;;AC9BA,SAAS,eAAAC,cAAa,oBAAoB;AAE1C,YAAYD,YAAW;;;ACPvB,SAAS,gBAAgB;AAEzB,SAAS,eAAAC,cAAa,wBAAwB;AAE9C,YAAYD,YAAW;AAWhB,SAAS,KACf,OACA,KACI;AACJ,QAAM,QAAc,kBAAW,YAAY;AAC3C,QAAM,aACL,MAAM,SAAS,iBACf,MAAM,SAAS,yBACf,MAAM,SAAS,qBACf,MAAM,SAAS,6BACZC,aAAY,OAAO,KAAU,KAAK,IAClC;AACJ,QAAM,KAAW,aAAM;AACvB,SAAa;AAAA,IACZ,CAAC,aAAa,iBAAiB,YAAY,UAAU,SAAS,EAAE,IAAI,KAAK;AAAA,IACzE,MAAM,SAAS,YAAY,KAAK;AAAA,IAChC,MAAM,SAAS,YAAY,KAAK;AAAA,EACjC;AACD;;;ADZO,SAAS,QAIf,OAGA,KACe;AACf,QAAM,QAAc,kBAAW,YAAY;AAC3C,QAAM,aACL,MAAM,SAAS,wBACZA,aAAY,OAAO,KAAY,KAAK,IACpC;AACJ,QAAM,YAAY,aAAa,UAAU;AACzC,SAAO,KAAK,SAAS;AACtB;;;AErCA,SAAS,MAAM,YAAY;AAE3B,SAAS,qBAAqB,gBAAgB;AAC9C,YAAYD,YAAW;AAWhB,SAAS,MAAM,OAAyC;AAC9D,QAAM,QAAc,kBAAW,YAAY;AAC3C,QAAM,KAAW,aAAM;AACvB,QAAM,WAAW,SAAS,OAAO,KAAK;AACtC,QAAM,WAAiB,cAAO,KAAK;AACnC,QAAM,cAAc,MAAM;AAnB3B;AAoBE,WAAO;AAAA,MACN,KAAI,0CAAU,OAAV,YAAgB;AAAA,MACpB,SAAQ,0CAAU,QAAQ,WAAlB,YAA4B;AAAA,MACpC,MAAM,MAAM,KAAK,KAAK;AAAA,MACtB,MAAM,MAAM,KAAK,KAAK;AAAA,IACvB;AAAA,EACD;AACA,QAAM,OAAa,cAAqB,YAAY,CAAC;AACrD,QAAM,WAAW,MAAM;AACtB,QACC,KAAK,QAAQ,QAAO,qCAAU,OAC9B,KAAK,QAAQ,YAAW,qCAAU,QAAQ,WAC1C,SAAS,YAAY,OACpB;AACD,eAAS,UAAU;AACnB,WAAK,UAAU,YAAY;AAAA,IAC5B;AACA,WAAO,KAAK;AAAA,EACb;AACA,SAAa;AAAA,IACZ,CAAC,aAAa,oBAAoB,OAAO,UAAU,UAAU,EAAE,IAAI,KAAK;AAAA,IACxE;AAAA,IACA;AAAA,EACD;AACD","sourcesContent":["import type { Store } from \"atom.io/internal\"\nimport { IMPLICIT } from \"atom.io/internal\"\nimport * as React from \"react\"\n\nexport const StoreContext = React.createContext<Store>(IMPLICIT.STORE)\n\nexport const StoreProvider: React.FC<{\n\tchildren: React.ReactNode\n\tstore?: Store\n}> = ({ children, store = IMPLICIT.STORE }) => (\n\t<StoreContext.Provider value={store}>{children}</StoreContext.Provider>\n)\n","import { setState } from \"atom.io\"\nimport type { ReadableToken, WritableFamilyToken, WritableToken } from \"atom.io\"\nimport { findInStore } from \"atom.io/internal\"\nimport type { Json } from \"atom.io/json\"\nimport * as React from \"react\"\n\nimport { StoreContext } from \"./store-context\"\n\nexport function useI<T>(\n\ttoken: WritableToken<T>,\n): <New extends T>(next: New | ((old: T) => New)) => void\n\nexport function useI<T, K extends Json.Serializable>(\n\ttoken: WritableFamilyToken<T, K>,\n\tkey: K,\n): <New extends T>(next: New | ((old: T) => New)) => void\n\nexport function useI<T, K extends Json.Serializable>(\n\ttoken: WritableFamilyToken<T, K> | WritableToken<T>,\n\tkey?: K,\n): <New extends T>(next: New | ((old: T) => New)) => void {\n\tconst store = React.useContext(StoreContext)\n\tconst stateToken: ReadableToken<any> =\n\t\ttoken.type === `atom_family` ||\n\t\ttoken.type === `mutable_atom_family` ||\n\t\ttoken.type === `selector_family`\n\t\t\t? findInStore(token, key as K, store)\n\t\t\t: token\n\tconst setter: React.MutableRefObject<\n\t\t(<New extends T>(next: New | ((old: T) => New)) => void) | null\n\t> = React.useRef(null)\n\tif (setter.current === null) {\n\t\tsetter.current = (next) => setState(stateToken, next, store)\n\t}\n\treturn setter.current\n}\n","import type {\n\tMutableAtomFamilyToken,\n\tMutableAtomToken,\n\tReadableToken,\n} from \"atom.io\"\nimport { findInStore, getJsonToken } from \"atom.io/internal\"\nimport type { Json } from \"atom.io/json\"\nimport * as React from \"react\"\n\nimport { StoreContext } from \"./store-context\"\nimport { useO } from \"./use-o\"\n\nexport function useJSON<Serializable extends Json.Serializable>(\n\ttoken: MutableAtomToken<any, Serializable>,\n): Serializable\n\nexport function useJSON<\n\tSerializable extends Json.Serializable,\n\tKey extends Serializable,\n>(token: MutableAtomFamilyToken<any, Serializable, Key>, key: Key): Serializable\n\nexport function useJSON<\n\tSerializable extends Json.Serializable,\n\tKey extends Serializable,\n>(\n\ttoken:\n\t\t| MutableAtomFamilyToken<any, Serializable, Key>\n\t\t| MutableAtomToken<any, Serializable>,\n\tkey?: Key,\n): Serializable {\n\tconst store = React.useContext(StoreContext)\n\tconst stateToken: ReadableToken<any> =\n\t\ttoken.type === `mutable_atom_family`\n\t\t\t? findInStore(token, key as Key, store)\n\t\t\t: token\n\tconst jsonToken = getJsonToken(stateToken)\n\treturn useO(jsonToken)\n}\n","import { getState } from \"atom.io\"\nimport type { ReadableFamilyToken, ReadableToken } from \"atom.io\"\nimport { findInStore, subscribeToState } from \"atom.io/internal\"\nimport type { Json } from \"atom.io/json\"\nimport * as React from \"react\"\n\nimport { StoreContext } from \"./store-context\"\n\nexport function useO<T>(token: ReadableToken<T>): T\n\nexport function useO<T, K extends Json.Serializable>(\n\ttoken: ReadableFamilyToken<T, K>,\n\tkey: K,\n): T\n\nexport function useO<T, K extends Json.Serializable>(\n\ttoken: ReadableFamilyToken<T, K> | ReadableToken<T>,\n\tkey?: K,\n): T {\n\tconst store = React.useContext(StoreContext)\n\tconst stateToken: ReadableToken<any> =\n\t\ttoken.type === `atom_family` ||\n\t\ttoken.type === `mutable_atom_family` ||\n\t\ttoken.type === `selector_family` ||\n\t\ttoken.type === `readonly_selector_family`\n\t\t\t? findInStore(token, key as K, store)\n\t\t\t: token\n\tconst id = React.useId()\n\treturn React.useSyncExternalStore<T>(\n\t\t(dispatch) => subscribeToState(stateToken, dispatch, `use-o:${id}`, store),\n\t\t() => getState(stateToken, store),\n\t\t() => getState(stateToken, store),\n\t)\n}\n","import { redo, undo } from \"atom.io\"\nimport type { TimelineToken } from \"atom.io\"\nimport { subscribeToTimeline, withdraw } from \"atom.io/internal\"\nimport * as React from \"react\"\n\nimport { StoreContext } from \"./store-context\"\n\nexport type TimelineMeta = {\n\tat: number\n\tlength: number\n\tundo: () => void\n\tredo: () => void\n}\n\nexport function useTL(token: TimelineToken<any>): TimelineMeta {\n\tconst store = React.useContext(StoreContext)\n\tconst id = React.useId()\n\tconst timeline = withdraw(token, store)\n\tconst tokenRef = React.useRef(token)\n\tconst rebuildMeta = () => {\n\t\treturn {\n\t\t\tat: timeline?.at ?? NaN,\n\t\t\tlength: timeline?.history.length ?? NaN,\n\t\t\tundo: () => undo(token),\n\t\t\tredo: () => redo(token),\n\t\t}\n\t}\n\tconst meta = React.useRef<TimelineMeta>(rebuildMeta())\n\tconst retrieve = () => {\n\t\tif (\n\t\t\tmeta.current.at !== timeline?.at ||\n\t\t\tmeta.current.length !== timeline?.history.length ||\n\t\t\ttokenRef.current !== token\n\t\t) {\n\t\t\ttokenRef.current = token\n\t\t\tmeta.current = rebuildMeta()\n\t\t}\n\t\treturn meta.current\n\t}\n\treturn React.useSyncExternalStore<TimelineMeta>(\n\t\t(dispatch) => subscribeToTimeline(token, dispatch, `use-tl:${id}`, store),\n\t\tretrieve,\n\t\tretrieve,\n\t)\n}\n"]}
|
package/react/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Store } from 'atom.io/internal';
|
|
2
2
|
import * as React from 'react';
|
|
3
|
-
import { WritableToken,
|
|
3
|
+
import { WritableToken, WritableFamilyToken, MutableAtomToken, MutableAtomFamilyToken, ReadableToken, ReadableFamilyToken, TimelineToken } from 'atom.io';
|
|
4
4
|
import { Json } from 'atom.io/json';
|
|
5
5
|
|
|
6
6
|
declare const StoreContext: React.Context<Store>;
|
|
@@ -10,8 +10,14 @@ declare const StoreProvider: React.FC<{
|
|
|
10
10
|
}>;
|
|
11
11
|
|
|
12
12
|
declare function useI<T>(token: WritableToken<T>): <New extends T>(next: New | ((old: T) => New)) => void;
|
|
13
|
-
declare function
|
|
13
|
+
declare function useI<T, K extends Json.Serializable>(token: WritableFamilyToken<T, K>, key: K): <New extends T>(next: New | ((old: T) => New)) => void;
|
|
14
|
+
|
|
14
15
|
declare function useJSON<Serializable extends Json.Serializable>(token: MutableAtomToken<any, Serializable>): Serializable;
|
|
16
|
+
declare function useJSON<Serializable extends Json.Serializable, Key extends Serializable>(token: MutableAtomFamilyToken<any, Serializable, Key>, key: Key): Serializable;
|
|
17
|
+
|
|
18
|
+
declare function useO<T>(token: ReadableToken<T>): T;
|
|
19
|
+
declare function useO<T, K extends Json.Serializable>(token: ReadableFamilyToken<T, K>, key: K): T;
|
|
20
|
+
|
|
15
21
|
type TimelineMeta = {
|
|
16
22
|
at: number;
|
|
17
23
|
length: number;
|
package/react/dist/index.js
CHANGED
|
@@ -1,37 +1,43 @@
|
|
|
1
1
|
import '../../dist/chunk-PZLG2HP3.js';
|
|
2
|
-
import { IMPLICIT, subscribeToState, getJsonToken, withdraw, subscribeToTimeline } from 'atom.io/internal';
|
|
3
|
-
import * as
|
|
2
|
+
import { IMPLICIT, findInStore, subscribeToState, getJsonToken, withdraw, subscribeToTimeline } from 'atom.io/internal';
|
|
3
|
+
import * as React5 from 'react';
|
|
4
4
|
import { jsx } from 'react/jsx-runtime';
|
|
5
5
|
import { setState, getState, undo, redo } from 'atom.io';
|
|
6
6
|
|
|
7
|
-
var StoreContext =
|
|
7
|
+
var StoreContext = React5.createContext(IMPLICIT.STORE);
|
|
8
8
|
var StoreProvider = ({ children, store = IMPLICIT.STORE }) => /* @__PURE__ */ jsx(StoreContext.Provider, { value: store, children });
|
|
9
|
-
function useI(token) {
|
|
10
|
-
const store =
|
|
11
|
-
const
|
|
9
|
+
function useI(token, key) {
|
|
10
|
+
const store = React5.useContext(StoreContext);
|
|
11
|
+
const stateToken = token.type === `atom_family` || token.type === `mutable_atom_family` || token.type === `selector_family` ? findInStore(token, key, store) : token;
|
|
12
|
+
const setter = React5.useRef(null);
|
|
12
13
|
if (setter.current === null) {
|
|
13
|
-
setter.current = (next) => setState(
|
|
14
|
+
setter.current = (next) => setState(stateToken, next, store);
|
|
14
15
|
}
|
|
15
16
|
return setter.current;
|
|
16
17
|
}
|
|
17
|
-
function useO(token) {
|
|
18
|
-
const store =
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
() =>
|
|
23
|
-
() => getState(
|
|
18
|
+
function useO(token, key) {
|
|
19
|
+
const store = React5.useContext(StoreContext);
|
|
20
|
+
const stateToken = token.type === `atom_family` || token.type === `mutable_atom_family` || token.type === `selector_family` || token.type === `readonly_selector_family` ? findInStore(token, key, store) : token;
|
|
21
|
+
const id = React5.useId();
|
|
22
|
+
return React5.useSyncExternalStore(
|
|
23
|
+
(dispatch) => subscribeToState(stateToken, dispatch, `use-o:${id}`, store),
|
|
24
|
+
() => getState(stateToken, store),
|
|
25
|
+
() => getState(stateToken, store)
|
|
24
26
|
);
|
|
25
27
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
|
|
29
|
+
// react/src/use-json.ts
|
|
30
|
+
function useJSON(token, key) {
|
|
31
|
+
const store = React5.useContext(StoreContext);
|
|
32
|
+
const stateToken = token.type === `mutable_atom_family` ? findInStore(token, key, store) : token;
|
|
33
|
+
const jsonToken = getJsonToken(stateToken);
|
|
28
34
|
return useO(jsonToken);
|
|
29
35
|
}
|
|
30
36
|
function useTL(token) {
|
|
31
|
-
const store =
|
|
32
|
-
const id =
|
|
37
|
+
const store = React5.useContext(StoreContext);
|
|
38
|
+
const id = React5.useId();
|
|
33
39
|
const timeline = withdraw(token, store);
|
|
34
|
-
const tokenRef =
|
|
40
|
+
const tokenRef = React5.useRef(token);
|
|
35
41
|
const rebuildMeta = () => {
|
|
36
42
|
var _a, _b;
|
|
37
43
|
return {
|
|
@@ -41,7 +47,7 @@ function useTL(token) {
|
|
|
41
47
|
redo: () => redo(token)
|
|
42
48
|
};
|
|
43
49
|
};
|
|
44
|
-
const meta =
|
|
50
|
+
const meta = React5.useRef(rebuildMeta());
|
|
45
51
|
const retrieve = () => {
|
|
46
52
|
if (meta.current.at !== (timeline == null ? void 0 : timeline.at) || meta.current.length !== (timeline == null ? void 0 : timeline.history.length) || tokenRef.current !== token) {
|
|
47
53
|
tokenRef.current = token;
|
|
@@ -49,7 +55,7 @@ function useTL(token) {
|
|
|
49
55
|
}
|
|
50
56
|
return meta.current;
|
|
51
57
|
};
|
|
52
|
-
return
|
|
58
|
+
return React5.useSyncExternalStore(
|
|
53
59
|
(dispatch) => subscribeToTimeline(token, dispatch, `use-tl:${id}`, store),
|
|
54
60
|
retrieve,
|
|
55
61
|
retrieve
|
package/react/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/store-context.tsx","../src/
|
|
1
|
+
{"version":3,"sources":["../src/store-context.tsx","../src/use-i.ts","../src/use-json.ts","../src/use-o.ts","../src/use-tl.ts"],"names":["React","findInStore"],"mappings":";;;AACA,SAAS,gBAAgB;AACzB,YAAY,WAAW;AAQtB;AANM,IAAM,eAAqB,oBAAqB,SAAS,KAAK;AAE9D,IAAM,gBAGR,CAAC,EAAE,UAAU,QAAQ,SAAS,MAAM,MACxC,oBAAC,aAAa,UAAb,EAAsB,OAAO,OAAQ,UAAS;;;ACVhD,SAAS,gBAAgB;AAEzB,SAAS,mBAAmB;AAE5B,YAAYA,YAAW;AAahB,SAAS,KACf,OACA,KACyD;AACzD,QAAM,QAAc,kBAAW,YAAY;AAC3C,QAAM,aACL,MAAM,SAAS,iBACf,MAAM,SAAS,yBACf,MAAM,SAAS,oBACZ,YAAY,OAAO,KAAU,KAAK,IAClC;AACJ,QAAM,SAEI,cAAO,IAAI;AACrB,MAAI,OAAO,YAAY,MAAM;AAC5B,WAAO,UAAU,CAAC,SAAS,SAAS,YAAY,MAAM,KAAK;AAAA,EAC5D;AACA,SAAO,OAAO;AACf;;;AC9BA,SAAS,eAAAC,cAAa,oBAAoB;AAE1C,YAAYD,YAAW;;;ACPvB,SAAS,gBAAgB;AAEzB,SAAS,eAAAC,cAAa,wBAAwB;AAE9C,YAAYD,YAAW;AAWhB,SAAS,KACf,OACA,KACI;AACJ,QAAM,QAAc,kBAAW,YAAY;AAC3C,QAAM,aACL,MAAM,SAAS,iBACf,MAAM,SAAS,yBACf,MAAM,SAAS,qBACf,MAAM,SAAS,6BACZC,aAAY,OAAO,KAAU,KAAK,IAClC;AACJ,QAAM,KAAW,aAAM;AACvB,SAAa;AAAA,IACZ,CAAC,aAAa,iBAAiB,YAAY,UAAU,SAAS,EAAE,IAAI,KAAK;AAAA,IACzE,MAAM,SAAS,YAAY,KAAK;AAAA,IAChC,MAAM,SAAS,YAAY,KAAK;AAAA,EACjC;AACD;;;ADZO,SAAS,QAIf,OAGA,KACe;AACf,QAAM,QAAc,kBAAW,YAAY;AAC3C,QAAM,aACL,MAAM,SAAS,wBACZA,aAAY,OAAO,KAAY,KAAK,IACpC;AACJ,QAAM,YAAY,aAAa,UAAU;AACzC,SAAO,KAAK,SAAS;AACtB;;;AErCA,SAAS,MAAM,YAAY;AAE3B,SAAS,qBAAqB,gBAAgB;AAC9C,YAAYD,YAAW;AAWhB,SAAS,MAAM,OAAyC;AAC9D,QAAM,QAAc,kBAAW,YAAY;AAC3C,QAAM,KAAW,aAAM;AACvB,QAAM,WAAW,SAAS,OAAO,KAAK;AACtC,QAAM,WAAiB,cAAO,KAAK;AACnC,QAAM,cAAc,MAAM;AAnB3B;AAoBE,WAAO;AAAA,MACN,KAAI,0CAAU,OAAV,YAAgB;AAAA,MACpB,SAAQ,0CAAU,QAAQ,WAAlB,YAA4B;AAAA,MACpC,MAAM,MAAM,KAAK,KAAK;AAAA,MACtB,MAAM,MAAM,KAAK,KAAK;AAAA,IACvB;AAAA,EACD;AACA,QAAM,OAAa,cAAqB,YAAY,CAAC;AACrD,QAAM,WAAW,MAAM;AACtB,QACC,KAAK,QAAQ,QAAO,qCAAU,OAC9B,KAAK,QAAQ,YAAW,qCAAU,QAAQ,WAC1C,SAAS,YAAY,OACpB;AACD,eAAS,UAAU;AACnB,WAAK,UAAU,YAAY;AAAA,IAC5B;AACA,WAAO,KAAK;AAAA,EACb;AACA,SAAa;AAAA,IACZ,CAAC,aAAa,oBAAoB,OAAO,UAAU,UAAU,EAAE,IAAI,KAAK;AAAA,IACxE;AAAA,IACA;AAAA,EACD;AACD","sourcesContent":["import type { Store } from \"atom.io/internal\"\nimport { IMPLICIT } from \"atom.io/internal\"\nimport * as React from \"react\"\n\nexport const StoreContext = React.createContext<Store>(IMPLICIT.STORE)\n\nexport const StoreProvider: React.FC<{\n\tchildren: React.ReactNode\n\tstore?: Store\n}> = ({ children, store = IMPLICIT.STORE }) => (\n\t<StoreContext.Provider value={store}>{children}</StoreContext.Provider>\n)\n","import { setState } from \"atom.io\"\nimport type { ReadableToken, WritableFamilyToken, WritableToken } from \"atom.io\"\nimport { findInStore } from \"atom.io/internal\"\nimport type { Json } from \"atom.io/json\"\nimport * as React from \"react\"\n\nimport { StoreContext } from \"./store-context\"\n\nexport function useI<T>(\n\ttoken: WritableToken<T>,\n): <New extends T>(next: New | ((old: T) => New)) => void\n\nexport function useI<T, K extends Json.Serializable>(\n\ttoken: WritableFamilyToken<T, K>,\n\tkey: K,\n): <New extends T>(next: New | ((old: T) => New)) => void\n\nexport function useI<T, K extends Json.Serializable>(\n\ttoken: WritableFamilyToken<T, K> | WritableToken<T>,\n\tkey?: K,\n): <New extends T>(next: New | ((old: T) => New)) => void {\n\tconst store = React.useContext(StoreContext)\n\tconst stateToken: ReadableToken<any> =\n\t\ttoken.type === `atom_family` ||\n\t\ttoken.type === `mutable_atom_family` ||\n\t\ttoken.type === `selector_family`\n\t\t\t? findInStore(token, key as K, store)\n\t\t\t: token\n\tconst setter: React.MutableRefObject<\n\t\t(<New extends T>(next: New | ((old: T) => New)) => void) | null\n\t> = React.useRef(null)\n\tif (setter.current === null) {\n\t\tsetter.current = (next) => setState(stateToken, next, store)\n\t}\n\treturn setter.current\n}\n","import type {\n\tMutableAtomFamilyToken,\n\tMutableAtomToken,\n\tReadableToken,\n} from \"atom.io\"\nimport { findInStore, getJsonToken } from \"atom.io/internal\"\nimport type { Json } from \"atom.io/json\"\nimport * as React from \"react\"\n\nimport { StoreContext } from \"./store-context\"\nimport { useO } from \"./use-o\"\n\nexport function useJSON<Serializable extends Json.Serializable>(\n\ttoken: MutableAtomToken<any, Serializable>,\n): Serializable\n\nexport function useJSON<\n\tSerializable extends Json.Serializable,\n\tKey extends Serializable,\n>(token: MutableAtomFamilyToken<any, Serializable, Key>, key: Key): Serializable\n\nexport function useJSON<\n\tSerializable extends Json.Serializable,\n\tKey extends Serializable,\n>(\n\ttoken:\n\t\t| MutableAtomFamilyToken<any, Serializable, Key>\n\t\t| MutableAtomToken<any, Serializable>,\n\tkey?: Key,\n): Serializable {\n\tconst store = React.useContext(StoreContext)\n\tconst stateToken: ReadableToken<any> =\n\t\ttoken.type === `mutable_atom_family`\n\t\t\t? findInStore(token, key as Key, store)\n\t\t\t: token\n\tconst jsonToken = getJsonToken(stateToken)\n\treturn useO(jsonToken)\n}\n","import { getState } from \"atom.io\"\nimport type { ReadableFamilyToken, ReadableToken } from \"atom.io\"\nimport { findInStore, subscribeToState } from \"atom.io/internal\"\nimport type { Json } from \"atom.io/json\"\nimport * as React from \"react\"\n\nimport { StoreContext } from \"./store-context\"\n\nexport function useO<T>(token: ReadableToken<T>): T\n\nexport function useO<T, K extends Json.Serializable>(\n\ttoken: ReadableFamilyToken<T, K>,\n\tkey: K,\n): T\n\nexport function useO<T, K extends Json.Serializable>(\n\ttoken: ReadableFamilyToken<T, K> | ReadableToken<T>,\n\tkey?: K,\n): T {\n\tconst store = React.useContext(StoreContext)\n\tconst stateToken: ReadableToken<any> =\n\t\ttoken.type === `atom_family` ||\n\t\ttoken.type === `mutable_atom_family` ||\n\t\ttoken.type === `selector_family` ||\n\t\ttoken.type === `readonly_selector_family`\n\t\t\t? findInStore(token, key as K, store)\n\t\t\t: token\n\tconst id = React.useId()\n\treturn React.useSyncExternalStore<T>(\n\t\t(dispatch) => subscribeToState(stateToken, dispatch, `use-o:${id}`, store),\n\t\t() => getState(stateToken, store),\n\t\t() => getState(stateToken, store),\n\t)\n}\n","import { redo, undo } from \"atom.io\"\nimport type { TimelineToken } from \"atom.io\"\nimport { subscribeToTimeline, withdraw } from \"atom.io/internal\"\nimport * as React from \"react\"\n\nimport { StoreContext } from \"./store-context\"\n\nexport type TimelineMeta = {\n\tat: number\n\tlength: number\n\tundo: () => void\n\tredo: () => void\n}\n\nexport function useTL(token: TimelineToken<any>): TimelineMeta {\n\tconst store = React.useContext(StoreContext)\n\tconst id = React.useId()\n\tconst timeline = withdraw(token, store)\n\tconst tokenRef = React.useRef(token)\n\tconst rebuildMeta = () => {\n\t\treturn {\n\t\t\tat: timeline?.at ?? NaN,\n\t\t\tlength: timeline?.history.length ?? NaN,\n\t\t\tundo: () => undo(token),\n\t\t\tredo: () => redo(token),\n\t\t}\n\t}\n\tconst meta = React.useRef<TimelineMeta>(rebuildMeta())\n\tconst retrieve = () => {\n\t\tif (\n\t\t\tmeta.current.at !== timeline?.at ||\n\t\t\tmeta.current.length !== timeline?.history.length ||\n\t\t\ttokenRef.current !== token\n\t\t) {\n\t\t\ttokenRef.current = token\n\t\t\tmeta.current = rebuildMeta()\n\t\t}\n\t\treturn meta.current\n\t}\n\treturn React.useSyncExternalStore<TimelineMeta>(\n\t\t(dispatch) => subscribeToTimeline(token, dispatch, `use-tl:${id}`, store),\n\t\tretrieve,\n\t\tretrieve,\n\t)\n}\n"]}
|
package/react/src/index.ts
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { setState } from "atom.io"
|
|
2
|
+
import type { ReadableToken, WritableFamilyToken, WritableToken } from "atom.io"
|
|
3
|
+
import { findInStore } from "atom.io/internal"
|
|
4
|
+
import type { Json } from "atom.io/json"
|
|
5
|
+
import * as React from "react"
|
|
6
|
+
|
|
7
|
+
import { StoreContext } from "./store-context"
|
|
8
|
+
|
|
9
|
+
export function useI<T>(
|
|
10
|
+
token: WritableToken<T>,
|
|
11
|
+
): <New extends T>(next: New | ((old: T) => New)) => void
|
|
12
|
+
|
|
13
|
+
export function useI<T, K extends Json.Serializable>(
|
|
14
|
+
token: WritableFamilyToken<T, K>,
|
|
15
|
+
key: K,
|
|
16
|
+
): <New extends T>(next: New | ((old: T) => New)) => void
|
|
17
|
+
|
|
18
|
+
export function useI<T, K extends Json.Serializable>(
|
|
19
|
+
token: WritableFamilyToken<T, K> | WritableToken<T>,
|
|
20
|
+
key?: K,
|
|
21
|
+
): <New extends T>(next: New | ((old: T) => New)) => void {
|
|
22
|
+
const store = React.useContext(StoreContext)
|
|
23
|
+
const stateToken: ReadableToken<any> =
|
|
24
|
+
token.type === `atom_family` ||
|
|
25
|
+
token.type === `mutable_atom_family` ||
|
|
26
|
+
token.type === `selector_family`
|
|
27
|
+
? findInStore(token, key as K, store)
|
|
28
|
+
: token
|
|
29
|
+
const setter: React.MutableRefObject<
|
|
30
|
+
(<New extends T>(next: New | ((old: T) => New)) => void) | null
|
|
31
|
+
> = React.useRef(null)
|
|
32
|
+
if (setter.current === null) {
|
|
33
|
+
setter.current = (next) => setState(stateToken, next, store)
|
|
34
|
+
}
|
|
35
|
+
return setter.current
|
|
36
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
MutableAtomFamilyToken,
|
|
3
|
+
MutableAtomToken,
|
|
4
|
+
ReadableToken,
|
|
5
|
+
} from "atom.io"
|
|
6
|
+
import { findInStore, getJsonToken } from "atom.io/internal"
|
|
7
|
+
import type { Json } from "atom.io/json"
|
|
8
|
+
import * as React from "react"
|
|
9
|
+
|
|
10
|
+
import { StoreContext } from "./store-context"
|
|
11
|
+
import { useO } from "./use-o"
|
|
12
|
+
|
|
13
|
+
export function useJSON<Serializable extends Json.Serializable>(
|
|
14
|
+
token: MutableAtomToken<any, Serializable>,
|
|
15
|
+
): Serializable
|
|
16
|
+
|
|
17
|
+
export function useJSON<
|
|
18
|
+
Serializable extends Json.Serializable,
|
|
19
|
+
Key extends Serializable,
|
|
20
|
+
>(token: MutableAtomFamilyToken<any, Serializable, Key>, key: Key): Serializable
|
|
21
|
+
|
|
22
|
+
export function useJSON<
|
|
23
|
+
Serializable extends Json.Serializable,
|
|
24
|
+
Key extends Serializable,
|
|
25
|
+
>(
|
|
26
|
+
token:
|
|
27
|
+
| MutableAtomFamilyToken<any, Serializable, Key>
|
|
28
|
+
| MutableAtomToken<any, Serializable>,
|
|
29
|
+
key?: Key,
|
|
30
|
+
): Serializable {
|
|
31
|
+
const store = React.useContext(StoreContext)
|
|
32
|
+
const stateToken: ReadableToken<any> =
|
|
33
|
+
token.type === `mutable_atom_family`
|
|
34
|
+
? findInStore(token, key as Key, store)
|
|
35
|
+
: token
|
|
36
|
+
const jsonToken = getJsonToken(stateToken)
|
|
37
|
+
return useO(jsonToken)
|
|
38
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getState } from "atom.io"
|
|
2
|
+
import type { ReadableFamilyToken, ReadableToken } from "atom.io"
|
|
3
|
+
import { findInStore, subscribeToState } from "atom.io/internal"
|
|
4
|
+
import type { Json } from "atom.io/json"
|
|
5
|
+
import * as React from "react"
|
|
6
|
+
|
|
7
|
+
import { StoreContext } from "./store-context"
|
|
8
|
+
|
|
9
|
+
export function useO<T>(token: ReadableToken<T>): T
|
|
10
|
+
|
|
11
|
+
export function useO<T, K extends Json.Serializable>(
|
|
12
|
+
token: ReadableFamilyToken<T, K>,
|
|
13
|
+
key: K,
|
|
14
|
+
): T
|
|
15
|
+
|
|
16
|
+
export function useO<T, K extends Json.Serializable>(
|
|
17
|
+
token: ReadableFamilyToken<T, K> | ReadableToken<T>,
|
|
18
|
+
key?: K,
|
|
19
|
+
): T {
|
|
20
|
+
const store = React.useContext(StoreContext)
|
|
21
|
+
const stateToken: ReadableToken<any> =
|
|
22
|
+
token.type === `atom_family` ||
|
|
23
|
+
token.type === `mutable_atom_family` ||
|
|
24
|
+
token.type === `selector_family` ||
|
|
25
|
+
token.type === `readonly_selector_family`
|
|
26
|
+
? findInStore(token, key as K, store)
|
|
27
|
+
: token
|
|
28
|
+
const id = React.useId()
|
|
29
|
+
return React.useSyncExternalStore<T>(
|
|
30
|
+
(dispatch) => subscribeToState(stateToken, dispatch, `use-o:${id}`, store),
|
|
31
|
+
() => getState(stateToken, store),
|
|
32
|
+
() => getState(stateToken, store),
|
|
33
|
+
)
|
|
34
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { redo, undo } from "atom.io"
|
|
2
|
+
import type { TimelineToken } from "atom.io"
|
|
3
|
+
import { subscribeToTimeline, withdraw } from "atom.io/internal"
|
|
4
|
+
import * as React from "react"
|
|
5
|
+
|
|
6
|
+
import { StoreContext } from "./store-context"
|
|
7
|
+
|
|
8
|
+
export type TimelineMeta = {
|
|
9
|
+
at: number
|
|
10
|
+
length: number
|
|
11
|
+
undo: () => void
|
|
12
|
+
redo: () => void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function useTL(token: TimelineToken<any>): TimelineMeta {
|
|
16
|
+
const store = React.useContext(StoreContext)
|
|
17
|
+
const id = React.useId()
|
|
18
|
+
const timeline = withdraw(token, store)
|
|
19
|
+
const tokenRef = React.useRef(token)
|
|
20
|
+
const rebuildMeta = () => {
|
|
21
|
+
return {
|
|
22
|
+
at: timeline?.at ?? NaN,
|
|
23
|
+
length: timeline?.history.length ?? NaN,
|
|
24
|
+
undo: () => undo(token),
|
|
25
|
+
redo: () => redo(token),
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const meta = React.useRef<TimelineMeta>(rebuildMeta())
|
|
29
|
+
const retrieve = () => {
|
|
30
|
+
if (
|
|
31
|
+
meta.current.at !== timeline?.at ||
|
|
32
|
+
meta.current.length !== timeline?.history.length ||
|
|
33
|
+
tokenRef.current !== token
|
|
34
|
+
) {
|
|
35
|
+
tokenRef.current = token
|
|
36
|
+
meta.current = rebuildMeta()
|
|
37
|
+
}
|
|
38
|
+
return meta.current
|
|
39
|
+
}
|
|
40
|
+
return React.useSyncExternalStore<TimelineMeta>(
|
|
41
|
+
(dispatch) => subscribeToTimeline(token, dispatch, `use-tl:${id}`, store),
|
|
42
|
+
retrieve,
|
|
43
|
+
retrieve,
|
|
44
|
+
)
|
|
45
|
+
}
|