@plitzi/nexus 0.31.2 → 0.32.1
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 +12 -0
- package/README.md +30 -5
- package/dist/advanced/index.d.ts +7 -0
- package/dist/advanced/index.mjs +16 -0
- package/dist/async/createAsync.mjs +8 -1
- package/dist/createStore/createStore.d.ts +4 -45
- package/dist/createStore/createStore.mjs +58 -36
- package/dist/createStore/helpers/PathTrie.d.ts +2 -0
- package/dist/createStore/helpers/PathTrie.mjs +55 -20
- package/dist/createStore/helpers/createChainReads.d.ts +1 -1
- package/dist/createStore/helpers/createChainReads.mjs +18 -19
- package/dist/createStore/helpers/createSetState.mjs +24 -33
- package/dist/createStore/hooks/shared.d.ts +1 -2
- package/dist/createStore/hooks/shared.mjs +7 -18
- package/dist/createStore/hooks/useStore.d.ts +4 -49
- package/dist/createStore/hooks/useStore.mjs +19 -27
- package/dist/createStore/hooks/useStoreGetter.d.ts +2 -5
- package/dist/createStore/hooks/useStoreGetter.mjs +19 -22
- package/dist/createStore/hooks/useStoreSetter.mjs +2 -4
- package/dist/derived/createDerived.d.ts +1 -1
- package/dist/derived/createDerived.mjs +12 -9
- package/dist/entities/createEntityStore.d.ts +38 -0
- package/dist/entities/createEntityStore.mjs +102 -0
- package/dist/entities/index.d.ts +2 -0
- package/dist/entities/index.mjs +2 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.mjs +23 -22
- package/dist/middleware/historyMiddleware.d.ts +3 -3
- package/dist/middleware/historyMiddleware.mjs +35 -30
- package/dist/middleware/index.d.ts +1 -1
- package/dist/middleware/isDisabled.d.ts +2 -0
- package/dist/middleware/isDisabled.mjs +4 -0
- package/dist/middleware/loggerMiddleware.d.ts +2 -2
- package/dist/middleware/loggerMiddleware.mjs +14 -15
- package/dist/middleware/persistMiddleware.d.ts +7 -5
- package/dist/middleware/persistMiddleware.mjs +56 -28
- package/dist/middleware/reduxDevToolsMiddleware.d.ts +2 -2
- package/dist/middleware/reduxDevToolsMiddleware.mjs +26 -21
- package/dist/types/StoreTypes.d.ts +49 -21
- package/package.json +17 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -22,26 +22,51 @@ function Counter() {
|
|
|
22
22
|
}
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
## The whole imperative API in three verbs
|
|
26
|
+
|
|
27
|
+
Outside React, a store speaks three words. `get`, `set` and `watch` are typed against your state and cover the common case — the longer `getState` / `getPath` / `subscribePath` names stay for advanced reads (default values) and back-compat.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { createStore } from '@plitzi/nexus';
|
|
31
|
+
|
|
32
|
+
const store = createStore<State>({ count: 0, user: { name: 'Carlos' } });
|
|
33
|
+
|
|
34
|
+
store.get('user.name'); // 'Carlos' (one path, no full merge)
|
|
35
|
+
store.get(); // entire state
|
|
36
|
+
store.set('user.name', 'Ada'); // typed dot-path write
|
|
37
|
+
store.set('count', n => n + 1); // updater form
|
|
38
|
+
const off = store.watch('user.name', name => render(name)); // fires only for this path
|
|
39
|
+
```
|
|
40
|
+
|
|
25
41
|
## What each piece is for
|
|
26
42
|
|
|
43
|
+
### Core — the 90%
|
|
44
|
+
|
|
27
45
|
| Import | What it's for |
|
|
28
46
|
|---|---|
|
|
29
|
-
| `createStore` | Create a vanilla store (no React needed). |
|
|
47
|
+
| `createStore` | Create a vanilla store (no React needed). Exposes `get` / `set` / `watch`. |
|
|
30
48
|
| `createStoreHook<T>()` | Bind your state type once → fully-typed `useStore*` hooks. |
|
|
31
49
|
| `StoreProvider` | Put a store on React context; optionally scope/sync/record it. |
|
|
32
50
|
| `useStore` | Subscribe + write a path (or paths). Re-renders on change only. |
|
|
51
|
+
|
|
52
|
+
### Advanced — reach for it when you need it
|
|
53
|
+
|
|
54
|
+
Also importable from the focused `@plitzi/nexus/advanced` entry point, which keeps root autocomplete on the Core.
|
|
55
|
+
|
|
56
|
+
| Import | What it's for |
|
|
57
|
+
|---|---|
|
|
33
58
|
| `useStoreSetter` | A stable setter — write without subscribing/re-rendering. |
|
|
34
59
|
| `useStoreGetter` | A stable getter — read current values in callbacks, no re-render. |
|
|
35
60
|
| `useStoreById` | Get a named ancestor store's `StoreApi` by `id` (reachable across disconnected providers). |
|
|
36
61
|
| `useStoreSync` | Mirror an external value (props) **into** the store. |
|
|
37
62
|
| `createDerived` / `useDerived` | A memoized value computed from store paths (reselect-style). |
|
|
38
|
-
| `createEntityAdapter` | CRUD
|
|
63
|
+
| `createEntityAdapter` / `createEntityStore` | CRUD + selectors for a normalized `Record<id, T>` map; the store variant adds O(1) per-item updates. |
|
|
64
|
+
| `createAsync` / `useAsync` / `useAsyncValue` | Race-safe fetch landed on a path; inline UI or Suspense. |
|
|
39
65
|
| `loggerMiddleware` / `persistMiddleware` / `historyMiddleware` / `reduxDevToolsMiddleware` | Middlewares: log, persist to storage, record time-travel, connect to Redux DevTools. |
|
|
66
|
+
| `getStoreHistory` / `useStoreHistory` | Undo / redo / jump-to-snapshot action log (enabled by `historyMiddleware`). |
|
|
40
67
|
| `setCodegenEnabled` | Force on/off the `new Function` codegen path (auto-detects CSP/SSR). |
|
|
41
|
-
| `createServerSnapshot` | Mark server-fetched data for RSC → Client handoff. |
|
|
42
|
-
| `isServerSnapshot` | Check if a value has the SSR marker flag. |
|
|
68
|
+
| `createServerSnapshot` / `isServerSnapshot` | Mark server-fetched data for RSC → Client handoff, and check the marker. |
|
|
43
69
|
| `bindServerAction` | Optimistic updates + revalidation for Next.js Server Actions. |
|
|
44
|
-
| `getStoreHistory` / `useStoreHistory` | Undo / redo / jump-to-snapshot action log (enabled by `historyMiddleware`). |
|
|
45
70
|
|
|
46
71
|
## Architecture
|
|
47
72
|
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default as useStoreById } from '../createStore/hooks/useStoreById';
|
|
2
|
+
export { default as useStoreSetter } from '../createStore/hooks/useStoreSetter';
|
|
3
|
+
export * from '../async';
|
|
4
|
+
export * from '../derived';
|
|
5
|
+
export * from '../entities';
|
|
6
|
+
export * from '../history';
|
|
7
|
+
export * from '../middleware';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import e from "../createStore/hooks/useStoreById.mjs";
|
|
2
|
+
import t from "../createStore/hooks/useStoreSetter.mjs";
|
|
3
|
+
import { createAsync as n } from "../async/createAsync.mjs";
|
|
4
|
+
import { useAsync as r } from "../async/hooks/useAsync.mjs";
|
|
5
|
+
import { useAsyncValue as i } from "../async/hooks/useAsyncValue.mjs";
|
|
6
|
+
import { createDerived as a } from "../derived/createDerived.mjs";
|
|
7
|
+
import { useDerived as o } from "../derived/hooks/useDerived.mjs";
|
|
8
|
+
import { createEntityAdapter as s } from "../entities/createEntityAdapter.mjs";
|
|
9
|
+
import { createEntityStore as c } from "../entities/createEntityStore.mjs";
|
|
10
|
+
import { getStoreHistory as l, historyMiddleware as u } from "../middleware/historyMiddleware.mjs";
|
|
11
|
+
import { useStoreHistory as d } from "../history/hooks/useStoreHistory.mjs";
|
|
12
|
+
import { loggerMiddleware as f } from "../middleware/loggerMiddleware.mjs";
|
|
13
|
+
import { persistMiddleware as p } from "../middleware/persistMiddleware.mjs";
|
|
14
|
+
import { reduxDevToolsMiddleware as m } from "../middleware/reduxDevToolsMiddleware.mjs";
|
|
15
|
+
import { cascade as h } from "../middleware/cascade.mjs";
|
|
16
|
+
export { h as cascade, n as createAsync, a as createDerived, s as createEntityAdapter, c as createEntityStore, l as getStoreHistory, u as historyMiddleware, f as loggerMiddleware, p as persistMiddleware, m as reduxDevToolsMiddleware, r as useAsync, i as useAsyncValue, o as useDerived, e as useStoreById, d as useStoreHistory, t as useStoreSetter };
|
|
@@ -11,7 +11,14 @@ function t(t, n, r, i = {}) {
|
|
|
11
11
|
}, d = l.get, f = (...e) => {
|
|
12
12
|
a = "pending", o = void 0;
|
|
13
13
|
let i = Promise.resolve().then(() => r(...e));
|
|
14
|
-
return s = i, c = i.then(() => void 0, () => void 0), u(), i.then((e) =>
|
|
14
|
+
return s = i, c = i.then(() => void 0, () => void 0), u(), i.then((e) => {
|
|
15
|
+
if (i === s) {
|
|
16
|
+
s = void 0, c = void 0, a = "success";
|
|
17
|
+
let r = t.getPath(n);
|
|
18
|
+
t.setState(n, e), Object.is(r, e) && u();
|
|
19
|
+
}
|
|
20
|
+
return e;
|
|
21
|
+
}, (e) => {
|
|
15
22
|
throw i === s && (s = void 0, c = void 0, a = "error", o = e, u()), e;
|
|
16
23
|
});
|
|
17
24
|
}, p = t.subscribePath(n, u);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GetState, GetterTuple, GetValueFn, GetValueFromBaseFn,
|
|
1
|
+
import { GetState, GetterTuple, GetValueFn, GetValueFromBaseFn, MultiPathReturn, PathOf, PathOrFn, PathOrFnSetters, PathOrFnValues, PathSetters, PathSetter, PathValue, PathValues, SetFromBaseFn, SetState, SetStateFn, StoreApi, StoreMiddleware, UseStoreGetterOptions, UseStoreMultiOptions, UseStoreOptions, UseStoreSetterOptions, UseStoreSyncMultiOptions, UseStoreSyncOptions } from '../types';
|
|
2
2
|
export type CreateStoreOptions<TState extends object> = {
|
|
3
3
|
id?: string;
|
|
4
4
|
parent?: StoreApi<TState>;
|
|
@@ -12,64 +12,26 @@ declare function createStore<TState extends object>(initializer: Partial<TState>
|
|
|
12
12
|
export declare const createStoreHook: <TState extends object>() => {
|
|
13
13
|
useStore: {
|
|
14
14
|
(options?: UseStoreOptions<TState, TState>): [TState, StoreApi<TState>["setState"]];
|
|
15
|
-
<P extends PathOf<TState>>(path: P, options: Omit<UseStoreOptions<PathValue<TState, P>, TState>, "defaultValue"> & {
|
|
16
|
-
defaultValue: undefined;
|
|
17
|
-
transformer?: never;
|
|
18
|
-
}): [PathValue<TState, P> | undefined, (value: PathValue<TState, P> | ((prev: PathValue<TState, P>) => PathValue<TState, P>)) => void];
|
|
19
15
|
<P extends PathOf<TState>>(path: P, options?: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
20
|
-
defaultValue?: never;
|
|
21
16
|
transformer?: never;
|
|
22
17
|
}): [PathValue<TState, P>, (value: PathValue<TState, P> | ((prev: PathValue<TState, P>) => PathValue<TState, P>)) => void];
|
|
23
|
-
<P extends PathOf<TState>, D>(path: P, options: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
24
|
-
defaultValue: D;
|
|
25
|
-
transformer?: never;
|
|
26
|
-
}): [NonNullable<PathValue<TState, P>> | D, (value: PathValue<TState, P> | ((prev: PathValue<TState, P>) => PathValue<TState, P>)) => void];
|
|
27
18
|
<P extends PathOf<TState>, TResult>(path: P, options: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
28
19
|
transformer: (value: PathValue<TState, P>) => TResult;
|
|
29
20
|
}): [TResult, (value: PathValue<TState, P> | ((prev: PathValue<TState, P>) => PathValue<TState, P>)) => void];
|
|
30
|
-
<P extends PathOf<TState>>(pathFn: (state: TState) => P, options: Omit<UseStoreOptions<PathValue<TState, P>, TState>, "defaultValue"> & {
|
|
31
|
-
defaultValue: undefined;
|
|
32
|
-
transformer?: never;
|
|
33
|
-
}): [PathValue<TState, P> | undefined, PathSetter<TState, P>];
|
|
34
21
|
<P extends PathOf<TState>>(pathFn: (state: TState) => P, options?: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
35
|
-
defaultValue?: never;
|
|
36
22
|
transformer?: never;
|
|
37
23
|
}): [PathValue<TState, P>, PathSetter<TState, P>];
|
|
38
|
-
<P extends PathOf<TState>, D_1>(pathFn: (state: TState) => P, options: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
39
|
-
defaultValue: D_1;
|
|
40
|
-
transformer?: never;
|
|
41
|
-
}): [NonNullable<PathValue<TState, P>> | D_1, PathSetter<TState, P>];
|
|
42
24
|
<P extends PathOf<TState>, TResult_1>(pathFn: (state: TState) => P, options: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
43
25
|
transformer: (value: PathValue<TState, P>) => TResult_1;
|
|
44
26
|
}): [TResult_1, PathSetter<TState, P>];
|
|
45
|
-
<const Paths extends ReadonlyArray<PathOf<TState>>>(paths: Paths, options?: Omit<UseStoreMultiOptions<TState, Paths>, "
|
|
46
|
-
<const Paths extends ReadonlyArray<PathOf<TState>>>(paths: Paths, options: UseStoreMultiOptions<TState, Paths> & {
|
|
47
|
-
defaultValue: undefined;
|
|
48
|
-
transformer?: never;
|
|
49
|
-
}): MultiPathReturn<TState, Paths, undefined>;
|
|
50
|
-
<const Paths extends ReadonlyArray<PathOf<TState>>, const TDefaultValue extends readonly (PathValue<TState, Paths[number]> | undefined)[]>(paths: Paths, options: UseStoreMultiOptions<TState, Paths, TDefaultValue> & {
|
|
51
|
-
defaultValue: TDefaultValue;
|
|
52
|
-
transformer?: never;
|
|
53
|
-
}): MultiPathReturn<TState, Paths, TDefaultValue>;
|
|
54
|
-
<const Paths extends ReadonlyArray<PathOf<TState>>, TDefaultValue_1 extends PathValue<TState, Paths[number]>>(paths: Paths, options: UseStoreMultiOptions<TState, Paths, TDefaultValue_1> & {
|
|
55
|
-
defaultValue: TDefaultValue_1;
|
|
56
|
-
transformer?: never;
|
|
57
|
-
}): MultiPathReturn<TState, Paths, TDefaultValue_1>;
|
|
27
|
+
<const Paths extends ReadonlyArray<PathOf<TState>>>(paths: Paths, options?: Omit<UseStoreMultiOptions<TState, Paths>, "transformer">): MultiPathReturn<TState, Paths>;
|
|
58
28
|
<const Paths extends ReadonlyArray<PathOf<TState>>, TResult_2>(paths: Paths, options: UseStoreMultiOptions<TState, Paths> & {
|
|
59
29
|
transformer: (values: PathValues<TState, Paths>) => TResult_2;
|
|
60
30
|
}): [TResult_2, ...PathSetters<TState, Paths>];
|
|
61
|
-
<const Entries extends ReadonlyArray<PathOrFn<TState>>>(paths: Entries, options?: Omit<UseStoreMultiOptions<TState, any>, "
|
|
31
|
+
<const Entries extends ReadonlyArray<PathOrFn<TState>>>(paths: Entries, options?: Omit<UseStoreMultiOptions<TState, any>, "transformer"> & {
|
|
62
32
|
transformer?: never;
|
|
63
33
|
}): [PathOrFnValues<TState, Entries>, ...PathOrFnSetters<TState, Entries>];
|
|
64
|
-
<const Entries extends ReadonlyArray<PathOrFn<TState
|
|
65
|
-
defaultValue: undefined;
|
|
66
|
-
transformer?: never;
|
|
67
|
-
}): [{ [I in keyof Entries]: PathOrFnValue<TState, Entries[I]> | undefined; }, ...PathOrFnSetters<TState, Entries>];
|
|
68
|
-
<const Entries extends ReadonlyArray<PathOrFn<TState>>, const TDefaultValue_2 extends readonly unknown[]>(paths: Entries, options: Omit<UseStoreMultiOptions<TState, any>, "defaultValue" | "transformer"> & {
|
|
69
|
-
defaultValue: TDefaultValue_2;
|
|
70
|
-
transformer?: never;
|
|
71
|
-
}): [{ [I in keyof Entries]: I extends keyof TDefaultValue_2 ? TDefaultValue_2[I] extends undefined ? PathOrFnValue<TState, Entries[I]> | undefined : NonNullable<PathOrFnValue<TState, Entries[I]>> | TDefaultValue_2[I] : PathOrFnValue<TState, Entries[I]>; }, ...PathOrFnSetters<TState, Entries>];
|
|
72
|
-
<const Entries extends ReadonlyArray<PathOrFn<TState>>, TResult_3>(paths: Entries, options: Omit<UseStoreMultiOptions<TState, any>, "defaultValue" | "transformer"> & {
|
|
34
|
+
<const Entries extends ReadonlyArray<PathOrFn<TState>>, TResult_3>(paths: Entries, options: Omit<UseStoreMultiOptions<TState, any>, "transformer"> & {
|
|
73
35
|
transformer: (values: PathOrFnValues<TState, Entries>) => TResult_3;
|
|
74
36
|
}): [TResult_3, ...PathOrFnSetters<TState, Entries>];
|
|
75
37
|
};
|
|
@@ -82,9 +44,6 @@ export declare const createStoreHook: <TState extends object>() => {
|
|
|
82
44
|
useStoreGetter: {
|
|
83
45
|
(options?: UseStoreGetterOptions<TState>): GetValueFn<TState>;
|
|
84
46
|
<P extends PathOf<TState>>(basePath: P, options?: UseStoreGetterOptions<TState>): GetValueFromBaseFn<PathValue<TState, P>>;
|
|
85
|
-
<P extends PathOf<TState>, D_2>(basePath: P, options: UseStoreGetterOptions<TState, D_2> & {
|
|
86
|
-
defaultValue: D_2;
|
|
87
|
-
}): GetValueFromBaseWithDefaultFn<PathValue<TState, P>, D_2>;
|
|
88
47
|
<const Entries extends ReadonlyArray<PathOf<TState> | ((state: TState) => unknown)>>(entries: Entries, options?: UseStoreGetterOptions<TState>): GetterTuple<TState, Entries>;
|
|
89
48
|
};
|
|
90
49
|
useStoreSetter: {
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import r from "./helpers/
|
|
5
|
-
import i from "./helpers/
|
|
1
|
+
import e from "./hooks/useStoreSetter.mjs";
|
|
2
|
+
import t from "./helpers/Subscribers.mjs";
|
|
3
|
+
import { createChainReads as n } from "./helpers/createChainReads.mjs";
|
|
4
|
+
import { createSetState as r } from "./helpers/createSetState.mjs";
|
|
5
|
+
import { forwardParentChanges as i } from "./helpers/forwardParentChanges.mjs";
|
|
6
|
+
import a from "./helpers/PathTrie.mjs";
|
|
6
7
|
import "./helpers/scopeCollisions.mjs";
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import s from "./hooks/useStoreSetter.mjs";
|
|
8
|
+
import o from "./hooks/useStore.mjs";
|
|
9
|
+
import s from "./hooks/useStoreGetter.mjs";
|
|
10
10
|
import c from "./hooks/useStoreSync.mjs";
|
|
11
11
|
//#region src/createStore/createStore.ts
|
|
12
12
|
var l = 0;
|
|
13
|
-
function u(
|
|
14
|
-
let s = {}, c, u = new
|
|
15
|
-
p.forEach((e) => e());
|
|
13
|
+
function u(e, o) {
|
|
14
|
+
let s = {}, c, u = new t(), d = new t(), f = new a(), p = new t(), m = () => {
|
|
15
|
+
p.length !== 0 && p.forEach((e) => e());
|
|
16
16
|
}, h = [], g = [], _ = [], v = o?.parent;
|
|
17
17
|
++l;
|
|
18
18
|
let y = (e, t, n) => {
|
|
@@ -23,9 +23,11 @@ function u(a, o) {
|
|
|
23
23
|
path: n
|
|
24
24
|
};
|
|
25
25
|
for (let e = 0, t = g.length; e < t; e++) g[e](r);
|
|
26
|
-
}, b = () => s, x = () => c ??= { ...s }, { getState: S, getPath: C, invalidate: w,
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
}, b = () => s, x = () => c ??= { ...s }, { getState: S, getPath: C, invalidate: w, resetCache: T, getMergeCount: E } = n(b, x, v), D = () => {
|
|
27
|
+
w(), m();
|
|
28
|
+
}, O, k, A = !1;
|
|
29
|
+
v && (T(), k = v.subscribeInvalidate?.(D));
|
|
30
|
+
let { setState: j, batch: M } = r({
|
|
29
31
|
getOwnState: b,
|
|
30
32
|
getOwnSnapshot: x,
|
|
31
33
|
setOwnState: (e) => {
|
|
@@ -42,35 +44,55 @@ function u(a, o) {
|
|
|
42
44
|
reportError: y,
|
|
43
45
|
invalidateDescendants: m,
|
|
44
46
|
onDelegateToParent: void 0
|
|
45
|
-
}),
|
|
46
|
-
w(),
|
|
47
|
-
}, A, j, M = !1, N = () => u.length > 0 || f.size > 0 || d.length > 0, P = () => {
|
|
48
|
-
A || !v || (w(), T(!0), A = n(v, u, f, d, S, y, w), j = v.subscribeInvalidate?.(k), d.length > 0 && A.seedBaseline());
|
|
47
|
+
}), N = () => u.length > 0 || f.size > 0 || d.length > 0, P = () => {
|
|
48
|
+
O || !v || (w(), T(), O = i(v, u, f, d, S, y, w), d.length > 0 && O.seedBaseline());
|
|
49
49
|
}, F = () => {
|
|
50
|
-
|
|
50
|
+
O && (O.unsubscribe(), O = void 0, T());
|
|
51
51
|
}, I = () => {
|
|
52
|
-
v && (!
|
|
52
|
+
v && (!A && N() ? P() : F());
|
|
53
53
|
}, L = (e) => (I(), () => {
|
|
54
54
|
e(), I();
|
|
55
55
|
}), R = (e) => L(u.add(e)), z = (e, t) => L(f.add(e, t)), B = (e) => {
|
|
56
56
|
let t = L(d.add(e));
|
|
57
|
-
return
|
|
57
|
+
return O?.seedBaseline(), t;
|
|
58
58
|
};
|
|
59
|
-
s = typeof
|
|
59
|
+
s = typeof e == "function" ? e(j, S) : e;
|
|
60
60
|
let V = {
|
|
61
61
|
id: o?.id,
|
|
62
62
|
getState: S,
|
|
63
63
|
getPath: C,
|
|
64
|
-
setState:
|
|
65
|
-
|
|
64
|
+
setState: j,
|
|
65
|
+
get: ((e) => e === void 0 ? S() : C(e)),
|
|
66
|
+
set: j,
|
|
67
|
+
watch: ((e, t) => typeof e == "function" ? R(e) : z(e, t)),
|
|
68
|
+
withBase: (e) => {
|
|
69
|
+
let t = (t, n) => j(t === void 0 ? e : `${e}.${t}`, n);
|
|
70
|
+
return {
|
|
71
|
+
getState: (t) => {
|
|
72
|
+
let n = C(e);
|
|
73
|
+
return n === void 0 && t !== void 0 ? t : n;
|
|
74
|
+
},
|
|
75
|
+
getPath: (t, n) => {
|
|
76
|
+
let r = C(`${e}.${t}`);
|
|
77
|
+
return r === void 0 && n !== void 0 ? n : r;
|
|
78
|
+
},
|
|
79
|
+
setState: t,
|
|
80
|
+
subscribe: (t) => z(e, t),
|
|
81
|
+
subscribePath: (t, n) => z(`${e}.${t}`, n),
|
|
82
|
+
get: (t) => C(t === void 0 ? e : `${e}.${t}`),
|
|
83
|
+
set: t,
|
|
84
|
+
watch: (t, n) => typeof t == "function" ? z(e, t) : z(`${e}.${t}`, n)
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
batch: M,
|
|
66
88
|
subscribe: R,
|
|
67
89
|
subscribePath: z,
|
|
68
90
|
subscribeChange: B,
|
|
69
91
|
destroy: () => {
|
|
70
|
-
|
|
92
|
+
A = !0, F(), u.clear(), f.clear(), d.clear(), p.clear(), k?.();
|
|
71
93
|
},
|
|
72
94
|
reconnect: () => {
|
|
73
|
-
|
|
95
|
+
A = !1, v && (k?.(), k = v.subscribeInvalidate?.(D)), I();
|
|
74
96
|
},
|
|
75
97
|
subscribeInvalidate: (e) => p.add(e),
|
|
76
98
|
hydrate: () => {
|
|
@@ -86,23 +108,23 @@ function u(a, o) {
|
|
|
86
108
|
return V;
|
|
87
109
|
}
|
|
88
110
|
var d = () => {
|
|
89
|
-
function
|
|
90
|
-
return
|
|
111
|
+
function t(e, t) {
|
|
112
|
+
return o(e, t);
|
|
91
113
|
}
|
|
92
|
-
function
|
|
114
|
+
function n(e, t, n) {
|
|
93
115
|
c(e, t, n);
|
|
94
116
|
}
|
|
95
|
-
function n(e, t) {
|
|
96
|
-
return o(e, t);
|
|
97
|
-
}
|
|
98
117
|
function r(e, t) {
|
|
99
118
|
return s(e, t);
|
|
100
119
|
}
|
|
120
|
+
function i(t, n) {
|
|
121
|
+
return e(t, n);
|
|
122
|
+
}
|
|
101
123
|
return {
|
|
102
|
-
useStore:
|
|
103
|
-
useStoreSync:
|
|
104
|
-
useStoreGetter:
|
|
105
|
-
useStoreSetter:
|
|
124
|
+
useStore: t,
|
|
125
|
+
useStoreSync: n,
|
|
126
|
+
useStoreGetter: r,
|
|
127
|
+
useStoreSetter: i
|
|
106
128
|
};
|
|
107
129
|
};
|
|
108
130
|
//#endregion
|
|
@@ -3,9 +3,11 @@ import { Listener, Path } from '../../types';
|
|
|
3
3
|
declare class PathTrie {
|
|
4
4
|
readonly direct: Map<string, Subscribers<Listener>>;
|
|
5
5
|
private readonly descendants;
|
|
6
|
+
private readonly root;
|
|
6
7
|
size: number;
|
|
7
8
|
add(path: string, listener: Listener): () => void;
|
|
8
9
|
private remove;
|
|
10
|
+
walkAncestors(segments: readonly string[], cb: (subs: Subscribers<Listener>) => void): void;
|
|
9
11
|
getDescendants(path: string): Set<string> | undefined;
|
|
10
12
|
forEachAffected(changedPath: Path | undefined, cb: (listener: Listener) => void, onError?: (error: unknown) => void): void;
|
|
11
13
|
clear(): void;
|
|
@@ -1,42 +1,77 @@
|
|
|
1
1
|
import e from "../../helpers/parsePath.mjs";
|
|
2
2
|
import t from "./Subscribers.mjs";
|
|
3
3
|
//#region src/createStore/helpers/PathTrie.ts
|
|
4
|
-
var n =
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
var n = class {
|
|
5
|
+
subscribers = null;
|
|
6
|
+
children = null;
|
|
7
7
|
}, r = class {
|
|
8
8
|
direct = /* @__PURE__ */ new Map();
|
|
9
9
|
descendants = /* @__PURE__ */ new Map();
|
|
10
|
+
root = new n();
|
|
10
11
|
size = 0;
|
|
11
12
|
add(r, i) {
|
|
12
|
-
let a =
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
let a = e(r), o = this.direct.get(r);
|
|
14
|
+
if (!o) {
|
|
15
|
+
o = new t(), this.direct.set(r, o);
|
|
16
|
+
let e = this.root;
|
|
17
|
+
for (let t of a) {
|
|
18
|
+
e.children ||= /* @__PURE__ */ new Map();
|
|
19
|
+
let r = e.children.get(t);
|
|
20
|
+
r || (r = new n(), e.children.set(t, r)), e = r;
|
|
21
|
+
}
|
|
22
|
+
e.subscribers = o, this.size++;
|
|
23
|
+
let i = "";
|
|
24
|
+
for (let e = 0; e < a.length - 1; e++) {
|
|
25
|
+
i = i ? `${i}.${a[e]}` : a[e];
|
|
26
|
+
let t = this.descendants.get(i);
|
|
27
|
+
t || (t = /* @__PURE__ */ new Set(), this.descendants.set(i, t)), t.add(r);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return o.add(i), () => this.remove(r, o, i, a);
|
|
17
31
|
}
|
|
18
|
-
remove(e, t,
|
|
19
|
-
this.direct.get(e)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
32
|
+
remove(e, t, n, r) {
|
|
33
|
+
if (this.direct.get(e) !== t || (t.remove(n), t.length > 0)) return;
|
|
34
|
+
this.direct.delete(e), this.size--;
|
|
35
|
+
let i = this.root;
|
|
36
|
+
for (let e of r) {
|
|
37
|
+
if (!i.children) return;
|
|
38
|
+
let t = i.children.get(e);
|
|
39
|
+
if (!t) return;
|
|
40
|
+
i = t;
|
|
41
|
+
}
|
|
42
|
+
i.subscribers === t && (i.subscribers = null);
|
|
43
|
+
let a = "";
|
|
44
|
+
for (let t = 0; t < r.length - 1; t++) {
|
|
45
|
+
a = a ? `${a}.${r[t]}` : r[t];
|
|
46
|
+
let n = this.descendants.get(a);
|
|
47
|
+
n && (n.delete(e), n.size === 0 && this.descendants.delete(a));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
walkAncestors(e, t) {
|
|
51
|
+
let n = this.root;
|
|
52
|
+
for (let r of e) {
|
|
53
|
+
if (!n.children) return;
|
|
54
|
+
let e = n.children.get(r);
|
|
55
|
+
if (!e) return;
|
|
56
|
+
n = e, n.subscribers && t(n.subscribers);
|
|
57
|
+
}
|
|
23
58
|
}
|
|
24
59
|
getDescendants(e) {
|
|
25
60
|
return this.descendants.get(e);
|
|
26
61
|
}
|
|
27
|
-
forEachAffected(t,
|
|
28
|
-
let
|
|
29
|
-
e && e.forEach(
|
|
62
|
+
forEachAffected(t, n, r) {
|
|
63
|
+
let i = (e) => {
|
|
64
|
+
e && e.forEach(n, r);
|
|
30
65
|
};
|
|
31
66
|
if (typeof t != "string") {
|
|
32
|
-
this.direct.forEach(
|
|
67
|
+
this.direct.forEach(i);
|
|
33
68
|
return;
|
|
34
69
|
}
|
|
35
|
-
let
|
|
36
|
-
|
|
70
|
+
let a = e(t);
|
|
71
|
+
this.walkAncestors(a, i), this.descendants.get(t)?.forEach((e) => i(this.direct.get(e)));
|
|
37
72
|
}
|
|
38
73
|
clear() {
|
|
39
|
-
this.direct.clear(), this.descendants.clear(), this.size = 0;
|
|
74
|
+
this.direct.clear(), this.descendants.clear(), this.root.children = null, this.root.subscribers = null, this.size = 0;
|
|
40
75
|
}
|
|
41
76
|
};
|
|
42
77
|
//#endregion
|
|
@@ -4,7 +4,7 @@ export type ChainReads<TState extends object> = {
|
|
|
4
4
|
getState: GetState<TState>;
|
|
5
5
|
getPath: GetPath<TState>;
|
|
6
6
|
invalidate: () => void;
|
|
7
|
-
|
|
7
|
+
resetCache: () => void;
|
|
8
8
|
getMergeCount: () => number;
|
|
9
9
|
};
|
|
10
10
|
export declare function createChainReads<TState extends object>(getOwnState: () => TState, getOwnSnapshot: () => TState, parent: StoreApi<TState> | undefined): ChainReads<TState>;
|
|
@@ -1,36 +1,35 @@
|
|
|
1
|
-
import
|
|
2
|
-
import n from "
|
|
1
|
+
import e from "../../helpers/getByPath.mjs";
|
|
2
|
+
import { deepMerge as t, isPlainObject as n } from "./deepMerge.mjs";
|
|
3
3
|
//#region src/createStore/helpers/createChainReads.ts
|
|
4
4
|
function r(r, i, a) {
|
|
5
5
|
if (!a) return {
|
|
6
6
|
getState: i,
|
|
7
|
-
getPath: (
|
|
7
|
+
getPath: (t) => e(r(), t),
|
|
8
8
|
invalidate: () => {},
|
|
9
|
-
|
|
9
|
+
resetCache: () => {},
|
|
10
10
|
getMergeCount: () => 0
|
|
11
11
|
};
|
|
12
|
-
let o = a, s = !0, c = !
|
|
13
|
-
let i =
|
|
14
|
-
if (i === void 0) return o.getPath(
|
|
15
|
-
let a = o.getPath(
|
|
16
|
-
return a === void 0 || !
|
|
12
|
+
let o = a, s = !0, c = !1, l, u = 0, d = /* @__PURE__ */ new Map(), f = () => (u++, t(o.getState(), i())), p = () => ((s || l === void 0) && (l = f(), s = !1), l), m = (t) => {
|
|
13
|
+
let i = e(r(), t);
|
|
14
|
+
if (i === void 0) return o.getPath(t);
|
|
15
|
+
let a = o.getPath(t);
|
|
16
|
+
return a === void 0 || !n(i) || !n(a) ? i : e(p(), t);
|
|
17
17
|
};
|
|
18
18
|
return {
|
|
19
|
-
getState:
|
|
19
|
+
getState: p,
|
|
20
20
|
getPath: (e) => {
|
|
21
|
-
if (
|
|
22
|
-
if (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return f.set(e, t), t;
|
|
21
|
+
if (c) d.clear(), c = !1;
|
|
22
|
+
else if (d.has(e)) return d.get(e);
|
|
23
|
+
let t = m(e);
|
|
24
|
+
return d.set(e, t), t;
|
|
26
25
|
},
|
|
27
26
|
invalidate: () => {
|
|
28
|
-
|
|
27
|
+
s = !0, c = !0;
|
|
29
28
|
},
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
resetCache: () => {
|
|
30
|
+
d.clear(), l = void 0, s = !0, c = !1;
|
|
32
31
|
},
|
|
33
|
-
getMergeCount: () =>
|
|
32
|
+
getMergeCount: () => u
|
|
34
33
|
};
|
|
35
34
|
}
|
|
36
35
|
//#endregion
|
|
@@ -44,12 +44,14 @@ function u(u) {
|
|
|
44
44
|
for (let e = 0, t = n.length; e < t; e++) T.add(n[e]);
|
|
45
45
|
return;
|
|
46
46
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
47
|
+
if (n.length !== 0) {
|
|
48
|
+
e.begin();
|
|
49
|
+
try {
|
|
50
|
+
let e = s(n, t, n.length);
|
|
51
|
+
e !== o && b(e, "notify", t);
|
|
52
|
+
} finally {
|
|
53
|
+
e.end();
|
|
54
|
+
}
|
|
53
55
|
}
|
|
54
56
|
}, D = (e) => {
|
|
55
57
|
w++;
|
|
@@ -77,19 +79,14 @@ function u(u) {
|
|
|
77
79
|
v.end();
|
|
78
80
|
}
|
|
79
81
|
}, k = (e, t) => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (a) for (let o of a) {
|
|
89
|
-
let a = _.direct.get(o);
|
|
90
|
-
if (!a) continue;
|
|
91
|
-
let s = o.slice(i);
|
|
92
|
-
t(n, s) !== t(r, s) && E(a, e);
|
|
82
|
+
_.walkAncestors(t, (t) => E(t, e));
|
|
83
|
+
}, A = (n, r, i, a) => {
|
|
84
|
+
let o = _.getDescendants(n);
|
|
85
|
+
if (o) for (let s of o) {
|
|
86
|
+
let o = _.direct.get(s);
|
|
87
|
+
if (!o) continue;
|
|
88
|
+
let c = e(s).slice(a);
|
|
89
|
+
t(r, c) !== t(i, c) && E(o, n);
|
|
93
90
|
}
|
|
94
91
|
}, j = (e, n, r, o) => {
|
|
95
92
|
let s = e ? t(r, e) : void 0, c = e ? typeof n == "function" ? n(s) : n : void 0;
|
|
@@ -108,15 +105,9 @@ function u(u) {
|
|
|
108
105
|
if (e === a) return;
|
|
109
106
|
l = e;
|
|
110
107
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
E(g, e), _.size > 0 && _.direct.forEach((n, i) => {
|
|
117
|
-
t(r, i) !== t(l, i) && E(n, e);
|
|
118
|
-
});
|
|
119
|
-
}
|
|
108
|
+
l !== r && (p(l), v.length > 0 && O(e, r, l), x(), o && (E(g, e), _.size > 0 && _.direct.forEach((n, i) => {
|
|
109
|
+
t(r, i) !== t(l, i) && E(n, e);
|
|
110
|
+
})));
|
|
120
111
|
};
|
|
121
112
|
return {
|
|
122
113
|
setState: (i, o, s = !0) => {
|
|
@@ -139,11 +130,11 @@ function u(u) {
|
|
|
139
130
|
}
|
|
140
131
|
if (e === n) return;
|
|
141
132
|
let r = v.length > 0 ? f() : void 0;
|
|
142
|
-
if (m(i, n), r !== void 0 && O(i, r, f()), s) {
|
|
143
|
-
E(g, i);
|
|
133
|
+
if (m(i, n), r !== void 0 && O(i, r, f()), s && (E(g, i), _.size > 0)) {
|
|
144
134
|
let t = _.direct.get(i);
|
|
145
|
-
t && E(t, i), A(i, e, n,
|
|
146
|
-
}
|
|
135
|
+
t && E(t, i), A(i, e, n, 1);
|
|
136
|
+
}
|
|
137
|
+
x();
|
|
147
138
|
return;
|
|
148
139
|
}
|
|
149
140
|
let b = e(i), w;
|
|
@@ -154,7 +145,7 @@ function u(u) {
|
|
|
154
145
|
} else w = r(u, i, b, o, typeof o == "function");
|
|
155
146
|
if (w === n) return;
|
|
156
147
|
let T = w;
|
|
157
|
-
p(T), v.length > 0 && O(i, u, T), s
|
|
148
|
+
p(T), v.length > 0 && O(i, u, T), s && (E(g, i), _.size > 0 && (k(i, b), A(i, u, T, 0))), x();
|
|
158
149
|
},
|
|
159
150
|
batch: D
|
|
160
151
|
};
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { PathOf, PathOrFn, StoreApi, SyncMode } from '../../types';
|
|
2
2
|
import { RefObject } from 'react';
|
|
3
3
|
export declare const defaultMultiEqualityFn: (a: unknown[], b: unknown[]) => boolean;
|
|
4
|
-
export declare function makeSingleSnapshot<TState extends object>(store: StoreApi<TState>, pathOrFn: PathOf<TState> | ((state: TState) => PathOf<TState>) | undefined
|
|
4
|
+
export declare function makeSingleSnapshot<TState extends object>(store: StoreApi<TState>, pathOrFn: PathOf<TState> | ((state: TState) => PathOf<TState>) | undefined): () => unknown;
|
|
5
5
|
export declare function makeMultiSnapshot<TState extends object>(store: StoreApi<TState>, pathsRef: RefObject<ReadonlyArray<PathOrFn<TState>>>, lastRef: RefObject<unknown[] | null>, options?: {
|
|
6
6
|
equalityFn?: (a: unknown[], b: unknown[]) => boolean;
|
|
7
|
-
defaultValue?: unknown;
|
|
8
7
|
}): () => unknown[];
|
|
9
8
|
export declare function useResolvedStore<TState extends object>(optionStore: StoreApi<TState> | undefined, hookName: string, storeId?: string): StoreApi<TState>;
|
|
10
9
|
export declare function useMultiSubscribe<TState extends object>(store: StoreApi<TState>, pathsRef: RefObject<ReadonlyArray<PathOrFn<TState>>>, pathsKey: string, enabled: boolean, mode: SyncMode): (cb: () => void) => () => void;
|