@iadev93/zuno-react 0.0.3 → 0.0.5
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/dist/bindReact.d.ts +59 -0
- package/dist/bindReact.d.ts.map +1 -0
- package/dist/bindReact.js +128 -0
- package/dist/createZunoReact.d.ts +27 -0
- package/dist/createZunoReact.d.ts.map +1 -0
- package/{src/createZunoReact.ts → dist/createZunoReact.js} +10 -13
- package/{src/index.ts → dist/index.d.ts} +1 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/package.json +25 -2
- package/src/bindReact.ts +0 -213
- package/tsconfig.json +0 -16
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type ZunoSubscribableStore } from "@iadev93/zuno";
|
|
2
|
+
/**
|
|
3
|
+
* Type definition for an equality function.
|
|
4
|
+
* It takes two values of the same type and returns true if they are considered equal, false otherwise.
|
|
5
|
+
* Used to prevent unnecessary re-renders in React hooks when the selected state hasn't "meaningfully" changed.
|
|
6
|
+
*/
|
|
7
|
+
type EqualityFn<T> = (a: T, b: T) => boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Type definition for a selector function.
|
|
10
|
+
* It takes the full state of a store and returns a selected portion of that state.
|
|
11
|
+
* This allows components to subscribe only to the parts of the state they care about,
|
|
12
|
+
* optimizing performance by avoiding re-renders for unrelated state changes.
|
|
13
|
+
*/
|
|
14
|
+
type Selector<TState, TSelected> = (state: TState) => TSelected;
|
|
15
|
+
/**
|
|
16
|
+
* An extended interface for a Zuno store that includes methods for setting state
|
|
17
|
+
* and a unique key identifier. This represents a store that has been "bound" or registered.
|
|
18
|
+
*/
|
|
19
|
+
type BoundStore<T> = {
|
|
20
|
+
key: string;
|
|
21
|
+
get: () => T;
|
|
22
|
+
set: (next: T | ((prev: T) => T)) => Promise<any>;
|
|
23
|
+
subscribe: (cb: (state: T) => void) => () => void;
|
|
24
|
+
raw: () => ZunoSubscribableStore<T>;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* The core interface for the Zuno library, providing methods to create,
|
|
28
|
+
* retrieve, and update stores.
|
|
29
|
+
*/
|
|
30
|
+
type ZunoCore = {
|
|
31
|
+
store<T>(storeKey: string, init: () => T): BoundStore<T>;
|
|
32
|
+
set<T>(storeKey: string, next: T | ((prev: T) => T), init?: () => T): Promise<any>;
|
|
33
|
+
get<T>(storeKey: string, init?: () => T): T;
|
|
34
|
+
stop?: () => void;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* An extended interface for a Zuno store that includes methods for setting state
|
|
38
|
+
* and a unique key identifier. This represents a store that has been "bound" or registered.
|
|
39
|
+
*/
|
|
40
|
+
type ReactBoundStore<T> = BoundStore<T> & {
|
|
41
|
+
/**
|
|
42
|
+
* React hook for this store.
|
|
43
|
+
* Optional selector + equality for derived values and avoiding rerenders.
|
|
44
|
+
*/
|
|
45
|
+
use: <TSelected = T>(selector?: Selector<T, TSelected>, equalityFn?: EqualityFn<TSelected>) => TSelected;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Binds Zuno to React.
|
|
49
|
+
* @param zuno The Zuno instance to bind.
|
|
50
|
+
* @returns A React hook for accessing the store.
|
|
51
|
+
*/
|
|
52
|
+
export declare const bindReact: (zuno: ZunoCore) => {
|
|
53
|
+
store: <T>(storeKey: string, init: () => T) => ReactBoundStore<T>;
|
|
54
|
+
set<T>(storeKey: string, next: T | ((prev: T) => T), init?: () => T): Promise<any>;
|
|
55
|
+
get<T>(storeKey: string, init?: () => T): T;
|
|
56
|
+
stop?: () => void;
|
|
57
|
+
};
|
|
58
|
+
export {};
|
|
59
|
+
//# sourceMappingURL=bindReact.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bindReact.d.ts","sourceRoot":"","sources":["../src/bindReact.ts"],"names":[],"mappings":"AACA,OAAO,EAAqB,KAAK,qBAAqB,EAAc,MAAM,eAAe,CAAC;AAE1F;;;;GAIG;AACH,KAAK,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC;AAE7C;;;;;GAKG;AACH,KAAK,QAAQ,CAAC,MAAM,EAAE,SAAS,IAAI,CAAC,KAAK,EAAE,MAAM,KAAK,SAAS,CAAC;AAQhE;;;GAGG;AACH,KAAK,UAAU,CAAC,CAAC,IAAI;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC,CAAC;IACb,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;IAClD,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,KAAK,MAAM,IAAI,CAAC;IAClD,GAAG,EAAE,MAAM,qBAAqB,CAAC,CAAC,CAAC,CAAC;CACrC,CAAC;AAEF;;;GAGG;AACH,KAAK,QAAQ,GAAG;IACd,KAAK,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;IACzD,GAAG,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IACnF,GAAG,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;IAC5C,IAAI,CAAC,EAAE,MAAM,IAAI,CAAC;CACnB,CAAC;AAEF;;;GAGG;AACH,KAAK,eAAe,CAAC,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,GAAG;IACxC;;;OAGG;IACH,GAAG,EAAE,CAAC,SAAS,GAAG,CAAC,EACjB,QAAQ,CAAC,EAAE,QAAQ,CAAC,CAAC,EAAE,SAAS,CAAC,EACjC,UAAU,CAAC,EAAE,UAAU,CAAC,SAAS,CAAC,KAC/B,SAAS,CAAC;CAChB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,SAAS,GAAI,MAAM,QAAQ;YAgHvB,CAAC,YAAa,MAAM,QAAQ,MAAM,CAAC,KAAG,eAAe,CAAC,CAAC,CAAC;QAzInE,CAAC,YAAY,MAAM,QAAQ,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,SAAS,MAAM,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC;QAC9E,CAAC,YAAY,MAAM,SAAS,MAAM,CAAC,GAAG,CAAC;WACpC,MAAM,IAAI;CAwKlB,CAAC"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { toReadable } from "@iadev93/zuno";
|
|
3
|
+
/**
|
|
4
|
+
* The default equality function, using `Object.is` for strict equality comparison.
|
|
5
|
+
* This is a common and safe default for comparing primitive values and references.
|
|
6
|
+
*/
|
|
7
|
+
const defaultEq = Object.is;
|
|
8
|
+
/**
|
|
9
|
+
* Binds Zuno to React.
|
|
10
|
+
* @param zuno The Zuno instance to bind.
|
|
11
|
+
* @returns A React hook for accessing the store.
|
|
12
|
+
*/
|
|
13
|
+
export const bindReact = (zuno) => {
|
|
14
|
+
/**
|
|
15
|
+
* A custom hook for accessing a Zuno store in a React component.
|
|
16
|
+
* @param store The Zuno store to access.
|
|
17
|
+
* @param selector Optional selector function to extract a specific part of the store's state.
|
|
18
|
+
* @param equalityFn Optional equality function to determine if the selected state has changed.
|
|
19
|
+
* @returns The selected state from the store.
|
|
20
|
+
*/
|
|
21
|
+
const useExternalStore = (readable, selector, equalityFn = defaultEq) => {
|
|
22
|
+
/**
|
|
23
|
+
* Memoized selector function.
|
|
24
|
+
* If no selector is provided, the entire state is selected.
|
|
25
|
+
*/
|
|
26
|
+
const select = React.useMemo(() => {
|
|
27
|
+
return (selector ??
|
|
28
|
+
((s) => s));
|
|
29
|
+
}, [selector]);
|
|
30
|
+
/**
|
|
31
|
+
* Reference to the last selected state.
|
|
32
|
+
* Used to compare with the new state to determine if a re-render is needed.
|
|
33
|
+
*/
|
|
34
|
+
const lastRef = React.useRef(null);
|
|
35
|
+
/**
|
|
36
|
+
* Flag to indicate if the last state has been set.
|
|
37
|
+
* Used to determine if the component should re-render.
|
|
38
|
+
*/
|
|
39
|
+
const hasLast = React.useRef(false);
|
|
40
|
+
/**
|
|
41
|
+
* Function to get the current state of the store.
|
|
42
|
+
* If the state has changed since the last render, the component will re-render.
|
|
43
|
+
*/
|
|
44
|
+
const getSnapshot = React.useCallback(() => {
|
|
45
|
+
/**
|
|
46
|
+
* Get the current state of the store.
|
|
47
|
+
*/
|
|
48
|
+
const next = select(readable.getSnapshot());
|
|
49
|
+
/**
|
|
50
|
+
* If the last state has not been set, set it and return the next state.
|
|
51
|
+
* This is the initial state of the component.
|
|
52
|
+
*/
|
|
53
|
+
if (!hasLast.current) {
|
|
54
|
+
hasLast.current = true;
|
|
55
|
+
lastRef.current = next;
|
|
56
|
+
return next;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Previous state.
|
|
60
|
+
* Used to compare with the new state to determine if a re-render is needed.
|
|
61
|
+
*/
|
|
62
|
+
const prev = lastRef.current;
|
|
63
|
+
/**
|
|
64
|
+
* If the previous state is equal to the next state, return the previous state.
|
|
65
|
+
* This prevents unnecessary re-renders.
|
|
66
|
+
*/
|
|
67
|
+
if (equalityFn(prev, next))
|
|
68
|
+
return prev;
|
|
69
|
+
/**
|
|
70
|
+
* Update the last state and return the next state.
|
|
71
|
+
*/
|
|
72
|
+
lastRef.current = next;
|
|
73
|
+
return next;
|
|
74
|
+
}, [readable, select, equalityFn]);
|
|
75
|
+
/**
|
|
76
|
+
* Function to subscribe to changes in the store.
|
|
77
|
+
* When the store changes, the component will re-render.
|
|
78
|
+
*/
|
|
79
|
+
const subscribe = React.useCallback((onChange) => readable.subscribe(onChange), [readable]);
|
|
80
|
+
const getServerSnapshot = React.useCallback(() => {
|
|
81
|
+
const s = readable.getServerSnapshot ? readable.getServerSnapshot() : readable.getSnapshot();
|
|
82
|
+
return select(s);
|
|
83
|
+
}, [readable, select]);
|
|
84
|
+
/**
|
|
85
|
+
* Reset the last state and lastRef when the selector or equalityFn changes.
|
|
86
|
+
* This ensures that the component re-renders when the selector or equalityFn changes.
|
|
87
|
+
*/
|
|
88
|
+
React.useEffect(() => {
|
|
89
|
+
hasLast.current = false;
|
|
90
|
+
lastRef.current = null;
|
|
91
|
+
}, [select, equalityFn]);
|
|
92
|
+
/**
|
|
93
|
+
* Returns the current state of the store.
|
|
94
|
+
* If the state has changed since the last render, the component will re-render.
|
|
95
|
+
*/
|
|
96
|
+
return React.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Creates a store and returns a React hook for accessing the store.
|
|
100
|
+
* @param storeKey The key of the store.
|
|
101
|
+
* @param init The initialization function for the store.
|
|
102
|
+
* @returns A React hook for accessing the store.
|
|
103
|
+
*/
|
|
104
|
+
const store = (storeKey, init) => {
|
|
105
|
+
/**
|
|
106
|
+
* Creates a store and returns a React hook for accessing the store.
|
|
107
|
+
*/
|
|
108
|
+
const base = zuno.store(storeKey, init);
|
|
109
|
+
/**
|
|
110
|
+
* Returns a React hook for accessing the store.
|
|
111
|
+
*/
|
|
112
|
+
return {
|
|
113
|
+
...base,
|
|
114
|
+
use: (selector, equalityFn = defaultEq) => {
|
|
115
|
+
// IMPORTANT: call hook only inside components
|
|
116
|
+
const readable = React.useMemo(() => toReadable(base.raw()), [storeKey]);
|
|
117
|
+
return useExternalStore(readable, selector, equalityFn);
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Returns a Zuno instance with React-enhanced bound store.
|
|
123
|
+
*/
|
|
124
|
+
return {
|
|
125
|
+
...zuno,
|
|
126
|
+
store, // overrides store() with React-enhanced bound store
|
|
127
|
+
};
|
|
128
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { CreateZunoOptions } from "@iadev93/zuno";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a Zuno instance and returns a React hook for accessing the store.
|
|
4
|
+
*
|
|
5
|
+
* ⚠️ IMPORTANT:
|
|
6
|
+
*
|
|
7
|
+
* Call this at **module scope**, not inside *React components*.
|
|
8
|
+
* This creates a single Zuno instance.
|
|
9
|
+
*
|
|
10
|
+
* @param opts The options for the Zuno instance.
|
|
11
|
+
* @returns An object with a `useStore` hook for accessing the store.
|
|
12
|
+
*/
|
|
13
|
+
export declare const createZunoReact: (opts: CreateZunoOptions) => {
|
|
14
|
+
store: <T>(storeKey: string, init: () => T) => {
|
|
15
|
+
key: string;
|
|
16
|
+
get: () => T;
|
|
17
|
+
set: (next: T | ((prev: T) => T)) => Promise<any>;
|
|
18
|
+
subscribe: (cb: (state: T) => void) => () => void;
|
|
19
|
+
raw: () => import("@iadev93/zuno").ZunoSubscribableStore<T>;
|
|
20
|
+
} & {
|
|
21
|
+
use: <TSelected = T>(selector?: ((state: T) => TSelected) | undefined, equalityFn?: ((a: TSelected, b: TSelected) => boolean) | undefined) => TSelected;
|
|
22
|
+
};
|
|
23
|
+
set<T>(storeKey: string, next: T | ((prev: T) => T), init?: () => T): Promise<any>;
|
|
24
|
+
get<T>(storeKey: string, init?: () => T): T;
|
|
25
|
+
stop?: () => void;
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=createZunoReact.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createZunoReact.d.ts","sourceRoot":"","sources":["../src/createZunoReact.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAG9D;;;;;;;;;;GAUG;AACH,eAAO,MAAM,eAAe,GAAI,MAAM,iBAAiB;;;;;;;;;;;;;CAOtD,CAAC"}
|
|
@@ -1,22 +1,19 @@
|
|
|
1
|
-
import { createZuno
|
|
1
|
+
import { createZuno } from "@iadev93/zuno";
|
|
2
2
|
import { bindReact } from "./bindReact";
|
|
3
|
-
|
|
4
3
|
/**
|
|
5
4
|
* Creates a Zuno instance and returns a React hook for accessing the store.
|
|
6
|
-
*
|
|
5
|
+
*
|
|
7
6
|
* ⚠️ IMPORTANT:
|
|
8
|
-
*
|
|
7
|
+
*
|
|
9
8
|
* Call this at **module scope**, not inside *React components*.
|
|
10
9
|
* This creates a single Zuno instance.
|
|
11
|
-
*
|
|
10
|
+
*
|
|
12
11
|
* @param opts The options for the Zuno instance.
|
|
13
12
|
* @returns An object with a `useStore` hook for accessing the store.
|
|
14
13
|
*/
|
|
15
|
-
export const createZunoReact = (opts
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return bindReact(zuno)
|
|
22
|
-
};
|
|
14
|
+
export const createZunoReact = (opts) => {
|
|
15
|
+
/** Zuno */
|
|
16
|
+
const zuno = createZuno(opts);
|
|
17
|
+
/** Return zuno */
|
|
18
|
+
return bindReact(zuno);
|
|
19
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAGxC,YAAY,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iadev93/zuno-react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist",
|
|
8
|
+
"README.md"
|
|
9
|
+
],
|
|
6
10
|
"exports": {
|
|
7
11
|
".": {
|
|
8
12
|
"types": "./dist/index.d.ts",
|
|
@@ -20,5 +24,24 @@
|
|
|
20
24
|
"@types/react": "^18.0.0",
|
|
21
25
|
"typescript": "^5.9.3",
|
|
22
26
|
"@iadev93/zuno": "workspace:*"
|
|
23
|
-
}
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"state",
|
|
30
|
+
"state-management",
|
|
31
|
+
"sync",
|
|
32
|
+
"sse",
|
|
33
|
+
"server-sent-events",
|
|
34
|
+
"broadcastchannel",
|
|
35
|
+
"distributed-state",
|
|
36
|
+
"event-sourcing",
|
|
37
|
+
"universal-state",
|
|
38
|
+
"realtime"
|
|
39
|
+
],
|
|
40
|
+
"homepage": "https://github.com/IADev-Channel/zuno",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/IADev-Channel/zuno.git"
|
|
44
|
+
},
|
|
45
|
+
"author": "Ibrahim Aftab",
|
|
46
|
+
"license": "MIT"
|
|
24
47
|
}
|
package/src/bindReact.ts
DELETED
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { type ZunoReadable, type ZunoSubscribableStore, toReadable } from "@iadev93/zuno";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Type definition for an equality function.
|
|
6
|
-
* It takes two values of the same type and returns true if they are considered equal, false otherwise.
|
|
7
|
-
* Used to prevent unnecessary re-renders in React hooks when the selected state hasn't "meaningfully" changed.
|
|
8
|
-
*/
|
|
9
|
-
type EqualityFn<T> = (a: T, b: T) => boolean;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Type definition for a selector function.
|
|
13
|
-
* It takes the full state of a store and returns a selected portion of that state.
|
|
14
|
-
* This allows components to subscribe only to the parts of the state they care about,
|
|
15
|
-
* optimizing performance by avoiding re-renders for unrelated state changes.
|
|
16
|
-
*/
|
|
17
|
-
type Selector<TState, TSelected> = (state: TState) => TSelected;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* The default equality function, using `Object.is` for strict equality comparison.
|
|
21
|
-
* This is a common and safe default for comparing primitive values and references.
|
|
22
|
-
*/
|
|
23
|
-
const defaultEq: EqualityFn<any> = Object.is;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* An extended interface for a Zuno store that includes methods for setting state
|
|
27
|
-
* and a unique key identifier. This represents a store that has been "bound" or registered.
|
|
28
|
-
*/
|
|
29
|
-
type BoundStore<T> = {
|
|
30
|
-
key: string;
|
|
31
|
-
get: () => T;
|
|
32
|
-
set: (next: T | ((prev: T) => T)) => Promise<any>;
|
|
33
|
-
subscribe: (cb: (state: T) => void) => () => void;
|
|
34
|
-
raw: () => ZunoSubscribableStore<T>;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* The core interface for the Zuno library, providing methods to create,
|
|
39
|
-
* retrieve, and update stores.
|
|
40
|
-
*/
|
|
41
|
-
type ZunoCore = {
|
|
42
|
-
store<T>(storeKey: string, init: () => T): BoundStore<T>;
|
|
43
|
-
set<T>(storeKey: string, next: T | ((prev: T) => T), init?: () => T): Promise<any>;
|
|
44
|
-
get<T>(storeKey: string, init?: () => T): T;
|
|
45
|
-
stop?: () => void;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* An extended interface for a Zuno store that includes methods for setting state
|
|
50
|
-
* and a unique key identifier. This represents a store that has been "bound" or registered.
|
|
51
|
-
*/
|
|
52
|
-
type ReactBoundStore<T> = BoundStore<T> & {
|
|
53
|
-
/**
|
|
54
|
-
* React hook for this store.
|
|
55
|
-
* Optional selector + equality for derived values and avoiding rerenders.
|
|
56
|
-
*/
|
|
57
|
-
use: <TSelected = T>(
|
|
58
|
-
selector?: Selector<T, TSelected>,
|
|
59
|
-
equalityFn?: EqualityFn<TSelected>
|
|
60
|
-
) => TSelected;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Binds Zuno to React.
|
|
65
|
-
* @param zuno The Zuno instance to bind.
|
|
66
|
-
* @returns A React hook for accessing the store.
|
|
67
|
-
*/
|
|
68
|
-
export const bindReact = (zuno: ZunoCore) => {
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* A custom hook for accessing a Zuno store in a React component.
|
|
72
|
-
* @param store The Zuno store to access.
|
|
73
|
-
* @param selector Optional selector function to extract a specific part of the store's state.
|
|
74
|
-
* @param equalityFn Optional equality function to determine if the selected state has changed.
|
|
75
|
-
* @returns The selected state from the store.
|
|
76
|
-
*/
|
|
77
|
-
const useExternalStore = <TState, TSelected = TState>(
|
|
78
|
-
readable: ZunoReadable<TState>,
|
|
79
|
-
selector?: Selector<TState, TSelected>,
|
|
80
|
-
equalityFn: EqualityFn<TSelected> = defaultEq
|
|
81
|
-
): TSelected => {
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Memoized selector function.
|
|
85
|
-
* If no selector is provided, the entire state is selected.
|
|
86
|
-
*/
|
|
87
|
-
const select = React.useMemo(() => {
|
|
88
|
-
return (selector ??
|
|
89
|
-
((s: TState) => s as unknown as TSelected)) as Selector<TState, TSelected>;
|
|
90
|
-
}, [selector]);
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Reference to the last selected state.
|
|
94
|
-
* Used to compare with the new state to determine if a re-render is needed.
|
|
95
|
-
*/
|
|
96
|
-
const lastRef = React.useRef<TSelected | null>(null);
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Flag to indicate if the last state has been set.
|
|
100
|
-
* Used to determine if the component should re-render.
|
|
101
|
-
*/
|
|
102
|
-
const hasLast = React.useRef(false);
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Function to get the current state of the store.
|
|
106
|
-
* If the state has changed since the last render, the component will re-render.
|
|
107
|
-
*/
|
|
108
|
-
const getSnapshot = React.useCallback(() => {
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Get the current state of the store.
|
|
112
|
-
*/
|
|
113
|
-
const next = select(readable.getSnapshot());
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* If the last state has not been set, set it and return the next state.
|
|
117
|
-
* This is the initial state of the component.
|
|
118
|
-
*/
|
|
119
|
-
if (!hasLast.current) {
|
|
120
|
-
hasLast.current = true;
|
|
121
|
-
lastRef.current = next;
|
|
122
|
-
return next;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Previous state.
|
|
127
|
-
* Used to compare with the new state to determine if a re-render is needed.
|
|
128
|
-
*/
|
|
129
|
-
const prev = lastRef.current as TSelected;
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* If the previous state is equal to the next state, return the previous state.
|
|
133
|
-
* This prevents unnecessary re-renders.
|
|
134
|
-
*/
|
|
135
|
-
if (equalityFn(prev, next)) return prev;
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Update the last state and return the next state.
|
|
139
|
-
*/
|
|
140
|
-
lastRef.current = next;
|
|
141
|
-
return next;
|
|
142
|
-
}, [readable, select, equalityFn]);
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Function to subscribe to changes in the store.
|
|
146
|
-
* When the store changes, the component will re-render.
|
|
147
|
-
*/
|
|
148
|
-
const subscribe = React.useCallback(
|
|
149
|
-
(onChange: () => void) => readable.subscribe(onChange),
|
|
150
|
-
[readable]
|
|
151
|
-
);
|
|
152
|
-
|
|
153
|
-
const getServerSnapshot = React.useCallback(() => {
|
|
154
|
-
const s = readable.getServerSnapshot ? readable.getServerSnapshot() : readable.getSnapshot();
|
|
155
|
-
return select(s);
|
|
156
|
-
}, [readable, select]);
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Reset the last state and lastRef when the selector or equalityFn changes.
|
|
160
|
-
* This ensures that the component re-renders when the selector or equalityFn changes.
|
|
161
|
-
*/
|
|
162
|
-
React.useEffect(() => {
|
|
163
|
-
hasLast.current = false;
|
|
164
|
-
lastRef.current = null;
|
|
165
|
-
}, [select, equalityFn]);
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Returns the current state of the store.
|
|
169
|
-
* If the state has changed since the last render, the component will re-render.
|
|
170
|
-
*/
|
|
171
|
-
return React.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Creates a store and returns a React hook for accessing the store.
|
|
176
|
-
* @param storeKey The key of the store.
|
|
177
|
-
* @param init The initialization function for the store.
|
|
178
|
-
* @returns A React hook for accessing the store.
|
|
179
|
-
*/
|
|
180
|
-
const store = <T,>(storeKey: string, init: () => T): ReactBoundStore<T> => {
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Creates a store and returns a React hook for accessing the store.
|
|
184
|
-
*/
|
|
185
|
-
const base = zuno.store<T>(storeKey, init);
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Returns a React hook for accessing the store.
|
|
189
|
-
*/
|
|
190
|
-
return {
|
|
191
|
-
...base,
|
|
192
|
-
use: <TSelected = T>(
|
|
193
|
-
selector?: Selector<T, TSelected>,
|
|
194
|
-
equalityFn: EqualityFn<TSelected> = defaultEq
|
|
195
|
-
) => {
|
|
196
|
-
// IMPORTANT: call hook only inside components
|
|
197
|
-
const readable = React.useMemo(
|
|
198
|
-
() => toReadable(base.raw() as ZunoSubscribableStore<T>),
|
|
199
|
-
[storeKey]
|
|
200
|
-
);
|
|
201
|
-
return useExternalStore<T, TSelected>(readable, selector, equalityFn);
|
|
202
|
-
},
|
|
203
|
-
};
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Returns a Zuno instance with React-enhanced bound store.
|
|
208
|
-
*/
|
|
209
|
-
return {
|
|
210
|
-
...zuno,
|
|
211
|
-
store, // overrides store() with React-enhanced bound store
|
|
212
|
-
};
|
|
213
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.base.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"rootDir": "src",
|
|
5
|
-
"outDir": "dist",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"jsx": "react",
|
|
8
|
-
"declarationMap": true,
|
|
9
|
-
"emitDeclarationOnly": false,
|
|
10
|
-
"module": "ESNext",
|
|
11
|
-
"moduleResolution": "Bundler"
|
|
12
|
-
},
|
|
13
|
-
"include": [
|
|
14
|
-
"src/**/*"
|
|
15
|
-
]
|
|
16
|
-
}
|