@plitzi/nexus 0.31.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 +31 -0
- package/LICENSE +74 -0
- package/README.md +563 -0
- package/dist/StoreContext.d.ts +10 -0
- package/dist/StoreContext.mjs +11 -0
- package/dist/StoreProvider.d.ts +24 -0
- package/dist/StoreProvider.mjs +51 -0
- package/dist/async/createAsync.d.ts +19 -0
- package/dist/async/createAsync.mjs +29 -0
- package/dist/async/hooks/useAsync.d.ts +2 -0
- package/dist/async/hooks/useAsync.mjs +7 -0
- package/dist/async/hooks/useAsyncValue.d.ts +2 -0
- package/dist/async/hooks/useAsyncValue.mjs +13 -0
- package/dist/async/index.d.ts +4 -0
- package/dist/async/index.mjs +4 -0
- package/dist/createStore/createStore.d.ts +90 -0
- package/dist/createStore/createStore.mjs +104 -0
- package/dist/createStore/helpers/PathTrie.d.ts +13 -0
- package/dist/createStore/helpers/PathTrie.mjs +43 -0
- package/dist/createStore/helpers/Subscribers.d.ts +15 -0
- package/dist/createStore/helpers/Subscribers.mjs +59 -0
- package/dist/createStore/helpers/createChainReads.d.ts +10 -0
- package/dist/createStore/helpers/createChainReads.mjs +37 -0
- package/dist/createStore/helpers/createSetState.d.ts +22 -0
- package/dist/createStore/helpers/createSetState.mjs +163 -0
- package/dist/createStore/helpers/deepMerge.d.ts +2 -0
- package/dist/createStore/helpers/deepMerge.mjs +9 -0
- package/dist/createStore/helpers/forwardParentChanges.d.ts +7 -0
- package/dist/createStore/helpers/forwardParentChanges.mjs +23 -0
- package/dist/createStore/helpers/scopeCollisions.d.ts +4 -0
- package/dist/createStore/helpers/scopeCollisions.mjs +14 -0
- package/dist/createStore/helpers/writeByPath.d.ts +11 -0
- package/dist/createStore/helpers/writeByPath.mjs +51 -0
- package/dist/createStore/hooks/shared.d.ts +12 -0
- package/dist/createStore/hooks/shared.mjs +85 -0
- package/dist/createStore/hooks/useStore.d.ts +73 -0
- package/dist/createStore/hooks/useStore.mjs +57 -0
- package/dist/createStore/hooks/useStoreById.d.ts +3 -0
- package/dist/createStore/hooks/useStoreById.mjs +7 -0
- package/dist/createStore/hooks/useStoreGetter.d.ts +9 -0
- package/dist/createStore/hooks/useStoreGetter.mjs +38 -0
- package/dist/createStore/hooks/useStoreSetter.d.ts +5 -0
- package/dist/createStore/hooks/useStoreSetter.mjs +11 -0
- package/dist/createStore/hooks/useStoreSync.d.ts +7 -0
- package/dist/createStore/hooks/useStoreSync.mjs +55 -0
- package/dist/createStore/index.d.ts +3 -0
- package/dist/createStore/index.mjs +5 -0
- package/dist/derived/createDerived.d.ts +10 -0
- package/dist/derived/createDerived.mjs +21 -0
- package/dist/derived/hooks/useDerived.d.ts +2 -0
- package/dist/derived/hooks/useDerived.mjs +7 -0
- package/dist/derived/index.d.ts +3 -0
- package/dist/derived/index.mjs +3 -0
- package/dist/entities/createEntityAdapter.d.ts +32 -0
- package/dist/entities/createEntityAdapter.mjs +73 -0
- package/dist/entities/index.d.ts +2 -0
- package/dist/entities/index.mjs +2 -0
- package/dist/helpers/createObservable.d.ts +9 -0
- package/dist/helpers/createObservable.mjs +18 -0
- package/dist/helpers/getByPath.d.ts +4 -0
- package/dist/helpers/getByPath.mjs +40 -0
- package/dist/helpers/isPathAffected.d.ts +3 -0
- package/dist/helpers/isPathAffected.mjs +4 -0
- package/dist/helpers/parsePath.d.ts +2 -0
- package/dist/helpers/parsePath.mjs +7 -0
- package/dist/helpers/setByPath.d.ts +3 -0
- package/dist/helpers/setByPath.mjs +19 -0
- package/dist/helpers/shallowEqual.d.ts +2 -0
- package/dist/helpers/shallowEqual.mjs +11 -0
- package/dist/helpers/useIsomorphicLayoutEffect.d.ts +3 -0
- package/dist/helpers/useIsomorphicLayoutEffect.mjs +5 -0
- package/dist/history/hooks/useStoreHistory.d.ts +8 -0
- package/dist/history/hooks/useStoreHistory.mjs +24 -0
- package/dist/history/index.d.ts +4 -0
- package/dist/history/index.mjs +3 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.mjs +21 -0
- package/dist/middleware/cascade.d.ts +2 -0
- package/dist/middleware/cascade.mjs +4 -0
- package/dist/middleware/historyMiddleware.d.ts +28 -0
- package/dist/middleware/historyMiddleware.mjs +81 -0
- package/dist/middleware/index.d.ts +8 -0
- package/dist/middleware/index.mjs +6 -0
- package/dist/middleware/loggerMiddleware.d.ts +7 -0
- package/dist/middleware/loggerMiddleware.mjs +20 -0
- package/dist/middleware/persistMiddleware.d.ts +16 -0
- package/dist/middleware/persistMiddleware.mjs +36 -0
- package/dist/middleware/reduxDevToolsMiddleware.d.ts +6 -0
- package/dist/middleware/reduxDevToolsMiddleware.mjs +26 -0
- package/dist/types/StoreTypes.d.ts +143 -0
- package/dist/types/StoreTypes.mjs +4 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.mjs +2 -0
- package/package.json +257 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createContext as e } from "react";
|
|
2
|
+
//#region src/StoreContext.ts
|
|
3
|
+
var t = e(void 0);
|
|
4
|
+
t.displayName = "StoreContext";
|
|
5
|
+
var n = e(void 0);
|
|
6
|
+
n.displayName = "StoreRegistryContext";
|
|
7
|
+
var r = (e, t) => {
|
|
8
|
+
for (let n = e; n; n = n.parent) if (n.id === t) return n.store;
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
export { t as StoreContext, n as StoreRegistryContext, r as findStoreInRegistry };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { StoreContext, StoreRegistryContext } from './StoreContext';
|
|
2
|
+
import { StoreApi, StoreMiddleware } from './types';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
export type StoreProviderProps<TState extends object = any> = {
|
|
5
|
+
store?: StoreApi<TState>;
|
|
6
|
+
id?: string;
|
|
7
|
+
path?: string;
|
|
8
|
+
value?: Partial<TState> | ((state: TState) => TState);
|
|
9
|
+
/**
|
|
10
|
+
* How this scope derives from the parent store:
|
|
11
|
+
* - `'snapshot'`: copy parent keys once at init; isolated thereafter (writes stay local, parent updates do
|
|
12
|
+
* not propagate). Use for draft/diverge editors.
|
|
13
|
+
* - `'live'`: live scope chain — reads fall through to the parent, own keys shadow inherited ones, and writes
|
|
14
|
+
* delegate to the owning scope. Parent updates propagate.
|
|
15
|
+
* - `undefined` (default): no inheritance.
|
|
16
|
+
*/
|
|
17
|
+
inherit?: 'snapshot' | 'live';
|
|
18
|
+
autoSync?: boolean;
|
|
19
|
+
middlewares?: StoreMiddleware<TState>[];
|
|
20
|
+
children?: ReactNode;
|
|
21
|
+
};
|
|
22
|
+
declare const StoreProvider: <TState extends object = any>({ store, id, path, value, inherit, autoSync, middlewares, children }: StoreProviderProps<TState>) => import("react/jsx-runtime").JSX.Element;
|
|
23
|
+
export { StoreContext, StoreRegistryContext };
|
|
24
|
+
export default StoreProvider;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { StoreContext as e, StoreRegistryContext as t } from "./StoreContext.mjs";
|
|
2
|
+
import n from "./createStore/hooks/useStoreSync.mjs";
|
|
3
|
+
import r from "./createStore/index.mjs";
|
|
4
|
+
import { createContext as i, useContext as a, useEffect as o, useMemo as s, useRef as c } from "react";
|
|
5
|
+
import { jsx as l } from "react/jsx-runtime";
|
|
6
|
+
//#region src/StoreProvider.tsx
|
|
7
|
+
var u = i(void 0), d = (e) => e.cascade === !0, f = ({ store: i, id: f, path: p, value: m, inherit: h, autoSync: g = !0, middlewares: _, children: v }) => {
|
|
8
|
+
let y = a(e), b = a(u), x = a(t), S = c(void 0), C = h === "live", w = s(() => {
|
|
9
|
+
let e = h === "snapshot" && y ? y.getState() : {};
|
|
10
|
+
return typeof m == "function" ? m(e) : {
|
|
11
|
+
...e,
|
|
12
|
+
...m
|
|
13
|
+
};
|
|
14
|
+
}, [
|
|
15
|
+
h,
|
|
16
|
+
y,
|
|
17
|
+
m
|
|
18
|
+
]), T = _ ?? [], E = b ? [...b, ...T] : T, D = s(() => [...b ?? [], ...T.filter(d)], [b, _]);
|
|
19
|
+
S.current ||= i ?? r(() => w, {
|
|
20
|
+
id: f,
|
|
21
|
+
parent: C ? y : void 0,
|
|
22
|
+
middlewares: E.length > 0 ? E : void 0
|
|
23
|
+
});
|
|
24
|
+
let O = s(() => f ? {
|
|
25
|
+
id: f,
|
|
26
|
+
store: S.current,
|
|
27
|
+
parent: x
|
|
28
|
+
} : x, [f, x]);
|
|
29
|
+
c(!1), o(() => {}, [f, x]), o(() => (C && !i && S.current?.reconnect?.(), () => {
|
|
30
|
+
C && !i && S.current?.destroy?.();
|
|
31
|
+
}), [C, i]);
|
|
32
|
+
let k = !!m && g;
|
|
33
|
+
return n(p, p ? m : w, {
|
|
34
|
+
enabled: k && !!p,
|
|
35
|
+
store: S.current
|
|
36
|
+
}), n(void 0, w, {
|
|
37
|
+
enabled: k && !p,
|
|
38
|
+
store: S.current
|
|
39
|
+
}), /* @__PURE__ */ l(u, {
|
|
40
|
+
value: D.length > 0 ? D : void 0,
|
|
41
|
+
children: /* @__PURE__ */ l(t, {
|
|
42
|
+
value: O,
|
|
43
|
+
children: /* @__PURE__ */ l(e, {
|
|
44
|
+
value: S.current,
|
|
45
|
+
children: v
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
//#endregion
|
|
51
|
+
export { f as default };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { PathOf, PathValue, StoreApi } from '../types';
|
|
2
|
+
export type AsyncStatus = 'idle' | 'pending' | 'success' | 'error';
|
|
3
|
+
export type AsyncSnapshot<T> = {
|
|
4
|
+
status: AsyncStatus;
|
|
5
|
+
data: T | undefined;
|
|
6
|
+
error: unknown;
|
|
7
|
+
isLoading: boolean;
|
|
8
|
+
};
|
|
9
|
+
export type AsyncResource<T, Args extends readonly unknown[]> = {
|
|
10
|
+
get: () => AsyncSnapshot<T>;
|
|
11
|
+
run: (...args: Args) => Promise<T>;
|
|
12
|
+
subscribe: (listener: () => void) => () => void;
|
|
13
|
+
suspend: () => Promise<void> | undefined;
|
|
14
|
+
destroy: () => void;
|
|
15
|
+
};
|
|
16
|
+
export type AsyncOptions<Args extends readonly unknown[]> = {
|
|
17
|
+
immediate?: Args;
|
|
18
|
+
};
|
|
19
|
+
export declare function createAsync<TState extends object, P extends PathOf<TState>, Args extends readonly unknown[]>(store: StoreApi<TState>, path: P, fetcher: (...args: Args) => Promise<PathValue<TState, P>>, options?: AsyncOptions<Args>): AsyncResource<PathValue<TState, P>, Args>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createObservable as e } from "../helpers/createObservable.mjs";
|
|
2
|
+
//#region src/async/createAsync.ts
|
|
3
|
+
function t(t, n, r, i = {}) {
|
|
4
|
+
let a = "idle", o, s, c, l = e(() => ({
|
|
5
|
+
status: a,
|
|
6
|
+
data: t.getPath(n),
|
|
7
|
+
error: o,
|
|
8
|
+
isLoading: a === "pending"
|
|
9
|
+
})), u = () => {
|
|
10
|
+
l.invalidate(), l.notify();
|
|
11
|
+
}, d = l.get, f = (...e) => {
|
|
12
|
+
a = "pending", o = void 0;
|
|
13
|
+
let i = Promise.resolve().then(() => r(...e));
|
|
14
|
+
return s = i, c = i.then(() => void 0, () => void 0), u(), i.then((e) => (i === s && (s = void 0, c = void 0, a = "success", t.setState(n, e), u()), e), (e) => {
|
|
15
|
+
throw i === s && (s = void 0, c = void 0, a = "error", o = e, u()), e;
|
|
16
|
+
});
|
|
17
|
+
}, p = t.subscribePath(n, u);
|
|
18
|
+
return i.immediate && f(...i.immediate).catch(() => void 0), {
|
|
19
|
+
get: d,
|
|
20
|
+
run: f,
|
|
21
|
+
subscribe: l.subscribe,
|
|
22
|
+
suspend: () => c,
|
|
23
|
+
destroy: () => {
|
|
24
|
+
p(), l.destroy();
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { t as createAsync };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useAsync as e } from "./useAsync.mjs";
|
|
2
|
+
//#region src/async/hooks/useAsyncValue.ts
|
|
3
|
+
function t(t) {
|
|
4
|
+
let n = e(t);
|
|
5
|
+
if (n.status === "error") throw n.error;
|
|
6
|
+
if (n.status === "pending") {
|
|
7
|
+
let e = t.suspend();
|
|
8
|
+
if (e) throw e;
|
|
9
|
+
}
|
|
10
|
+
return n.data;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { t as useAsyncValue };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { GetState, GetterTuple, GetValueFn, GetValueFromBaseFn, GetValueFromBaseWithDefaultFn, MultiPathReturn, PathOf, PathOrFn, PathOrFnSetters, PathOrFnValue, PathOrFnValues, PathSetters, PathSetter, PathValue, PathValues, SetFromBaseFn, SetState, SetStateFn, StoreApi, StoreMiddleware, UseStoreGetterOptions, UseStoreMultiOptions, UseStoreOptions, UseStoreSetterOptions, UseStoreSyncMultiOptions, UseStoreSyncOptions } from '../types';
|
|
2
|
+
declare function createStore<TState extends object>(initializer: Partial<TState> | ((set: SetState<TState>, get: GetState<TState>) => Partial<TState>), storeOptions?: {
|
|
3
|
+
id?: string;
|
|
4
|
+
parent?: StoreApi<TState>;
|
|
5
|
+
middlewares?: StoreMiddleware<TState>[];
|
|
6
|
+
}): StoreApi<TState>;
|
|
7
|
+
export declare const createStoreHook: <TState extends object>() => {
|
|
8
|
+
useStore: {
|
|
9
|
+
(options?: UseStoreOptions<TState, TState>): [TState, StoreApi<TState>["setState"]];
|
|
10
|
+
<P extends PathOf<TState>>(path: P, options: Omit<UseStoreOptions<PathValue<TState, P>, TState>, "defaultValue"> & {
|
|
11
|
+
defaultValue: undefined;
|
|
12
|
+
transformer?: never;
|
|
13
|
+
}): [PathValue<TState, P> | undefined, (value: PathValue<TState, P> | ((prev: PathValue<TState, P>) => PathValue<TState, P>)) => void];
|
|
14
|
+
<P extends PathOf<TState>>(path: P, options?: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
15
|
+
defaultValue?: never;
|
|
16
|
+
transformer?: never;
|
|
17
|
+
}): [PathValue<TState, P>, (value: PathValue<TState, P> | ((prev: PathValue<TState, P>) => PathValue<TState, P>)) => void];
|
|
18
|
+
<P extends PathOf<TState>, D>(path: P, options: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
19
|
+
defaultValue: D;
|
|
20
|
+
transformer?: never;
|
|
21
|
+
}): [NonNullable<PathValue<TState, P>> | D, (value: PathValue<TState, P> | ((prev: PathValue<TState, P>) => PathValue<TState, P>)) => void];
|
|
22
|
+
<P extends PathOf<TState>, TResult>(path: P, options: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
23
|
+
transformer: (value: PathValue<TState, P>) => TResult;
|
|
24
|
+
}): [TResult, (value: PathValue<TState, P> | ((prev: PathValue<TState, P>) => PathValue<TState, P>)) => void];
|
|
25
|
+
<P extends PathOf<TState>>(pathFn: (state: TState) => P, options: Omit<UseStoreOptions<PathValue<TState, P>, TState>, "defaultValue"> & {
|
|
26
|
+
defaultValue: undefined;
|
|
27
|
+
transformer?: never;
|
|
28
|
+
}): [PathValue<TState, P> | undefined, PathSetter<TState, P>];
|
|
29
|
+
<P extends PathOf<TState>>(pathFn: (state: TState) => P, options?: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
30
|
+
defaultValue?: never;
|
|
31
|
+
transformer?: never;
|
|
32
|
+
}): [PathValue<TState, P>, PathSetter<TState, P>];
|
|
33
|
+
<P extends PathOf<TState>, D_1>(pathFn: (state: TState) => P, options: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
34
|
+
defaultValue: D_1;
|
|
35
|
+
transformer?: never;
|
|
36
|
+
}): [NonNullable<PathValue<TState, P>> | D_1, PathSetter<TState, P>];
|
|
37
|
+
<P extends PathOf<TState>, TResult_1>(pathFn: (state: TState) => P, options: UseStoreOptions<PathValue<TState, P>, TState> & {
|
|
38
|
+
transformer: (value: PathValue<TState, P>) => TResult_1;
|
|
39
|
+
}): [TResult_1, PathSetter<TState, P>];
|
|
40
|
+
<const Paths extends ReadonlyArray<PathOf<TState>>>(paths: Paths, options?: Omit<UseStoreMultiOptions<TState, Paths>, "defaultValue" | "transformer">): MultiPathReturn<TState, Paths>;
|
|
41
|
+
<const Paths extends ReadonlyArray<PathOf<TState>>>(paths: Paths, options: UseStoreMultiOptions<TState, Paths> & {
|
|
42
|
+
defaultValue: undefined;
|
|
43
|
+
transformer?: never;
|
|
44
|
+
}): MultiPathReturn<TState, Paths, undefined>;
|
|
45
|
+
<const Paths extends ReadonlyArray<PathOf<TState>>, const TDefaultValue extends readonly (PathValue<TState, Paths[number]> | undefined)[]>(paths: Paths, options: UseStoreMultiOptions<TState, Paths, TDefaultValue> & {
|
|
46
|
+
defaultValue: TDefaultValue;
|
|
47
|
+
transformer?: never;
|
|
48
|
+
}): MultiPathReturn<TState, Paths, TDefaultValue>;
|
|
49
|
+
<const Paths extends ReadonlyArray<PathOf<TState>>, TDefaultValue_1 extends PathValue<TState, Paths[number]>>(paths: Paths, options: UseStoreMultiOptions<TState, Paths, TDefaultValue_1> & {
|
|
50
|
+
defaultValue: TDefaultValue_1;
|
|
51
|
+
transformer?: never;
|
|
52
|
+
}): MultiPathReturn<TState, Paths, TDefaultValue_1>;
|
|
53
|
+
<const Paths extends ReadonlyArray<PathOf<TState>>, TResult_2>(paths: Paths, options: UseStoreMultiOptions<TState, Paths> & {
|
|
54
|
+
transformer: (values: PathValues<TState, Paths>) => TResult_2;
|
|
55
|
+
}): [TResult_2, ...PathSetters<TState, Paths>];
|
|
56
|
+
<const Entries extends ReadonlyArray<PathOrFn<TState>>>(paths: Entries, options?: Omit<UseStoreMultiOptions<TState, any>, "defaultValue" | "transformer"> & {
|
|
57
|
+
transformer?: never;
|
|
58
|
+
}): [PathOrFnValues<TState, Entries>, ...PathOrFnSetters<TState, Entries>];
|
|
59
|
+
<const Entries extends ReadonlyArray<PathOrFn<TState>>>(paths: Entries, options: Omit<UseStoreMultiOptions<TState, any>, "defaultValue" | "transformer"> & {
|
|
60
|
+
defaultValue: undefined;
|
|
61
|
+
transformer?: never;
|
|
62
|
+
}): [{ [I in keyof Entries]: PathOrFnValue<TState, Entries[I]> | undefined; }, ...PathOrFnSetters<TState, Entries>];
|
|
63
|
+
<const Entries extends ReadonlyArray<PathOrFn<TState>>, const TDefaultValue_2 extends readonly unknown[]>(paths: Entries, options: Omit<UseStoreMultiOptions<TState, any>, "defaultValue" | "transformer"> & {
|
|
64
|
+
defaultValue: TDefaultValue_2;
|
|
65
|
+
transformer?: never;
|
|
66
|
+
}): [{ [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>];
|
|
67
|
+
<const Entries extends ReadonlyArray<PathOrFn<TState>>, TResult_3>(paths: Entries, options: Omit<UseStoreMultiOptions<TState, any>, "defaultValue" | "transformer"> & {
|
|
68
|
+
transformer: (values: PathOrFnValues<TState, Entries>) => TResult_3;
|
|
69
|
+
}): [TResult_3, ...PathOrFnSetters<TState, Entries>];
|
|
70
|
+
};
|
|
71
|
+
useStoreSync: {
|
|
72
|
+
(path: undefined, value: TState | Partial<TState>, options?: UseStoreSyncOptions<TState, TState>): void;
|
|
73
|
+
<P extends PathOf<TState>>(path: P | ((state: TState) => P), value: PathValue<TState, P>, options?: UseStoreSyncOptions<PathValue<TState, P>, TState>): void;
|
|
74
|
+
<const Paths extends ReadonlyArray<PathOf<TState>>>(paths: Paths, values: PathValues<TState, Paths>, options?: UseStoreSyncMultiOptions<TState>): void;
|
|
75
|
+
(paths: ReadonlyArray<PathOrFn<TState>>, values: readonly unknown[], options?: UseStoreSyncMultiOptions<TState>): void;
|
|
76
|
+
};
|
|
77
|
+
useStoreGetter: {
|
|
78
|
+
(options?: UseStoreGetterOptions<TState>): GetValueFn<TState>;
|
|
79
|
+
<P extends PathOf<TState>>(basePath: P, options?: UseStoreGetterOptions<TState>): GetValueFromBaseFn<PathValue<TState, P>>;
|
|
80
|
+
<P extends PathOf<TState>, D_2>(basePath: P, options: UseStoreGetterOptions<TState, D_2> & {
|
|
81
|
+
defaultValue: D_2;
|
|
82
|
+
}): GetValueFromBaseWithDefaultFn<PathValue<TState, P>, D_2>;
|
|
83
|
+
<const Entries extends ReadonlyArray<PathOf<TState> | ((state: TState) => unknown)>>(entries: Entries, options?: UseStoreGetterOptions<TState>): GetterTuple<TState, Entries>;
|
|
84
|
+
};
|
|
85
|
+
useStoreSetter: {
|
|
86
|
+
(options?: UseStoreSetterOptions<TState>): SetStateFn<TState>;
|
|
87
|
+
<P extends PathOf<TState>>(basePath: P, options?: UseStoreSetterOptions<TState>): SetFromBaseFn<PathValue<TState, P>>;
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
export default createStore;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { createChainReads as e } from "./helpers/createChainReads.mjs";
|
|
2
|
+
import { createSetState as t } from "./helpers/createSetState.mjs";
|
|
3
|
+
import { forwardParentChanges as n } from "./helpers/forwardParentChanges.mjs";
|
|
4
|
+
import r from "./helpers/Subscribers.mjs";
|
|
5
|
+
import i from "./helpers/PathTrie.mjs";
|
|
6
|
+
import "./helpers/scopeCollisions.mjs";
|
|
7
|
+
import a from "./hooks/useStore.mjs";
|
|
8
|
+
import o from "./hooks/useStoreGetter.mjs";
|
|
9
|
+
import s from "./hooks/useStoreSetter.mjs";
|
|
10
|
+
import c from "./hooks/useStoreSync.mjs";
|
|
11
|
+
//#region src/createStore/createStore.ts
|
|
12
|
+
var l = 0;
|
|
13
|
+
function u(a, o) {
|
|
14
|
+
let s = {}, c, u = new r(), d = new r(), f = new i(), p = new r(), m = () => {
|
|
15
|
+
p.forEach((e) => e());
|
|
16
|
+
}, h = [], g = [], _ = o?.parent;
|
|
17
|
+
++l;
|
|
18
|
+
let v = (e, t, n) => {
|
|
19
|
+
if (g.length === 0) throw e;
|
|
20
|
+
let r = {
|
|
21
|
+
error: e,
|
|
22
|
+
phase: t,
|
|
23
|
+
path: n
|
|
24
|
+
};
|
|
25
|
+
for (let e = 0, t = g.length; e < t; e++) g[e](r);
|
|
26
|
+
}, y = () => s, b = () => c ??= { ...s }, { getState: x, getPath: S, invalidate: C, setActive: w, getMergeCount: T } = e(y, b, _);
|
|
27
|
+
_ && w(!1);
|
|
28
|
+
let { setState: E, batch: D } = t({
|
|
29
|
+
getOwnState: y,
|
|
30
|
+
getOwnSnapshot: b,
|
|
31
|
+
setOwnState: (e) => {
|
|
32
|
+
s = e, c = void 0, C();
|
|
33
|
+
},
|
|
34
|
+
mutateOwnKey: (e, t) => {
|
|
35
|
+
s[e] = t, C(), c = void 0;
|
|
36
|
+
},
|
|
37
|
+
parent: _,
|
|
38
|
+
listeners: u,
|
|
39
|
+
pathListeners: f,
|
|
40
|
+
changeListeners: d,
|
|
41
|
+
interceptors: h,
|
|
42
|
+
reportError: v,
|
|
43
|
+
invalidateDescendants: m,
|
|
44
|
+
onDelegateToParent: void 0
|
|
45
|
+
}), O = () => {
|
|
46
|
+
C(), m();
|
|
47
|
+
}, k, A, j = !1, M = () => u.length > 0 || f.size > 0 || d.length > 0, N = () => {
|
|
48
|
+
k || !_ || (C(), w(!0), k = n(_, u, f, d, x, v, C), A = _.subscribeInvalidate?.(O), d.length > 0 && k.seedBaseline());
|
|
49
|
+
}, P = () => {
|
|
50
|
+
k && (k.unsubscribe(), k = void 0, A?.(), A = void 0, w(!1));
|
|
51
|
+
}, F = () => {
|
|
52
|
+
_ && (!j && M() ? N() : P());
|
|
53
|
+
}, I = (e) => (F(), () => {
|
|
54
|
+
e(), F();
|
|
55
|
+
}), L = (e) => I(u.add(e)), R = (e, t) => I(f.add(e, t)), z = (e) => {
|
|
56
|
+
let t = I(d.add(e));
|
|
57
|
+
return k?.seedBaseline(), t;
|
|
58
|
+
};
|
|
59
|
+
s = typeof a == "function" ? a(E, x) : a;
|
|
60
|
+
let B = {
|
|
61
|
+
id: o?.id,
|
|
62
|
+
getState: x,
|
|
63
|
+
getPath: S,
|
|
64
|
+
setState: E,
|
|
65
|
+
batch: D,
|
|
66
|
+
subscribe: L,
|
|
67
|
+
subscribePath: R,
|
|
68
|
+
subscribeChange: z,
|
|
69
|
+
destroy: () => {
|
|
70
|
+
j = !0, P(), u.clear(), f.clear(), d.clear(), p.clear();
|
|
71
|
+
},
|
|
72
|
+
reconnect: () => {
|
|
73
|
+
j = !1, F();
|
|
74
|
+
},
|
|
75
|
+
subscribeInvalidate: (e) => p.add(e)
|
|
76
|
+
};
|
|
77
|
+
if (o?.middlewares) for (let e of o.middlewares) {
|
|
78
|
+
let t = e(B);
|
|
79
|
+
t?.beforeChange && h.push(t.beforeChange), t?.onChange && z(t.onChange), t?.onError && g.push(t.onError);
|
|
80
|
+
}
|
|
81
|
+
return B;
|
|
82
|
+
}
|
|
83
|
+
var d = () => {
|
|
84
|
+
function e(e, t) {
|
|
85
|
+
return a(e, t);
|
|
86
|
+
}
|
|
87
|
+
function t(e, t, n) {
|
|
88
|
+
c(e, t, n);
|
|
89
|
+
}
|
|
90
|
+
function n(e, t) {
|
|
91
|
+
return o(e, t);
|
|
92
|
+
}
|
|
93
|
+
function r(e, t) {
|
|
94
|
+
return s(e, t);
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
useStore: e,
|
|
98
|
+
useStoreSync: t,
|
|
99
|
+
useStoreGetter: n,
|
|
100
|
+
useStoreSetter: r
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
//#endregion
|
|
104
|
+
export { d as createStoreHook, u as default };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { default as Subscribers } from './Subscribers';
|
|
2
|
+
import { Listener, Path } from '../../types';
|
|
3
|
+
declare class PathTrie {
|
|
4
|
+
readonly direct: Map<string, Subscribers<Listener>>;
|
|
5
|
+
private readonly descendants;
|
|
6
|
+
size: number;
|
|
7
|
+
add(path: string, listener: Listener): () => void;
|
|
8
|
+
private remove;
|
|
9
|
+
getDescendants(path: string): Set<string> | undefined;
|
|
10
|
+
forEachAffected(changedPath: Path | undefined, cb: (listener: Listener) => void, onError?: (error: unknown) => void): void;
|
|
11
|
+
clear(): void;
|
|
12
|
+
}
|
|
13
|
+
export default PathTrie;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import e from "../../helpers/parsePath.mjs";
|
|
2
|
+
import t from "./Subscribers.mjs";
|
|
3
|
+
//#region src/createStore/helpers/PathTrie.ts
|
|
4
|
+
var n = (e, t, n) => {
|
|
5
|
+
let r = "";
|
|
6
|
+
for (let i = 0; i < t; i++) r = r ? `${r}.${e[i]}` : e[i], n(r);
|
|
7
|
+
}, r = class {
|
|
8
|
+
direct = /* @__PURE__ */ new Map();
|
|
9
|
+
descendants = /* @__PURE__ */ new Map();
|
|
10
|
+
size = 0;
|
|
11
|
+
add(r, i) {
|
|
12
|
+
let a = this.direct.get(r), o = e(r);
|
|
13
|
+
return a || (a = new t(), this.direct.set(r, a), this.size++, n(o, o.length - 1, (e) => {
|
|
14
|
+
let t = this.descendants.get(e);
|
|
15
|
+
t || (t = /* @__PURE__ */ new Set(), this.descendants.set(e, t)), t.add(r);
|
|
16
|
+
})), a.add(i), () => this.remove(r, a, i, o);
|
|
17
|
+
}
|
|
18
|
+
remove(e, t, r, i) {
|
|
19
|
+
this.direct.get(e) === t && (t.remove(r), !(t.length > 0) && (this.direct.delete(e), this.size--, n(i, i.length - 1, (t) => {
|
|
20
|
+
let n = this.descendants.get(t);
|
|
21
|
+
n && (n.delete(e), n.size === 0 && this.descendants.delete(t));
|
|
22
|
+
})));
|
|
23
|
+
}
|
|
24
|
+
getDescendants(e) {
|
|
25
|
+
return this.descendants.get(e);
|
|
26
|
+
}
|
|
27
|
+
forEachAffected(t, r, i) {
|
|
28
|
+
let a = (e) => {
|
|
29
|
+
e && e.forEach(r, i);
|
|
30
|
+
};
|
|
31
|
+
if (typeof t != "string") {
|
|
32
|
+
this.direct.forEach(a);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
let o = e(t);
|
|
36
|
+
n(o, o.length, (e) => a(this.direct.get(e))), this.descendants.get(t)?.forEach((e) => a(this.direct.get(e)));
|
|
37
|
+
}
|
|
38
|
+
clear() {
|
|
39
|
+
this.direct.clear(), this.descendants.clear(), this.size = 0;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
//#endregion
|
|
43
|
+
export { r as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type AnyListener = (...args: never[]) => void;
|
|
2
|
+
declare class Subscribers<F extends AnyListener> {
|
|
3
|
+
readonly items: F[];
|
|
4
|
+
private notifying;
|
|
5
|
+
private dead;
|
|
6
|
+
get length(): number;
|
|
7
|
+
add(listener: F): () => void;
|
|
8
|
+
remove(listener: F): void;
|
|
9
|
+
begin(): void;
|
|
10
|
+
end(): void;
|
|
11
|
+
forEach(cb: (listener: F) => void, onError?: (error: unknown) => void): void;
|
|
12
|
+
clear(): void;
|
|
13
|
+
private compact;
|
|
14
|
+
}
|
|
15
|
+
export default Subscribers;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//#region src/createStore/helpers/Subscribers.ts
|
|
2
|
+
var e = () => {}, t = class {
|
|
3
|
+
items = [];
|
|
4
|
+
notifying = 0;
|
|
5
|
+
dead = 0;
|
|
6
|
+
get length() {
|
|
7
|
+
return this.items.length - this.dead;
|
|
8
|
+
}
|
|
9
|
+
add(e) {
|
|
10
|
+
return this.items.push(e), () => this.remove(e);
|
|
11
|
+
}
|
|
12
|
+
remove(t) {
|
|
13
|
+
let n = this.items.indexOf(t);
|
|
14
|
+
if (n === -1) return;
|
|
15
|
+
if (this.notifying > 0) {
|
|
16
|
+
this.items[n] = e, this.dead++;
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
let r = this.items.pop();
|
|
20
|
+
n < this.items.length && (this.items[n] = r);
|
|
21
|
+
}
|
|
22
|
+
begin() {
|
|
23
|
+
this.notifying++;
|
|
24
|
+
}
|
|
25
|
+
end() {
|
|
26
|
+
--this.notifying === 0 && this.dead > 0 && this.compact();
|
|
27
|
+
}
|
|
28
|
+
forEach(e, t) {
|
|
29
|
+
this.begin();
|
|
30
|
+
try {
|
|
31
|
+
let { items: n } = this, r = n.length, i = 0, a, o = !1;
|
|
32
|
+
for (; i < r;) try {
|
|
33
|
+
for (; i < r; i++) e(n[i]);
|
|
34
|
+
} catch (e) {
|
|
35
|
+
o || (o = !0, a = e), i++;
|
|
36
|
+
}
|
|
37
|
+
if (o) if (t) t(a);
|
|
38
|
+
else throw a;
|
|
39
|
+
} finally {
|
|
40
|
+
this.end();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
clear() {
|
|
44
|
+
if (this.notifying > 0) {
|
|
45
|
+
let { items: t } = this;
|
|
46
|
+
for (let n = 0, r = t.length; n < r; n++) t[n] = e;
|
|
47
|
+
this.dead = t.length;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
this.items.length = 0, this.dead = 0;
|
|
51
|
+
}
|
|
52
|
+
compact() {
|
|
53
|
+
let { items: t } = this, n = 0;
|
|
54
|
+
for (let r = 0; r < t.length; r++) t[r] !== e && (t[n++] = t[r]);
|
|
55
|
+
t.length = n, this.dead = 0;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
//#endregion
|
|
59
|
+
export { t as default };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { GetState, PathOf, PathValue, StoreApi } from '../../types';
|
|
2
|
+
export type GetPath<TState extends object> = <P extends PathOf<TState>>(path: P) => PathValue<TState, P> | undefined;
|
|
3
|
+
export type ChainReads<TState extends object> = {
|
|
4
|
+
getState: GetState<TState>;
|
|
5
|
+
getPath: GetPath<TState>;
|
|
6
|
+
invalidate: () => void;
|
|
7
|
+
setActive: (active: boolean) => void;
|
|
8
|
+
getMergeCount: () => number;
|
|
9
|
+
};
|
|
10
|
+
export declare function createChainReads<TState extends object>(getOwnState: () => TState, getOwnSnapshot: () => TState, parent: StoreApi<TState> | undefined): ChainReads<TState>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { deepMerge as e, isPlainObject as t } from "./deepMerge.mjs";
|
|
2
|
+
import n from "../../helpers/getByPath.mjs";
|
|
3
|
+
//#region src/createStore/helpers/createChainReads.ts
|
|
4
|
+
function r(r, i, a) {
|
|
5
|
+
if (!a) return {
|
|
6
|
+
getState: i,
|
|
7
|
+
getPath: (e) => n(r(), e),
|
|
8
|
+
invalidate: () => {},
|
|
9
|
+
setActive: () => {},
|
|
10
|
+
getMergeCount: () => 0
|
|
11
|
+
};
|
|
12
|
+
let o = a, s = !0, c = !0, l = !1, u, d = 0, f = /* @__PURE__ */ new Map(), p = () => (d++, e(o.getState(), i())), m = () => s ? ((c || u === void 0) && (u = p(), c = !1), u) : p(), h = (e) => {
|
|
13
|
+
let i = n(r(), e);
|
|
14
|
+
if (i === void 0) return o.getPath(e);
|
|
15
|
+
let a = o.getPath(e);
|
|
16
|
+
return a === void 0 || !t(i) || !t(a) ? i : n(m(), e);
|
|
17
|
+
};
|
|
18
|
+
return {
|
|
19
|
+
getState: m,
|
|
20
|
+
getPath: (e) => {
|
|
21
|
+
if (!s) return h(e);
|
|
22
|
+
if (l) f.clear(), l = !1;
|
|
23
|
+
else if (f.has(e)) return f.get(e);
|
|
24
|
+
let t = h(e);
|
|
25
|
+
return f.set(e, t), t;
|
|
26
|
+
},
|
|
27
|
+
invalidate: () => {
|
|
28
|
+
c = !0, l = !0;
|
|
29
|
+
},
|
|
30
|
+
setActive: (e) => {
|
|
31
|
+
s = e, e || (f.clear(), u = void 0, c = !0, l = !1);
|
|
32
|
+
},
|
|
33
|
+
getMergeCount: () => d
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { r as createChainReads };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { default as PathTrie } from './PathTrie';
|
|
2
|
+
import { default as Subscribers } from './Subscribers';
|
|
3
|
+
import { ChangeListener, Listener, SetState, StoreApi, StoreErrorReporter, WriteInterceptor } from '../../types';
|
|
4
|
+
export type SetStateDeps<TState extends object> = {
|
|
5
|
+
getOwnState: () => TState;
|
|
6
|
+
getOwnSnapshot: () => TState;
|
|
7
|
+
setOwnState: (next: TState) => void;
|
|
8
|
+
mutateOwnKey: (key: string, value: unknown) => void;
|
|
9
|
+
parent: StoreApi<TState> | undefined;
|
|
10
|
+
listeners: Subscribers<Listener>;
|
|
11
|
+
changeListeners: Subscribers<ChangeListener<TState>>;
|
|
12
|
+
pathListeners: PathTrie;
|
|
13
|
+
interceptors: WriteInterceptor<TState>[];
|
|
14
|
+
reportError: StoreErrorReporter<TState>;
|
|
15
|
+
invalidateDescendants: () => void;
|
|
16
|
+
onDelegateToParent?: (path: string) => void;
|
|
17
|
+
};
|
|
18
|
+
export type SetStateApi<TState extends object> = {
|
|
19
|
+
setState: SetState<TState>;
|
|
20
|
+
batch: <R>(fn: () => R) => R;
|
|
21
|
+
};
|
|
22
|
+
export declare function createSetState<TState extends object>(deps: SetStateDeps<TState>): SetStateApi<TState>;
|