@iadev93/zuno-react 0.0.6 → 0.0.7

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/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ibrahim Aftab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,70 @@
1
- export { createZunoReact } from "./createZunoReact";
2
- export { bindReact } from "./bindReact";
1
+ import { type ZunoSubscribableStore, type CreateZunoOptions } 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
+ export 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
+ export 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
+ export 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
+ export 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 React-specific features.
38
+ */
39
+ export type ReactBoundStore<T> = BoundStore<T> & {
40
+ /**
41
+ * React hook for this store.
42
+ * Optional selector + equality for derived values and avoiding rerenders.
43
+ */
44
+ use: <TSelected = T>(selector?: Selector<T, TSelected>, equalityFn?: EqualityFn<TSelected>) => TSelected;
45
+ };
46
+ /**
47
+ * Binds Zuno to React.
48
+ * @param zuno The Zuno instance to bind.
49
+ * @returns A React-enhanced Zuno instance.
50
+ */
51
+ export declare const bindReact: (zuno: ZunoCore) => {
52
+ store: <T>(storeKey: string, init: () => T) => ReactBoundStore<T>;
53
+ set<T>(storeKey: string, next: T | ((prev: T) => T), init?: () => T): Promise<any>;
54
+ get<T>(storeKey: string, init?: () => T): T;
55
+ stop?: () => void;
56
+ };
57
+ /**
58
+ * Creates a Zuno instance and returns a React-enhanced instance.
59
+ *
60
+ * @param opts The options for the Zuno instance.
61
+ * @returns An object with a `store` method that returns stores with a `use` hook.
62
+ */
63
+ export declare const createZunoReact: (opts: CreateZunoOptions) => {
64
+ store: <T>(storeKey: string, init: () => T) => ReactBoundStore<T>;
65
+ set<T>(storeKey: string, next: T | ((prev: T) => T), init?: () => T): Promise<any>;
66
+ get<T>(storeKey: string, init?: () => T): T;
67
+ stop?: () => void;
68
+ };
3
69
  export type { ZunoReadable } from "@iadev93/zuno";
4
70
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAqB,KAAK,qBAAqB,EAA0B,KAAK,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAE9H;;;;GAIG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC;AAEpD;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,CAAC,MAAM,EAAE,SAAS,IAAI,CAAC,KAAK,EAAE,MAAM,KAAK,SAAS,CAAC;AAQvE;;;GAGG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI;IAC1B,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,MAAM,MAAM,QAAQ,GAAG;IACrB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,CAAC,CAAC,CAAC;QA9EnE,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;CA6GlB,CAAC;AAGF,YAAY,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC"}
package/dist/index.js CHANGED
@@ -1,2 +1,75 @@
1
- export { createZunoReact } from "./createZunoReact";
2
- export { bindReact } from "./bindReact";
1
+ import * as React from "react";
2
+ import { toReadable, createZuno } 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-enhanced Zuno instance.
12
+ */
13
+ export const bindReact = (zuno) => {
14
+ /**
15
+ * A custom hook for accessing a Zuno store in a React component.
16
+ */
17
+ const useExternalStore = (readable, selector, equalityFn = defaultEq) => {
18
+ const select = React.useMemo(() => {
19
+ return (selector ??
20
+ ((s) => s));
21
+ }, [selector]);
22
+ const lastRef = React.useRef(null);
23
+ const hasLast = React.useRef(false);
24
+ const getSnapshot = React.useCallback(() => {
25
+ const next = select(readable.getSnapshot());
26
+ if (!hasLast.current) {
27
+ hasLast.current = true;
28
+ lastRef.current = next;
29
+ return next;
30
+ }
31
+ const prev = lastRef.current;
32
+ if (equalityFn(prev, next))
33
+ return prev;
34
+ lastRef.current = next;
35
+ return next;
36
+ }, [readable, select, equalityFn]);
37
+ const subscribe = React.useCallback((onChange) => readable.subscribe(onChange), [readable]);
38
+ const getServerSnapshot = React.useCallback(() => {
39
+ const s = readable.getServerSnapshot ? readable.getServerSnapshot() : readable.getSnapshot();
40
+ return select(s);
41
+ }, [readable, select]);
42
+ React.useEffect(() => {
43
+ hasLast.current = false;
44
+ lastRef.current = null;
45
+ }, [select, equalityFn]);
46
+ return React.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
47
+ };
48
+ /**
49
+ * Creates a React-enhanced store.
50
+ */
51
+ const store = (storeKey, init) => {
52
+ const base = zuno.store(storeKey, init);
53
+ return {
54
+ ...base,
55
+ use: (selector, equalityFn = defaultEq) => {
56
+ const readable = React.useMemo(() => toReadable(base.raw()), [storeKey]);
57
+ return useExternalStore(readable, selector, equalityFn);
58
+ },
59
+ };
60
+ };
61
+ return {
62
+ ...zuno,
63
+ store,
64
+ };
65
+ };
66
+ /**
67
+ * Creates a Zuno instance and returns a React-enhanced instance.
68
+ *
69
+ * @param opts The options for the Zuno instance.
70
+ * @returns An object with a `store` method that returns stores with a `use` hook.
71
+ */
72
+ export const createZunoReact = (opts) => {
73
+ const zuno = createZuno(opts);
74
+ return bindReact(zuno);
75
+ };
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@iadev93/zuno-react",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "files": [
7
7
  "dist",
8
- "README.md"
8
+ "README.md",
9
+ "LICENSE"
9
10
  ],
10
11
  "exports": {
11
12
  ".": {
@@ -13,19 +14,18 @@
13
14
  "default": "./dist/index.js"
14
15
  }
15
16
  },
16
- "scripts": {
17
- "build": "tsc -p tsconfig.json"
18
- },
19
17
  "peerDependencies": {
20
- "@iadev93/zuno": "workspace:*",
21
- "react": ">=18"
18
+ "react": ">=18",
19
+ "@iadev93/zuno": "0.0.5"
22
20
  },
23
21
  "devDependencies": {
24
22
  "@types/react": "^18.0.0",
25
23
  "typescript": "^5.9.3",
26
- "@iadev93/zuno": "workspace:*"
24
+ "@iadev93/zuno": "0.0.5"
27
25
  },
28
26
  "keywords": [
27
+ "zuno",
28
+ "react",
29
29
  "state",
30
30
  "state-management",
31
31
  "sync",
@@ -43,5 +43,8 @@
43
43
  "url": "git+https://github.com/IADev-Channel/zuno.git"
44
44
  },
45
45
  "author": "Ibrahim Aftab",
46
- "license": "MIT"
47
- }
46
+ "license": "MIT",
47
+ "scripts": {
48
+ "build": "tsc -p tsconfig.json"
49
+ }
50
+ }