@lunarhue/store 0.3.1 → 0.4.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 +21 -0
- package/dist/chunk-JB6XVML6.js +146 -0
- package/dist/chunk-JB6XVML6.js.map +1 -0
- package/dist/{chunk-GHGDIRN2.js → chunk-SOWFT4FH.js} +160 -19
- package/dist/chunk-SOWFT4FH.js.map +1 -0
- package/dist/chunk-VMOEEBP4.js +268 -0
- package/dist/chunk-VMOEEBP4.js.map +1 -0
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +18 -4
- package/dist/core/index.js.map +1 -1
- package/dist/plugins/actions/index.d.ts +1 -1
- package/dist/plugins/persist/index.d.ts +2 -1
- package/dist/plugins/persist/index.js +83 -6
- package/dist/plugins/persist/index.js.map +1 -1
- package/dist/react/index.d.ts +9 -6
- package/dist/react/index.js +2 -2
- package/dist/{types-CugArdrs.d.ts → types-DjDVKPhw.d.ts} +64 -2
- package/package.json +1 -1
- package/dist/chunk-GHGDIRN2.js.map +0 -1
- package/dist/chunk-PCSRXZL4.js +0 -12
- package/dist/chunk-PCSRXZL4.js.map +0 -1
- package/dist/chunk-TCEPLIBH.js +0 -141
- package/dist/chunk-TCEPLIBH.js.map +0 -1
package/README.md
CHANGED
|
@@ -38,6 +38,27 @@ uninitialized runtime store. In that case, provide `initialState` or
|
|
|
38
38
|
`loadInitialState` when using the React provider, or call
|
|
39
39
|
`store.setInitialState(...)` before reading or writing state directly.
|
|
40
40
|
|
|
41
|
+
## Runtime debugging
|
|
42
|
+
|
|
43
|
+
Debugging is explicit and per runtime store instance.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
const events = []
|
|
47
|
+
const store = CounterStore.create(undefined, {
|
|
48
|
+
debug: {
|
|
49
|
+
level: 'trace',
|
|
50
|
+
console: false,
|
|
51
|
+
sink(event) {
|
|
52
|
+
events.push(event)
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Builder-owned `StoreProvider` and `PersistStoreProvider` also accept `debug`,
|
|
59
|
+
and `useLocalStore(builder, { debug: ... })` forwards the same config for
|
|
60
|
+
locally owned runtimes.
|
|
61
|
+
|
|
41
62
|
## React usage
|
|
42
63
|
|
|
43
64
|
```tsx
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// src/core/logger.ts
|
|
2
|
+
var builderLoggerMetadataKey = /* @__PURE__ */ Symbol("lunarhue.store.logger.builder");
|
|
3
|
+
var storeLoggerMetadataKey = /* @__PURE__ */ Symbol("lunarhue.store.logger.store");
|
|
4
|
+
var debugLevelRank = {
|
|
5
|
+
basic: 0,
|
|
6
|
+
verbose: 1,
|
|
7
|
+
trace: 2
|
|
8
|
+
};
|
|
9
|
+
var nextBuilderId = 1;
|
|
10
|
+
var nextStoreId = 1;
|
|
11
|
+
var nextSubscriptionId = 1;
|
|
12
|
+
function createBuilderLoggerMetadata() {
|
|
13
|
+
return {
|
|
14
|
+
builderId: `b${nextBuilderId++}`
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function defineBuilderLoggerMetadata(builder, metadata) {
|
|
18
|
+
Object.defineProperty(builder, builderLoggerMetadataKey, {
|
|
19
|
+
configurable: false,
|
|
20
|
+
enumerable: false,
|
|
21
|
+
value: metadata,
|
|
22
|
+
writable: false
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function createStoreLoggerMetadata(args) {
|
|
26
|
+
return {
|
|
27
|
+
builderId: args.builderId,
|
|
28
|
+
storeId: `s${nextStoreId++}`,
|
|
29
|
+
sequence: 0,
|
|
30
|
+
options: resolveStoreDebugOptions(args.debug)
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function defineStoreLoggerMetadata(store, metadata) {
|
|
34
|
+
Object.defineProperty(store, storeLoggerMetadataKey, {
|
|
35
|
+
configurable: false,
|
|
36
|
+
enumerable: false,
|
|
37
|
+
value: metadata,
|
|
38
|
+
writable: false
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function getStoreLoggerMetadata(store) {
|
|
42
|
+
return store[storeLoggerMetadataKey];
|
|
43
|
+
}
|
|
44
|
+
function createSubscriptionLoggerId() {
|
|
45
|
+
return `sub${nextSubscriptionId++}`;
|
|
46
|
+
}
|
|
47
|
+
function shouldEmitStoreDebugEvent(configuredLevel, minimumLevel) {
|
|
48
|
+
return debugLevelRank[configuredLevel] >= debugLevelRank[minimumLevel];
|
|
49
|
+
}
|
|
50
|
+
function emitStoreDebugEvent(store, args) {
|
|
51
|
+
const metadata = getStoreLoggerMetadata(store);
|
|
52
|
+
if (!metadata?.options.enabled) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const minimumLevel = args.minimumLevel ?? "basic";
|
|
56
|
+
if (!shouldEmitStoreDebugEvent(metadata.options.level, minimumLevel)) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
const event = {
|
|
60
|
+
builderId: metadata.builderId,
|
|
61
|
+
event: args.event,
|
|
62
|
+
level: metadata.options.level,
|
|
63
|
+
sequence: ++metadata.sequence,
|
|
64
|
+
source: args.source,
|
|
65
|
+
storeId: metadata.storeId,
|
|
66
|
+
timestamp: Date.now()
|
|
67
|
+
};
|
|
68
|
+
if (args.detail !== void 0) {
|
|
69
|
+
event.detail = args.detail;
|
|
70
|
+
}
|
|
71
|
+
if (args.error !== void 0) {
|
|
72
|
+
event.error = args.error;
|
|
73
|
+
}
|
|
74
|
+
if (args.status !== void 0) {
|
|
75
|
+
event.status = args.status;
|
|
76
|
+
}
|
|
77
|
+
if (args.subscriptionId !== void 0) {
|
|
78
|
+
event.subscriptionId = args.subscriptionId;
|
|
79
|
+
}
|
|
80
|
+
if (metadata.options.level === "trace") {
|
|
81
|
+
if (args.previousState !== void 0) {
|
|
82
|
+
event.previousState = args.previousState;
|
|
83
|
+
}
|
|
84
|
+
if (args.nextState !== void 0) {
|
|
85
|
+
event.nextState = args.nextState;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (metadata.options.console) {
|
|
89
|
+
writeStoreDebugConsoleEvent(event);
|
|
90
|
+
}
|
|
91
|
+
if (metadata.options.sink) {
|
|
92
|
+
try {
|
|
93
|
+
metadata.options.sink(event);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
console.error("[lunarhue/store][debug] sink failed", error);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return event;
|
|
99
|
+
}
|
|
100
|
+
function transitionStoreLifecycle(store, lifecycleMeta, nextState, args) {
|
|
101
|
+
const previousState = lifecycleMeta.get();
|
|
102
|
+
lifecycleMeta.setState(() => nextState);
|
|
103
|
+
if (previousState.status === nextState.status && Object.is(previousState.error, nextState.error)) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
emitStoreDebugEvent(store, {
|
|
107
|
+
detail: args?.detail,
|
|
108
|
+
error: nextState.error ?? void 0,
|
|
109
|
+
event: "store.lifecycle.changed",
|
|
110
|
+
minimumLevel: args?.minimumLevel,
|
|
111
|
+
source: args?.source ?? "core",
|
|
112
|
+
status: nextState.status
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function resolveStoreDebugOptions(debug) {
|
|
116
|
+
if (!debug) {
|
|
117
|
+
return {
|
|
118
|
+
console: false,
|
|
119
|
+
enabled: false,
|
|
120
|
+
level: "basic"
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
console: debug.console ?? true,
|
|
125
|
+
enabled: true,
|
|
126
|
+
level: debug.level ?? "basic",
|
|
127
|
+
sink: debug.sink
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function writeStoreDebugConsoleEvent(event) {
|
|
131
|
+
const method = event.error !== void 0 || event.event.endsWith(".error") || event.event.endsWith(".failed") ? console.error : console.log;
|
|
132
|
+
method("[lunarhue/store][debug]", event);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// src/core/builder-registry.ts
|
|
136
|
+
var storeBuilderMap = /* @__PURE__ */ new WeakMap();
|
|
137
|
+
function registerStoreBuilder(store, builder) {
|
|
138
|
+
storeBuilderMap.set(store, builder);
|
|
139
|
+
}
|
|
140
|
+
function getStoreBuilder(store) {
|
|
141
|
+
return storeBuilderMap.get(store);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export { createBuilderLoggerMetadata, createStoreLoggerMetadata, createSubscriptionLoggerId, defineBuilderLoggerMetadata, defineStoreLoggerMetadata, emitStoreDebugEvent, getStoreBuilder, registerStoreBuilder, transitionStoreLifecycle };
|
|
145
|
+
//# sourceMappingURL=chunk-JB6XVML6.js.map
|
|
146
|
+
//# sourceMappingURL=chunk-JB6XVML6.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/logger.ts","../src/core/builder-registry.ts"],"names":[],"mappings":";AAUA,IAAM,wBAAA,0BAAkC,+BAA+B,CAAA;AACvE,IAAM,sBAAA,0BAAgC,6BAA6B,CAAA;AAEnE,IAAM,cAAA,GAAkD;AAAA,EACtD,KAAA,EAAO,CAAA;AAAA,EACP,OAAA,EAAS,CAAA;AAAA,EACT,KAAA,EAAO;AACT,CAAA;AAEA,IAAI,aAAA,GAAgB,CAAA;AACpB,IAAI,WAAA,GAAc,CAAA;AAClB,IAAI,kBAAA,GAAqB,CAAA;AAyBlB,SAAS,2BAAA,GAA0D;AACxE,EAAA,OAAO;AAAA,IACL,SAAA,EAAW,IAAI,aAAA,EAAe,CAAA;AAAA,GAChC;AACF;AAEO,SAAS,2BAAA,CACd,SACA,QAAA,EACM;AACN,EAAA,MAAA,CAAO,cAAA,CAAe,SAAS,wBAAA,EAA0B;AAAA,IACvD,YAAA,EAAc,KAAA;AAAA,IACd,UAAA,EAAY,KAAA;AAAA,IACZ,KAAA,EAAO,QAAA;AAAA,IACP,QAAA,EAAU;AAAA,GACX,CAAA;AACH;AAYO,SAAS,0BAAkC,IAAA,EAGlB;AAC9B,EA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1B,OAAA,CAAQ,QACR,OAAA,CAAQ,GAAA;AAEd,EAAA,MAAA,CAAO,2BAA2B,KAAK,CAAA;AACzC;;;ACpPA,IAAM,eAAA,uBAAsB,OAAA,EAAiD;AAEtE,SAAS,oBAAA,CACd,OACA,OAAA,EACM;AACN,EAAA,eAAA,CAAgB,GAAA,CAAI,OAAO,OAAO,CAAA;AACpC;AAEO,SAAS,gBACd,KAAA,EAC4C;AAC5C,EAAA,OAAO,eAAA,CAAgB,IAAI,KAAK,CAAA;AAClC","file":"chunk-JB6XVML6.js","sourcesContent":["import type { Store } from './types'\nimport type {\n StoreDebugEvent,\n StoreDebugEventName,\n StoreDebugLevel,\n StoreDebugLogArgs,\n StoreDebugOptions,\n StoreLifecycleMeta,\n} from './types'\n\nconst builderLoggerMetadataKey = Symbol('lunarhue.store.logger.builder')\nconst storeLoggerMetadataKey = Symbol('lunarhue.store.logger.store')\n\nconst debugLevelRank: Record<StoreDebugLevel, number> = {\n basic: 0,\n verbose: 1,\n trace: 2,\n}\n\nlet nextBuilderId = 1\nlet nextStoreId = 1\nlet nextSubscriptionId = 1\n\ntype ResolvedStoreDebugOptions<TState> = {\n enabled: boolean\n level: StoreDebugLevel\n console: boolean\n sink?: ((event: StoreDebugEvent<TState>) => void) | undefined\n}\n\nexport type StoreBuilderLoggerMetadata = {\n builderId: string\n}\n\nexport type StoreLoggerMetadata<TState> = {\n builderId: string\n storeId: string\n sequence: number\n options: ResolvedStoreDebugOptions<TState>\n}\n\ntype WritableReadableStore<TState> = {\n get(): TState\n setState(updater: (prev: TState) => TState): void\n}\n\nexport function createBuilderLoggerMetadata(): StoreBuilderLoggerMetadata {\n return {\n builderId: `b${nextBuilderId++}`,\n }\n}\n\nexport function defineBuilderLoggerMetadata<TBuilder extends object>(\n builder: TBuilder,\n metadata: StoreBuilderLoggerMetadata,\n): void {\n Object.defineProperty(builder, builderLoggerMetadataKey, {\n configurable: false,\n enumerable: false,\n value: metadata,\n writable: false,\n })\n}\n\nexport function getBuilderLoggerMetadata<TBuilder extends object>(\n builder: TBuilder,\n): StoreBuilderLoggerMetadata | undefined {\n return (\n builder as TBuilder & {\n [builderLoggerMetadataKey]?: StoreBuilderLoggerMetadata\n }\n )[builderLoggerMetadataKey]\n}\n\nexport function createStoreLoggerMetadata<TState>(args: {\n builderId: string\n debug?: StoreDebugOptions<TState>\n}): StoreLoggerMetadata<TState> {\n return {\n builderId: args.builderId,\n storeId: `s${nextStoreId++}`,\n sequence: 0,\n options: resolveStoreDebugOptions(args.debug),\n }\n}\n\nexport function defineStoreLoggerMetadata<TState>(\n store: Store<TState, any>,\n metadata: StoreLoggerMetadata<TState>,\n): void {\n Object.defineProperty(store, storeLoggerMetadataKey, {\n configurable: false,\n enumerable: false,\n value: metadata,\n writable: false,\n })\n}\n\nexport function getStoreLoggerMetadata<TState>(\n store: Store<TState, any>,\n): StoreLoggerMetadata<TState> | undefined {\n return (\n store as Store<TState, any> & {\n [storeLoggerMetadataKey]?: StoreLoggerMetadata<TState>\n }\n )[storeLoggerMetadataKey]\n}\n\nexport function createSubscriptionLoggerId(): string {\n return `sub${nextSubscriptionId++}`\n}\n\nexport function shouldEmitStoreDebugEvent(\n configuredLevel: StoreDebugLevel,\n minimumLevel: StoreDebugLevel,\n): boolean {\n return debugLevelRank[configuredLevel] >= debugLevelRank[minimumLevel]\n}\n\nexport function emitStoreDebugEvent<TState>(\n store: Store<TState, any>,\n args: StoreDebugLogArgs<TState>,\n): StoreDebugEvent<TState> | null {\n const metadata = getStoreLoggerMetadata(store)\n\n if (!metadata?.options.enabled) {\n return null\n }\n\n const minimumLevel = args.minimumLevel ?? 'basic'\n\n if (!shouldEmitStoreDebugEvent(metadata.options.level, minimumLevel)) {\n return null\n }\n\n const event: StoreDebugEvent<TState> = {\n builderId: metadata.builderId,\n event: args.event,\n level: metadata.options.level,\n sequence: ++metadata.sequence,\n source: args.source,\n storeId: metadata.storeId,\n timestamp: Date.now(),\n }\n\n if (args.detail !== undefined) {\n event.detail = args.detail\n }\n\n if (args.error !== undefined) {\n event.error = args.error\n }\n\n if (args.status !== undefined) {\n event.status = args.status\n }\n\n if (args.subscriptionId !== undefined) {\n event.subscriptionId = args.subscriptionId\n }\n\n if (metadata.options.level === 'trace') {\n if (args.previousState !== undefined) {\n event.previousState = args.previousState\n }\n\n if (args.nextState !== undefined) {\n event.nextState = args.nextState\n }\n }\n\n if (metadata.options.console) {\n writeStoreDebugConsoleEvent(event)\n }\n\n if (metadata.options.sink) {\n try {\n metadata.options.sink(event)\n } catch (error) {\n console.error('[lunarhue/store][debug] sink failed', error)\n }\n }\n\n return event\n}\n\nexport function transitionStoreLifecycle<TState>(\n store: Store<TState, any>,\n lifecycleMeta: WritableReadableStore<StoreLifecycleMeta>,\n nextState: StoreLifecycleMeta,\n args?: {\n source?: string\n minimumLevel?: StoreDebugLevel\n detail?: Record<string, unknown>\n },\n): void {\n const previousState = lifecycleMeta.get()\n lifecycleMeta.setState(() => nextState)\n\n if (\n previousState.status === nextState.status &&\n Object.is(previousState.error, nextState.error)\n ) {\n return\n }\n\n emitStoreDebugEvent(store, {\n detail: args?.detail,\n error: nextState.error ?? undefined,\n event: 'store.lifecycle.changed',\n minimumLevel: args?.minimumLevel,\n source: args?.source ?? 'core',\n status: nextState.status,\n })\n}\n\nfunction resolveStoreDebugOptions<TState>(\n debug?: StoreDebugOptions<TState>,\n): ResolvedStoreDebugOptions<TState> {\n if (!debug) {\n return {\n console: false,\n enabled: false,\n level: 'basic',\n }\n }\n\n return {\n console: debug.console ?? true,\n enabled: true,\n level: debug.level ?? 'basic',\n sink: debug.sink,\n }\n}\n\nfunction writeStoreDebugConsoleEvent<TState>(\n event: StoreDebugEvent<TState>,\n): void {\n const method =\n event.error !== undefined ||\n event.event.endsWith('.error') ||\n event.event.endsWith('.failed')\n ? console.error\n : console.log\n\n method('[lunarhue/store][debug]', event)\n}\n","import type { Store, StoreBuilder } from './types'\n\nconst storeBuilderMap = new WeakMap<Store<any, any>, StoreBuilder<any, any>>()\n\nexport function registerStoreBuilder<TState, TPlugins>(\n store: Store<TState, TPlugins>,\n builder: StoreBuilder<TState, TPlugins>,\n): void {\n storeBuilderMap.set(store, builder)\n}\n\nexport function getStoreBuilder<TState, TPlugins>(\n store: Store<TState, TPlugins>,\n): StoreBuilder<TState, TPlugins> | undefined {\n return storeBuilderMap.get(store) as StoreBuilder<TState, TPlugins> | undefined\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getStoreBuilder } from './chunk-
|
|
1
|
+
import { emitStoreDebugEvent, transitionStoreLifecycle, getStoreBuilder } from './chunk-JB6XVML6.js';
|
|
2
2
|
import { useRef, useState, useEffectEvent, useEffect, useContext, createContext } from 'react';
|
|
3
3
|
import { jsx } from 'react/jsx-runtime';
|
|
4
4
|
import { useStore as useStore$1 } from '@tanstack/react-store';
|
|
@@ -18,6 +18,7 @@ function StoreProvider(props) {
|
|
|
18
18
|
BuilderOwnedStoreProvider,
|
|
19
19
|
{
|
|
20
20
|
builder: props.builder,
|
|
21
|
+
debug: props.debug,
|
|
21
22
|
hasInitialState: "initialState" in props,
|
|
22
23
|
initialState: props.initialState,
|
|
23
24
|
loadInitialState: props.loadInitialState,
|
|
@@ -27,12 +28,10 @@ function StoreProvider(props) {
|
|
|
27
28
|
}
|
|
28
29
|
return /* @__PURE__ */ jsx(ExternalStoreProvider, { store: props.store, children: props.children });
|
|
29
30
|
}
|
|
30
|
-
function updateLifecycleMeta(store, updater) {
|
|
31
|
-
store.lifecycle.meta.setState(updater);
|
|
32
|
-
}
|
|
33
31
|
function BuilderOwnedStoreProvider({
|
|
34
32
|
builder,
|
|
35
33
|
children,
|
|
34
|
+
debug,
|
|
36
35
|
hasInitialState,
|
|
37
36
|
initialState,
|
|
38
37
|
loadInitialState
|
|
@@ -53,26 +52,67 @@ function BuilderOwnedStoreProvider({
|
|
|
53
52
|
}
|
|
54
53
|
}
|
|
55
54
|
if (!storeRef.current) {
|
|
56
|
-
storeRef.current =
|
|
55
|
+
storeRef.current = builder.create(
|
|
56
|
+
hasInitialState ? initialState : void 0,
|
|
57
|
+
{ debug }
|
|
58
|
+
);
|
|
57
59
|
}
|
|
58
60
|
const initializeStore = useEffectEvent(async () => {
|
|
59
61
|
const ownedStore2 = storeRef.current;
|
|
60
62
|
if (!ownedStore2 || !loadInitialState) {
|
|
61
63
|
return;
|
|
62
64
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
65
|
+
emitStoreDebugEvent(ownedStore2, {
|
|
66
|
+
detail: {
|
|
67
|
+
initMode: "loadInitialState",
|
|
68
|
+
owner: "provider"
|
|
69
|
+
},
|
|
70
|
+
event: "provider.initialize.started",
|
|
71
|
+
source: "react"
|
|
72
|
+
});
|
|
73
|
+
transitionStoreLifecycle(
|
|
74
|
+
ownedStore2,
|
|
75
|
+
ownedStore2.lifecycle.meta,
|
|
76
|
+
{
|
|
77
|
+
status: "initializing",
|
|
78
|
+
error: null
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
source: "react"
|
|
82
|
+
}
|
|
83
|
+
);
|
|
68
84
|
try {
|
|
69
85
|
const nextState = await loadInitialState({ store: ownedStore2 });
|
|
70
86
|
await ownedStore2.setInitialState(nextState);
|
|
87
|
+
emitStoreDebugEvent(ownedStore2, {
|
|
88
|
+
detail: {
|
|
89
|
+
initMode: "loadInitialState",
|
|
90
|
+
owner: "provider"
|
|
91
|
+
},
|
|
92
|
+
event: "provider.initialize.completed",
|
|
93
|
+
source: "react"
|
|
94
|
+
});
|
|
71
95
|
} catch (error) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
96
|
+
emitStoreDebugEvent(ownedStore2, {
|
|
97
|
+
detail: {
|
|
98
|
+
initMode: "loadInitialState",
|
|
99
|
+
owner: "provider"
|
|
100
|
+
},
|
|
101
|
+
error,
|
|
102
|
+
event: "provider.initialize.failed",
|
|
103
|
+
source: "react"
|
|
104
|
+
});
|
|
105
|
+
transitionStoreLifecycle(
|
|
106
|
+
ownedStore2,
|
|
107
|
+
ownedStore2.lifecycle.meta,
|
|
108
|
+
{
|
|
109
|
+
status: "error",
|
|
110
|
+
error
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
source: "react"
|
|
114
|
+
}
|
|
115
|
+
);
|
|
76
116
|
}
|
|
77
117
|
});
|
|
78
118
|
useEffect(() => {
|
|
@@ -80,10 +120,28 @@ function BuilderOwnedStoreProvider({
|
|
|
80
120
|
if (!ownedStore2) {
|
|
81
121
|
return;
|
|
82
122
|
}
|
|
123
|
+
emitStoreDebugEvent(ownedStore2, {
|
|
124
|
+
detail: {
|
|
125
|
+
mode: "builder",
|
|
126
|
+
owner: "provider"
|
|
127
|
+
},
|
|
128
|
+
event: "provider.mount",
|
|
129
|
+
minimumLevel: "verbose",
|
|
130
|
+
source: "react"
|
|
131
|
+
});
|
|
83
132
|
const subscription = ownedStore2.lifecycle.meta.subscribe(() => {
|
|
84
133
|
forceRender((value) => value + 1);
|
|
85
134
|
});
|
|
86
135
|
return () => {
|
|
136
|
+
emitStoreDebugEvent(ownedStore2, {
|
|
137
|
+
detail: {
|
|
138
|
+
mode: "builder",
|
|
139
|
+
owner: "provider"
|
|
140
|
+
},
|
|
141
|
+
event: "provider.unmount",
|
|
142
|
+
minimumLevel: "verbose",
|
|
143
|
+
source: "react"
|
|
144
|
+
});
|
|
87
145
|
subscription.unsubscribe();
|
|
88
146
|
if (!ownedStore2) {
|
|
89
147
|
return;
|
|
@@ -125,6 +183,28 @@ function ExternalStoreProvider({
|
|
|
125
183
|
"StoreProvider could not resolve a builder for the provided store. Pass a store created by @lunarhue/store."
|
|
126
184
|
);
|
|
127
185
|
}
|
|
186
|
+
useEffect(() => {
|
|
187
|
+
emitStoreDebugEvent(store, {
|
|
188
|
+
detail: {
|
|
189
|
+
mode: "external",
|
|
190
|
+
owner: "provider"
|
|
191
|
+
},
|
|
192
|
+
event: "provider.mount",
|
|
193
|
+
minimumLevel: "verbose",
|
|
194
|
+
source: "react"
|
|
195
|
+
});
|
|
196
|
+
return () => {
|
|
197
|
+
emitStoreDebugEvent(store, {
|
|
198
|
+
detail: {
|
|
199
|
+
mode: "external",
|
|
200
|
+
owner: "provider"
|
|
201
|
+
},
|
|
202
|
+
event: "provider.unmount",
|
|
203
|
+
minimumLevel: "verbose",
|
|
204
|
+
source: "react"
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
}, [store]);
|
|
128
208
|
const context = getStoreContext(builder);
|
|
129
209
|
const content = typeof children === "function" ? children({ store }) : children;
|
|
130
210
|
return /* @__PURE__ */ jsx(context.Provider, { value: store, children: content });
|
|
@@ -132,25 +212,86 @@ function ExternalStoreProvider({
|
|
|
132
212
|
function useLocalStore(builder, options) {
|
|
133
213
|
const initializeStartedRef = useRef(false);
|
|
134
214
|
const localStoreRef = useRef(null);
|
|
215
|
+
const [, forceRender] = useState(0);
|
|
135
216
|
const hasInitialState = options !== void 0 && Object.prototype.hasOwnProperty.call(options, "initialState");
|
|
136
217
|
if (!localStoreRef.current) {
|
|
137
|
-
localStoreRef.current =
|
|
218
|
+
localStoreRef.current = builder.create(
|
|
219
|
+
hasInitialState ? options?.initialState : void 0,
|
|
220
|
+
{ debug: options?.debug }
|
|
221
|
+
);
|
|
138
222
|
}
|
|
139
223
|
const initializeStore = useEffectEvent(async () => {
|
|
140
224
|
const localStore = localStoreRef.current;
|
|
141
225
|
if (!localStore || !options?.loadInitialState) {
|
|
142
226
|
return;
|
|
143
227
|
}
|
|
144
|
-
|
|
145
|
-
|
|
228
|
+
emitStoreDebugEvent(localStore, {
|
|
229
|
+
detail: {
|
|
230
|
+
initMode: "loadInitialState",
|
|
231
|
+
owner: "local"
|
|
232
|
+
},
|
|
233
|
+
event: "local.initialize.started",
|
|
234
|
+
source: "react"
|
|
235
|
+
});
|
|
236
|
+
transitionStoreLifecycle(
|
|
237
|
+
localStore,
|
|
238
|
+
localStore.lifecycle.meta,
|
|
239
|
+
{
|
|
240
|
+
status: "initializing",
|
|
241
|
+
error: null
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
source: "react"
|
|
245
|
+
}
|
|
246
|
+
);
|
|
247
|
+
try {
|
|
248
|
+
const nextState = await options.loadInitialState({ store: localStore });
|
|
249
|
+
await localStore.setInitialState(nextState);
|
|
250
|
+
emitStoreDebugEvent(localStore, {
|
|
251
|
+
detail: {
|
|
252
|
+
initMode: "loadInitialState",
|
|
253
|
+
owner: "local"
|
|
254
|
+
},
|
|
255
|
+
event: "local.initialize.completed",
|
|
256
|
+
source: "react"
|
|
257
|
+
});
|
|
258
|
+
} catch (error) {
|
|
259
|
+
emitStoreDebugEvent(localStore, {
|
|
260
|
+
detail: {
|
|
261
|
+
initMode: "loadInitialState",
|
|
262
|
+
owner: "local"
|
|
263
|
+
},
|
|
264
|
+
error,
|
|
265
|
+
event: "local.initialize.failed",
|
|
266
|
+
source: "react"
|
|
267
|
+
});
|
|
268
|
+
transitionStoreLifecycle(
|
|
269
|
+
localStore,
|
|
270
|
+
localStore.lifecycle.meta,
|
|
271
|
+
{
|
|
272
|
+
status: "error",
|
|
273
|
+
error
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
source: "react"
|
|
277
|
+
}
|
|
278
|
+
);
|
|
279
|
+
}
|
|
146
280
|
});
|
|
147
281
|
useEffect(() => {
|
|
148
282
|
const localStore = localStoreRef.current;
|
|
283
|
+
if (!localStore) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
const subscription = localStore.lifecycle.meta.subscribe(() => {
|
|
287
|
+
forceRender((value) => value + 1);
|
|
288
|
+
});
|
|
149
289
|
if (localStore && !initializeStartedRef.current && localStore.lifecycle.meta.get().status === "uninitialized" && options?.loadInitialState) {
|
|
150
290
|
initializeStartedRef.current = true;
|
|
151
291
|
void initializeStore();
|
|
152
292
|
}
|
|
153
293
|
return () => {
|
|
294
|
+
subscription.unsubscribe();
|
|
154
295
|
if (!localStore) {
|
|
155
296
|
return;
|
|
156
297
|
}
|
|
@@ -183,5 +324,5 @@ function useStoreSelector(builder, selector, compare) {
|
|
|
183
324
|
}
|
|
184
325
|
|
|
185
326
|
export { StoreProvider, useLocalStore, useSelector, useStore, useStoreSelector };
|
|
186
|
-
//# sourceMappingURL=chunk-
|
|
187
|
-
//# sourceMappingURL=chunk-
|
|
327
|
+
//# sourceMappingURL=chunk-SOWFT4FH.js.map
|
|
328
|
+
//# sourceMappingURL=chunk-SOWFT4FH.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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":["ownedStore","useRef","useState","useEffectEvent","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;ACsEO,SAAS,cACd,KAAA,EACA;AACA,EAAA,IAAI,KAAA,CAAM,YAAY,MAAA,EAAW;AAC/B,IAAA,uBACE,GAAA;AAAA,MAAC,yBAAA;AAAA,MAAA;AAAA,QACC,SAAS,KAAA,CAAM,OAAA;AAAA,QACf,OAAO,KAAA,CAAM,KAAA;AAAA,QACb,iBAAiB,cAAA,IAAkB,KAAA;AAAA,QACnC,cAAc,KAAA,CAAM,YAAA;AAAA,QACpB,kBAAkB,KAAA,CAAM,gBAAA;AAAA,QAEvB,QAAA,EAAA,KAAA,CAAM;AAAA;AAAA,KACT;AAAA,EAEJ;AAEA,EAAA,2BACG,qBAAA,EAAA,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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-SOWFT4FH.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 {\n useEffect,\n useEffectEvent,\n useRef,\n useState,\n type ReactNode,\n} from 'react'\n\nimport { emitStoreDebugEvent, transitionStoreLifecycle } from '../core/logger'\nimport { getStoreBuilder } from '../core/builder-registry'\nimport { getStoreContext } from './context'\n\nimport type {\n Store,\n StoreBuilder,\n StoreDebugOptions,\n StoreInitialStateLoader,\n StoreLifecycleMeta,\n} from '../core'\n\ntype StoreProviderChildren<TState, TPlugins> =\n | ReactNode\n | ((args: { store: Store<TState, TPlugins> }) => ReactNode)\n\ntype BuilderProviderBaseProps<TState, TPlugins> = {\n builder: StoreBuilder<TState, TPlugins>\n children?: StoreProviderChildren<TState, TPlugins>\n debug?: StoreDebugOptions<TState>\n hasInitialState?: boolean\n store?: never\n}\n\ntype BuilderProviderWithInitialStateProps<TState, TPlugins> =\n BuilderProviderBaseProps<TState, TPlugins> & {\n initialState: TState\n loadInitialState?: never\n }\n\ntype BuilderProviderWithLoadInitialStateProps<TState, TPlugins> =\n BuilderProviderBaseProps<TState, TPlugins> & {\n initialState?: never\n loadInitialState: StoreInitialStateLoader<TState, TPlugins>\n }\n\ntype BuilderProviderWithDeclaredInitialStateProps<TState, TPlugins> =\n BuilderProviderBaseProps<TState, TPlugins> & {\n initialState?: never\n loadInitialState?: never\n }\n\ntype BuilderProviderProps<TState, TPlugins> =\n | BuilderProviderWithDeclaredInitialStateProps<TState, TPlugins>\n | BuilderProviderWithInitialStateProps<TState, TPlugins>\n | BuilderProviderWithLoadInitialStateProps<TState, TPlugins>\n\ntype BuilderOwnedStoreProviderProps<TState, TPlugins> = {\n builder: StoreBuilder<TState, TPlugins>\n children?: StoreProviderChildren<TState, TPlugins>\n debug?: StoreDebugOptions<TState>\n hasInitialState: boolean\n initialState?: TState\n loadInitialState?: StoreInitialStateLoader<TState, TPlugins>\n}\n\ntype StoreProviderProps<TState, TPlugins> = {\n builder?: never\n children?: StoreProviderChildren<TState, TPlugins>\n store: Store<TState, TPlugins>\n}\n\n/**\n * Props for {@link StoreProvider}.\n *\n * Use either `builder` to let the provider create and own the runtime store,\n * or `store` to provide an already-created runtime store. When a builder has\n * no declared initial state, the builder-owned form must also provide either\n * `initialState` or `loadInitialState`.\n */\nexport type ProviderProps<TState, TPlugins = {}> =\n | BuilderProviderProps<TState, TPlugins>\n | StoreProviderProps<TState, TPlugins>\n\n/**\n * Provides a builder-scoped runtime store to React descendants.\n *\n * Builder-owned providers create and dispose the runtime store automatically.\n * External-store providers reuse an existing runtime store and do not own its\n * disposal. The `builder` prop must remain stable for the provider lifetime.\n * Children may be plain JSX or a render prop that receives the resolved\n * runtime store instance.\n */\nexport function StoreProvider<TState, TPlugins>(\n props: ProviderProps<TState, TPlugins>,\n) {\n if (props.builder !== undefined) {\n return (\n <BuilderOwnedStoreProvider\n builder={props.builder}\n debug={props.debug}\n hasInitialState={'initialState' in props}\n initialState={props.initialState}\n loadInitialState={props.loadInitialState}\n >\n {props.children}\n </BuilderOwnedStoreProvider>\n )\n }\n\n return (\n <ExternalStoreProvider store={props.store}>\n {props.children}\n </ExternalStoreProvider>\n )\n}\n\n// Native and Expo declare this globally in development mode.\ndeclare const __DEV__: boolean\n\nfunction BuilderOwnedStoreProvider<TState, TPlugins>({\n builder,\n children,\n debug,\n hasInitialState,\n initialState,\n loadInitialState,\n}: BuilderOwnedStoreProviderProps<TState, TPlugins>) {\n const context = getStoreContext(builder)\n const builderRef = useRef<StoreBuilder<TState, TPlugins> | null>(null)\n const initializeStartedRef = useRef(false)\n const storeRef = useRef<Store<TState, TPlugins> | null>(null)\n const [, forceRender] = useState(0)\n\n if (!builderRef.current) {\n builderRef.current = builder\n } else if (builderRef.current !== builder) {\n const isDev =\n process.env.NODE_ENV !== 'production' ||\n (typeof __DEV__ !== 'undefined' && __DEV__)\n\n if (isDev) {\n console.warn('StoreProvider builder prop must remain stable.')\n } else {\n throw new Error('StoreProvider builder prop must remain stable.')\n }\n }\n\n if (!storeRef.current) {\n storeRef.current = builder.create(\n hasInitialState ? (initialState as TState) : undefined,\n { debug },\n )\n }\n\n const initializeStore = useEffectEvent(async () => {\n const ownedStore = storeRef.current\n\n if (!ownedStore || !loadInitialState) {\n return\n }\n\n emitStoreDebugEvent(ownedStore, {\n detail: {\n initMode: 'loadInitialState',\n owner: 'provider',\n },\n event: 'provider.initialize.started',\n source: 'react',\n })\n transitionStoreLifecycle(\n ownedStore,\n ownedStore.lifecycle.meta as unknown as WritableReadableStore<StoreLifecycleMeta>,\n {\n status: 'initializing',\n error: null,\n },\n {\n source: 'react',\n },\n )\n\n try {\n const nextState = await loadInitialState({ store: ownedStore })\n await ownedStore.setInitialState(nextState)\n emitStoreDebugEvent(ownedStore, {\n detail: {\n initMode: 'loadInitialState',\n owner: 'provider',\n },\n event: 'provider.initialize.completed',\n source: 'react',\n })\n } catch (error) {\n emitStoreDebugEvent(ownedStore, {\n detail: {\n initMode: 'loadInitialState',\n owner: 'provider',\n },\n error,\n event: 'provider.initialize.failed',\n source: 'react',\n })\n transitionStoreLifecycle(\n ownedStore,\n ownedStore.lifecycle.meta as unknown as WritableReadableStore<StoreLifecycleMeta>,\n {\n status: 'error',\n error,\n },\n {\n source: 'react',\n },\n )\n }\n })\n\n useEffect(() => {\n const ownedStore = storeRef.current\n\n if (!ownedStore) {\n return\n }\n\n emitStoreDebugEvent(ownedStore, {\n detail: {\n mode: 'builder',\n owner: 'provider',\n },\n event: 'provider.mount',\n minimumLevel: 'verbose',\n source: 'react',\n })\n\n const subscription = ownedStore.lifecycle.meta.subscribe(() => {\n forceRender((value) => value + 1)\n })\n\n return () => {\n emitStoreDebugEvent(ownedStore, {\n detail: {\n mode: 'builder',\n owner: 'provider',\n },\n event: 'provider.unmount',\n minimumLevel: 'verbose',\n source: 'react',\n })\n subscription.unsubscribe()\n\n if (!ownedStore) {\n return\n }\n\n void ownedStore.dispose()\n }\n }, [])\n\n useEffect(() => {\n const ownedStore = storeRef.current\n\n if (\n !ownedStore ||\n initializeStartedRef.current ||\n ownedStore.lifecycle.meta.get().status !== 'uninitialized' ||\n !loadInitialState\n ) {\n return\n }\n\n initializeStartedRef.current = true\n void initializeStore()\n }, [loadInitialState, initializeStore])\n\n const ownedStore = storeRef.current\n const lifecycleMeta = ownedStore.lifecycle.meta.get()\n\n if (lifecycleMeta.status === 'error') {\n throw lifecycleMeta.error ?? new Error('Store initialization failed.')\n }\n\n if (lifecycleMeta.status === 'uninitialized' && !loadInitialState) {\n throw new Error(\n 'StoreProvider requires initialState or loadInitialState when the builder has no declared initial state.',\n )\n }\n\n if (\n lifecycleMeta.status === 'uninitialized' ||\n lifecycleMeta.status === 'initializing'\n ) {\n return <context.Provider value={ownedStore}>{null}</context.Provider>\n }\n\n const content =\n typeof children === 'function'\n ? children({ store: ownedStore })\n : children\n\n return <context.Provider value={ownedStore}>{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 useEffect(() => {\n emitStoreDebugEvent(store, {\n detail: {\n mode: 'external',\n owner: 'provider',\n },\n event: 'provider.mount',\n minimumLevel: 'verbose',\n source: 'react',\n })\n\n return () => {\n emitStoreDebugEvent(store, {\n detail: {\n mode: 'external',\n owner: 'provider',\n },\n event: 'provider.unmount',\n minimumLevel: 'verbose',\n source: 'react',\n })\n }\n }, [store])\n\n const context = getStoreContext(builder)\n const content =\n typeof children === 'function' ? children({ store }) : children\n\n return <context.Provider value={store}>{content}</context.Provider>\n}\n\ntype WritableReadableStore<TState> = {\n get(): TState\n setState(updater: (prev: TState) => TState): void\n}\n","import { useEffect, useEffectEvent, useRef, useState } from 'react'\n\nimport { emitStoreDebugEvent, transitionStoreLifecycle } from '../core/logger'\nimport type { Store, StoreBuilder, StoreInitialStateLoader } from '../core'\nimport type { StoreDebugOptions, StoreLifecycleMeta } from '../core'\n\ntype LocalStoreOptionsBase<TState> = {\n debug?: StoreDebugOptions<TState>\n}\n\ntype LocalStoreWithInitialStateOptions<TState> = LocalStoreOptionsBase<TState> & {\n initialState: TState\n loadInitialState?: never\n}\n\ntype LocalStoreWithLoadInitialStateOptions<TState, TPlugins> =\n LocalStoreOptionsBase<TState> & {\n initialState?: never\n loadInitialState: StoreInitialStateLoader<TState, TPlugins>\n }\n\ntype LocalStoreDeclaredInitialStateOptions<TState> = LocalStoreOptionsBase<TState> & {\n initialState?: never\n loadInitialState?: never\n}\n\ntype LocalStoreOptions<TState, TPlugins> =\n | LocalStoreDeclaredInitialStateOptions<TState>\n | LocalStoreWithInitialStateOptions<TState>\n | LocalStoreWithLoadInitialStateOptions<TState, TPlugins>\n\n/**\n * Creates and owns a local runtime store for the current hook call site.\n *\n * The returned store is disposed on unmount. Pass `initialState` to start the\n * local store ready immediately, or `loadInitialState` to initialize a builder\n * that was declared without a default value. Without a declared default or one\n * of those options, the returned runtime store begins uninitialized.\n */\nexport function useLocalStore<TState, TPlugins>(\n builder: StoreBuilder<TState, TPlugins>,\n options?: LocalStoreOptions<TState, TPlugins>,\n): Store<TState, TPlugins> {\n const initializeStartedRef = useRef(false)\n const localStoreRef = useRef<Store<TState, TPlugins> | null>(null)\n const [, forceRender] = useState(0)\n const hasInitialState =\n options !== undefined &&\n Object.prototype.hasOwnProperty.call(options, 'initialState')\n\n if (!localStoreRef.current) {\n localStoreRef.current = builder.create(\n hasInitialState ? (options?.initialState as TState) : undefined,\n { debug: options?.debug },\n )\n }\n\n const initializeStore = useEffectEvent(async () => {\n const localStore = localStoreRef.current\n\n if (!localStore || !options?.loadInitialState) {\n return\n }\n\n emitStoreDebugEvent(localStore, {\n detail: {\n initMode: 'loadInitialState',\n owner: 'local',\n },\n event: 'local.initialize.started',\n source: 'react',\n })\n transitionStoreLifecycle(\n localStore,\n localStore.lifecycle.meta as unknown as WritableReadableStore<StoreLifecycleMeta>,\n {\n status: 'initializing',\n error: null,\n },\n {\n source: 'react',\n },\n )\n\n try {\n const nextState = await options.loadInitialState({ store: localStore })\n await localStore.setInitialState(nextState)\n emitStoreDebugEvent(localStore, {\n detail: {\n initMode: 'loadInitialState',\n owner: 'local',\n },\n event: 'local.initialize.completed',\n source: 'react',\n })\n } catch (error) {\n emitStoreDebugEvent(localStore, {\n detail: {\n initMode: 'loadInitialState',\n owner: 'local',\n },\n error,\n event: 'local.initialize.failed',\n source: 'react',\n })\n transitionStoreLifecycle(\n localStore,\n localStore.lifecycle.meta as unknown as WritableReadableStore<StoreLifecycleMeta>,\n {\n status: 'error',\n error,\n },\n {\n source: 'react',\n },\n )\n }\n })\n\n useEffect(() => {\n const localStore = localStoreRef.current\n\n if (!localStore) {\n return\n }\n\n const subscription = localStore.lifecycle.meta.subscribe(() => {\n forceRender((value) => value + 1)\n })\n\n if (\n localStore &&\n !initializeStartedRef.current &&\n localStore.lifecycle.meta.get().status === 'uninitialized' &&\n options?.loadInitialState\n ) {\n initializeStartedRef.current = true\n void initializeStore()\n }\n\n return () => {\n subscription.unsubscribe()\n\n if (!localStore) {\n return\n }\n\n void localStore.dispose()\n }\n }, [initializeStore, options?.loadInitialState])\n\n return localStoreRef.current\n}\n\ntype WritableReadableStore<TState> = {\n get(): TState\n setState(updater: (prev: TState) => TState): void\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\n/**\n * Subscribes to a selected slice of a runtime store instance.\n *\n * Use this when you already have a runtime store object. For builder-scoped\n * selection through context, use {@link useStoreSelector}.\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\n/**\n * Returns the runtime store provided for a builder.\n *\n * This hook requires a matching {@link StoreProvider} ancestor using either\n * the same builder or a runtime store created from that builder.\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\n/**\n * Convenience hook that resolves a builder-scoped runtime store from context\n * and subscribes to a selected slice of its state.\n *\n * This has the same matching-provider requirement as {@link useStore}.\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"]}
|