@plitzi/nexus 0.32.0 → 0.32.2
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/CHANGELOG.md +39 -0
- package/README.md +211 -27
- package/dist/advanced/index.d.ts +5 -0
- package/dist/advanced/index.mjs +10 -0
- package/dist/async/index.d.ts +0 -2
- package/dist/async/index.mjs +1 -3
- package/dist/createStore/createStore.d.ts +3 -43
- package/dist/createStore/createStore.mjs +85 -100
- package/dist/createStore/helpers/PathTrie.mjs +14 -14
- package/dist/createStore/helpers/createChainReads.d.ts +1 -1
- package/dist/createStore/helpers/createChainReads.mjs +22 -13
- package/dist/createStore/helpers/createSetState.mjs +11 -9
- package/dist/createStore/helpers/forwardParentChanges.mjs +16 -13
- package/dist/createStore/index.mjs +3 -3
- package/dist/derived/index.d.ts +0 -1
- package/dist/derived/index.mjs +1 -2
- package/dist/entities/createEntityStore.d.ts +0 -3
- package/dist/entities/createEntityStore.mjs +71 -78
- package/dist/env.d.ts +4 -0
- package/dist/env.mjs +3 -0
- package/dist/history/index.d.ts +0 -2
- package/dist/history/index.mjs +1 -2
- package/dist/index.d.ts +23 -15
- package/dist/index.mjs +14 -22
- package/dist/middleware/historyMiddleware.mjs +26 -26
- package/dist/middleware/persistMiddleware.mjs +11 -11
- package/dist/middleware/recorderMiddleware.d.ts +21 -0
- package/dist/middleware/recorderMiddleware.mjs +33 -0
- package/dist/{StoreContext.d.ts → react/StoreContext.d.ts} +1 -1
- package/dist/{StoreContext.mjs → react/StoreContext.mjs} +1 -1
- package/dist/{StoreProvider.d.ts → react/StoreProvider.d.ts} +4 -2
- package/dist/react/StoreProvider.mjs +62 -0
- package/dist/react/createStoreHook.d.ts +44 -0
- package/dist/react/createStoreHook.mjs +27 -0
- package/dist/{createStore → react}/hooks/shared.mjs +2 -2
- package/dist/{async → react}/hooks/useAsync.d.ts +1 -1
- package/dist/{async → react}/hooks/useAsync.mjs +1 -1
- package/dist/{async → react}/hooks/useAsyncValue.d.ts +1 -1
- package/dist/{async → react}/hooks/useAsyncValue.mjs +1 -1
- package/dist/{derived → react}/hooks/useDerived.d.ts +1 -1
- package/dist/{derived → react}/hooks/useDerived.mjs +1 -1
- package/dist/react/hooks/useEntity.d.ts +10 -0
- package/dist/react/hooks/useEntity.mjs +23 -0
- package/dist/{createStore → react}/hooks/useStore.mjs +2 -2
- package/dist/{createStore → react}/hooks/useStoreById.mjs +2 -2
- package/dist/{createStore → react}/hooks/useStoreGetter.mjs +1 -1
- package/dist/react/hooks/useStoreHistory.mjs +29 -0
- package/dist/{createStore → react}/hooks/useStoreSetter.mjs +1 -1
- package/dist/{createStore → react}/hooks/useStoreSync.mjs +2 -2
- package/dist/react/index.d.ts +17 -0
- package/dist/react/index.mjs +16 -0
- package/dist/{helpers → react}/useIsomorphicLayoutEffect.mjs +1 -1
- package/dist/types/StoreTypes.d.ts +21 -0
- package/dist/vue/index.d.ts +8 -0
- package/dist/vue/index.mjs +7 -0
- package/dist/vue/injection.d.ts +3 -0
- package/dist/vue/injection.mjs +13 -0
- package/dist/vue/useAsync.d.ts +3 -0
- package/dist/vue/useAsync.mjs +10 -0
- package/dist/vue/useDerived.d.ts +3 -0
- package/dist/vue/useDerived.mjs +10 -0
- package/dist/vue/useEntity.d.ts +11 -0
- package/dist/vue/useEntity.mjs +29 -0
- package/dist/vue/useStore.d.ts +13 -0
- package/dist/vue/useStore.mjs +31 -0
- package/dist/vue/useStoreHistory.d.ts +12 -0
- package/dist/vue/useStoreHistory.mjs +33 -0
- package/logo.svg +77 -22
- package/package.json +133 -56
- package/dist/StoreProvider.mjs +0 -57
- package/dist/history/hooks/useStoreHistory.mjs +0 -24
- /package/dist/{createStore → react}/hooks/shared.d.ts +0 -0
- /package/dist/{createStore → react}/hooks/useStore.d.ts +0 -0
- /package/dist/{createStore → react}/hooks/useStoreById.d.ts +0 -0
- /package/dist/{createStore → react}/hooks/useStoreGetter.d.ts +0 -0
- /package/dist/{history → react}/hooks/useStoreHistory.d.ts +0 -0
- /package/dist/{createStore → react}/hooks/useStoreSetter.d.ts +0 -0
- /package/dist/{createStore → react}/hooks/useStoreSync.d.ts +0 -0
- /package/dist/{helpers → react}/useIsomorphicLayoutEffect.d.ts +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
import { default as useStoreById } from './createStore/hooks/useStoreById';
|
|
3
|
-
import { default as useStoreSetter } from './createStore/hooks/useStoreSetter';
|
|
4
|
-
import { default as StoreProvider } from './StoreProvider';
|
|
5
|
-
export * from './async';
|
|
6
|
-
export * from './createStore';
|
|
7
|
-
export * from './derived';
|
|
8
|
-
export * from './entities';
|
|
9
|
-
export * from './history';
|
|
10
|
-
export * from './middleware';
|
|
11
|
-
export * from './StoreProvider';
|
|
12
|
-
export * from './createStore/hooks/useStoreSetter';
|
|
13
|
-
export * from './createStore/hooks/useStoreById';
|
|
14
|
-
export * from './types';
|
|
15
|
-
export { createStore, StoreProvider, useStoreById, useStoreSetter };
|
|
1
|
+
export { default as createStore } from './createStore/createStore';
|
|
16
2
|
export { setCodegenEnabled } from './createStore/helpers/writeByPath';
|
|
3
|
+
export { createAsync } from './async/createAsync';
|
|
4
|
+
export { createDerived } from './derived/createDerived';
|
|
5
|
+
export { createEntityAdapter } from './entities/createEntityAdapter';
|
|
6
|
+
export { createEntityStore } from './entities/createEntityStore';
|
|
7
|
+
export { loggerMiddleware } from './middleware/loggerMiddleware';
|
|
8
|
+
export { persistMiddleware } from './middleware/persistMiddleware';
|
|
9
|
+
export { historyMiddleware } from './middleware/historyMiddleware';
|
|
10
|
+
export { reduxDevToolsMiddleware } from './middleware/reduxDevToolsMiddleware';
|
|
11
|
+
export { createRecorder } from './middleware/recorderMiddleware';
|
|
12
|
+
export { cascade } from './middleware/cascade';
|
|
13
|
+
export { getStoreHistory } from './middleware/historyMiddleware';
|
|
17
14
|
export { createServerSnapshot, isServerSnapshot } from './rsc';
|
|
15
|
+
export * from './types';
|
|
16
|
+
export type { CreateStoreOptions } from './createStore/createStore';
|
|
17
|
+
export type { AsyncOptions, AsyncResource, AsyncSnapshot, AsyncStatus } from './async/createAsync';
|
|
18
|
+
export type { Derived, DerivedOptions } from './derived/createDerived';
|
|
19
|
+
export type { EntityAdapter, EntityAdapterOptions, EntityId, EntityMap, EntityUpdate, EntityUpdater } from './entities/createEntityAdapter';
|
|
20
|
+
export type { EntityStore, EntityStoreOptions, EntityChangeListener } from './entities/createEntityStore';
|
|
21
|
+
export type { LoggerOptions } from './middleware/loggerMiddleware';
|
|
22
|
+
export type { PersistOptions, PersistStorage, PersistTarget, PersistTargetOption } from './middleware/persistMiddleware';
|
|
23
|
+
export type { ReduxDevToolsOptions } from './middleware/reduxDevToolsMiddleware';
|
|
24
|
+
export type { Recorder, RecorderEntry, RecorderOptions } from './middleware/recorderMiddleware';
|
|
25
|
+
export type { HistoryEntry, HistorySnapshot, StoreHistory, StoreHistoryOptions } from './middleware/historyMiddleware';
|
package/dist/index.mjs
CHANGED
|
@@ -1,23 +1,15 @@
|
|
|
1
1
|
import { createAsync as e } from "./async/createAsync.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import s from "./
|
|
8
|
-
import {
|
|
9
|
-
import l from "./
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
import _ from "./createStore/hooks/useStoreById.mjs";
|
|
17
|
-
import { createServerSnapshot as v, isServerSnapshot as y } from "./rsc/index.mjs";
|
|
18
|
-
import b from "./StoreProvider.mjs";
|
|
19
|
-
import { loggerMiddleware as x } from "./middleware/loggerMiddleware.mjs";
|
|
20
|
-
import { persistMiddleware as S } from "./middleware/persistMiddleware.mjs";
|
|
21
|
-
import { reduxDevToolsMiddleware as C } from "./middleware/reduxDevToolsMiddleware.mjs";
|
|
22
|
-
import { cascade as w } from "./middleware/cascade.mjs";
|
|
23
|
-
export { i as CANCEL, a as StoreContext, b as StoreProvider, o as StoreRegistryContext, w as cascade, e as createAsync, u as createDerived, f as createEntityAdapter, p as createEntityStore, v as createServerSnapshot, l as createStore, c as createStoreHook, m as getStoreHistory, h as historyMiddleware, y as isServerSnapshot, x as loggerMiddleware, S as persistMiddleware, C as reduxDevToolsMiddleware, r as setCodegenEnabled, t as useAsync, n as useAsyncValue, d as useDerived, _ as useStoreById, g as useStoreHistory, s as useStoreSetter };
|
|
2
|
+
import { createDerived as t } from "./derived/createDerived.mjs";
|
|
3
|
+
import { createEntityAdapter as n } from "./entities/createEntityAdapter.mjs";
|
|
4
|
+
import { createEntityStore as r } from "./entities/createEntityStore.mjs";
|
|
5
|
+
import { getStoreHistory as i, historyMiddleware as a } from "./middleware/historyMiddleware.mjs";
|
|
6
|
+
import { loggerMiddleware as o } from "./middleware/loggerMiddleware.mjs";
|
|
7
|
+
import { persistMiddleware as s } from "./middleware/persistMiddleware.mjs";
|
|
8
|
+
import { reduxDevToolsMiddleware as c } from "./middleware/reduxDevToolsMiddleware.mjs";
|
|
9
|
+
import { cascade as l } from "./middleware/cascade.mjs";
|
|
10
|
+
import { setCodegenEnabled as u } from "./createStore/helpers/writeByPath.mjs";
|
|
11
|
+
import { CANCEL as d } from "./types/StoreTypes.mjs";
|
|
12
|
+
import f from "./createStore/createStore.mjs";
|
|
13
|
+
import { createRecorder as p } from "./middleware/recorderMiddleware.mjs";
|
|
14
|
+
import { createServerSnapshot as m, isServerSnapshot as h } from "./rsc/index.mjs";
|
|
15
|
+
export { d as CANCEL, l as cascade, e as createAsync, t as createDerived, n as createEntityAdapter, r as createEntityStore, p as createRecorder, m as createServerSnapshot, f as createStore, i as getStoreHistory, a as historyMiddleware, h as isServerSnapshot, o as loggerMiddleware, s as persistMiddleware, c as reduxDevToolsMiddleware, u as setCodegenEnabled };
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import t from "../helpers/
|
|
3
|
-
import
|
|
1
|
+
import { isDisabled as e } from "./isDisabled.mjs";
|
|
2
|
+
import { isPlainObject as t } from "../createStore/helpers/deepMerge.mjs";
|
|
3
|
+
import n from "../helpers/getByPath.mjs";
|
|
4
4
|
import r from "../helpers/isPathAffected.mjs";
|
|
5
5
|
//#region src/middleware/historyMiddleware.ts
|
|
6
|
-
var i = (
|
|
7
|
-
if (!Object.is(
|
|
8
|
-
if (r <= 0 || !e
|
|
6
|
+
var i = (e, n, r = 12) => {
|
|
7
|
+
if (!Object.is(e, n)) {
|
|
8
|
+
if (r <= 0 || !t(e) || !t(n)) return {
|
|
9
9
|
path: "",
|
|
10
10
|
value: n
|
|
11
11
|
};
|
|
12
|
-
for (let
|
|
13
|
-
if (Object.is(t
|
|
14
|
-
let a = i(t
|
|
12
|
+
for (let t of /* @__PURE__ */ new Set([...Object.keys(e), ...Object.keys(n)])) {
|
|
13
|
+
if (Object.is(e[t], n[t])) continue;
|
|
14
|
+
let a = i(e[t], n[t], r - 1);
|
|
15
15
|
if (a) return {
|
|
16
|
-
path: a.path ? `${
|
|
16
|
+
path: a.path ? `${t}.${a.path}` : t,
|
|
17
17
|
value: a.value
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
|
-
function a(e,
|
|
23
|
-
let a =
|
|
22
|
+
function a(e, t = {}) {
|
|
23
|
+
let a = t.limit ?? 100, o = t.path, s = t.shouldRecord, c = [{
|
|
24
24
|
state: e.getState(),
|
|
25
25
|
timestamp: Date.now()
|
|
26
26
|
}], l = 0, u = !1, d = () => ({
|
|
@@ -30,11 +30,11 @@ function a(e, n = {}) {
|
|
|
30
30
|
canRedo: l < c.length - 1
|
|
31
31
|
}), f = d(), p = /* @__PURE__ */ new Set(), m = () => {
|
|
32
32
|
f = d(), p.forEach((e) => e());
|
|
33
|
-
}, h = ({ path:
|
|
34
|
-
if (u || s && !s(
|
|
35
|
-
let d = l < c.length - 1 ? c.slice(0, l + 1) : c, f = d[d.length - 1].state, p = e.getState(), h =
|
|
33
|
+
}, h = ({ path: t }) => {
|
|
34
|
+
if (u || s && !s(t) || o && t !== void 0 && !r(t, o)) return;
|
|
35
|
+
let d = l < c.length - 1 ? c.slice(0, l + 1) : c, f = d[d.length - 1].state, p = e.getState(), h = t ?? o, g, _ = t ?? o;
|
|
36
36
|
if (_) try {
|
|
37
|
-
let e = i(
|
|
37
|
+
let e = i(n(f, _), n(p, _));
|
|
38
38
|
e && (h = e.path ? `${_}.${e.path}` : _, g = e.value);
|
|
39
39
|
} catch {}
|
|
40
40
|
let v = [...d, {
|
|
@@ -44,14 +44,14 @@ function a(e, n = {}) {
|
|
|
44
44
|
timestamp: Date.now()
|
|
45
45
|
}];
|
|
46
46
|
c = v.length > a ? v.slice(v.length - a) : v, l = c.length - 1, m();
|
|
47
|
-
}, g = (
|
|
48
|
-
if (
|
|
49
|
-
let r = c[
|
|
47
|
+
}, g = (t) => {
|
|
48
|
+
if (t < 0 || t >= c.length || t === l) return;
|
|
49
|
+
let r = c[t].state;
|
|
50
50
|
if (u = !0, o) {
|
|
51
|
-
let
|
|
52
|
-
e.setState(o, () =>
|
|
51
|
+
let t = n(r, o);
|
|
52
|
+
e.setState(o, () => t);
|
|
53
53
|
} else e.setState(void 0, () => r);
|
|
54
|
-
u = !1, l =
|
|
54
|
+
u = !1, l = t, m();
|
|
55
55
|
};
|
|
56
56
|
return {
|
|
57
57
|
handle: {
|
|
@@ -74,11 +74,11 @@ var o = /* @__PURE__ */ new WeakMap();
|
|
|
74
74
|
function s(e) {
|
|
75
75
|
return o.get(e);
|
|
76
76
|
}
|
|
77
|
-
var c = (
|
|
78
|
-
let
|
|
77
|
+
var c = (t) => {
|
|
78
|
+
let n = t?.enabled;
|
|
79
79
|
return (r) => {
|
|
80
|
-
if (n
|
|
81
|
-
let { handle: i, record: s } = a(r,
|
|
80
|
+
if (e(n, r.getState())) return;
|
|
81
|
+
let { handle: i, record: s } = a(r, t);
|
|
82
82
|
return o.set(r, i), { onChange: s };
|
|
83
83
|
};
|
|
84
84
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import e from "
|
|
2
|
-
import
|
|
1
|
+
import { isDisabled as e } from "./isDisabled.mjs";
|
|
2
|
+
import t from "../helpers/getByPath.mjs";
|
|
3
3
|
import n from "../helpers/isPathAffected.mjs";
|
|
4
4
|
//#region src/middleware/persistMiddleware.ts
|
|
5
5
|
var r = (e = "local") => {
|
|
@@ -16,13 +16,13 @@ var r = (e = "local") => {
|
|
|
16
16
|
if (!m) return h;
|
|
17
17
|
let t = m(e);
|
|
18
18
|
return t ? r(t) : void 0;
|
|
19
|
-
}, _ = (
|
|
19
|
+
}, _ = (e) => {
|
|
20
20
|
if (c) {
|
|
21
21
|
let n = {};
|
|
22
|
-
for (let r of c) n[r] = e
|
|
22
|
+
for (let r of c) n[r] = t(e, r);
|
|
23
23
|
return n;
|
|
24
24
|
}
|
|
25
|
-
return l ? l(
|
|
25
|
+
return l ? l(e) : e;
|
|
26
26
|
}, v = (e) => {
|
|
27
27
|
let t = e.getState(), n = g(t);
|
|
28
28
|
if (!n) return;
|
|
@@ -32,17 +32,17 @@ var r = (e = "local") => {
|
|
|
32
32
|
};
|
|
33
33
|
n.setItem(o, JSON.stringify(r));
|
|
34
34
|
}, y = (e) => !c || c.some((t) => n(e, t));
|
|
35
|
-
return (
|
|
36
|
-
if (
|
|
35
|
+
return (t) => {
|
|
36
|
+
if (e(p, t.getState())) return;
|
|
37
37
|
let n = !1, r = () => {
|
|
38
38
|
if (n) return;
|
|
39
|
-
let
|
|
40
|
-
|
|
39
|
+
let e = g(t.getState());
|
|
40
|
+
e && (n = !0, a(t, o, e, u, c, d, f));
|
|
41
41
|
};
|
|
42
42
|
return {
|
|
43
43
|
hydrate: r,
|
|
44
|
-
onChange: (
|
|
45
|
-
r(), y(
|
|
44
|
+
onChange: (e) => {
|
|
45
|
+
r(), y(e.path) && v(t);
|
|
46
46
|
}
|
|
47
47
|
};
|
|
48
48
|
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { MiddlewareOptions, StoreChange, StoreMiddleware } from '../types';
|
|
2
|
+
export type RecorderEntry = {
|
|
3
|
+
seq: number;
|
|
4
|
+
time: number;
|
|
5
|
+
path: string | undefined;
|
|
6
|
+
prevValue: unknown;
|
|
7
|
+
nextValue: unknown;
|
|
8
|
+
};
|
|
9
|
+
export type RecorderOptions<TState extends object> = MiddlewareOptions<TState> & {
|
|
10
|
+
max?: number;
|
|
11
|
+
};
|
|
12
|
+
export type Recorder<TState extends object> = {
|
|
13
|
+
middleware: StoreMiddleware<TState>;
|
|
14
|
+
record: (change: Pick<StoreChange<TState>, 'path' | 'prevValue' | 'nextValue'>) => void;
|
|
15
|
+
getEntries: () => readonly RecorderEntry[];
|
|
16
|
+
entriesSince: (seq: number) => RecorderEntry[];
|
|
17
|
+
lastSeq: () => number;
|
|
18
|
+
subscribe: (listener: () => void) => () => void;
|
|
19
|
+
clear: () => void;
|
|
20
|
+
};
|
|
21
|
+
export declare const createRecorder: <TState extends object>(options?: RecorderOptions<TState>) => Recorder<TState>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import e from "../createStore/helpers/Subscribers.mjs";
|
|
2
|
+
import { isDisabled as t } from "./isDisabled.mjs";
|
|
3
|
+
//#region src/middleware/recorderMiddleware.ts
|
|
4
|
+
var n = () => typeof performance < "u" ? performance.now() : Date.now(), r = (r = {}) => {
|
|
5
|
+
let { max: i = 100, enabled: a } = r, o = new e(), s = [], c = 0, l = () => {
|
|
6
|
+
o.forEach((e) => e());
|
|
7
|
+
}, u = (e) => {
|
|
8
|
+
c += 1;
|
|
9
|
+
let t = {
|
|
10
|
+
seq: c,
|
|
11
|
+
time: n(),
|
|
12
|
+
path: e.path,
|
|
13
|
+
prevValue: e.prevValue,
|
|
14
|
+
nextValue: e.nextValue
|
|
15
|
+
}, r = s.length >= i ? s.slice(s.length - i + 1) : s.slice();
|
|
16
|
+
r.push(t), s = r, l();
|
|
17
|
+
};
|
|
18
|
+
return {
|
|
19
|
+
middleware: (e) => {
|
|
20
|
+
if (!t(a, e.getState())) return { onChange: u };
|
|
21
|
+
},
|
|
22
|
+
record: u,
|
|
23
|
+
getEntries: () => s,
|
|
24
|
+
entriesSince: (e) => s.filter((t) => t.seq > e),
|
|
25
|
+
lastSeq: () => c,
|
|
26
|
+
subscribe: (e) => o.add(e),
|
|
27
|
+
clear: () => {
|
|
28
|
+
s = [], l();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
//#endregion
|
|
33
|
+
export { r as createRecorder };
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { StoreContext, StoreRegistryContext } from './StoreContext';
|
|
2
|
-
import { StoreApi, StoreMiddleware } from '
|
|
2
|
+
import { StoreApi, StoreMiddleware } from '../types';
|
|
3
3
|
import { ReactNode } from 'react';
|
|
4
4
|
export type StoreProviderProps<TState extends object = any> = {
|
|
5
5
|
store?: StoreApi<TState>;
|
|
6
6
|
id?: string;
|
|
7
|
+
segment?: string;
|
|
8
|
+
isolate?: ReadonlyArray<string>;
|
|
7
9
|
path?: string;
|
|
8
10
|
value?: Partial<TState> | ((state: TState) => TState);
|
|
9
11
|
/**
|
|
@@ -19,6 +21,6 @@ export type StoreProviderProps<TState extends object = any> = {
|
|
|
19
21
|
middlewares?: StoreMiddleware<TState>[];
|
|
20
22
|
children?: ReactNode;
|
|
21
23
|
};
|
|
22
|
-
declare const StoreProvider: <TState extends object = any>({ store, id, path, value, inherit, autoSync, middlewares, children }: StoreProviderProps<TState>) => import("react
|
|
24
|
+
declare const StoreProvider: <TState extends object = any>({ store, id, segment, isolate, path, value, inherit, autoSync, middlewares, children }: StoreProviderProps<TState>) => import("react").JSX.Element;
|
|
23
25
|
export { StoreContext, StoreRegistryContext };
|
|
24
26
|
export default StoreProvider;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { isDev as e } from "../env.mjs";
|
|
2
|
+
import t from "../createStore/index.mjs";
|
|
3
|
+
import { isServerSnapshot as n, stripServerFlag as r } from "../rsc/index.mjs";
|
|
4
|
+
import { StoreContext as i, StoreRegistryContext as a, findStoreInRegistry as o } from "./StoreContext.mjs";
|
|
5
|
+
import s from "./hooks/useStoreSync.mjs";
|
|
6
|
+
import { createContext as c, useContext as l, useEffect as u, useMemo as d, useRef as f } from "react";
|
|
7
|
+
import { jsx as p } from "react/jsx-runtime";
|
|
8
|
+
//#region src/react/StoreProvider.tsx
|
|
9
|
+
var m = c(void 0), h = (e) => e.cascade === !0, g = ({ store: c, id: g, segment: _, isolate: v, path: y, value: b, inherit: x, autoSync: S = !0, middlewares: C, children: w }) => {
|
|
10
|
+
let T = l(i), E = l(m), D = l(a), O = T?.scopePath ?? "", k = d(() => _ === void 0 ? O : O ? `${O}/${_}` : _, [O, _]), A = f(void 0), j = x === "live", M = d(() => {
|
|
11
|
+
let e = x === "snapshot" && T ? T.getState() : {}, t = typeof b == "function" ? b(e) : {
|
|
12
|
+
...e,
|
|
13
|
+
...b
|
|
14
|
+
};
|
|
15
|
+
return n(t) ? r(t) : t;
|
|
16
|
+
}, [
|
|
17
|
+
x,
|
|
18
|
+
T,
|
|
19
|
+
b
|
|
20
|
+
]), N = C ?? [], P = E ? [...E, ...N] : N, F = d(() => {
|
|
21
|
+
let e = N.filter(h);
|
|
22
|
+
return e.length === 0 ? E : [...E ?? [], ...e];
|
|
23
|
+
}, [E, C]);
|
|
24
|
+
A.current ||= c ?? t(() => M, {
|
|
25
|
+
id: g,
|
|
26
|
+
scopePath: k,
|
|
27
|
+
exclusive: j ? v : void 0,
|
|
28
|
+
parent: j ? T : void 0,
|
|
29
|
+
middlewares: P.length > 0 ? P : void 0,
|
|
30
|
+
deferHydrate: !0
|
|
31
|
+
});
|
|
32
|
+
let I = d(() => g ? {
|
|
33
|
+
id: g,
|
|
34
|
+
store: A.current,
|
|
35
|
+
parent: D
|
|
36
|
+
} : D, [g, D]), L = f(!1);
|
|
37
|
+
u(() => {
|
|
38
|
+
e && g && !L.current && o(D, g) && (L.current = !0, console.warn(`@plitzi/nexus: duplicate StoreProvider id "${g}" — it shadows an ancestor provider with the same id, so a descendant's { storeId: "${g}" } or useStoreById("${g}") resolves to this (nearer) store. Use a distinct id, or remove one of the providers.`));
|
|
39
|
+
}, [g, D]), u(() => (j && !c && A.current?.reconnect?.(), () => {
|
|
40
|
+
j && !c && A.current?.destroy?.();
|
|
41
|
+
}), [j, c]);
|
|
42
|
+
let R = f(!1);
|
|
43
|
+
u(() => {
|
|
44
|
+
!R.current && A.current?.hydrate && (A.current.hydrate(), R.current = !0);
|
|
45
|
+
}, []), s(y, y ? b : M, {
|
|
46
|
+
enabled: !!b && S,
|
|
47
|
+
store: A.current
|
|
48
|
+
});
|
|
49
|
+
let z = /* @__PURE__ */ p(i, {
|
|
50
|
+
value: A.current,
|
|
51
|
+
children: w
|
|
52
|
+
});
|
|
53
|
+
return I !== D && (z = /* @__PURE__ */ p(a, {
|
|
54
|
+
value: I,
|
|
55
|
+
children: z
|
|
56
|
+
})), F !== E && (z = /* @__PURE__ */ p(m, {
|
|
57
|
+
value: F,
|
|
58
|
+
children: z
|
|
59
|
+
})), z;
|
|
60
|
+
};
|
|
61
|
+
//#endregion
|
|
62
|
+
export { i as StoreContext, a as StoreRegistryContext, g as default };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { GetterTuple, GetValueFn, GetValueFromBaseFn, MultiPathReturn, PathOf, PathOrFn, PathOrFnSetters, PathOrFnValues, PathSetters, PathSetter, PathValue, PathValues, SetFromBaseFn, SetStateFn, StoreApi, UseStoreGetterOptions, UseStoreMultiOptions, UseStoreOptions, UseStoreSetterOptions, UseStoreSyncMultiOptions, UseStoreSyncOptions } from '../types';
|
|
2
|
+
export declare const createStoreHook: <TState extends object>() => {
|
|
3
|
+
useStore: {
|
|
4
|
+
(options?: UseStoreOptions<TState, TState>): [TState, StoreApi<TState>["setState"]];
|
|
5
|
+
<P extends PathOf<TState>>(path: P, options?: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
6
|
+
transformer?: never;
|
|
7
|
+
}): [PathValue<TState, P>, (value: PathValue<TState, P> | ((prev: PathValue<TState, P>) => PathValue<TState, P>)) => void];
|
|
8
|
+
<P extends PathOf<TState>, TResult>(path: P, options: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
9
|
+
transformer: (value: PathValue<TState, P>) => TResult;
|
|
10
|
+
}): [TResult, (value: PathValue<TState, P> | ((prev: PathValue<TState, P>) => PathValue<TState, P>)) => void];
|
|
11
|
+
<P extends PathOf<TState>>(pathFn: (state: TState) => P, options?: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
12
|
+
transformer?: never;
|
|
13
|
+
}): [PathValue<TState, P>, PathSetter<TState, P>];
|
|
14
|
+
<P extends PathOf<TState>, TResult_1>(pathFn: (state: TState) => P, options: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
15
|
+
transformer: (value: PathValue<TState, P>) => TResult_1;
|
|
16
|
+
}): [TResult_1, PathSetter<TState, P>];
|
|
17
|
+
<const Paths extends ReadonlyArray<PathOf<TState>>>(paths: Paths, options?: Omit<UseStoreMultiOptions<TState, Paths>, "transformer">): MultiPathReturn<TState, Paths>;
|
|
18
|
+
<const Paths extends ReadonlyArray<PathOf<TState>>, TResult_2>(paths: Paths, options: UseStoreMultiOptions<TState, Paths> & {
|
|
19
|
+
transformer: (values: PathValues<TState, Paths>) => TResult_2;
|
|
20
|
+
}): [TResult_2, ...PathSetters<TState, Paths>];
|
|
21
|
+
<const Entries extends ReadonlyArray<PathOrFn<TState>>>(paths: Entries, options?: Omit<UseStoreMultiOptions<TState, any>, "transformer"> & {
|
|
22
|
+
transformer?: never;
|
|
23
|
+
}): [PathOrFnValues<TState, Entries>, ...PathOrFnSetters<TState, Entries>];
|
|
24
|
+
<const Entries extends ReadonlyArray<PathOrFn<TState>>, TResult_3>(paths: Entries, options: Omit<UseStoreMultiOptions<TState, any>, "transformer"> & {
|
|
25
|
+
transformer: (values: PathOrFnValues<TState, Entries>) => TResult_3;
|
|
26
|
+
}): [TResult_3, ...PathOrFnSetters<TState, Entries>];
|
|
27
|
+
};
|
|
28
|
+
useStoreSync: {
|
|
29
|
+
(path: undefined, value: TState | Partial<TState>, options?: UseStoreSyncOptions<TState, TState>): void;
|
|
30
|
+
<P extends PathOf<TState>>(path: P | ((state: TState) => P), value: PathValue<TState, P>, options?: UseStoreSyncOptions<PathValue<TState, P>, TState>): void;
|
|
31
|
+
<const Paths extends ReadonlyArray<PathOf<TState>>>(paths: Paths, values: PathValues<TState, Paths>, options?: UseStoreSyncMultiOptions<TState>): void;
|
|
32
|
+
(paths: ReadonlyArray<PathOrFn<TState>>, values: readonly unknown[], options?: UseStoreSyncMultiOptions<TState>): void;
|
|
33
|
+
};
|
|
34
|
+
useStoreGetter: {
|
|
35
|
+
(options?: UseStoreGetterOptions<TState>): GetValueFn<TState>;
|
|
36
|
+
<P extends PathOf<TState>>(basePath: P, options?: UseStoreGetterOptions<TState>): GetValueFromBaseFn<PathValue<TState, P>>;
|
|
37
|
+
<const Entries extends ReadonlyArray<PathOf<TState> | ((state: TState) => unknown)>>(entries: Entries, options?: UseStoreGetterOptions<TState>): GetterTuple<TState, Entries>;
|
|
38
|
+
};
|
|
39
|
+
useStoreSetter: {
|
|
40
|
+
(options?: UseStoreSetterOptions<TState>): SetStateFn<TState>;
|
|
41
|
+
<P extends PathOf<TState>>(basePath: P, options?: UseStoreSetterOptions<TState>): SetFromBaseFn<PathValue<TState, P>>;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
export default createStoreHook;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import e from "./hooks/useStoreSync.mjs";
|
|
2
|
+
import t from "./hooks/useStore.mjs";
|
|
3
|
+
import n from "./hooks/useStoreGetter.mjs";
|
|
4
|
+
import r from "./hooks/useStoreSetter.mjs";
|
|
5
|
+
//#region src/react/createStoreHook.ts
|
|
6
|
+
var i = () => {
|
|
7
|
+
function i(e, n) {
|
|
8
|
+
return t(e, n);
|
|
9
|
+
}
|
|
10
|
+
function a(t, n, r) {
|
|
11
|
+
e(t, n, r);
|
|
12
|
+
}
|
|
13
|
+
function o(e, t) {
|
|
14
|
+
return n(e, t);
|
|
15
|
+
}
|
|
16
|
+
function s(e, t) {
|
|
17
|
+
return r(e, t);
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
useStore: i,
|
|
21
|
+
useStoreSync: a,
|
|
22
|
+
useStoreGetter: o,
|
|
23
|
+
useStoreSetter: s
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
//#endregion
|
|
27
|
+
export { i as createStoreHook, i as default };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import e from "../../helpers/getByPath.mjs";
|
|
2
|
-
import { StoreContext as t, StoreRegistryContext as n, findStoreInRegistry as r } from "
|
|
2
|
+
import { StoreContext as t, StoreRegistryContext as n, findStoreInRegistry as r } from "../StoreContext.mjs";
|
|
3
3
|
import { useCallback as i, useContext as a, useMemo as o, useSyncExternalStore as s } from "react";
|
|
4
|
-
//#region src/
|
|
4
|
+
//#region src/react/hooks/shared.ts
|
|
5
5
|
var c = (e, t) => {
|
|
6
6
|
if (e.length !== t.length) return !1;
|
|
7
7
|
for (let n = 0; n < e.length; n++) if (!Object.is(e[n], t[n])) return !1;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { AsyncResource, AsyncSnapshot } from '
|
|
1
|
+
import { AsyncResource, AsyncSnapshot } from '../../async/createAsync';
|
|
2
2
|
export declare function useAsync<T, Args extends readonly unknown[]>(resource: AsyncResource<T, Args>): AsyncSnapshot<T>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { AsyncResource } from '
|
|
1
|
+
import { AsyncResource } from '../../async/createAsync';
|
|
2
2
|
export declare function useAsyncValue<T, Args extends readonly unknown[]>(resource: AsyncResource<T, Args>): T;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Derived } from '
|
|
1
|
+
import { Derived } from '../../derived/createDerived';
|
|
2
2
|
export declare function useDerived<R>(derived: Derived<R>): R;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EntityId } from '../../entities/createEntityAdapter';
|
|
2
|
+
import { EntityStore } from '../../entities/createEntityStore';
|
|
3
|
+
export declare function useEntityOne<T>(store: EntityStore<T>, id: EntityId): T | undefined;
|
|
4
|
+
export declare function useEntityIds<T>(store: EntityStore<T>): string[];
|
|
5
|
+
export declare function useEntityAll<T>(store: EntityStore<T>): T[];
|
|
6
|
+
export declare function useEntity<T>(store: EntityStore<T>): {
|
|
7
|
+
useOne: (id: EntityId) => T | undefined;
|
|
8
|
+
useIds: () => string[];
|
|
9
|
+
useAll: () => T[];
|
|
10
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useCallback as e, useSyncExternalStore as t } from "react";
|
|
2
|
+
//#region src/react/hooks/useEntity.ts
|
|
3
|
+
function n(n, r) {
|
|
4
|
+
let i = String(r), a = e((e) => n.subscribeOne(i, e), [n, i]), o = e(() => n.getOne(i), [n, i]);
|
|
5
|
+
return t(a, o, o);
|
|
6
|
+
}
|
|
7
|
+
function r(n) {
|
|
8
|
+
let r = e((e) => n.subscribeIds(e), [n]), i = e(() => n.getIds(), [n]);
|
|
9
|
+
return t(r, i, i);
|
|
10
|
+
}
|
|
11
|
+
function i(n) {
|
|
12
|
+
let r = e((e) => n.subscribeAll(e), [n]), i = e(() => n.getAll(), [n]);
|
|
13
|
+
return t(r, i, i);
|
|
14
|
+
}
|
|
15
|
+
function a(e) {
|
|
16
|
+
return {
|
|
17
|
+
useOne: (t) => n(e, t),
|
|
18
|
+
useIds: () => r(e),
|
|
19
|
+
useAll: () => i(e)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { a as useEntity, i as useEntityAll, r as useEntityIds, n as useEntityOne };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { defaultMultiEqualityFn as e, makeMultiSnapshot as t, makeSingleSnapshot as n, useMultiExternalStore as r, useMultiSetters as i, useMultiSubscribe as a, useResolvedStore as o } from "./shared.mjs";
|
|
2
2
|
import s from "../../helpers/shallowEqual.mjs";
|
|
3
3
|
import { useCallback as c, useMemo as l, useRef as u, useSyncExternalStore as d } from "react";
|
|
4
|
-
//#region src/
|
|
4
|
+
//#region src/react/hooks/useStore.ts
|
|
5
5
|
var f = [];
|
|
6
6
|
function p(e, t, r) {
|
|
7
7
|
let i = r.mode ?? "sync", a = r.enabled ?? !0, o = t === void 0, f = r.equalityFn ?? (o ? s : Object.is), p = r.transformer, m = u(p);
|
|
@@ -46,4 +46,4 @@ function h(e, t = {}) {
|
|
|
46
46
|
return r ? a : i;
|
|
47
47
|
}
|
|
48
48
|
//#endregion
|
|
49
|
-
export { h as default };
|
|
49
|
+
export { h as default, e as defaultMultiEqualityFn };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useResolvedStore as e } from "./shared.mjs";
|
|
2
|
-
//#region src/
|
|
2
|
+
//#region src/react/hooks/useStoreById.ts
|
|
3
3
|
function t(t) {
|
|
4
4
|
return e(void 0, "useStoreById", t);
|
|
5
5
|
}
|
|
6
6
|
//#endregion
|
|
7
|
-
export { t as default };
|
|
7
|
+
export { t as default, t as useStoreById };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import e from "../../helpers/getByPath.mjs";
|
|
2
2
|
import { useResolvedStore as t } from "./shared.mjs";
|
|
3
3
|
import { useMemo as n } from "react";
|
|
4
|
-
//#region src/
|
|
4
|
+
//#region src/react/hooks/useStoreGetter.ts
|
|
5
5
|
function r(r, i) {
|
|
6
6
|
let a = typeof r == "string" ? r : void 0, o = Array.isArray(r) ? r : void 0, s = typeof r == "object" && !Array.isArray(r) ? r : i, c = t(s?.store, "useStoreGetter", s?.storeId), l = o?.map((e) => typeof e == "function" ? e.toString() : e).join("|");
|
|
7
7
|
return n(() => {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { getStoreHistory as e } from "../../middleware/historyMiddleware.mjs";
|
|
2
|
+
import { isProd as t } from "../../env.mjs";
|
|
3
|
+
import { useResolvedStore as n } from "./shared.mjs";
|
|
4
|
+
import { useEffect as r, useSyncExternalStore as i } from "react";
|
|
5
|
+
//#region src/react/hooks/useStoreHistory.ts
|
|
6
|
+
var a = {
|
|
7
|
+
entries: [],
|
|
8
|
+
index: -1,
|
|
9
|
+
canUndo: !1,
|
|
10
|
+
canRedo: !1
|
|
11
|
+
}, o = () => {}, s = () => o, c = () => a, l = /* @__PURE__ */ new WeakSet(), u = (e) => {
|
|
12
|
+
t || l.has(e) || (l.add(e), console.warn("[nexus] useStoreHistory: this store has no history. Add `historyMiddleware()` to its middlewares to record an action log — until then the hook returns an empty, no-op view."));
|
|
13
|
+
};
|
|
14
|
+
function d() {
|
|
15
|
+
let t = n(void 0, "useStoreHistory"), a = e(t);
|
|
16
|
+
r(() => {
|
|
17
|
+
a || u(t);
|
|
18
|
+
}, [a, t]);
|
|
19
|
+
let l = a?.getSnapshot ?? c;
|
|
20
|
+
return {
|
|
21
|
+
...i(a?.subscribe ?? s, l, l),
|
|
22
|
+
undo: a?.undo ?? o,
|
|
23
|
+
redo: a?.redo ?? o,
|
|
24
|
+
travelTo: a?.travelTo ?? o,
|
|
25
|
+
clear: a?.clear ?? o
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { d as useStoreHistory };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useResolvedStore as e } from "./shared.mjs";
|
|
2
2
|
import { useMemo as t } from "react";
|
|
3
|
-
//#region src/
|
|
3
|
+
//#region src/react/hooks/useStoreSetter.ts
|
|
4
4
|
function n(n, r) {
|
|
5
5
|
let i = typeof n == "string" ? n : void 0, a = typeof n == "object" ? n : r, o = e(a?.store, "useStoreSetter", a?.storeId);
|
|
6
6
|
return t(() => i === void 0 ? o.setState : o.withBase(i).setState, [o, i]);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { useResolvedStore as e } from "./shared.mjs";
|
|
2
2
|
import t from "../../helpers/shallowEqual.mjs";
|
|
3
|
-
import n from "
|
|
3
|
+
import n from "../useIsomorphicLayoutEffect.mjs";
|
|
4
4
|
import { useRef as r } from "react";
|
|
5
|
-
//#region src/
|
|
5
|
+
//#region src/react/hooks/useStoreSync.ts
|
|
6
6
|
function i(e, t, i, a) {
|
|
7
7
|
let { mode: o = "sync", enabled: s = !0, syncStrategy: c = "afterRender" } = a, l = t.map((e, t) => typeof e == "function" ? `fn_${t}` : e).join("|"), u = r(!1), d = !u.current;
|
|
8
8
|
u.current = !0;
|