@lunarhue/store 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -10
- package/dist/chunk-C5U3Q6O3.js +133 -0
- package/dist/chunk-C5U3Q6O3.js.map +1 -0
- package/dist/chunk-GHGDIRN2.js +187 -0
- package/dist/chunk-GHGDIRN2.js.map +1 -0
- package/dist/core/index.d.ts +12 -2
- package/dist/core/index.js +8 -3
- package/dist/core/index.js.map +1 -1
- package/dist/plugins/actions/index.d.ts +45 -1
- package/dist/plugins/actions/index.js.map +1 -1
- package/dist/plugins/persist/index.d.ts +138 -41
- package/dist/plugins/persist/index.js +149 -218
- package/dist/plugins/persist/index.js.map +1 -1
- package/dist/react/index.d.ts +75 -4
- package/dist/react/index.js +1 -1
- package/dist/types-CugArdrs.d.ts +107 -0
- package/package.json +2 -1
- package/dist/chunk-ALACXQX3.js +0 -103
- package/dist/chunk-ALACXQX3.js.map +0 -1
- package/dist/chunk-MIBJNMOV.js +0 -45
- package/dist/chunk-MIBJNMOV.js.map +0 -1
- package/dist/types-BfzW2j9T.d.ts +0 -20
package/dist/react/index.d.ts
CHANGED
|
@@ -1,30 +1,101 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
|
-
import { S as StoreBuilder, a as Store,
|
|
3
|
+
import { S as StoreBuilder, a as Store, c as StoreInitialStateLoader, h as StoreState } from '../types-CugArdrs.js';
|
|
4
4
|
import '@tanstack/store';
|
|
5
5
|
|
|
6
6
|
type StoreProviderChildren<TState, TPlugins> = ReactNode | ((args: {
|
|
7
7
|
store: Store<TState, TPlugins>;
|
|
8
8
|
}) => ReactNode);
|
|
9
|
-
type
|
|
9
|
+
type BuilderProviderBaseProps<TState, TPlugins> = {
|
|
10
10
|
builder: StoreBuilder<TState, TPlugins>;
|
|
11
11
|
children?: StoreProviderChildren<TState, TPlugins>;
|
|
12
|
+
hasInitialState?: boolean;
|
|
12
13
|
store?: never;
|
|
13
14
|
};
|
|
15
|
+
type BuilderProviderWithInitialStateProps<TState, TPlugins> = BuilderProviderBaseProps<TState, TPlugins> & {
|
|
16
|
+
initialState: TState;
|
|
17
|
+
loadInitialState?: never;
|
|
18
|
+
};
|
|
19
|
+
type BuilderProviderWithLoadInitialStateProps<TState, TPlugins> = BuilderProviderBaseProps<TState, TPlugins> & {
|
|
20
|
+
initialState?: never;
|
|
21
|
+
loadInitialState: StoreInitialStateLoader<TState, TPlugins>;
|
|
22
|
+
};
|
|
23
|
+
type BuilderProviderWithDeclaredInitialStateProps<TState, TPlugins> = BuilderProviderBaseProps<TState, TPlugins> & {
|
|
24
|
+
initialState?: never;
|
|
25
|
+
loadInitialState?: never;
|
|
26
|
+
};
|
|
27
|
+
type BuilderProviderProps<TState, TPlugins> = BuilderProviderWithDeclaredInitialStateProps<TState, TPlugins> | BuilderProviderWithInitialStateProps<TState, TPlugins> | BuilderProviderWithLoadInitialStateProps<TState, TPlugins>;
|
|
14
28
|
type StoreProviderProps<TState, TPlugins> = {
|
|
15
29
|
builder?: never;
|
|
16
30
|
children?: StoreProviderChildren<TState, TPlugins>;
|
|
17
31
|
store: Store<TState, TPlugins>;
|
|
18
32
|
};
|
|
19
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Props for {@link StoreProvider}.
|
|
35
|
+
*
|
|
36
|
+
* Use either `builder` to let the provider create and own the runtime store,
|
|
37
|
+
* or `store` to provide an already-created runtime store. When a builder has
|
|
38
|
+
* no declared initial state, the builder-owned form must also provide either
|
|
39
|
+
* `initialState` or `loadInitialState`.
|
|
40
|
+
*/
|
|
41
|
+
type ProviderProps<TState, TPlugins = {}> = BuilderProviderProps<TState, TPlugins> | StoreProviderProps<TState, TPlugins>;
|
|
42
|
+
/**
|
|
43
|
+
* Provides a builder-scoped runtime store to React descendants.
|
|
44
|
+
*
|
|
45
|
+
* Builder-owned providers create and dispose the runtime store automatically.
|
|
46
|
+
* External-store providers reuse an existing runtime store and do not own its
|
|
47
|
+
* disposal. The `builder` prop must remain stable for the provider lifetime.
|
|
48
|
+
* Children may be plain JSX or a render prop that receives the resolved
|
|
49
|
+
* runtime store instance.
|
|
50
|
+
*/
|
|
20
51
|
declare function StoreProvider<TState, TPlugins>(props: ProviderProps<TState, TPlugins>): react_jsx_runtime.JSX.Element;
|
|
21
52
|
|
|
22
|
-
|
|
53
|
+
type LocalStoreOptionsBase = {};
|
|
54
|
+
type LocalStoreWithInitialStateOptions<TState> = LocalStoreOptionsBase & {
|
|
55
|
+
initialState: TState;
|
|
56
|
+
loadInitialState?: never;
|
|
57
|
+
};
|
|
58
|
+
type LocalStoreWithLoadInitialStateOptions<TState, TPlugins> = LocalStoreOptionsBase & {
|
|
59
|
+
initialState?: never;
|
|
60
|
+
loadInitialState: StoreInitialStateLoader<TState, TPlugins>;
|
|
61
|
+
};
|
|
62
|
+
type LocalStoreDeclaredInitialStateOptions = LocalStoreOptionsBase & {
|
|
63
|
+
initialState?: never;
|
|
64
|
+
loadInitialState?: never;
|
|
65
|
+
};
|
|
66
|
+
type LocalStoreOptions<TState, TPlugins> = LocalStoreDeclaredInitialStateOptions | LocalStoreWithInitialStateOptions<TState> | LocalStoreWithLoadInitialStateOptions<TState, TPlugins>;
|
|
67
|
+
/**
|
|
68
|
+
* Creates and owns a local runtime store for the current hook call site.
|
|
69
|
+
*
|
|
70
|
+
* The returned store is disposed on unmount. Pass `initialState` to start the
|
|
71
|
+
* local store ready immediately, or `loadInitialState` to initialize a builder
|
|
72
|
+
* that was declared without a default value. Without a declared default or one
|
|
73
|
+
* of those options, the returned runtime store begins uninitialized.
|
|
74
|
+
*/
|
|
75
|
+
declare function useLocalStore<TState, TPlugins>(builder: StoreBuilder<TState, TPlugins>, options?: LocalStoreOptions<TState, TPlugins>): Store<TState, TPlugins>;
|
|
23
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Subscribes to a selected slice of a runtime store instance.
|
|
79
|
+
*
|
|
80
|
+
* Use this when you already have a runtime store object. For builder-scoped
|
|
81
|
+
* selection through context, use {@link useStoreSelector}.
|
|
82
|
+
*/
|
|
24
83
|
declare function useSelector<TStore extends Store<any, any>, TSelected, TState extends StoreState<TStore>>(store: TStore, selector: (snapshot: TState) => TSelected, compare?: (a: TSelected, b: TSelected) => boolean): TSelected;
|
|
25
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Returns the runtime store provided for a builder.
|
|
87
|
+
*
|
|
88
|
+
* This hook requires a matching {@link StoreProvider} ancestor using either
|
|
89
|
+
* the same builder or a runtime store created from that builder.
|
|
90
|
+
*/
|
|
26
91
|
declare function useStore<TState, TPlugins>(builder: StoreBuilder<TState, TPlugins>): Store<TState, TPlugins>;
|
|
27
92
|
|
|
93
|
+
/**
|
|
94
|
+
* Convenience hook that resolves a builder-scoped runtime store from context
|
|
95
|
+
* and subscribes to a selected slice of its state.
|
|
96
|
+
*
|
|
97
|
+
* This has the same matching-provider requirement as {@link useStore}.
|
|
98
|
+
*/
|
|
28
99
|
declare function useStoreSelector<TState, TPlugins, TSelected, TBuilder extends StoreBuilder<TState, TPlugins>>(builder: TBuilder, selector: (snapshot: StoreState<ReturnType<TBuilder['create']>>) => TSelected, compare?: (a: TSelected, b: TSelected) => boolean): TSelected;
|
|
29
100
|
|
|
30
101
|
export { StoreProvider, type ProviderProps as StoreProviderProps, useLocalStore, useSelector, useStore, useStoreSelector };
|
package/dist/react/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { StoreProvider, useLocalStore, useSelector, useStore, useStoreSelector } from '../chunk-
|
|
1
|
+
export { StoreProvider, useLocalStore, useSelector, useStore, useStoreSelector } from '../chunk-GHGDIRN2.js';
|
|
2
2
|
import '../chunk-PCSRXZL4.js';
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Readable, Store as Store$1 } from '@tanstack/store';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Cleanup registered by a plugin to run when a runtime store is disposed.
|
|
5
|
+
*/
|
|
6
|
+
type StoreCleanup = () => void | Promise<void>;
|
|
7
|
+
/**
|
|
8
|
+
* Minimal read-only store surface exposed for lifecycle and metadata stores.
|
|
9
|
+
*/
|
|
10
|
+
type ReadableStore<TState> = Pick<Readable<TState>, 'get' | 'subscribe'> & {
|
|
11
|
+
/**
|
|
12
|
+
* Current snapshot.
|
|
13
|
+
*/
|
|
14
|
+
readonly state: TState;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Initialization state for a runtime store.
|
|
18
|
+
*/
|
|
19
|
+
type StoreLifecycleStatus = 'uninitialized' | 'initializing' | 'ready' | 'error';
|
|
20
|
+
/**
|
|
21
|
+
* Current initialization metadata for a runtime store.
|
|
22
|
+
*/
|
|
23
|
+
type StoreLifecycleMeta = {
|
|
24
|
+
/**
|
|
25
|
+
* Current readiness state for the runtime store.
|
|
26
|
+
*/
|
|
27
|
+
status: StoreLifecycleStatus;
|
|
28
|
+
/**
|
|
29
|
+
* Initialization error captured while resolving initial state.
|
|
30
|
+
*/
|
|
31
|
+
error: unknown | null;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Loads initial state for a runtime store that was created without a declared
|
|
35
|
+
* default value.
|
|
36
|
+
*/
|
|
37
|
+
type StoreInitialStateLoader<TState, TPlugins = {}> = (args: {
|
|
38
|
+
store: Store<TState, TPlugins>;
|
|
39
|
+
}) => Promise<TState> | TState;
|
|
40
|
+
type StoreLifecycleSurface<TState> = {
|
|
41
|
+
/**
|
|
42
|
+
* Initializes a runtime store that was created without a ready state.
|
|
43
|
+
*
|
|
44
|
+
* This may only be called once per runtime store.
|
|
45
|
+
*/
|
|
46
|
+
setInitialState(nextState: TState): Promise<void>;
|
|
47
|
+
lifecycle: {
|
|
48
|
+
/**
|
|
49
|
+
* Read-only lifecycle metadata for the runtime store.
|
|
50
|
+
*/
|
|
51
|
+
meta: ReadableStore<StoreLifecycleMeta>;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Runtime Lunarhue store surface.
|
|
56
|
+
*
|
|
57
|
+
* This extends the TanStack store instance with lifecycle metadata,
|
|
58
|
+
* one-time initialization for uninitialized stores, disposal, and any plugin
|
|
59
|
+
* surfaces attached through the builder.
|
|
60
|
+
*/
|
|
61
|
+
type Store<TState, TPlugins = {}> = Store$1<TState> & StoreLifecycleSurface<TState> & {
|
|
62
|
+
dispose(): Promise<void>;
|
|
63
|
+
} & TPlugins;
|
|
64
|
+
/**
|
|
65
|
+
* Infers the state shape from a runtime store type.
|
|
66
|
+
*/
|
|
67
|
+
type StoreState<TStore extends Store<any, any>> = TStore extends {
|
|
68
|
+
get: () => infer TState;
|
|
69
|
+
} ? TState : never;
|
|
70
|
+
/**
|
|
71
|
+
* Context passed to a store plugin while a runtime store is being created.
|
|
72
|
+
*/
|
|
73
|
+
type StorePluginContext<TState, TPlugins> = {
|
|
74
|
+
/**
|
|
75
|
+
* Runtime store instance being extended.
|
|
76
|
+
*/
|
|
77
|
+
store: Store<TState, TPlugins>;
|
|
78
|
+
/**
|
|
79
|
+
* Registers cleanup work to run when the runtime store is disposed.
|
|
80
|
+
*/
|
|
81
|
+
onDispose(cleanup: StoreCleanup): void;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Additive plugin that extends the runtime store surface without storing
|
|
85
|
+
* plugin metadata inside application state.
|
|
86
|
+
*/
|
|
87
|
+
type StorePlugin<TState, TPlugins, TNextPlugins> = (context: StorePluginContext<TState, TPlugins>) => TNextPlugins;
|
|
88
|
+
/**
|
|
89
|
+
* Immutable builder used to declare plugins once and create fresh runtime
|
|
90
|
+
* stores on demand.
|
|
91
|
+
*/
|
|
92
|
+
type StoreBuilder<TState, TPlugins = {}> = {
|
|
93
|
+
/**
|
|
94
|
+
* Creates a fresh runtime store instance.
|
|
95
|
+
*
|
|
96
|
+
* Passing `initialState` makes the runtime store ready immediately even when
|
|
97
|
+
* the builder was declared without a default value.
|
|
98
|
+
*/
|
|
99
|
+
create(initialState?: TState): Store<TState, TPlugins>;
|
|
100
|
+
/**
|
|
101
|
+
* Returns a new builder with the plugin surface merged into the runtime
|
|
102
|
+
* store type.
|
|
103
|
+
*/
|
|
104
|
+
extend<TNextPlugins>(plugin: StorePlugin<TState, TPlugins, TNextPlugins>): StoreBuilder<TState, TPlugins & TNextPlugins>;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export type { ReadableStore as R, StoreBuilder as S, Store as a, StoreCleanup as b, StoreInitialStateLoader as c, StoreLifecycleMeta as d, StoreLifecycleStatus as e, StorePlugin as f, StorePluginContext as g, StoreState as h };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunarhue/store",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Framework-agnostic state built on top of @tanstack/store with typed plugins and React bindings.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/LunarHUE/store#readme",
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"build": "tsup",
|
|
50
|
+
"dev": "tsup --watch",
|
|
50
51
|
"contract": "bun run build && node ./scripts/check-package-contract.mjs",
|
|
51
52
|
"test": "vitest run",
|
|
52
53
|
"typecheck": "tsc --noEmit",
|
package/dist/chunk-ALACXQX3.js
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { getStoreBuilder } from './chunk-PCSRXZL4.js';
|
|
2
|
-
import { useRef, useEffect, useContext, createContext } from 'react';
|
|
3
|
-
import { jsx } from 'react/jsx-runtime';
|
|
4
|
-
import { useStore as useStore$1 } from '@tanstack/react-store';
|
|
5
|
-
|
|
6
|
-
var contextMap = /* @__PURE__ */ new WeakMap();
|
|
7
|
-
function getStoreContext(builder) {
|
|
8
|
-
let context = contextMap.get(builder);
|
|
9
|
-
if (!context) {
|
|
10
|
-
context = createContext(void 0);
|
|
11
|
-
contextMap.set(builder, context);
|
|
12
|
-
}
|
|
13
|
-
return context;
|
|
14
|
-
}
|
|
15
|
-
function StoreProvider(props) {
|
|
16
|
-
if (props.builder !== void 0) {
|
|
17
|
-
return /* @__PURE__ */ jsx(BuilderOwnedStoreProvider, { builder: props.builder, children: props.children });
|
|
18
|
-
}
|
|
19
|
-
return /* @__PURE__ */ jsx(ExternalStoreProvider, { store: props.store, children: props.children });
|
|
20
|
-
}
|
|
21
|
-
function BuilderOwnedStoreProvider({
|
|
22
|
-
builder,
|
|
23
|
-
children
|
|
24
|
-
}) {
|
|
25
|
-
const context = getStoreContext(builder);
|
|
26
|
-
const builderRef = useRef(null);
|
|
27
|
-
const storeRef = useRef(null);
|
|
28
|
-
if (!builderRef.current) {
|
|
29
|
-
builderRef.current = builder;
|
|
30
|
-
} else if (builderRef.current !== builder) {
|
|
31
|
-
throw new Error("StoreProvider builder prop must remain stable.");
|
|
32
|
-
}
|
|
33
|
-
if (!storeRef.current) {
|
|
34
|
-
storeRef.current = builder.create();
|
|
35
|
-
}
|
|
36
|
-
useEffect(() => {
|
|
37
|
-
const ownedStore = storeRef.current;
|
|
38
|
-
return () => {
|
|
39
|
-
if (!ownedStore) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
void ownedStore.dispose();
|
|
43
|
-
};
|
|
44
|
-
}, []);
|
|
45
|
-
const content = typeof children === "function" ? children({ store: storeRef.current }) : children;
|
|
46
|
-
return /* @__PURE__ */ jsx(context.Provider, { value: storeRef.current, children: content });
|
|
47
|
-
}
|
|
48
|
-
function ExternalStoreProvider({
|
|
49
|
-
children,
|
|
50
|
-
store
|
|
51
|
-
}) {
|
|
52
|
-
const builder = getStoreBuilder(store);
|
|
53
|
-
if (!builder) {
|
|
54
|
-
throw new Error(
|
|
55
|
-
"StoreProvider could not resolve a builder for the provided store. Pass a store created by @lunarhue/store."
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
const context = getStoreContext(builder);
|
|
59
|
-
const content = typeof children === "function" ? children({ store }) : children;
|
|
60
|
-
return /* @__PURE__ */ jsx(context.Provider, { value: store, children: content });
|
|
61
|
-
}
|
|
62
|
-
function useLocalStore(builder) {
|
|
63
|
-
const localStoreRef = useRef(null);
|
|
64
|
-
if (!localStoreRef.current) {
|
|
65
|
-
localStoreRef.current = builder.create();
|
|
66
|
-
}
|
|
67
|
-
useEffect(() => {
|
|
68
|
-
const localStore = localStoreRef.current;
|
|
69
|
-
return () => {
|
|
70
|
-
if (!localStore) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
void localStore.dispose();
|
|
74
|
-
};
|
|
75
|
-
}, []);
|
|
76
|
-
return localStoreRef.current;
|
|
77
|
-
}
|
|
78
|
-
function useSelector(store, selector, compare) {
|
|
79
|
-
return useStore$1(
|
|
80
|
-
store,
|
|
81
|
-
selector,
|
|
82
|
-
compare
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
function useStore(builder) {
|
|
86
|
-
const contextValue = useContext(getStoreContext(builder));
|
|
87
|
-
if (!contextValue) {
|
|
88
|
-
throw new Error(
|
|
89
|
-
"useStore(builder) requires a matching <StoreProvider builder={...}> or <StoreProvider store={...}> ancestor."
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
return contextValue;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// src/react/use-store-selector.ts
|
|
96
|
-
function useStoreSelector(builder, selector, compare) {
|
|
97
|
-
const store = useStore(builder);
|
|
98
|
-
return useSelector(store, selector, compare);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export { StoreProvider, useLocalStore, useSelector, useStore, useStoreSelector };
|
|
102
|
-
//# sourceMappingURL=chunk-ALACXQX3.js.map
|
|
103
|
-
//# sourceMappingURL=chunk-ALACXQX3.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/react/context.ts","../src/react/provider.tsx","../src/react/use-local-store.ts","../src/react/use-selector.ts","../src/react/use-store.ts","../src/react/use-store-selector.ts"],"names":["useRef","useEffect","useTanStackStore"],"mappings":";;;;;AAQA,IAAM,UAAA,uBAAiB,OAAA,EAAwD;AAExE,SAAS,gBACd,OAAA,EACgC;AAChC,EAAA,IAAI,OAAA,GAAU,UAAA,CAAW,GAAA,CAAI,OAAO,CAAA;AAEpC,EAAA,IAAI,CAAC,OAAA,EAAS;AACZ,IAAA,OAAA,GAAU,cAAmD,MAAS,CAAA;AACtE,IAAA,UAAA,CAAW,GAAA,CAAI,SAAS,OAAO,CAAA;AAAA,EACjC;AAEA,EAAA,OAAO,OAAA;AACT;ACMO,SAAS,cACd,KAAA,EACA;AACA,EAAA,IAAI,KAAA,CAAM,YAAY,MAAA,EAAW;AAC/B,IAAA,2BACG,yBAAA,EAAA,EAA0B,OAAA,EAAS,KAAA,CAAM,OAAA,EACvC,gBAAM,QAAA,EACT,CAAA;AAAA,EAEJ;AAEA,EAAA,2BACG,qBAAA,EAAA,EAAsB,KAAA,EAAO,KAAA,CAAM,KAAA,EACjC,gBAAM,QAAA,EACT,CAAA;AAEJ;AAEA,SAAS,yBAAA,CAA4C;AAAA,EACnD,OAAA;AAAA,EACA;AACF,CAAA,EAA2C;AACzC,EAAA,MAAM,OAAA,GAAU,gBAAgB,OAAO,CAAA;AACvC,EAAA,MAAM,UAAA,GAAa,OAA8C,IAAI,CAAA;AACrE,EAAA,MAAM,QAAA,GAAW,OAAuC,IAAI,CAAA;AAE5D,EAAA,IAAI,CAAC,WAAW,OAAA,EAAS;AACvB,IAAA,UAAA,CAAW,OAAA,GAAU,OAAA;AAAA,EACvB,CAAA,MAAA,IAAW,UAAA,CAAW,OAAA,KAAY,OAAA,EAAS;AACzC,IAAA,MAAM,IAAI,MAAM,gDAAgD,CAAA;AAAA,EAClE;AAEA,EAAA,IAAI,CAAC,SAAS,OAAA,EAAS;AACr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AAA,EACzC;AAEA,EAAAC,UAAU,MAAM;AACd,IAAA,MAAM,aAAa,aAAA,CAAc,OAAA;AAEjC,IAAA,OAAO,MAAM;AACX,MAAA,IAAI,CAAC,UAAA,EAAY;AACf,QAAA;AAAA,MACF;AAEA,MAAA,KAAK,WAAW,OAAA,EAAQ;AAAA,IAC1B,CAAA;AAAA,EACF,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,OAAO,aAAA,CAAc,OAAA;AACvB;ACrBO,SAAS,WAAA,CAKd,KAAA,EACA,QAAA,EACA,OAAA,EACW;AACX,EAAA,OAAOC,UAAA;AAAA,IACL,KAAA;AAAA,IACA,QAAA;AAAA,IACA;AAAA,GACF;AACF;ACbO,SAAS,SACd,OAAA,EACyB;AACzB,EAAA,MAAM,YAAA,GAAe,UAAA,CAAW,eAAA,CAAgB,OAAO,CAAC,CAAA;AAExD,EAAA,IAAI,CAAC,YAAA,EAAc;AACjB,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KACF;AAAA,EACF;AAEA,EAAA,OAAO,YAAA;AACT;;;ACbO,SAAS,gBAAA,CAMd,OAAA,EACA,QAAA,EACA,OAAA,EACW;AACX,EAAA,MAAM,KAAA,GAAQ,SAAS,OAAO,CAAA;AAE9B,EAAA,OAAO,WAAA,CAAY,KAAA,EAAO,QAAA,EAAU,OAAO,CAAA;AAC7C","file":"chunk-ALACXQX3.js","sourcesContent":["import { createContext, type Context } from 'react'\n\nimport type { Store, StoreBuilder } from '../core'\n\nexport type StoreContext<TState, TPlugins> = Context<\n Store<TState, TPlugins> | undefined\n>\n\nconst contextMap = new WeakMap<StoreBuilder<any, any>, StoreContext<any, any>>()\n\nexport function getStoreContext<TState, TPlugins>(\n builder: StoreBuilder<TState, TPlugins>,\n): StoreContext<TState, TPlugins> {\n let context = contextMap.get(builder)\n\n if (!context) {\n context = createContext<Store<TState, TPlugins> | undefined>(undefined)\n contextMap.set(builder, context)\n }\n\n return context\n}\n","import { useEffect, useRef, type ReactNode } from 'react'\n\nimport { getStoreBuilder } from '../core/builder-registry'\nimport { getStoreContext } from './context'\n\nimport type { Store, StoreBuilder } from '../core'\n\ntype StoreProviderChildren<TState, TPlugins> =\n | ReactNode\n | ((args: { store: Store<TState, TPlugins> }) => ReactNode)\n\ntype BuilderProviderProps<TState, TPlugins> = {\n builder: StoreBuilder<TState, TPlugins>\n children?: StoreProviderChildren<TState, TPlugins>\n store?: never\n}\n\ntype StoreProviderProps<TState, TPlugins> = {\n builder?: never\n children?: StoreProviderChildren<TState, TPlugins>\n store: Store<TState, TPlugins>\n}\n\nexport type ProviderProps<TState, TPlugins> =\n | BuilderProviderProps<TState, TPlugins>\n | StoreProviderProps<TState, TPlugins>\n\nexport function StoreProvider<TState, TPlugins>(\n props: ProviderProps<TState, TPlugins>,\n) {\n if (props.builder !== undefined) {\n return (\n <BuilderOwnedStoreProvider builder={props.builder}>\n {props.children}\n </BuilderOwnedStoreProvider>\n )\n }\n\n return (\n <ExternalStoreProvider store={props.store}>\n {props.children}\n </ExternalStoreProvider>\n )\n}\n\nfunction BuilderOwnedStoreProvider<TState, TPlugins>({\n builder,\n children,\n}: BuilderProviderProps<TState, TPlugins>) {\n const context = getStoreContext(builder)\n const builderRef = useRef<StoreBuilder<TState, TPlugins> | null>(null)\n const storeRef = useRef<Store<TState, TPlugins> | null>(null)\n\n if (!builderRef.current) {\n builderRef.current = builder\n } else if (builderRef.current !== builder) {\n throw new Error('StoreProvider builder prop must remain stable.')\n }\n\n if (!storeRef.current) {\n storeRef.current = builder.create()\n }\n\n useEffect(() => {\n const ownedStore = storeRef.current\n\n return () => {\n if (!ownedStore) {\n return\n }\n\n void ownedStore.dispose()\n }\n }, [])\n\n const content =\n typeof children === 'function'\n ? children({ store: storeRef.current })\n : children\n\n return <context.Provider value={storeRef.current}>{content}</context.Provider>\n}\n\nfunction ExternalStoreProvider<TState, TPlugins>({\n children,\n store,\n}: StoreProviderProps<TState, TPlugins>) {\n const builder = getStoreBuilder(store)\n\n if (!builder) {\n throw new Error(\n 'StoreProvider could not resolve a builder for the provided store. Pass a store created by @lunarhue/store.',\n )\n }\n\n const context = getStoreContext(builder)\n const content = typeof children === 'function' ? children({ store }) : children\n\n return <context.Provider value={store}>{content}</context.Provider>\n}\n","import { useEffect, useRef } from 'react'\n\nimport type { Store, StoreBuilder } from '../core'\n\nexport function useLocalStore<TState, TPlugins>(\n builder: StoreBuilder<TState, TPlugins>,\n): Store<TState, TPlugins> {\n const localStoreRef = useRef<Store<TState, TPlugins> | null>(null)\n\n if (!localStoreRef.current) {\n localStoreRef.current = builder.create()\n }\n\n useEffect(() => {\n const localStore = localStoreRef.current\n\n return () => {\n if (!localStore) {\n return\n }\n\n void localStore.dispose()\n }\n }, [])\n\n return localStoreRef.current\n}\n","import { useStore as useTanStackStore } from '@tanstack/react-store'\nimport type { Store as BaseStore } from '@tanstack/store'\n\nimport type { Store, StoreState } from '../core'\n\nexport function useSelector<\n TStore extends Store<any, any>,\n TSelected,\n TState extends StoreState<TStore>,\n>(\n store: TStore,\n selector: (snapshot: TState) => TSelected,\n compare?: (a: TSelected, b: TSelected) => boolean,\n): TSelected {\n return useTanStackStore<BaseStore<TState>, TSelected>(\n store as BaseStore<TState>,\n selector,\n compare,\n )\n}\n","import { useContext } from 'react'\n\nimport { getStoreContext } from './context'\n\nimport type { Store, StoreBuilder } from '../core'\n\nexport function useStore<TState, TPlugins>(\n builder: StoreBuilder<TState, TPlugins>,\n): Store<TState, TPlugins> {\n const contextValue = useContext(getStoreContext(builder))\n\n if (!contextValue) {\n throw new Error(\n 'useStore(builder) requires a matching <StoreProvider builder={...}> or <StoreProvider store={...}> ancestor.',\n )\n }\n\n return contextValue\n}\n","import { useStore } from './use-store'\nimport { useSelector } from './use-selector'\n\nimport type { StoreBuilder, StoreState } from '../core'\n\nexport function useStoreSelector<\n TState,\n TPlugins,\n TSelected,\n TBuilder extends StoreBuilder<TState, TPlugins>,\n>(\n builder: TBuilder,\n selector: (snapshot: StoreState<ReturnType<TBuilder['create']>>) => TSelected,\n compare?: (a: TSelected, b: TSelected) => boolean,\n): TSelected {\n const store = useStore(builder)\n\n return useSelector(store, selector, compare)\n}\n"]}
|
package/dist/chunk-MIBJNMOV.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { createStore } from '@tanstack/store';
|
|
2
|
-
|
|
3
|
-
// src/core/store-instance.ts
|
|
4
|
-
function createStoreInstance(initialState) {
|
|
5
|
-
const store = createStore(initialState);
|
|
6
|
-
const cleanups = [];
|
|
7
|
-
let disposePromise = null;
|
|
8
|
-
const instance = store;
|
|
9
|
-
Object.defineProperty(instance, "dispose", {
|
|
10
|
-
configurable: true,
|
|
11
|
-
enumerable: true,
|
|
12
|
-
value: async () => {
|
|
13
|
-
if (disposePromise) {
|
|
14
|
-
return disposePromise;
|
|
15
|
-
}
|
|
16
|
-
disposePromise = (async () => {
|
|
17
|
-
for (const cleanup of [...cleanups].reverse()) {
|
|
18
|
-
await cleanup();
|
|
19
|
-
}
|
|
20
|
-
})();
|
|
21
|
-
return disposePromise;
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
return {
|
|
25
|
-
store: instance,
|
|
26
|
-
onDispose(cleanup) {
|
|
27
|
-
cleanups.push(cleanup);
|
|
28
|
-
},
|
|
29
|
-
attachSurface(surface) {
|
|
30
|
-
for (const key of Reflect.ownKeys(surface)) {
|
|
31
|
-
if (key in instance) {
|
|
32
|
-
throw new Error(`Store plugin surface collision on "${String(key)}".`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
Object.defineProperties(
|
|
36
|
-
instance,
|
|
37
|
-
Object.getOwnPropertyDescriptors(surface)
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export { createStoreInstance };
|
|
44
|
-
//# sourceMappingURL=chunk-MIBJNMOV.js.map
|
|
45
|
-
//# sourceMappingURL=chunk-MIBJNMOV.js.map
|
|
@@ -1 +0,0 @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":"chunk-MIBJNMOV.js","sourcesContent":["import { createStore as createTanStackStore } from '@tanstack/store'\n\nimport type { StoreCleanup, Store } from './types'\n\ntype StoreController<TState> = {\n store: Store<TState>\n onDispose(cleanup: StoreCleanup): void\n attachSurface(surface: object): void\n}\n\nexport function createStoreInstance<TState>(\n initialState: TState,\n): StoreController<TState> {\n const store = createTanStackStore(initialState)\n const cleanups: StoreCleanup[] = []\n let disposePromise: Promise<void> | null = null\n\n const instance = store as Store<TState>\n\n Object.defineProperty(instance, 'dispose', {\n configurable: true,\n enumerable: true,\n value: async () => {\n if (disposePromise) {\n return disposePromise\n }\n\n disposePromise = (async () => {\n for (const cleanup of [...cleanups].reverse()) {\n await cleanup()\n }\n })()\n\n return disposePromise\n },\n })\n\n return {\n store: instance,\n onDispose(cleanup) {\n cleanups.push(cleanup)\n },\n attachSurface(surface) {\n for (const key of Reflect.ownKeys(surface)) {\n if (key in instance) {\n throw new Error(`Store plugin surface collision on \"${String(key)}\".`)\n }\n }\n\n Object.defineProperties(\n instance,\n Object.getOwnPropertyDescriptors(surface),\n )\n },\n }\n}\n"]}
|
package/dist/types-BfzW2j9T.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Store as Store$1 } from '@tanstack/store';
|
|
2
|
-
|
|
3
|
-
type StoreCleanup = () => void | Promise<void>;
|
|
4
|
-
type Store<TState, TPlugins = {}> = Store$1<TState> & {
|
|
5
|
-
dispose(): Promise<void>;
|
|
6
|
-
} & TPlugins;
|
|
7
|
-
type StoreState<TStore extends Store<any, any>> = TStore extends {
|
|
8
|
-
get: () => infer TState;
|
|
9
|
-
} ? TState : never;
|
|
10
|
-
type StorePluginContext<TState, TPlugins> = {
|
|
11
|
-
store: Store<TState, TPlugins>;
|
|
12
|
-
onDispose(cleanup: StoreCleanup): void;
|
|
13
|
-
};
|
|
14
|
-
type StorePlugin<TState, TPlugins, TNextPlugins> = (context: StorePluginContext<TState, TPlugins>) => TNextPlugins;
|
|
15
|
-
type StoreBuilder<TState, TPlugins = {}> = {
|
|
16
|
-
create(): Store<TState, TPlugins>;
|
|
17
|
-
extend<TNextPlugins>(plugin: StorePlugin<TState, TPlugins, TNextPlugins>): StoreBuilder<TState, TPlugins & TNextPlugins>;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export type { StoreBuilder as S, Store as a, StoreCleanup as b, StorePlugin as c, StorePluginContext as d, StoreState as e };
|