@flexbase/store-react
Advanced tools
@@ -0,1 +1,2 @@ | ||
| import { SubscriptionCallback } from '@flexbase/observable-subject'; | ||
| import { Setter, Store } from '@flexbase/store'; | ||
@@ -8,2 +9,2 @@ /** | ||
| */ | ||
| export declare const useStore: <T>(store: Store<T>) => [T | undefined, Setter<T>]; | ||
| export declare const useStore: <T>(store: Store<T>, subscribe?: SubscriptionCallback<T> | undefined) => [T | undefined, Setter<T>]; |
+20
-17
@@ -0,25 +1,28 @@ | ||
| import { noopSubscription as f } from "@flexbase/observable-subject"; | ||
| import { StorageManager as c } from "@flexbase/store"; | ||
| import { useState as i, useEffect as g, useLayoutEffect as b } from "react"; | ||
| const S = (t) => { | ||
| const e = c.lookupManager(t); | ||
| if (e === void 0) | ||
| import { useState as i, useEffect as S, useLayoutEffect as l } from "react"; | ||
| const d = (t, e) => { | ||
| const r = c.lookupManager(t); | ||
| if (r === void 0) | ||
| throw Error(`Unable to find storage manager for ${t.key.toString()}`); | ||
| const r = e.getValue(t), [o, n] = i(r), a = e.getSetter(t); | ||
| return g(() => { | ||
| const s = e.subscribe(t, (u) => (n(u.value), Promise.resolve())); | ||
| return () => s.unsubscribe(); | ||
| }, [e, t]), [o, a]; | ||
| }, m = (t) => { | ||
| const o = r.getValue(t), [n, s] = i(o), u = r.getSetter(t); | ||
| return S(() => { | ||
| const a = r.subscribe(t, (g) => (s(g.value), Promise.resolve())), b = e ? r.subscribe(t, e) : f; | ||
| return () => { | ||
| b.unsubscribe(), a.unsubscribe(); | ||
| }; | ||
| }, [r, t]), [n, u]; | ||
| }, k = (t) => { | ||
| const e = c.lookupManager(t); | ||
| if (e === void 0) | ||
| throw Error(`Unable to find storage manager for ${t.key.toString()}`); | ||
| const r = e.getValue(t), [o, n] = i(r), a = e.getSetter(t); | ||
| return b(() => { | ||
| const s = e.subscribe(t, (u) => (n(u.value), Promise.resolve())); | ||
| return () => s.unsubscribe(); | ||
| }, [e, t]), [o, a]; | ||
| const r = e.getValue(t), [o, n] = i(r), s = e.getSetter(t); | ||
| return l(() => { | ||
| const u = e.subscribe(t, (a) => (n(a.value), Promise.resolve())); | ||
| return () => u.unsubscribe(); | ||
| }, [e, t]), [o, s]; | ||
| }; | ||
| export { | ||
| S as useStore, | ||
| m as useStoreLayout | ||
| d as useStore, | ||
| k as useStoreLayout | ||
| }; |
+3
-3
| { | ||
| "name": "@flexbase/store-react", | ||
| "version": "2.0.1", | ||
| "version": "2.1.0", | ||
| "description": "React extensions for @flexbase/store", | ||
@@ -20,4 +20,4 @@ "main": "./dist/index.js", | ||
| "dependencies": { | ||
| "@flexbase/observable-subject": "^2.0.0", | ||
| "@flexbase/store": "^2.0.1" | ||
| "@flexbase/observable-subject": "^2.1.0", | ||
| "@flexbase/store": "^2.0.3" | ||
| }, | ||
@@ -24,0 +24,0 @@ "peerDependencies": { "react": "^18.1.0" }, |
5298
5.04%47
9.3%