@lunarhue/store 0.3.0 → 0.3.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.
|
@@ -36,19 +36,27 @@ function createStoreInstance({
|
|
|
36
36
|
const lifecycle = {
|
|
37
37
|
meta: lifecycleMeta
|
|
38
38
|
};
|
|
39
|
-
const subscribe = (
|
|
39
|
+
const subscribe = (observerOrFn, error, complete) => {
|
|
40
40
|
const observer = typeof observerOrFn === "function" ? {
|
|
41
41
|
next: observerOrFn,
|
|
42
42
|
error,
|
|
43
43
|
complete
|
|
44
44
|
} : observerOrFn;
|
|
45
|
-
return nativeSubscribe(
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
return nativeSubscribe({
|
|
46
|
+
next(value) {
|
|
47
|
+
if (value === UNINITIALIZED) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
observer.next?.(value);
|
|
51
|
+
},
|
|
52
|
+
error(err) {
|
|
53
|
+
observer.error?.(err);
|
|
54
|
+
},
|
|
55
|
+
complete() {
|
|
56
|
+
observer.complete?.();
|
|
48
57
|
}
|
|
49
|
-
observer.next?.(value);
|
|
50
58
|
});
|
|
51
|
-
}
|
|
59
|
+
};
|
|
52
60
|
Object.defineProperty(instance, "dispose", {
|
|
53
61
|
configurable: true,
|
|
54
62
|
enumerable: true,
|
|
@@ -129,5 +137,5 @@ function createStoreInstance({
|
|
|
129
137
|
}
|
|
130
138
|
|
|
131
139
|
export { createStoreInstance };
|
|
132
|
-
//# sourceMappingURL=chunk-
|
|
133
|
-
//# sourceMappingURL=chunk-
|
|
140
|
+
//# sourceMappingURL=chunk-TCEPLIBH.js.map
|
|
141
|
+
//# sourceMappingURL=chunk-TCEPLIBH.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/store-instance.ts"],"names":["createTanStackStore"],"mappings":";;;AAyBA,IAAM,aAAA,0BAAuB,8BAA8B,CAAA;AAEpD,SAAS,mB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KAAA,CAAM,CAAA,mCAAA,EAAsC,MAAA,CAAO,GAAG,CAAC,CAAA,EAAA,CAAI,CAAA;AAAA,QACvE;AAAA,MACF;AAEA,MAAA,MAAA,CAAO,gBAAA;AAAA,QACL,QAAA;AAAA,QACA,MAAA,CAAO,0BAA0B,OAAO;AAAA,OAC1C;AAAA,IACF;AAAA,GACF;AACF","file":"chunk-TCEPLIBH.js","sourcesContent":["import {\n createStore as createTanStackStore,\n type Observer,\n type Store as BaseStore,\n type Subscription,\n} from '@tanstack/store'\n\nimport type {\n StoreCleanup,\n Store,\n StoreLifecycleMeta,\n ReadableStore,\n} from './types'\n\ntype StoreController<TState> = {\n store: Store<TState>\n onDispose(cleanup: StoreCleanup): void\n attachSurface(surface: object): void\n}\n\ntype CreateStoreInstanceOptions<TState> = {\n initialState?: TState\n readyOnCreate: boolean\n}\n\nconst UNINITIALIZED = Symbol('lunarhue.store.uninitialized')\n\nexport function createStoreInstance<TState>({\n initialState,\n readyOnCreate,\n}: CreateStoreInstanceOptions<TState>): StoreController<TState> {\n const store = createTanStackStore<TState | typeof UNINITIALIZED>(\n readyOnCreate ? (initialState as TState) : UNINITIALIZED,\n )\n const lifecycleMeta = createTanStackStore<StoreLifecycleMeta>({\n status: readyOnCreate ? 'ready' : 'uninitialized',\n error: null,\n })\n const cleanups: StoreCleanup[] = []\n let disposePromise: Promise<void> | null = null\n\n const nativeStateGetter = Object.getOwnPropertyDescriptor(\n Object.getPrototypeOf(store),\n 'state',\n )?.get\n\n if (!nativeStateGetter) {\n throw new Error('Failed to resolve the TanStack Store state getter.')\n }\n\n const nativeGet = () => nativeStateGetter.call(store)\n const nativeSetState = store.setState.bind(store)\n const nativeSubscribe = store.subscribe.bind(store)\n const instance = store as unknown as Store<TState>\n\n const assertReady = (value: TState | typeof UNINITIALIZED): TState => {\n if (value === UNINITIALIZED) {\n throw new Error('Store has not been initialized.')\n }\n\n return value\n }\n\n const getState = () => assertReady(nativeGet())\n const lifecycle = {\n meta: lifecycleMeta as ReadableStore<StoreLifecycleMeta>,\n }\n const subscribe = (\n observerOrFn: Observer<TState> | ((value: TState) => void),\n error?: (error: any) => void,\n complete?: () => void,\n ): Subscription => {\n const observer =\n typeof observerOrFn === 'function'\n ? {\n next: observerOrFn,\n error,\n complete,\n }\n : observerOrFn\n\n return nativeSubscribe({\n next(value) {\n if (value === UNINITIALIZED) {\n return\n }\n\n observer.next?.(value)\n },\n error(err) {\n observer.error?.(err)\n },\n complete() {\n observer.complete?.()\n },\n })\n }\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 Object.defineProperties(instance, {\n get: {\n configurable: true,\n enumerable: true,\n value: () => getState(),\n },\n state: {\n configurable: true,\n enumerable: true,\n get() {\n return getState()\n },\n },\n setState: {\n configurable: true,\n enumerable: true,\n value: (updater: (prev: TState) => TState) => {\n nativeSetState((prev) => updater(assertReady(prev)))\n },\n },\n subscribe: {\n configurable: true,\n enumerable: true,\n value: subscribe,\n },\n setInitialState: {\n configurable: true,\n enumerable: true,\n value: async (nextState: TState) => {\n if (nativeGet() !== UNINITIALIZED) {\n throw new Error('Store initial state has already been set.')\n }\n\n nativeSetState(() => nextState)\n lifecycleMeta.setState(() => ({\n status: 'ready',\n error: null,\n }))\n },\n },\n lifecycle: {\n configurable: true,\n enumerable: true,\n value: lifecycle,\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/core/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createStoreInstance } from '../../chunk-
|
|
1
|
+
import { createStoreInstance } from '../../chunk-TCEPLIBH.js';
|
|
2
2
|
import { StoreProvider } from '../../chunk-GHGDIRN2.js';
|
|
3
3
|
import { getStoreBuilder } from '../../chunk-PCSRXZL4.js';
|
|
4
4
|
import { useContext, createContext, useEffectEvent, useEffect } from 'react';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunarhue/store",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
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",
|
|
@@ -49,7 +49,9 @@
|
|
|
49
49
|
"build": "tsup",
|
|
50
50
|
"dev": "tsup --watch",
|
|
51
51
|
"contract": "bun run build && node ./scripts/check-package-contract.mjs",
|
|
52
|
-
"test": "
|
|
52
|
+
"test": "bun run test:web && bun run test:native",
|
|
53
|
+
"test:web": "vitest run",
|
|
54
|
+
"test:native": "jest --config ./jest.native.config.cjs",
|
|
53
55
|
"typecheck": "tsc --noEmit",
|
|
54
56
|
"check": "bun run typecheck && bun run test && bun run contract"
|
|
55
57
|
},
|
|
@@ -58,17 +60,25 @@
|
|
|
58
60
|
"@tanstack/store": "0.9.2"
|
|
59
61
|
},
|
|
60
62
|
"peerDependencies": {
|
|
61
|
-
"react": "^19.
|
|
63
|
+
"react": "^19.2.4"
|
|
62
64
|
},
|
|
63
65
|
"devDependencies": {
|
|
66
|
+
"@swc/core": "1.15.18",
|
|
67
|
+
"@swc/jest": "0.2.39",
|
|
64
68
|
"@testing-library/react": "^16.3.0",
|
|
65
|
-
"@
|
|
66
|
-
"@types/react
|
|
67
|
-
"
|
|
68
|
-
"react": "^19.
|
|
69
|
-
"
|
|
69
|
+
"@testing-library/react-native": "^13.3.3",
|
|
70
|
+
"@types/react": "^19.2.4",
|
|
71
|
+
"@types/react-dom": "^19.2.3",
|
|
72
|
+
"@types/react-native-web": "^0.19.2",
|
|
73
|
+
"jest": "30.3.0",
|
|
74
|
+
"jest-environment-jsdom": "30.3.0",
|
|
75
|
+
"jsdom": "^29.0.0",
|
|
76
|
+
"react": "19.2.4",
|
|
77
|
+
"react-dom": "19.2.4",
|
|
78
|
+
"react-native-web": "0.21.2",
|
|
79
|
+
"react-test-renderer": "19.2.4",
|
|
70
80
|
"tsup": "^8.5.0",
|
|
71
81
|
"typescript": "^5.9.2",
|
|
72
|
-
"vitest": "^
|
|
82
|
+
"vitest": "^4.1.0"
|
|
73
83
|
}
|
|
74
84
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/core/store-instance.ts"],"names":["createTanStackStore"],"mappings":";;;AAyBA,IAAM,aAAA,0BAAuB,8BAA8B,CAAA;AAEpD,SAAS,mBAAA,CAA4B;AAAA,EAC1C,YAAA;AAAA,EACA;AACF,CAAA,EAAgE;AAC9D,EAAA,MAAM,KAAA,GAAQA,WAAA;AAAA,IACZ,gBAAiB,YAAA,GAA0B;AAAA,GAC7C;AACA,EAAA,MAAM,gBAAgBA,WAAA,CAAwC;AAAA,IAC5D,MAAA,EAAQ,gBAAgB,OAAA,GAAU,eAAA;AAAA,IAClC,KAAA,EAAO;AAAA,GACR,CAAA;AACD,EAAA,MAAM,WAA2B,EAAC;AAClC,EAAA,IAAI,cAAA,GAAuC,IAAA;AAE3C,EAAA,MAAM,oBAAoB,MAAA,CAAO,wBAAA;AAAA,IAC/B,MAAA,CAAO,eAAe,KAAK,CAAA;AAAA,IAC3B;AAAA,GACF,EAAG,GAAA;AAEH,EAAA,IAAI,CAAC,iBAAA,EAAmB;AACtB,IAAA,MAAM,IAAI,MAAM,oDAAoD,CAAA;AAAA,EACtE;AAEA,EAAA,MAAM,SAAA,GAAY,MAAM,iBAAA,CAAkB,IAAA,CAAK,KAAK,CAAA;AACpD,EAAA,MAAM,cAAA,GA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c,IAAA;AAAA,MACd,UAAA,EAAY,IAAA;AAAA,MACZ,KAAA,EAAO;AAAA;AACT,GACD,CAAA;AAED,EAAA,OAAO;AAAA,IACL,KAAA,EAAO,QAAA;AAAA,IACP,UAAU,OAAA,EAAS;AACjB,MAAA,QAAA,CAAS,KAAK,OAAO,CAAA;AAAA,IACvB,CAAA;AAAA,IACA,cAAc,OAAA,EAAS;AACrB,MAAA,KAAA,MAAW,GAAA,IAAO,OAAA,CAAQ,OAAA,CAAQ,OAAO,CAAA,EAAG;AAC1C,QAAA,IAAI,OAAO,QAAA,EAAU;AACnB,UAAA,MAAM,IAAI,KAAA,CAAM,CAAA,mCAAA,EAAsC,MAAA,CAAO,GAAG,CAAC,CAAA,EAAA,CAAI,CAAA;AAAA,QACvE;AAAA,MACF;AAEA,MAAA,MAAA,CAAO,gBAAA;AAAA,QACL,QAAA;AAAA,QACA,MAAA,CAAO,0BAA0B,OAAO;AAAA,OAC1C;AAAA,IACF;AAAA,GACF;AACF","file":"chunk-C5U3Q6O3.js","sourcesContent":["import {\n createStore as createTanStackStore,\n type Observer,\n type Store as BaseStore,\n type Subscription,\n} from '@tanstack/store'\n\nimport type {\n StoreCleanup,\n Store,\n StoreLifecycleMeta,\n ReadableStore,\n} from './types'\n\ntype StoreController<TState> = {\n store: Store<TState>\n onDispose(cleanup: StoreCleanup): void\n attachSurface(surface: object): void\n}\n\ntype CreateStoreInstanceOptions<TState> = {\n initialState?: TState\n readyOnCreate: boolean\n}\n\nconst UNINITIALIZED = Symbol('lunarhue.store.uninitialized')\n\nexport function createStoreInstance<TState>({\n initialState,\n readyOnCreate,\n}: CreateStoreInstanceOptions<TState>): StoreController<TState> {\n const store = createTanStackStore<TState | typeof UNINITIALIZED>(\n readyOnCreate ? (initialState as TState) : UNINITIALIZED,\n )\n const lifecycleMeta = createTanStackStore<StoreLifecycleMeta>({\n status: readyOnCreate ? 'ready' : 'uninitialized',\n error: null,\n })\n const cleanups: StoreCleanup[] = []\n let disposePromise: Promise<void> | null = null\n\n const nativeStateGetter = Object.getOwnPropertyDescriptor(\n Object.getPrototypeOf(store),\n 'state',\n )?.get\n\n if (!nativeStateGetter) {\n throw new Error('Failed to resolve the TanStack Store state getter.')\n }\n\n const nativeGet = () => nativeStateGetter.call(store)\n const nativeSetState = store.setState.bind(store)\n const nativeSubscribe = store.subscribe.bind(store)\n const instance = store as unknown as Store<TState>\n\n const assertReady = (value: TState | typeof UNINITIALIZED): TState => {\n if (value === UNINITIALIZED) {\n throw new Error('Store has not been initialized.')\n }\n\n return value\n }\n\n const getState = () => assertReady(nativeGet())\n const lifecycle = {\n meta: lifecycleMeta as ReadableStore<StoreLifecycleMeta>,\n }\n const subscribe = ((\n observerOrFn: Observer<TState> | ((value: TState) => void),\n error?: (error: any) => void,\n complete?: () => void,\n ): Subscription => {\n const observer =\n typeof observerOrFn === 'function'\n ? {\n next: observerOrFn,\n error,\n complete,\n }\n : observerOrFn\n\n return nativeSubscribe((value) => {\n if (value === UNINITIALIZED) {\n return\n }\n\n observer.next?.(value)\n })\n }) as BaseStore<TState>['subscribe']\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 Object.defineProperties(instance, {\n get: {\n configurable: true,\n enumerable: true,\n value: () => getState(),\n },\n state: {\n configurable: true,\n enumerable: true,\n get() {\n return getState()\n },\n },\n setState: {\n configurable: true,\n enumerable: true,\n value: (updater: (prev: TState) => TState) => {\n nativeSetState((prev) => updater(assertReady(prev)))\n },\n },\n subscribe: {\n configurable: true,\n enumerable: true,\n value: subscribe,\n },\n setInitialState: {\n configurable: true,\n enumerable: true,\n value: async (nextState: TState) => {\n if (nativeGet() !== UNINITIALIZED) {\n throw new Error('Store initial state has already been set.')\n }\n\n nativeSetState(() => nextState)\n lifecycleMeta.setState(() => ({\n status: 'ready',\n error: null,\n }))\n },\n },\n lifecycle: {\n configurable: true,\n enumerable: true,\n value: lifecycle,\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"]}
|