@tanstack/solid-store
Advanced tools
| import type { AnyUpdater, Store } from '@tanstack/store'; | ||
| import type { Accessor } from 'solid-js'; | ||
| export * from '@tanstack/store'; | ||
| export type NoInfer<T> = [T][T extends any ? 0 : never]; | ||
| export declare function useStore<TState, TSelected = NoInfer<TState>, TUpdater extends AnyUpdater = AnyUpdater>(store: Store<TState, TUpdater>, selector?: (state: NoInfer<TState>) => TSelected): Accessor<TSelected>; |
| import { onCleanup } from 'solid-js'; | ||
| import { createStore, reconcile } from 'solid-js/store'; | ||
| export * from '@tanstack/store'; | ||
| export function useStore(store, selector = (d) => d) { | ||
| const [slice, setSlice] = createStore({ | ||
| value: selector(store.state), | ||
| }); | ||
| const unsub = store.subscribe(() => { | ||
| const newValue = selector(store.state); | ||
| setSlice('value', reconcile(newValue)); | ||
| }); | ||
| onCleanup(() => { | ||
| unsub(); | ||
| }); | ||
| return () => slice.value; | ||
| } | ||
| //# sourceMappingURL=index.js.map |
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AACpC,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAIvD,cAAc,iBAAiB,CAAA;AAI/B,MAAM,UAAU,QAAQ,CAKtB,KAA8B,EAC9B,WAAkD,CAAC,CAAC,EAAE,EAAE,CAAC,CAAQ;IAEjE,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,WAAW,CAAC;QACpC,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC;KAC7B,CAAC,CAAA;IAEF,MAAM,KAAK,GAAG,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACjC,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;QACtC,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAA;IACxC,CAAC,CAAC,CAAA;IAEF,SAAS,CAAC,GAAG,EAAE;QACb,KAAK,EAAE,CAAA;IACT,CAAC,CAAC,CAAA;IAEF,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC,KAAK,CAAA;AAC1B,CAAC"} |
+11
-11
| { | ||
| "name": "@tanstack/solid-store", | ||
| "author": "Tanner Linsley", | ||
| "version": "0.5.2", | ||
| "version": "0.5.3", | ||
| "license": "MIT", | ||
@@ -26,14 +26,14 @@ "repository": "tanstack/store", | ||
| "type": "module", | ||
| "types": "dist/esm/index.d.ts", | ||
| "main": "dist/cjs/index.cjs", | ||
| "module": "dist/esm/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "main": "dist/index.js", | ||
| "module": "dist/index.js", | ||
| "exports": { | ||
| ".": { | ||
| "solid": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| }, | ||
| "import": { | ||
| "types": "./dist/esm/index.d.ts", | ||
| "default": "./dist/esm/index.js" | ||
| }, | ||
| "require": { | ||
| "types": "./dist/cjs/index.d.cts", | ||
| "default": "./dist/cjs/index.cjs" | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| } | ||
@@ -48,3 +48,3 @@ }, | ||
| "dependencies": { | ||
| "@tanstack/store": "0.5.2" | ||
| "@tanstack/store": "0.5.3" | ||
| }, | ||
@@ -51,0 +51,0 @@ "devDependencies": { |
| "use strict"; | ||
| Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); | ||
| const solidJs = require("solid-js"); | ||
| const store$1 = require("solid-js/store"); | ||
| const store = require("@tanstack/store"); | ||
| function useStore(store2, selector = (d) => d) { | ||
| const [slice, setSlice] = store$1.createStore({ | ||
| value: selector(store2.state) | ||
| }); | ||
| const unsub = store2.subscribe(() => { | ||
| const newValue = selector(store2.state); | ||
| setSlice("value", store$1.reconcile(newValue)); | ||
| }); | ||
| solidJs.onCleanup(() => { | ||
| unsub(); | ||
| }); | ||
| return () => slice.value; | ||
| } | ||
| exports.useStore = useStore; | ||
| Object.keys(store).forEach((k) => { | ||
| if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, { | ||
| enumerable: true, | ||
| get: () => store[k] | ||
| }); | ||
| }); | ||
| //# sourceMappingURL=index.cjs.map |
| {"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["import { onCleanup } from 'solid-js'\nimport { createStore, reconcile } from 'solid-js/store'\nimport type { AnyUpdater, Store } from '@tanstack/store'\nimport type { Accessor } from 'solid-js'\n\nexport * from '@tanstack/store'\n\nexport type NoInfer<T> = [T][T extends any ? 0 : never]\n\nexport function useStore<\n TState,\n TSelected = NoInfer<TState>,\n TUpdater extends AnyUpdater = AnyUpdater,\n>(\n store: Store<TState, TUpdater>,\n selector: (state: NoInfer<TState>) => TSelected = (d) => d as any,\n): Accessor<TSelected> {\n const [slice, setSlice] = createStore({\n value: selector(store.state),\n })\n\n const unsub = store.subscribe(() => {\n const newValue = selector(store.state)\n setSlice('value', reconcile(newValue))\n })\n\n onCleanup(() => {\n unsub()\n })\n\n return () => slice.value\n}\n"],"names":["store","createStore","reconcile","onCleanup"],"mappings":";;;;;AASO,SAAS,SAKdA,QACA,WAAkD,CAAC,MAAM,GACpC;AACrB,QAAM,CAAC,OAAO,QAAQ,IAAIC,oBAAY;AAAA,IACpC,OAAO,SAASD,OAAM,KAAK;AAAA,EAAA,CAC5B;AAEK,QAAA,QAAQA,OAAM,UAAU,MAAM;AAC5B,UAAA,WAAW,SAASA,OAAM,KAAK;AAC5B,aAAA,SAASE,kBAAU,QAAQ,CAAC;AAAA,EAAA,CACtC;AAEDC,UAAAA,UAAU,MAAM;AACR;EAAA,CACP;AAED,SAAO,MAAM,MAAM;AACrB;;;;;;;;"} |
| import { AnyUpdater, Store } from '@tanstack/store'; | ||
| import { Accessor } from 'solid-js'; | ||
| export * from '@tanstack/store'; | ||
| export type NoInfer<T> = [T][T extends any ? 0 : never]; | ||
| export declare function useStore<TState, TSelected = NoInfer<TState>, TUpdater extends AnyUpdater = AnyUpdater>(store: Store<TState, TUpdater>, selector?: (state: NoInfer<TState>) => TSelected): Accessor<TSelected>; |
| import { AnyUpdater, Store } from '@tanstack/store'; | ||
| import { Accessor } from 'solid-js'; | ||
| export * from '@tanstack/store'; | ||
| export type NoInfer<T> = [T][T extends any ? 0 : never]; | ||
| export declare function useStore<TState, TSelected = NoInfer<TState>, TUpdater extends AnyUpdater = AnyUpdater>(store: Store<TState, TUpdater>, selector?: (state: NoInfer<TState>) => TSelected): Accessor<TSelected>; |
| import { onCleanup } from "solid-js"; | ||
| import { createStore, reconcile } from "solid-js/store"; | ||
| export * from "@tanstack/store"; | ||
| function useStore(store, selector = (d) => d) { | ||
| const [slice, setSlice] = createStore({ | ||
| value: selector(store.state) | ||
| }); | ||
| const unsub = store.subscribe(() => { | ||
| const newValue = selector(store.state); | ||
| setSlice("value", reconcile(newValue)); | ||
| }); | ||
| onCleanup(() => { | ||
| unsub(); | ||
| }); | ||
| return () => slice.value; | ||
| } | ||
| export { | ||
| useStore | ||
| }; | ||
| //# sourceMappingURL=index.js.map |
| {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["import { onCleanup } from 'solid-js'\nimport { createStore, reconcile } from 'solid-js/store'\nimport type { AnyUpdater, Store } from '@tanstack/store'\nimport type { Accessor } from 'solid-js'\n\nexport * from '@tanstack/store'\n\nexport type NoInfer<T> = [T][T extends any ? 0 : never]\n\nexport function useStore<\n TState,\n TSelected = NoInfer<TState>,\n TUpdater extends AnyUpdater = AnyUpdater,\n>(\n store: Store<TState, TUpdater>,\n selector: (state: NoInfer<TState>) => TSelected = (d) => d as any,\n): Accessor<TSelected> {\n const [slice, setSlice] = createStore({\n value: selector(store.state),\n })\n\n const unsub = store.subscribe(() => {\n const newValue = selector(store.state)\n setSlice('value', reconcile(newValue))\n })\n\n onCleanup(() => {\n unsub()\n })\n\n return () => slice.value\n}\n"],"names":[],"mappings":";;;AASO,SAAS,SAKd,OACA,WAAkD,CAAC,MAAM,GACpC;AACrB,QAAM,CAAC,OAAO,QAAQ,IAAI,YAAY;AAAA,IACpC,OAAO,SAAS,MAAM,KAAK;AAAA,EAAA,CAC5B;AAEK,QAAA,QAAQ,MAAM,UAAU,MAAM;AAC5B,UAAA,WAAW,SAAS,MAAM,KAAK;AAC5B,aAAA,SAAS,UAAU,QAAQ,CAAC;AAAA,EAAA,CACtC;AAED,YAAU,MAAM;AACR;EAAA,CACP;AAED,SAAO,MAAM,MAAM;AACrB;"} |
| import { describe, expect, it } from 'vitest' | ||
| import { render, renderHook } from '@solidjs/testing-library' | ||
| import { Store } from '@tanstack/store' | ||
| import { useStore } from '../index' | ||
| describe('useStore', () => { | ||
| it.todo('allows us to select state using a selector', async () => { | ||
| const store = new Store({ | ||
| select: 0, | ||
| ignored: 1, | ||
| }) | ||
| function Comp() { | ||
| const storeVal = useStore(store, (state) => state.select) | ||
| return <p>Store: {storeVal()}</p> | ||
| } | ||
| const { getByText } = render(() => <Comp />) | ||
| expect(getByText('Store: 0')).toBeInTheDocument() | ||
| }) | ||
| it('allows us to select state using a selector', () => { | ||
| const store = new Store({ | ||
| select: 0, | ||
| ignored: 1, | ||
| }) | ||
| const { result } = renderHook(() => | ||
| useStore(store, (state) => state.select), | ||
| ) | ||
| expect(result()).toBe(0) | ||
| }) | ||
| it('updates accessor value when state is updated', () => { | ||
| const store = new Store(0) | ||
| const { result } = renderHook(() => useStore(store)) | ||
| store.setState((prev) => prev + 1) | ||
| expect(result()).toBe(1) | ||
| }) | ||
| }) |
4623
-47.45%6
-40%47
-57.66%+ Added
- Removed
Updated