barrex 0.0.2 → 0.1.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.
package/README.md CHANGED
@@ -21,24 +21,20 @@ _(Requires `react` >= 18 as a peer dependency)_
21
21
  ### 1. Create a Notifier
22
22
 
23
23
  ```ts
24
- import { createNotifier } from "barrex";
24
+ import { notifier } from "barrex";
25
25
 
26
26
  // Define your plain state object type
27
- interface AppState {
27
+ interface CounterState {
28
28
  count: number;
29
- user: {
30
- name: string;
31
- isLoggedIn: boolean;
32
- };
29
+ increment: () => boolean;
30
+ reset: () => boolean;
33
31
  }
34
32
 
35
- // Create the notifier
36
- export const appNotifier = createNotifier<AppState>({
33
+ // Initialize the notifier
34
+ export const counterNotifier = notifier<CounterState>({
37
35
  count: 0,
38
- user: {
39
- name: "Alice",
40
- isLoggedIn: false
41
- }
36
+ increment: (): boolean => counterNotifier.count.set((c) => c + 1),
37
+ reset: (): boolean => counterNotifier.count.set(0)
42
38
  });
43
39
  ```
44
40
 
@@ -47,20 +43,17 @@ export const appNotifier = createNotifier<AppState>({
47
43
  ```tsx
48
44
  import { useListener } from "barrex";
49
45
 
50
- import { appNotifier } from "./app-notifier";
46
+ import { counterNotifier } from "./counter-notifier";
51
47
 
52
48
  export function Counter() {
53
49
  // Fine-grained subscription: component only re-renders when `count` changes
54
- const count = useListener(appNotifier.count);
50
+ const count = useListener(counterNotifier.count);
55
51
 
56
52
  return (
57
53
  <div>
58
54
  <p>Count: {count}</p>
59
- {/* Explicit .set() method with updater support */}
60
- <button onClick={() => appNotifier.count.set((c) => c + 1)}>
61
- Increment
62
- </button>
63
- <button onClick={() => appNotifier.count.set(0)}>Reset</button>
55
+ <button onClick={counterNotifier.increment}>Increment</button>
56
+ <button onClick={counterNotifier.reset}>Reset</button>
64
57
  </div>
65
58
  );
66
59
  }
@@ -68,6 +61,24 @@ export function Counter() {
68
61
 
69
62
  ---
70
63
 
64
+ ## References
65
+
66
+ - **[Core Concepts](./docs/core-concepts.md)**
67
+ - **Utilities**
68
+ - [notifier](./docs/utils/notifier.md)
69
+ - [listener](./docs/utils/listener.md)
70
+ - [multiListener](./docs/utils/multi-listener.md)
71
+ - **Hooks**
72
+ - [useNotifier](./docs/hooks/use-notifier.md)
73
+ - [useListener](./docs/hooks/use-listener.md)
74
+ - [useMultiListener](./docs/hooks/use-multi-listener.md)
75
+ - **Components**
76
+ - [Listener](./docs/components/listener.md)
77
+ - [MultiListener](./docs/components/multi-listener.md)
78
+ - [IsolatedContainer](./docs/components/isolated-container.md)
79
+
80
+ ---
81
+
71
82
  ## License
72
83
 
73
84
  [MIT](https://github.com/bhuvanpddahal/barrex/blob/HEAD/LICENSE)
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- 'use strict';var react=require('react'),jsxRuntime=require('react/jsx-runtime');function b(e){return typeof e=="object"&&e!==null}function u(e){return Array.isArray(e)}function c(e){return typeof e=="function"}var f=class{state;rootNode;constructor(t){this.state=t,this.rootNode=new h(this);}},p=class{cachedState;listeners;children;proxy;constructor(){this.cachedState=null,this.listeners=new Set,this.children=new Map,this.proxy=this.createProxy();}dispatchSet=t=>this.set(t,false);subscribe=t=>(this.listeners.add(t),()=>{this.listeners.delete(t);});notifyListeners(){this.listeners.forEach(t=>t());}notifySubtree(){this.cachedState=null,this.notifyListeners();for(let t of this.children.values())t.notifySubtree();}reconcileChildren(t,r){if(!(!u(t)||!u(r)))for(let[n,i]of this.children){let o=n,s=t[o];r[o]!==s&&i.notifySubtree();}}setTrap(){return false}createProxy(){return new Proxy({},{get:(t,r)=>{if(r==="get")return this.get;if(r==="set")return this.dispatchSet;if(r==="subscribe")return this.subscribe;let n=this.get();if(b(n)&&r in n){let i=n[r];if(c(i))return i;let o=this.children.get(r);return o||(o=new d(r,this),this.children.set(r,o)),o.proxy}},set:this.setTrap})}},h=class extends p{root;constructor(t){super(),this.root=t;}get=()=>this.root.state;set(t,r){let n=this.get(),i=c(t)?t(n):t;return i!==n?(this.root.state=i,this.notifyListeners(),r||this.reconcileChildren(n,i),true):false}},d=class extends p{key;parent;constructor(t,r){super(),this.key=t,this.parent=r;}get=()=>{if(this.cachedState!==null)return this.cachedState;let t=this.parent.get()[this.key];return this.cachedState=t,t};set(t,r){let n=this.parent.get(),i=n[this.key],o=c(t)?t(i):t;if(o!==i){let s=Object.assign(u(n)?[]:{},n);return s[this.key]=o,this.parent.set(s,true)?(this.cachedState=o,this.notifyListeners(),r||this.reconcileChildren(i,o),true):false}return false}};var C="A function cannot be a notifier. Replace it with other value, or add it as a property/method inside an object, or don't wrap it with notifier.";function m(e){let t=c(e)?e():e;if(c(t))throw new Error(C);return k(t)}function k(e){return new f(e).rootNode.proxy}function N(e,t){let r=e.subscribe(()=>{t(e.get());});return t(e.get()),r}function w(e,t){let r=()=>{let o=e.map(s=>s.get());t(o);},n=e.map(o=>o.subscribe(r)),i=()=>n.forEach(o=>o());return r(),i}function L(e){let[t]=react.useState(()=>m(e));return t}function y(e,t,r){let n=react.useRef(null);return react.useSyncExternalStore(e.subscribe,()=>{let i=t?t(e.get()):e.get();return r&&n.current!==null&&r(n.current,i)?n.current:(n.current=i,i)},()=>t?t(e.get()):e.get())}function V(e,t){return u(e)&&u(t)&&e.length===t.length?e.every((r,n)=>{let i=t[n];return r===i}):e===t}function S(e,t,r=V){let n=react.useRef(null),i=react.useCallback(s=>{let a=e.map(l=>l.subscribe(s));return ()=>a.forEach(l=>l())},e),o=()=>{let s=e.map(l=>l.get()),a=t?t(s):s;return n.current!==null&&r(n.current,a)?n.current:(n.current=a,a)};return react.useSyncExternalStore(i,o,o)}function q({notifier:e,selector:t,areStatesEqual:r,stableChild:n,children:i}){let o=y(e,t,r);return i(o,n)}var A=react.memo(q,(e,t)=>t.isolated?true:e.notifier===t.notifier&&e.selector===t.selector&&e.areStatesEqual===t.areStatesEqual&&e.stableChild===t.stableChild&&e.children===t.children);function F({notifiers:e,selector:t,areStatesEqual:r,stableChild:n,children:i}){let o=S(e,t,r);return i(o,n)}var j=react.memo(F,(e,t)=>t.isolated?true:e.notifiers===t.notifiers&&e.selector===t.selector&&e.areStatesEqual===t.areStatesEqual&&e.stableChild===t.stableChild&&e.children===t.children);function O({children:e}){return jsxRuntime.jsx(jsxRuntime.Fragment,{children:e})}var K=react.memo(O,()=>true);exports.IsolatedContainer=K;exports.Listener=A;exports.MultiListener=j;exports.createNotifier=m;exports.listener=N;exports.multiListener=w;exports.useListener=y;exports.useMultiListener=S;exports.useNotifier=L;//# sourceMappingURL=index.cjs.map
1
+ 'use strict';var react=require('react'),jsxRuntime=require('react/jsx-runtime');function b(e){return typeof e=="object"&&e!==null}function u(e){return Array.isArray(e)}function c(e){return typeof e=="function"}var f=class{state;rootNode;constructor(t){this.state=t,this.rootNode=new h(this);}},p=class{cachedState;listeners;children;proxy;constructor(){this.cachedState=null,this.listeners=new Set,this.children=new Map,this.proxy=this.createProxy();}dispatchSet=t=>this.set(t,false);subscribe=t=>(this.listeners.add(t),()=>{this.listeners.delete(t);});notifyListeners(){this.listeners.forEach(t=>t());}notifySubtree(){this.cachedState=null,this.notifyListeners();for(let t of this.children.values())t.notifySubtree();}reconcileChildren(t,r){if(!(!u(t)||!u(r)))for(let[n,i]of this.children){let o=n,s=t[o];r[o]!==s&&i.notifySubtree();}}setTrap(){return false}createProxy(){return new Proxy({},{get:(t,r)=>{if(r==="get")return this.get;if(r==="set")return this.dispatchSet;if(r==="subscribe")return this.subscribe;let n=this.get();if(b(n)&&r in n){let i=n[r];if(c(i))return i;let o=this.children.get(r);return o||(o=new d(r,this),this.children.set(r,o)),o.proxy}},set:this.setTrap})}},h=class extends p{root;constructor(t){super(),this.root=t;}get=()=>this.root.state;set(t,r){let n=this.get(),i=c(t)?t(n):t;return i!==n?(this.root.state=i,this.notifyListeners(),r||this.reconcileChildren(n,i),true):false}},d=class extends p{key;parent;constructor(t,r){super(),this.key=t,this.parent=r;}get=()=>{if(this.cachedState!==null)return this.cachedState;let t=this.parent.get()[this.key];return this.cachedState=t,t};set(t,r){let n=this.parent.get(),i=n[this.key],o=c(t)?t(i):t;if(o!==i){let s=Object.assign(u(n)?[]:{},n);return s[this.key]=o,this.parent.set(s,true)?(this.cachedState=o,this.notifyListeners(),r||this.reconcileChildren(i,o),true):false}return false}};var C="A function cannot be a notifier. Replace it with other value, or add it as a property/method inside an object, or don't wrap it with notifier.";function m(e){let t=c(e)?e():e;if(c(t))throw new Error(C);return k(t)}function k(e){return new f(e).rootNode.proxy}function w(e,t){let r=e.subscribe(()=>{t(e.get());});return t(e.get()),r}function N(e,t){let r=()=>{let o=e.map(s=>s.get());t(o);},n=e.map(o=>o.subscribe(r)),i=()=>n.forEach(o=>o());return r(),i}function L(e){let[t]=react.useState(()=>m(e));return t}function y(e,t,r){let n=react.useRef(null);return react.useSyncExternalStore(e.subscribe,()=>{let i=t?t(e.get()):e.get();return r&&n.current!==null&&r(n.current,i)?n.current:(n.current=i,i)},()=>t?t(e.get()):e.get())}function V(e,t){return u(e)&&u(t)&&e.length===t.length?e.every((r,n)=>{let i=t[n];return r===i}):e===t}function S(e,t,r=V){let n=react.useRef(null),i=react.useCallback(s=>{let a=e.map(l=>l.subscribe(s));return ()=>a.forEach(l=>l())},e),o=()=>{let s=e.map(l=>l.get()),a=t?t(s):s;return n.current!==null&&r(n.current,a)?n.current:(n.current=a,a)};return react.useSyncExternalStore(i,o,o)}function q({notifier:e,selector:t,areStatesEqual:r,stableChild:n,children:i}){let o=y(e,t,r);return i(o,n)}var A=react.memo(q,(e,t)=>t.isolated?true:e.notifier===t.notifier&&e.selector===t.selector&&e.areStatesEqual===t.areStatesEqual&&e.stableChild===t.stableChild&&e.children===t.children);function F({notifiers:e,selector:t,areStatesEqual:r,stableChild:n,children:i}){let o=S(e,t,r);return i(o,n)}var j=react.memo(F,(e,t)=>t.isolated?true:e.notifiers===t.notifiers&&e.selector===t.selector&&e.areStatesEqual===t.areStatesEqual&&e.stableChild===t.stableChild&&e.children===t.children);function O({children:e}){return jsxRuntime.jsx(jsxRuntime.Fragment,{children:e})}var K=react.memo(O,()=>true);exports.IsolatedContainer=K;exports.Listener=A;exports.MultiListener=j;exports.listener=w;exports.multiListener=N;exports.notifier=m;exports.useListener=y;exports.useMultiListener=S;exports.useNotifier=L;//# sourceMappingURL=index.cjs.map
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/utils/helpers.ts","../src/notifier.ts","../src/utils/create-notifier.ts","../src/utils/listener.ts","../src/utils/multi-listener.ts","../src/hooks/useNotifier.ts","../src/hooks/useListener.ts","../src/hooks/useMultiListener.ts","../src/components/Listener.tsx","../src/components/MultiListener.tsx","../src/components/IsolatedContainer.tsx"],"names":["isObjectOrArray","value","isArray","isFunction","NotifierRoot","state","RootNode","Node","callback","listener","child","prevState","newState","key","index","prevChildState","_","prop","childNode","ChildNode","root","fromChild","parent","parentState","newParentState","functionCannotBeNotifierMessage","createNotifier","initialState","createNotifierInternal","notifier","unsubscribe","multiListener","notifiers","unsubscribeFns","fn","useNotifier","useState","useListener","selector","areStatesEqual","useRef","useSyncExternalStore","nextState","areStatesEqualFn","prevValue","nextValue","useMultiListener","subscribe","useCallback","getSnapshot","ListenerComponent","stableChild","children","Listener","memo","prevProps","nextProps","MultiListenerComponent","MultiListener","IsolatedContainerComponent","jsx","Fragment","IsolatedContainer"],"mappings":"gFAAO,SAASA,CAAAA,CAAgBC,CAAAA,CAAiC,CAC7D,OAAO,OAAOA,GAAU,QAAA,EAAYA,CAAAA,GAAU,IAClD,CAEO,SAASC,CAAAA,CAAQD,EAAyC,CAC7D,OAAO,KAAA,CAAM,OAAA,CAAQA,CAAK,CAC9B,CAEO,SAASE,CAAAA,CAAWF,CAAAA,CAAmC,CAC1D,OAAO,OAAOA,GAAU,UAC5B,CCFO,IAAMG,CAAAA,CAAN,KAAmB,CACf,MACA,QAAA,CAEP,WAAA,CAAYC,CAAAA,CAAgB,CACxB,IAAA,CAAK,KAAA,CAAQA,EACb,IAAA,CAAK,QAA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AAaC,CAAAA,CAAU,QAG7C,ECvBA,SAASC,CAAAA,CAAuB,CAC5B,SAAA,CAAAzB,CAAAA,CACA,SAAAM,CAAAA,CACA,cAAA,CAAAC,CAAAA,CACA,WAAA,CAAAY,CAAAA,CACA,QAAA,CAAAC,CACJ,CAAA,CAAQ,CACJ,IAAM/C,CAAAA,CAAQyC,CAAAA,CAAiBd,CAAAA,CAAWM,EAAUC,CAAc,CAAA,CAClE,OAAOa,CAAAA,CAAS/C,CAAAA,CAAO8C,CAAW,CACtC,CAEO,IAAMO,CAAAA,CAA4CJ,UAAAA,CACrDG,CAAAA,CACA,CAACF,EAAWC,CAAAA,GACJA,CAAAA,CAAU,QAAA,CAAiB,IAAA,CAE3BD,CAAAA,CAAU,SAAA,GAAcC,EAAU,SAAA,EAClCD,CAAAA,CAAU,QAAA,GAAaC,CAAAA,CAAU,QAAA,EACjCD,CAAAA,CAAU,iBAAmBC,CAAAA,CAAU,cAAA,EACvCD,CAAAA,CAAU,WAAA,GAAgBC,CAAAA,CAAU,WAAA,EACpCD,EAAU,QAAA,GAAaC,CAAAA,CAAU,QAG7C,ECxBA,SAASG,CAAAA,CAA2B,CAAE,QAAA,CAAAP,CAAS,CAAA,CAA2B,CACtE,OAAOQ,cAAAA,CAAAC,mBAAAA,CAAA,CAAG,QAAA,CAAAT,CAAAA,CAAS,CACvB,CAEO,IAAMU,CAAAA,CAAoBR,UAAAA,CAC7BK,CAAAA,CACA,IAAM,IACV","file":"index.cjs","sourcesContent":["export function isObjectOrArray(value: unknown): value is object {\r\n return typeof value === \"object\" && value !== null;\r\n}\r\n\r\nexport function isArray(value: unknown): value is Array<unknown> {\r\n return Array.isArray(value);\r\n}\r\n\r\nexport function isFunction(value: unknown): value is Function {\r\n return typeof value === \"function\";\r\n}","import type {\r\n NodeKey,\r\n SetState,\r\n StateUpdater,\r\n VoidCallback\r\n} from \"./types\";\r\nimport { isArray, isFunction, isObjectOrArray } from \"./utils/helpers\";\r\n\r\nexport class NotifierRoot {\r\n public state: unknown;\r\n public rootNode: RootNode;\r\n\r\n constructor(state: unknown) {\r\n this.state = state;\r\n this.rootNode = new RootNode(this);\r\n }\r\n}\r\n\r\nabstract class Node {\r\n protected cachedState: unknown;\r\n private listeners: Set<VoidCallback>;\r\n private children: Map<NodeKey, ChildNode>;\r\n public proxy: object;\r\n\r\n constructor() {\r\n this.cachedState = null;\r\n this.listeners = new Set();\r\n this.children = new Map();\r\n this.proxy = this.createProxy();\r\n }\r\n\r\n public abstract get(): unknown;\r\n public abstract set(state: SetState<unknown>, fromChild: boolean): boolean;\r\n\r\n public dispatchSet = (state: SetState<unknown>) => {\r\n return this.set(state, false);\r\n };\r\n\r\n private subscribe = (callback: VoidCallback) => {\r\n this.listeners.add(callback);\r\n return () => {\r\n this.listeners.delete(callback);\r\n };\r\n };\r\n\r\n protected notifyListeners() {\r\n this.listeners.forEach((listener) => listener());\r\n }\r\n\r\n private notifySubtree() {\r\n this.cachedState = null;\r\n this.notifyListeners();\r\n for (const child of this.children.values()) {\r\n child.notifySubtree();\r\n }\r\n }\r\n\r\n protected reconcileChildren(prevState: unknown, newState: unknown) {\r\n if (!isArray(prevState) || !isArray(newState)) {\r\n return;\r\n }\r\n for (const [key, child] of this.children) {\r\n const index = key as `${number}`;\r\n const prevChildState = prevState[index];\r\n const newChildState = newState[index];\r\n if (newChildState !== prevChildState) {\r\n child.notifySubtree();\r\n }\r\n }\r\n }\r\n\r\n private setTrap() { return false; }\r\n\r\n private createProxy() {\r\n return new Proxy(\r\n {},\r\n {\r\n get: (_, prop) => {\r\n if (prop === \"get\") return this.get;\r\n if (prop === \"set\") return this.dispatchSet;\r\n if (prop === \"subscribe\") return this.subscribe;\r\n\r\n const state = this.get();\r\n if (isObjectOrArray(state) && prop in state) {\r\n const value = (state as any)[prop];\r\n if (isFunction(value)) return value;\r\n\r\n let childNode = this.children.get(prop);\r\n if (!childNode) {\r\n childNode = new ChildNode(prop, this);\r\n this.children.set(prop, childNode);\r\n }\r\n return childNode.proxy;\r\n }\r\n },\r\n set: this.setTrap\r\n }\r\n );\r\n }\r\n}\r\n\r\nclass RootNode extends Node {\r\n private root: NotifierRoot;\r\n\r\n constructor(root: NotifierRoot) {\r\n super();\r\n this.root = root;\r\n }\r\n\r\n public get = () => this.root.state;\r\n\r\n public set(state: SetState<unknown>, fromChild: boolean) {\r\n const prevState = this.get();\r\n const newState = isFunction(state)\r\n ? (state as StateUpdater<unknown>)(prevState)\r\n : state;\r\n if (newState !== prevState) {\r\n this.root.state = newState;\r\n this.notifyListeners();\r\n if (!fromChild) {\r\n this.reconcileChildren(prevState, newState);\r\n }\r\n return true;\r\n }\r\n return false;\r\n }\r\n}\r\n\r\nclass ChildNode extends Node {\r\n private key: NodeKey;\r\n private parent: Node;\r\n\r\n constructor(key: NodeKey, parent: Node) {\r\n super();\r\n this.key = key;\r\n this.parent = parent;\r\n }\r\n\r\n public get = (): unknown => {\r\n if (this.cachedState !== null) {\r\n return this.cachedState;\r\n }\r\n const state = (this.parent.get() as any)[this.key];\r\n this.cachedState = state;\r\n return state;\r\n }\r\n\r\n public set(state: SetState<unknown>, fromChild: boolean) {\r\n const parentState = this.parent.get();\r\n const prevState = (parentState as any)[this.key];\r\n const newState = isFunction(state)\r\n ? (state as StateUpdater<unknown>)(prevState)\r\n : state;\r\n if (newState !== prevState) {\r\n const newParentState = Object.assign(\r\n isArray(parentState) ? [] : {},\r\n parentState\r\n );\r\n (newParentState as any)[this.key] = newState;\r\n const isSet = this.parent.set(newParentState, true);\r\n if (isSet) {\r\n this.cachedState = newState;\r\n this.notifyListeners();\r\n if (!fromChild) {\r\n this.reconcileChildren(prevState, newState);\r\n }\r\n return true;\r\n }\r\n return false;\r\n }\r\n return false;\r\n }\r\n}","import { isFunction } from \"./helpers\";\r\nimport { NotifierRoot } from \"../notifier\";\r\nimport type { InitialState, Notifier } from \"../types\";\r\n\r\nconst functionCannotBeNotifierMessage =\r\n \"A function cannot be a notifier. Replace it with other value, or add it as a property/method inside an object, or don't wrap it with notifier.\";\r\n\r\nexport function createNotifier<T>(initialState: InitialState<T>): Notifier<T> {\r\n const state = (isFunction(initialState)\r\n ? (initialState as () => T)()\r\n : initialState) as T;\r\n if (isFunction(state)) {\r\n throw new Error(functionCannotBeNotifierMessage);\r\n }\r\n return createNotifierInternal(state);\r\n}\r\n\r\nfunction createNotifierInternal<T>(state: T): Notifier<T> {\r\n const root = new NotifierRoot(state);\r\n return root.rootNode.proxy as Notifier<T>;\r\n}","import type { ListenerCallback, Notifier, VoidCallback } from \"../types\";\r\n\r\nexport function listener<T>(\r\n notifier: Notifier<T>,\r\n callback: ListenerCallback<T>\r\n): VoidCallback {\r\n const unsubscribe = notifier.subscribe(() => {\r\n callback(notifier.get());\r\n });\r\n callback(notifier.get());\r\n return unsubscribe;\r\n}","import type {\r\n InferNotifierValues,\r\n ListenerCallback,\r\n Notifier,\r\n VoidCallback\r\n} from \"../types\";\r\n\r\nexport function multiListener<\r\n const T extends readonly Notifier<unknown>[]\r\n>(\r\n notifiers: T,\r\n callback: ListenerCallback<InferNotifierValues<T>>\r\n): VoidCallback {\r\n const listener = () => {\r\n const state = notifiers.map((notifier) => notifier.get()) as any;\r\n callback(state);\r\n };\r\n const unsubscribeFns = notifiers.map((notifier) => {\r\n return notifier.subscribe(listener);\r\n });\r\n const unsubscribe = () => unsubscribeFns.forEach((fn) => fn());\r\n listener();\r\n return unsubscribe;\r\n}","import { useState } from \"react\";\r\n\r\nimport type { InitialState, Notifier } from \"../types\";\r\nimport { createNotifier } from \"../utils/create-notifier\";\r\n\r\nexport function useNotifier<T>(initialState: InitialState<T>): Notifier<T> {\r\n const [notifier] = useState(() => createNotifier(initialState));\r\n return notifier;\r\n}","import { useRef, useSyncExternalStore } from \"react\";\r\n\r\nimport type { Notifier, StatesComparer, StateSelector } from \"../types\";\r\n\r\nexport function useListener<T, U = T>(\r\n notifier: Notifier<T>,\r\n selector?: StateSelector<T, U>,\r\n areStatesEqual?: StatesComparer<U>\r\n): U {\r\n const prevState = useRef<U>(null);\r\n\r\n return useSyncExternalStore(\r\n notifier.subscribe,\r\n () => {\r\n const nextState = selector\r\n ? selector(notifier.get())\r\n : notifier.get() as unknown as U;\r\n if (areStatesEqual && prevState.current !== null) {\r\n const areEqual = areStatesEqual(prevState.current, nextState);\r\n if (areEqual) return prevState.current;\r\n }\r\n prevState.current = nextState;\r\n return nextState;\r\n },\r\n () => (selector ? selector(notifier.get()) : notifier.get() as unknown as U)\r\n );\r\n}","import { useCallback, useRef, useSyncExternalStore } from \"react\";\r\n\r\nimport type {\r\n InferNotifierValues,\r\n Notifier,\r\n StatesComparer,\r\n StateSelector,\r\n VoidCallback\r\n} from \"../types\";\r\nimport { isArray } from \"../utils/helpers\";\r\n\r\nfunction areStatesEqualFn<T>(prevState: T, nextState: T) {\r\n if (\r\n isArray(prevState) &&\r\n isArray(nextState) &&\r\n prevState.length === nextState.length\r\n ) {\r\n return prevState.every((prevValue, index) => {\r\n const nextValue = nextState[index];\r\n return prevValue === nextValue;\r\n });\r\n }\r\n return prevState === nextState;\r\n};\r\n\r\nexport function useMultiListener<\r\n const T extends readonly Notifier<unknown>[],\r\n const U\r\n>(\r\n notifiers: T,\r\n selector: StateSelector<InferNotifierValues<T>, U>,\r\n areStatesEqual?: StatesComparer<U>\r\n): U;\r\n\r\nexport function useMultiListener<\r\n const T extends readonly Notifier<unknown>[]\r\n>(\r\n notifiers: T,\r\n selector?: undefined,\r\n areStatesEqual?: StatesComparer<InferNotifierValues<T>>\r\n): InferNotifierValues<T>;\r\n\r\nexport function useMultiListener(\r\n notifiers: Notifier<unknown>[],\r\n selector?: StateSelector<any, any>,\r\n areStatesEqual: StatesComparer<any> = areStatesEqualFn\r\n): unknown {\r\n const prevState = useRef<unknown>(null);\r\n\r\n const subscribe = useCallback((callback: VoidCallback) => {\r\n const unsubscribeFns = notifiers.map((notifier) => {\r\n return notifier.subscribe(callback);\r\n });\r\n return () => unsubscribeFns.forEach((fn) => fn());\r\n }, notifiers);\r\n\r\n const getSnapshot = () => {\r\n const state = notifiers.map((notifier) => notifier.get());\r\n const nextState = selector ? selector(state) : state;\r\n if (prevState.current !== null) {\r\n const areEqual = areStatesEqual(prevState.current, nextState);\r\n if (areEqual) return prevState.current;\r\n }\r\n prevState.current = nextState;\r\n return nextState;\r\n };\r\n\r\n return useSyncExternalStore(\r\n subscribe,\r\n getSnapshot,\r\n getSnapshot\r\n );\r\n}","import { memo } from \"react\";\r\n\r\nimport type { ListenerProps } from \"../types\";\r\nimport { useListener } from \"../hooks/useListener\";\r\n\r\nfunction ListenerComponent<T, U = T>({\r\n notifier,\r\n selector,\r\n areStatesEqual,\r\n stableChild,\r\n children\r\n}: ListenerProps<T, U>) {\r\n const state = useListener(notifier, selector, areStatesEqual);\r\n return children(state, stableChild);\r\n}\r\n\r\nexport const Listener = memo(\r\n ListenerComponent,\r\n (prevProps, nextProps) => {\r\n if (nextProps.isolated) return true;\r\n return (\r\n prevProps.notifier === nextProps.notifier &&\r\n prevProps.selector === nextProps.selector &&\r\n prevProps.areStatesEqual === nextProps.areStatesEqual &&\r\n prevProps.stableChild === nextProps.stableChild &&\r\n prevProps.children === nextProps.children\r\n );\r\n }\r\n) as typeof ListenerComponent;","import { memo } from \"react\";\r\n\r\nimport { useMultiListener } from \"../hooks/useMultiListener\";\r\nimport type { MultiListenerComponent as MultiListenerComponentType } from \"../types\";\r\n\r\nfunction MultiListenerComponent({\r\n notifiers,\r\n selector,\r\n areStatesEqual,\r\n stableChild,\r\n children\r\n}: any) {\r\n const state = useMultiListener(notifiers, selector, areStatesEqual);\r\n return children(state, stableChild);\r\n}\r\n\r\nexport const MultiListener: MultiListenerComponentType = memo(\r\n MultiListenerComponent,\r\n (prevProps, nextProps) => {\r\n if (nextProps.isolated) return true;\r\n return (\r\n prevProps.notifiers === nextProps.notifiers &&\r\n prevProps.selector === nextProps.selector &&\r\n prevProps.areStatesEqual === nextProps.areStatesEqual &&\r\n prevProps.stableChild === nextProps.stableChild &&\r\n prevProps.children === nextProps.children\r\n );\r\n }\r\n);","import { memo } from \"react\";\r\n\r\nimport type { IsolatedContainerProps } from \"../types\";\r\n\r\nfunction IsolatedContainerComponent({ children }: IsolatedContainerProps) {\r\n return <>{children}</>;\r\n}\r\n\r\nexport const IsolatedContainer = memo(\r\n IsolatedContainerComponent,\r\n () => true\r\n) as typeof IsolatedContainerComponent;"]}
1
+ {"version":3,"sources":["../src/utils/helpers.ts","../src/notifier.ts","../src/utils/notifier.ts","../src/utils/listener.ts","../src/utils/multi-listener.ts","../src/hooks/useNotifier.ts","../src/hooks/useListener.ts","../src/hooks/useMultiListener.ts","../src/components/Listener.tsx","../src/components/MultiListener.tsx","../src/components/IsolatedContainer.tsx"],"names":["isObjectOrArray","value","isArray","isFunction","NotifierRoot","state","RootNode","Node","callback","listener","child","prevState","newState","key","index","prevChildState","_","prop","childNode","ChildNode","root","fromChild","parent","parentState","newParentState","functionCannotBeNotifierMessage","notifier","initialState","createNotifier","unsubscribe","multiListener","notifiers","unsubscribeFns","fn","useNotifier","useState","useListener","selector","areStatesEqual","useRef","useSyncExternalStore","nextState","areStatesEqualFn","prevValue","nextValue","useMultiListener","subscribe","useCallback","getSnapshot","ListenerComponent","stableChild","children","Listener","memo","prevProps","nextProps","MultiListenerComponent","MultiListener","IsolatedContainerComponent","jsx","Fragment","IsolatedContainer"],"mappings":"gFAAO,SAASA,CAAAA,CAAgBC,CAAAA,CAAiC,CAC7D,OAAO,OAAOA,GAAU,QAAA,EAAYA,CAAAA,GAAU,IAClD,CAEO,SAASC,CAAAA,CAAQD,EAAyC,CAC7D,OAAO,KAAA,CAAM,OAAA,CAAQA,CAAK,CAC9B,CAEO,SAASE,CAAAA,CAAWF,CAAAA,CAAmC,CAC1D,OAAO,OAAOA,GAAU,UAC5B,CCFO,IAAMG,CAAAA,CAAN,KAAmB,CACf,MACA,QAAA,CAEP,WAAA,CAAYC,CAAAA,CAAgB,CACxB,IAAA,CAAK,KAAA,CAAQA,EACb,IAAA,CAAK,QAAA,CA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AaC,CAAAA,CAAU,QAG7C,EC5CA,SAASC,CAAAA,CAAuB,CAC5B,SAAA,CAAAzB,CAAAA,CACA,SAAAM,CAAAA,CACA,cAAA,CAAAC,CAAAA,CACA,WAAA,CAAAY,CAAAA,CACA,QAAA,CAAAC,CACJ,CAAA,CAAQ,CACJ,IAAM9C,CAAAA,CAAQwC,CAAAA,CAAiBd,CAAAA,CAAWM,EAAUC,CAAc,CAAA,CAClE,OAAOa,CAAAA,CAAS9C,CAAAA,CAAO6C,CAAW,CACtC,CAuBO,IAAMO,CAAAA,CAA4CJ,UAAAA,CACrDG,CAAAA,CACA,CAACF,EAAWC,CAAAA,GACJA,CAAAA,CAAU,QAAA,CAAiB,IAAA,CAE3BD,CAAAA,CAAU,SAAA,GAAcC,EAAU,SAAA,EAClCD,CAAAA,CAAU,QAAA,GAAaC,CAAAA,CAAU,QAAA,EACjCD,CAAAA,CAAU,iBAAmBC,CAAAA,CAAU,cAAA,EACvCD,CAAAA,CAAU,WAAA,GAAgBC,CAAAA,CAAU,WAAA,EACpCD,EAAU,QAAA,GAAaC,CAAAA,CAAU,QAG7C,EC7CA,SAASG,CAAAA,CAA2B,CAAE,QAAA,CAAAP,CAAS,CAAA,CAA2B,CACtE,OAAOQ,cAAAA,CAAAC,mBAAAA,CAAA,CAAG,QAAA,CAAAT,CAAAA,CAAS,CACvB,CAwBO,IAAMU,CAAAA,CAAoBR,UAAAA,CAC7BK,CAAAA,CACA,IAAM,IACV","file":"index.cjs","sourcesContent":["export function isObjectOrArray(value: unknown): value is object {\r\n return typeof value === \"object\" && value !== null;\r\n}\r\n\r\nexport function isArray(value: unknown): value is Array<unknown> {\r\n return Array.isArray(value);\r\n}\r\n\r\nexport function isFunction(value: unknown): value is Function {\r\n return typeof value === \"function\";\r\n}","import type {\r\n NodeKey,\r\n SetState,\r\n StateUpdater,\r\n VoidCallback\r\n} from \"./types\";\r\nimport { isArray, isFunction, isObjectOrArray } from \"./utils/helpers\";\r\n\r\nexport class NotifierRoot {\r\n public state: unknown;\r\n public rootNode: RootNode;\r\n\r\n constructor(state: unknown) {\r\n this.state = state;\r\n this.rootNode = new RootNode(this);\r\n }\r\n}\r\n\r\nabstract class Node {\r\n protected cachedState: unknown;\r\n private listeners: Set<VoidCallback>;\r\n private children: Map<NodeKey, ChildNode>;\r\n public proxy: object;\r\n\r\n constructor() {\r\n this.cachedState = null;\r\n this.listeners = new Set();\r\n this.children = new Map();\r\n this.proxy = this.createProxy();\r\n }\r\n\r\n public abstract get(): unknown;\r\n public abstract set(state: SetState<unknown>, fromChild: boolean): boolean;\r\n\r\n public dispatchSet = (state: SetState<unknown>) => {\r\n return this.set(state, false);\r\n };\r\n\r\n private subscribe = (callback: VoidCallback) => {\r\n this.listeners.add(callback);\r\n return () => {\r\n this.listeners.delete(callback);\r\n };\r\n };\r\n\r\n protected notifyListeners() {\r\n this.listeners.forEach((listener) => listener());\r\n }\r\n\r\n private notifySubtree() {\r\n this.cachedState = null;\r\n this.notifyListeners();\r\n for (const child of this.children.values()) {\r\n child.notifySubtree();\r\n }\r\n }\r\n\r\n protected reconcileChildren(prevState: unknown, newState: unknown) {\r\n if (!isArray(prevState) || !isArray(newState)) {\r\n return;\r\n }\r\n for (const [key, child] of this.children) {\r\n const index = key as `${number}`;\r\n const prevChildState = prevState[index];\r\n const newChildState = newState[index];\r\n if (newChildState !== prevChildState) {\r\n child.notifySubtree();\r\n }\r\n }\r\n }\r\n\r\n private setTrap() { return false; }\r\n\r\n private createProxy() {\r\n return new Proxy(\r\n {},\r\n {\r\n get: (_, prop) => {\r\n if (prop === \"get\") return this.get;\r\n if (prop === \"set\") return this.dispatchSet;\r\n if (prop === \"subscribe\") return this.subscribe;\r\n\r\n const state = this.get();\r\n if (isObjectOrArray(state) && prop in state) {\r\n const value = (state as any)[prop];\r\n if (isFunction(value)) return value;\r\n\r\n let childNode = this.children.get(prop);\r\n if (!childNode) {\r\n childNode = new ChildNode(prop, this);\r\n this.children.set(prop, childNode);\r\n }\r\n return childNode.proxy;\r\n }\r\n },\r\n set: this.setTrap\r\n }\r\n );\r\n }\r\n}\r\n\r\nclass RootNode extends Node {\r\n private root: NotifierRoot;\r\n\r\n constructor(root: NotifierRoot) {\r\n super();\r\n this.root = root;\r\n }\r\n\r\n public get = () => this.root.state;\r\n\r\n public set(state: SetState<unknown>, fromChild: boolean) {\r\n const prevState = this.get();\r\n const newState = isFunction(state)\r\n ? (state as StateUpdater<unknown>)(prevState)\r\n : state;\r\n if (newState !== prevState) {\r\n this.root.state = newState;\r\n this.notifyListeners();\r\n if (!fromChild) {\r\n this.reconcileChildren(prevState, newState);\r\n }\r\n return true;\r\n }\r\n return false;\r\n }\r\n}\r\n\r\nclass ChildNode extends Node {\r\n private key: NodeKey;\r\n private parent: Node;\r\n\r\n constructor(key: NodeKey, parent: Node) {\r\n super();\r\n this.key = key;\r\n this.parent = parent;\r\n }\r\n\r\n public get = (): unknown => {\r\n if (this.cachedState !== null) {\r\n return this.cachedState;\r\n }\r\n const state = (this.parent.get() as any)[this.key];\r\n this.cachedState = state;\r\n return state;\r\n }\r\n\r\n public set(state: SetState<unknown>, fromChild: boolean) {\r\n const parentState = this.parent.get();\r\n const prevState = (parentState as any)[this.key];\r\n const newState = isFunction(state)\r\n ? (state as StateUpdater<unknown>)(prevState)\r\n : state;\r\n if (newState !== prevState) {\r\n const newParentState = Object.assign(\r\n isArray(parentState) ? [] : {},\r\n parentState\r\n );\r\n (newParentState as any)[this.key] = newState;\r\n const isSet = this.parent.set(newParentState, true);\r\n if (isSet) {\r\n this.cachedState = newState;\r\n this.notifyListeners();\r\n if (!fromChild) {\r\n this.reconcileChildren(prevState, newState);\r\n }\r\n return true;\r\n }\r\n return false;\r\n }\r\n return false;\r\n }\r\n}","import { isFunction } from \"./helpers\";\r\nimport { NotifierRoot } from \"../notifier\";\r\nimport type { InitialState, Notifier } from \"../types\";\r\n\r\nconst functionCannotBeNotifierMessage =\r\n \"A function cannot be a notifier. Replace it with other value, or add it as a property/method inside an object, or don't wrap it with notifier.\";\r\n\r\n/**\r\n * Creates a core, global root-level state container wrapped in a deeply reactive proxy tree.\r\n * \r\n * @remarks\r\n * Serves as the primary entry point for global stores outside of React. Every nested property \r\n * automatically becomes a sub-notifier. Raw functions cannot be notifiers and will throw an error.\r\n * \r\n * @see [Barrex Docs](./docs/utils/notifier.md)\r\n * \r\n * @template T The shape of the initial state.\r\n * @param initialState The initial value of the state, or a lazy initialization function returning the state.\r\n * @returns A root Notifier proxy instance.\r\n * \r\n * @example\r\n * ```ts\r\n * import { notifier } from \"barrex\";\r\n * \r\n * const authNotifier = notifier({\r\n * user: null,\r\n * isAuthenticated: false\r\n * });\r\n * ```\r\n */\r\nexport function notifier<T>(initialState: InitialState<T>): Notifier<T> {\r\n const state = (isFunction(initialState)\r\n ? (initialState as () => T)()\r\n : initialState) as T;\r\n if (isFunction(state)) {\r\n throw new Error(functionCannotBeNotifierMessage);\r\n }\r\n return createNotifier(state);\r\n}\r\n\r\nfunction createNotifier<T>(state: T): Notifier<T> {\r\n const root = new NotifierRoot(state);\r\n return root.rootNode.proxy as Notifier<T>;\r\n}","import type { ListenerCallback, Notifier, VoidCallback } from \"../types\";\r\n\r\n/**\r\n * Subscribes imperatively to a `barrex` notifier outside of React component trees.\r\n * \r\n * @remarks\r\n * Executes the callback immediately upon registration with the current state value, \r\n * and subsequently every time the notifier updates. Returns a cleanup function to unsubscribe.\r\n * \r\n * @see [Barrex Docs](./docs/utils/listener.md)\r\n * \r\n * @param notifier The source notifier instance to subscribe to.\r\n * @param callback A callback function invoked immediately and on every state update.\r\n * @returns A cleanup function to remove the subscription.\r\n * \r\n * @example\r\n * ```ts\r\n * import { listener } from \"barrex\";\r\n * \r\n * const unsubscribe = listener(themeNotifier, (theme) => {\r\n * localStorage.setItem(\"theme\", theme);\r\n * });\r\n * ```\r\n */\r\nexport function listener<T>(\r\n notifier: Notifier<T>,\r\n callback: ListenerCallback<T>\r\n): VoidCallback {\r\n const unsubscribe = notifier.subscribe(() => {\r\n callback(notifier.get());\r\n });\r\n callback(notifier.get());\r\n return unsubscribe;\r\n}","import type {\r\n InferNotifierValues,\r\n ListenerCallback,\r\n Notifier,\r\n VoidCallback\r\n} from \"../types\";\r\n\r\n/**\r\n * Subscribes imperatively to multiple `barrex` notifiers simultaneously outside of React.\r\n * \r\n * @remarks\r\n * Accepts a tuple array of notifiers, infers their types automatically, executes \r\n * the callback immediately with the initial values tuple, and returns a single composite cleanup function.\r\n * \r\n * @see [Barrex Docs](./docs/utils/multi-listener.md)\r\n * \r\n * @param notifiers A readonly tuple array of source notifiers to subscribe to.\r\n * @param callback A callback function invoked immediately with the values tuple and on subsequent updates.\r\n * @returns A composite cleanup function to unsubscribe from all provided notifiers.\r\n * \r\n * @example\r\n * ```ts\r\n * import { multiListener } from \"barrex\";\r\n * \r\n * const unsubscribe = multiListener(\r\n * [userNotifier.name, settingsNotifier.theme],\r\n * ([name, theme]) => {\r\n * console.log(`User: ${name}, Theme: ${theme}`);\r\n * }\r\n * );\r\n * ```\r\n */\r\nexport function multiListener<\r\n const T extends readonly Notifier<unknown>[]\r\n>(\r\n notifiers: T,\r\n callback: ListenerCallback<InferNotifierValues<T>>\r\n): VoidCallback {\r\n const listener = () => {\r\n const state = notifiers.map((notifier) => notifier.get()) as any;\r\n callback(state);\r\n };\r\n const unsubscribeFns = notifiers.map((notifier) => {\r\n return notifier.subscribe(listener);\r\n });\r\n const unsubscribe = () => unsubscribeFns.forEach((fn) => fn());\r\n listener();\r\n return unsubscribe;\r\n}","import { useState } from \"react\";\r\n\r\nimport type { InitialState, Notifier } from \"../types\";\r\nimport { notifier as createNotifier } from \"../utils/notifier\";\r\n\r\n/**\r\n * Creates a component-scoped, lazy-initialized notifier with automatic \r\n * proxy-driven nested reactivity.\r\n * \r\n * @remarks\r\n * Tied directly to the component lifecycle. Every property on the initial state \r\n * object automatically becomes a first-class sub-notifier.\r\n * \r\n * @see [Barrex Docs](./docs/hooks/use-notifier.md)\r\n * \r\n * @template T The shape of the initial state.\r\n * @param initialState The initial value or a lazy initializer function returning the state.\r\n * @returns A root Notifier proxy instance.\r\n * \r\n * @example\r\n * ```tsx\r\n * import { useNotifier, useListener } from \"barrex\";\r\n * \r\n * function Profile() {\r\n * const userNotifier = useNotifier({ name: \"Alice\", age: 40 });\r\n * const name = useListener(userNotifier.name);\r\n * return <p>{name}</p>;\r\n * }\r\n * ```\r\n */\r\nexport function useNotifier<T>(initialState: InitialState<T>): Notifier<T> {\r\n const [notifier] = useState(() => createNotifier(initialState));\r\n return notifier;\r\n}","import { useRef, useSyncExternalStore } from \"react\";\r\n\r\nimport type { Notifier, StatesComparer, StateSelector } from \"../types\";\r\n\r\n/**\r\n * Subscribes a React component to a `barrex` notifier, enabling precise, \r\n * fine-grained reactivity using `useSyncExternalStore`.\r\n * \r\n * @remarks\r\n * Components re-render only when the exact state slice or computed selector \r\n * value updates, preventing unnecessary re-renders across the tree.\r\n * \r\n * @see [Barrex Docs](./docs/hooks/use-listener.md)\r\n * \r\n * @template T The raw type of the source notifier state.\r\n * @template U The resolved type after applying the selector.\r\n * \r\n * @param notifier The source notifier instance to subscribe to.\r\n * @param selector Optional function to transform or compute a specific slice from the notifier state.\r\n * @param areStatesEqual Optional custom comparer. Returns true to reuse the previous state reference and skip re-renders.\r\n * @returns The current resolved state (raw or selected).\r\n * \r\n * @example\r\n * ```tsx\r\n * import { useListener } from \"barrex\";\r\n * \r\n * function Counter() {\r\n * const count = useListener(counterNotifier);\r\n * return <span>Count: {count}</span>;\r\n * }\r\n * ```\r\n */\r\nexport function useListener<T, U = T>(\r\n notifier: Notifier<T>,\r\n selector?: StateSelector<T, U>,\r\n areStatesEqual?: StatesComparer<U>\r\n): U {\r\n const prevState = useRef<U>(null);\r\n\r\n return useSyncExternalStore(\r\n notifier.subscribe,\r\n () => {\r\n const nextState = selector\r\n ? selector(notifier.get())\r\n : notifier.get() as unknown as U;\r\n if (areStatesEqual && prevState.current !== null) {\r\n const areEqual = areStatesEqual(prevState.current, nextState);\r\n if (areEqual) return prevState.current;\r\n }\r\n prevState.current = nextState;\r\n return nextState;\r\n },\r\n () => (selector ? selector(notifier.get()) : notifier.get() as unknown as U)\r\n );\r\n}","import { useCallback, useRef, useSyncExternalStore } from \"react\";\r\n\r\nimport type {\r\n InferNotifierValues,\r\n Notifier,\r\n StatesComparer,\r\n StateSelector,\r\n VoidCallback\r\n} from \"../types\";\r\nimport { isArray } from \"../utils/helpers\";\r\n\r\nfunction areStatesEqualFn<T>(prevState: T, nextState: T) {\r\n if (\r\n isArray(prevState) &&\r\n isArray(nextState) &&\r\n prevState.length === nextState.length\r\n ) {\r\n return prevState.every((prevValue, index) => {\r\n const nextValue = nextState[index];\r\n return prevValue === nextValue;\r\n });\r\n }\r\n return prevState === nextState;\r\n};\r\n\r\n/**\r\n * Subscribes a React component to multiple notifiers simultaneously, \r\n * offering complete type safety and optimized derivations.\r\n * \r\n * @remarks\r\n * Bundles multiple notifier subscriptions into a single unified cleanup routine \r\n * and includes smart array/tuple shallow comparison by default.\r\n * \r\n * @see [Barrex Docs](./docs/hooks/use-multi-listener.md)\r\n * \r\n * @param notifiers A readonly tuple array of source notifiers to subscribe to.\r\n * @param selector Optional function to transform or combine the tuple values into a custom shape.\r\n * @param areStatesEqual Optional custom comparator. Defaults to a smart tuple-aware shallow equality checker.\r\n * @returns The resolved state tuple or the computed selector output.\r\n * \r\n * @example\r\n * ```tsx\r\n * import { useMultiListener } from \"barrex\";\r\n * \r\n * function Header() {\r\n * const [name, theme] = useMultiListener([userNotifier.name, themeNotifier]);\r\n * return <header className={theme}>Welcome, {name}</header>;\r\n * }\r\n * ```\r\n */\r\nexport function useMultiListener<\r\n const T extends readonly Notifier<unknown>[],\r\n const U\r\n>(\r\n notifiers: T,\r\n selector: StateSelector<InferNotifierValues<T>, U>,\r\n areStatesEqual?: StatesComparer<U>\r\n): U;\r\n\r\nexport function useMultiListener<\r\n const T extends readonly Notifier<unknown>[]\r\n>(\r\n notifiers: T,\r\n selector?: undefined,\r\n areStatesEqual?: StatesComparer<InferNotifierValues<T>>\r\n): InferNotifierValues<T>;\r\n\r\nexport function useMultiListener(\r\n notifiers: Notifier<unknown>[],\r\n selector?: StateSelector<any, any>,\r\n areStatesEqual: StatesComparer<any> = areStatesEqualFn\r\n): unknown {\r\n const prevState = useRef<unknown>(null);\r\n\r\n const subscribe = useCallback((callback: VoidCallback) => {\r\n const unsubscribeFns = notifiers.map((notifier) => {\r\n return notifier.subscribe(callback);\r\n });\r\n return () => unsubscribeFns.forEach((fn) => fn());\r\n }, notifiers);\r\n\r\n const getSnapshot = () => {\r\n const state = notifiers.map((notifier) => notifier.get());\r\n const nextState = selector ? selector(state) : state;\r\n if (prevState.current !== null) {\r\n const areEqual = areStatesEqual(prevState.current, nextState);\r\n if (areEqual) return prevState.current;\r\n }\r\n prevState.current = nextState;\r\n return nextState;\r\n };\r\n\r\n return useSyncExternalStore(\r\n subscribe,\r\n getSnapshot,\r\n getSnapshot\r\n );\r\n}","import { memo } from \"react\";\r\n\r\nimport type { ListenerProps } from \"../types\";\r\nimport { useListener } from \"../hooks/useListener\";\r\n\r\nfunction ListenerComponent<T, U = T>({\r\n notifier,\r\n selector,\r\n areStatesEqual,\r\n stableChild,\r\n children\r\n}: ListenerProps<T, U>) {\r\n const state = useListener(notifier, selector, areStatesEqual);\r\n return children(state, stableChild);\r\n}\r\n\r\n/**\r\n * A declarative component wrapper that brings fine-grained reactivity \r\n * and granular performance optimization directly into your JSX tree.\r\n * \r\n * @remarks\r\n * Confines updates strictly to its render-prop children without forcing \r\n * an entire component function to re-render.\r\n * \r\n * @see [Barrex Docs](./docs/components/listener.md)\r\n * \r\n * @param props The component props.\r\n * \r\n * @example\r\n * ```tsx\r\n * import { Listener } from \"barrex\";\r\n * \r\n * <Listener notifier={userNotifier.name}>\r\n * {(name) => <h1>Welcome, {name}!</h1>}\r\n * </Listener>\r\n * ```\r\n */\r\nexport const Listener = memo(\r\n ListenerComponent,\r\n (prevProps, nextProps) => {\r\n if (nextProps.isolated) return true;\r\n return (\r\n prevProps.notifier === nextProps.notifier &&\r\n prevProps.selector === nextProps.selector &&\r\n prevProps.areStatesEqual === nextProps.areStatesEqual &&\r\n prevProps.stableChild === nextProps.stableChild &&\r\n prevProps.children === nextProps.children\r\n );\r\n }\r\n) as typeof ListenerComponent;","import { memo } from \"react\";\r\n\r\nimport { useMultiListener } from \"../hooks/useMultiListener\";\r\nimport type { MultiListenerComponent as MultiListenerComponentType } from \"../types\";\r\n\r\nfunction MultiListenerComponent({\r\n notifiers,\r\n selector,\r\n areStatesEqual,\r\n stableChild,\r\n children\r\n}: any) {\r\n const state = useMultiListener(notifiers, selector, areStatesEqual);\r\n return children(state, stableChild);\r\n}\r\n\r\n/**\r\n * An advanced declarative component wrapper that enables fine-grained \r\n * subscriptions to multiple notifiers simultaneously with full type safety.\r\n * \r\n * @remarks\r\n * Automatically infers tuple types from the provided notifiers array \r\n * and includes smart array equality checking.\r\n * \r\n * @see [Barrex Docs](./docs/components/multi-listener.md)\r\n * \r\n * @param props The component props.\r\n * \r\n * @example\r\n * ```tsx\r\n * import { MultiListener } from \"barrex\";\r\n * \r\n * <MultiListener notifiers={[userNotifier.name, cartNotifier.count]}>\r\n * {([name, count]) => <p>{name} has {count} items</p>}\r\n * </MultiListener>\r\n * ```\r\n */\r\nexport const MultiListener: MultiListenerComponentType = memo(\r\n MultiListenerComponent,\r\n (prevProps, nextProps) => {\r\n if (nextProps.isolated) return true;\r\n return (\r\n prevProps.notifiers === nextProps.notifiers &&\r\n prevProps.selector === nextProps.selector &&\r\n prevProps.areStatesEqual === nextProps.areStatesEqual &&\r\n prevProps.stableChild === nextProps.stableChild &&\r\n prevProps.children === nextProps.children\r\n );\r\n }\r\n);","import { memo } from \"react\";\r\n\r\nimport type { IsolatedContainerProps } from \"../types\";\r\n\r\nfunction IsolatedContainerComponent({ children }: IsolatedContainerProps) {\r\n return <>{children}</>;\r\n}\r\n\r\n/**\r\n * A boundary component designed to completely shield its child \r\n * subtree from parent-driven re-render cascades.\r\n * \r\n * @remarks\r\n * Children inside an `IsolatedContainer` can still re-render independently \r\n * based on their own local state or external reactive bindings (like `barrex` notifiers).\r\n * \r\n * @see [Barrex Docs](./docs/components/isolated-container.md)\r\n * \r\n * @param props The component props.\r\n * \r\n * @example\r\n * ```tsx\r\n * import { IsolatedContainer } from \"barrex\";\r\n * \r\n * <IsolatedContainer>\r\n * <StaticHeader/>\r\n * <CounterWidget/>\r\n * </IsolatedContainer>\r\n * ```\r\n */\r\nexport const IsolatedContainer = memo(\r\n IsolatedContainerComponent,\r\n () => true\r\n) as typeof IsolatedContainerComponent;"]}
package/dist/index.d.cts CHANGED
@@ -1,11 +1,21 @@
1
1
  import * as react from 'react';
2
2
  import { JSX, ReactNode } from 'react';
3
3
 
4
+ /**
5
+ * The core methods available on every notifier node in the `barrex` tree.
6
+ */
4
7
  type BaseNotifier<T> = {
8
+ /** Retrieves the current value of the notifier state. */
5
9
  get(): T;
10
+ /** Updates the notifier state with a new value or an updater function, returning true if updated. */
6
11
  set(state: SetState<T>): boolean;
12
+ /** Subscribes a listener callback to state updates, returning an unsubscribe function. */
7
13
  subscribe(listener: VoidCallback): VoidCallback;
8
14
  };
15
+ /**
16
+ * Conditionally maps properties or array indices into nested sub-notifiers,
17
+ * while preserving functions as methods.
18
+ */
9
19
  type NotifierNode<T> = ([
10
20
  T
11
21
  ] extends [Array<any>] ? {
@@ -13,71 +23,338 @@ type NotifierNode<T> = ([
13
23
  } : [T] extends [object] ? {
14
24
  [K in keyof T]: T[K] extends Function ? T[K] : Notifier<T[K]>;
15
25
  } : object) & BaseNotifier<T>;
26
+ /**
27
+ * Represents a deeply reactive proxy container wrapping state of type `T`.
28
+ * Every nested property automatically becomes a sub-notifier. Raw functions cannot be notifiers.
29
+ */
16
30
  type Notifier<T> = ([T] extends [Function] ? never : NotifierNode<T>) & {};
31
+ /**
32
+ * Shared base props for declarative listener components.
33
+ */
17
34
  interface BaseListenerProps {
35
+ /** A pre-rendered element passed to the render function that is insulated from state updates. */
18
36
  stableChild?: JSX.Element;
37
+ /**
38
+ * When true, prevents the component from re-rendering when its parent component re-renders.
39
+ * @default false
40
+ */
19
41
  isolated?: boolean;
20
42
  }
43
+ /**
44
+ * Props for the single `Listener` component wrapper.
45
+ */
21
46
  interface ListenerProps<T, U = T> extends BaseListenerProps {
47
+ /** The source notifier instance to listen to. */
22
48
  notifier: Notifier<T>;
49
+ /** Optional function to transform or compute a specific slice from the notifier state. */
23
50
  selector?: StateSelector<T, U>;
51
+ /** Optional custom comparer. Returns true to skip re-rendering when states are equal. */
24
52
  areStatesEqual?: StatesComparer<U>;
53
+ /** A render-prop function receiving the reactive state and optional stableChild. */
25
54
  children: ChildrenRenderer<U>;
26
55
  }
56
+ /**
57
+ * Props for the `MultiListener` component.
58
+ */
27
59
  type MultiListenerProps<T extends readonly Notifier<unknown>[], U = InferNotifierValues<T>> = MultiListenerWithSelectorProps<T, U> | MultiListenerWithoutSelectorProps<T>;
60
+ /**
61
+ * Props for the `MultiListener` component when utilizing a selector transformation.
62
+ */
28
63
  interface MultiListenerWithSelectorProps<T extends readonly Notifier<unknown>[], U> extends BaseListenerProps {
64
+ /** A readonly tuple array of source notifiers to listen to. */
29
65
  notifiers: T;
66
+ /** Function to transform or combine the tuple values into a custom shape U. */
30
67
  selector: StateSelector<InferNotifierValues<T>, U>;
68
+ /**
69
+ * Optional custom comparer. Returns true to skip re-rendering when states are equal.
70
+ * Defaults to a smart tuple-aware shallow equality checker.
71
+ */
31
72
  areStatesEqual?: StatesComparer<U>;
73
+ /** A render-prop function receiving the resolved selector state and optional stableChild. */
32
74
  children: ChildrenRenderer<U>;
33
75
  }
76
+ /**
77
+ * Props for the `MultiListener` component without a selector (supplies raw values tuple).
78
+ */
34
79
  interface MultiListenerWithoutSelectorProps<T extends readonly Notifier<unknown>[]> extends BaseListenerProps {
80
+ /** A readonly tuple array of source notifiers to listen to. */
35
81
  notifiers: T;
36
82
  selector?: undefined;
83
+ /** Optional custom comparer for the state tuple. Defaults to a smart tuple-aware shallow equality checker. */
37
84
  areStatesEqual?: StatesComparer<InferNotifierValues<T>>;
85
+ /** A render-prop function receiving the resolved state tuple and optional stableChild. */
38
86
  children: ChildrenRenderer<InferNotifierValues<T>>;
39
87
  }
88
+ /**
89
+ * Overloaded component interface for `MultiListener` supporting both tuple and selector patterns.
90
+ */
40
91
  interface MultiListenerComponent {
41
92
  <const T extends readonly Notifier<unknown>[], const U>(props: MultiListenerWithSelectorProps<T, U>): ReactNode;
42
93
  <const T extends readonly Notifier<unknown>[]>(props: MultiListenerWithoutSelectorProps<T>): ReactNode;
43
94
  }
95
+ /**
96
+ * Props for the `IsolatedContainer` performance boundary component.
97
+ */
44
98
  interface IsolatedContainerProps {
45
99
  children: ReactNode;
46
100
  }
101
+ /** A standard callback function returning no value. */
47
102
  type VoidCallback = () => void;
103
+ /** A callback function receiving a state update of type `T`. */
48
104
  type ListenerCallback<T> = (state: T) => void;
105
+ /** A function returning an initial state value. */
49
106
  type StateInitializer<T> = () => T;
107
+ /** A function taking previous state and returning new state. */
50
108
  type StateUpdater<T> = (prevState: T) => T;
109
+ /** A function selecting or transforming state from `T` to `U`. */
51
110
  type StateSelector<T, U> = (state: T) => U;
111
+ /** A comparison function returning true if previous and next states are equal. */
52
112
  type StatesComparer<T> = (prevState: T, nextState: T) => boolean;
113
+ /** Accepts either a raw state value or an updater function. */
53
114
  type SetState<T> = T | StateUpdater<T>;
115
+ /** Initial state value or a lazy initializer function (excluding functions as root state). */
54
116
  type InitialState<T> = (T extends Function ? never : (T | StateInitializer<T>)) & {};
117
+ /** A render-prop function signature for rendering reactive components. */
55
118
  type ChildrenRenderer<T> = (state: T, stableChild?: JSX.Element) => ReactNode;
119
+ /** Helper type to extract the underlying value type T from a Notifier<T>. */
56
120
  type InferNotifierValue<N> = N extends Notifier<infer T> ? T : never;
121
+ /** Mapped tuple type to transform `[Notifier<A>, Notifier<B>]` into `[A, B]`. */
57
122
  type InferNotifierValues<T extends readonly Notifier<unknown>[]> = {
58
123
  [K in keyof T]: InferNotifierValue<T[K]>;
59
124
  };
60
125
 
61
- declare function createNotifier<T>(initialState: InitialState<T>): Notifier<T>;
126
+ /**
127
+ * Creates a core, global root-level state container wrapped in a deeply reactive proxy tree.
128
+ *
129
+ * @remarks
130
+ * Serves as the primary entry point for global stores outside of React. Every nested property
131
+ * automatically becomes a sub-notifier. Raw functions cannot be notifiers and will throw an error.
132
+ *
133
+ * @see [Barrex Docs](./docs/utils/notifier.md)
134
+ *
135
+ * @template T The shape of the initial state.
136
+ * @param initialState The initial value of the state, or a lazy initialization function returning the state.
137
+ * @returns A root Notifier proxy instance.
138
+ *
139
+ * @example
140
+ * ```ts
141
+ * import { notifier } from "barrex";
142
+ *
143
+ * const authNotifier = notifier({
144
+ * user: null,
145
+ * isAuthenticated: false
146
+ * });
147
+ * ```
148
+ */
149
+ declare function notifier<T>(initialState: InitialState<T>): Notifier<T>;
62
150
 
151
+ /**
152
+ * Subscribes imperatively to a `barrex` notifier outside of React component trees.
153
+ *
154
+ * @remarks
155
+ * Executes the callback immediately upon registration with the current state value,
156
+ * and subsequently every time the notifier updates. Returns a cleanup function to unsubscribe.
157
+ *
158
+ * @see [Barrex Docs](./docs/utils/listener.md)
159
+ *
160
+ * @param notifier The source notifier instance to subscribe to.
161
+ * @param callback A callback function invoked immediately and on every state update.
162
+ * @returns A cleanup function to remove the subscription.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * import { listener } from "barrex";
167
+ *
168
+ * const unsubscribe = listener(themeNotifier, (theme) => {
169
+ * localStorage.setItem("theme", theme);
170
+ * });
171
+ * ```
172
+ */
63
173
  declare function listener<T>(notifier: Notifier<T>, callback: ListenerCallback<T>): VoidCallback;
64
174
 
175
+ /**
176
+ * Subscribes imperatively to multiple `barrex` notifiers simultaneously outside of React.
177
+ *
178
+ * @remarks
179
+ * Accepts a tuple array of notifiers, infers their types automatically, executes
180
+ * the callback immediately with the initial values tuple, and returns a single composite cleanup function.
181
+ *
182
+ * @see [Barrex Docs](./docs/utils/multi-listener.md)
183
+ *
184
+ * @param notifiers A readonly tuple array of source notifiers to subscribe to.
185
+ * @param callback A callback function invoked immediately with the values tuple and on subsequent updates.
186
+ * @returns A composite cleanup function to unsubscribe from all provided notifiers.
187
+ *
188
+ * @example
189
+ * ```ts
190
+ * import { multiListener } from "barrex";
191
+ *
192
+ * const unsubscribe = multiListener(
193
+ * [userNotifier.name, settingsNotifier.theme],
194
+ * ([name, theme]) => {
195
+ * console.log(`User: ${name}, Theme: ${theme}`);
196
+ * }
197
+ * );
198
+ * ```
199
+ */
65
200
  declare function multiListener<const T extends readonly Notifier<unknown>[]>(notifiers: T, callback: ListenerCallback<InferNotifierValues<T>>): VoidCallback;
66
201
 
202
+ /**
203
+ * Creates a component-scoped, lazy-initialized notifier with automatic
204
+ * proxy-driven nested reactivity.
205
+ *
206
+ * @remarks
207
+ * Tied directly to the component lifecycle. Every property on the initial state
208
+ * object automatically becomes a first-class sub-notifier.
209
+ *
210
+ * @see [Barrex Docs](./docs/hooks/use-notifier.md)
211
+ *
212
+ * @template T The shape of the initial state.
213
+ * @param initialState The initial value or a lazy initializer function returning the state.
214
+ * @returns A root Notifier proxy instance.
215
+ *
216
+ * @example
217
+ * ```tsx
218
+ * import { useNotifier, useListener } from "barrex";
219
+ *
220
+ * function Profile() {
221
+ * const userNotifier = useNotifier({ name: "Alice", age: 40 });
222
+ * const name = useListener(userNotifier.name);
223
+ * return <p>{name}</p>;
224
+ * }
225
+ * ```
226
+ */
67
227
  declare function useNotifier<T>(initialState: InitialState<T>): Notifier<T>;
68
228
 
229
+ /**
230
+ * Subscribes a React component to a `barrex` notifier, enabling precise,
231
+ * fine-grained reactivity using `useSyncExternalStore`.
232
+ *
233
+ * @remarks
234
+ * Components re-render only when the exact state slice or computed selector
235
+ * value updates, preventing unnecessary re-renders across the tree.
236
+ *
237
+ * @see [Barrex Docs](./docs/hooks/use-listener.md)
238
+ *
239
+ * @template T The raw type of the source notifier state.
240
+ * @template U The resolved type after applying the selector.
241
+ *
242
+ * @param notifier The source notifier instance to subscribe to.
243
+ * @param selector Optional function to transform or compute a specific slice from the notifier state.
244
+ * @param areStatesEqual Optional custom comparer. Returns true to reuse the previous state reference and skip re-renders.
245
+ * @returns The current resolved state (raw or selected).
246
+ *
247
+ * @example
248
+ * ```tsx
249
+ * import { useListener } from "barrex";
250
+ *
251
+ * function Counter() {
252
+ * const count = useListener(counterNotifier);
253
+ * return <span>Count: {count}</span>;
254
+ * }
255
+ * ```
256
+ */
69
257
  declare function useListener<T, U = T>(notifier: Notifier<T>, selector?: StateSelector<T, U>, areStatesEqual?: StatesComparer<U>): U;
70
258
 
259
+ /**
260
+ * Subscribes a React component to multiple notifiers simultaneously,
261
+ * offering complete type safety and optimized derivations.
262
+ *
263
+ * @remarks
264
+ * Bundles multiple notifier subscriptions into a single unified cleanup routine
265
+ * and includes smart array/tuple shallow comparison by default.
266
+ *
267
+ * @see [Barrex Docs](./docs/hooks/use-multi-listener.md)
268
+ *
269
+ * @param notifiers A readonly tuple array of source notifiers to subscribe to.
270
+ * @param selector Optional function to transform or combine the tuple values into a custom shape.
271
+ * @param areStatesEqual Optional custom comparator. Defaults to a smart tuple-aware shallow equality checker.
272
+ * @returns The resolved state tuple or the computed selector output.
273
+ *
274
+ * @example
275
+ * ```tsx
276
+ * import { useMultiListener } from "barrex";
277
+ *
278
+ * function Header() {
279
+ * const [name, theme] = useMultiListener([userNotifier.name, themeNotifier]);
280
+ * return <header className={theme}>Welcome, {name}</header>;
281
+ * }
282
+ * ```
283
+ */
71
284
  declare function useMultiListener<const T extends readonly Notifier<unknown>[], const U>(notifiers: T, selector: StateSelector<InferNotifierValues<T>, U>, areStatesEqual?: StatesComparer<U>): U;
72
285
  declare function useMultiListener<const T extends readonly Notifier<unknown>[]>(notifiers: T, selector?: undefined, areStatesEqual?: StatesComparer<InferNotifierValues<T>>): InferNotifierValues<T>;
73
286
 
74
287
  declare function ListenerComponent<T, U = T>({ notifier, selector, areStatesEqual, stableChild, children }: ListenerProps<T, U>): react.ReactNode;
288
+ /**
289
+ * A declarative component wrapper that brings fine-grained reactivity
290
+ * and granular performance optimization directly into your JSX tree.
291
+ *
292
+ * @remarks
293
+ * Confines updates strictly to its render-prop children without forcing
294
+ * an entire component function to re-render.
295
+ *
296
+ * @see [Barrex Docs](./docs/components/listener.md)
297
+ *
298
+ * @param props The component props.
299
+ *
300
+ * @example
301
+ * ```tsx
302
+ * import { Listener } from "barrex";
303
+ *
304
+ * <Listener notifier={userNotifier.name}>
305
+ * {(name) => <h1>Welcome, {name}!</h1>}
306
+ * </Listener>
307
+ * ```
308
+ */
75
309
  declare const Listener: typeof ListenerComponent;
76
310
 
311
+ /**
312
+ * An advanced declarative component wrapper that enables fine-grained
313
+ * subscriptions to multiple notifiers simultaneously with full type safety.
314
+ *
315
+ * @remarks
316
+ * Automatically infers tuple types from the provided notifiers array
317
+ * and includes smart array equality checking.
318
+ *
319
+ * @see [Barrex Docs](./docs/components/multi-listener.md)
320
+ *
321
+ * @param props The component props.
322
+ *
323
+ * @example
324
+ * ```tsx
325
+ * import { MultiListener } from "barrex";
326
+ *
327
+ * <MultiListener notifiers={[userNotifier.name, cartNotifier.count]}>
328
+ * {([name, count]) => <p>{name} has {count} items</p>}
329
+ * </MultiListener>
330
+ * ```
331
+ */
77
332
  declare const MultiListener: MultiListenerComponent;
78
333
 
79
334
  declare function IsolatedContainerComponent({ children }: IsolatedContainerProps): react.JSX.Element;
335
+ /**
336
+ * A boundary component designed to completely shield its child
337
+ * subtree from parent-driven re-render cascades.
338
+ *
339
+ * @remarks
340
+ * Children inside an `IsolatedContainer` can still re-render independently
341
+ * based on their own local state or external reactive bindings (like `barrex` notifiers).
342
+ *
343
+ * @see [Barrex Docs](./docs/components/isolated-container.md)
344
+ *
345
+ * @param props The component props.
346
+ *
347
+ * @example
348
+ * ```tsx
349
+ * import { IsolatedContainer } from "barrex";
350
+ *
351
+ * <IsolatedContainer>
352
+ * <StaticHeader/>
353
+ * <CounterWidget/>
354
+ * </IsolatedContainer>
355
+ * ```
356
+ */
80
357
  declare const IsolatedContainer: typeof IsolatedContainerComponent;
81
358
 
82
- export { IsolatedContainer, Listener, MultiListener, createNotifier, listener, multiListener, useListener, useMultiListener, useNotifier };
359
+ export { IsolatedContainer, Listener, MultiListener, listener, multiListener, notifier, useListener, useMultiListener, useNotifier };
83
360
  export type { ChildrenRenderer, InitialState, IsolatedContainerProps, ListenerCallback, ListenerProps, MultiListenerComponent, MultiListenerProps, MultiListenerWithSelectorProps, MultiListenerWithoutSelectorProps, Notifier, SetState, StateInitializer, StateSelector, StateUpdater, StatesComparer, VoidCallback };
package/dist/index.d.ts CHANGED
@@ -1,11 +1,21 @@
1
1
  import * as react from 'react';
2
2
  import { JSX, ReactNode } from 'react';
3
3
 
4
+ /**
5
+ * The core methods available on every notifier node in the `barrex` tree.
6
+ */
4
7
  type BaseNotifier<T> = {
8
+ /** Retrieves the current value of the notifier state. */
5
9
  get(): T;
10
+ /** Updates the notifier state with a new value or an updater function, returning true if updated. */
6
11
  set(state: SetState<T>): boolean;
12
+ /** Subscribes a listener callback to state updates, returning an unsubscribe function. */
7
13
  subscribe(listener: VoidCallback): VoidCallback;
8
14
  };
15
+ /**
16
+ * Conditionally maps properties or array indices into nested sub-notifiers,
17
+ * while preserving functions as methods.
18
+ */
9
19
  type NotifierNode<T> = ([
10
20
  T
11
21
  ] extends [Array<any>] ? {
@@ -13,71 +23,338 @@ type NotifierNode<T> = ([
13
23
  } : [T] extends [object] ? {
14
24
  [K in keyof T]: T[K] extends Function ? T[K] : Notifier<T[K]>;
15
25
  } : object) & BaseNotifier<T>;
26
+ /**
27
+ * Represents a deeply reactive proxy container wrapping state of type `T`.
28
+ * Every nested property automatically becomes a sub-notifier. Raw functions cannot be notifiers.
29
+ */
16
30
  type Notifier<T> = ([T] extends [Function] ? never : NotifierNode<T>) & {};
31
+ /**
32
+ * Shared base props for declarative listener components.
33
+ */
17
34
  interface BaseListenerProps {
35
+ /** A pre-rendered element passed to the render function that is insulated from state updates. */
18
36
  stableChild?: JSX.Element;
37
+ /**
38
+ * When true, prevents the component from re-rendering when its parent component re-renders.
39
+ * @default false
40
+ */
19
41
  isolated?: boolean;
20
42
  }
43
+ /**
44
+ * Props for the single `Listener` component wrapper.
45
+ */
21
46
  interface ListenerProps<T, U = T> extends BaseListenerProps {
47
+ /** The source notifier instance to listen to. */
22
48
  notifier: Notifier<T>;
49
+ /** Optional function to transform or compute a specific slice from the notifier state. */
23
50
  selector?: StateSelector<T, U>;
51
+ /** Optional custom comparer. Returns true to skip re-rendering when states are equal. */
24
52
  areStatesEqual?: StatesComparer<U>;
53
+ /** A render-prop function receiving the reactive state and optional stableChild. */
25
54
  children: ChildrenRenderer<U>;
26
55
  }
56
+ /**
57
+ * Props for the `MultiListener` component.
58
+ */
27
59
  type MultiListenerProps<T extends readonly Notifier<unknown>[], U = InferNotifierValues<T>> = MultiListenerWithSelectorProps<T, U> | MultiListenerWithoutSelectorProps<T>;
60
+ /**
61
+ * Props for the `MultiListener` component when utilizing a selector transformation.
62
+ */
28
63
  interface MultiListenerWithSelectorProps<T extends readonly Notifier<unknown>[], U> extends BaseListenerProps {
64
+ /** A readonly tuple array of source notifiers to listen to. */
29
65
  notifiers: T;
66
+ /** Function to transform or combine the tuple values into a custom shape U. */
30
67
  selector: StateSelector<InferNotifierValues<T>, U>;
68
+ /**
69
+ * Optional custom comparer. Returns true to skip re-rendering when states are equal.
70
+ * Defaults to a smart tuple-aware shallow equality checker.
71
+ */
31
72
  areStatesEqual?: StatesComparer<U>;
73
+ /** A render-prop function receiving the resolved selector state and optional stableChild. */
32
74
  children: ChildrenRenderer<U>;
33
75
  }
76
+ /**
77
+ * Props for the `MultiListener` component without a selector (supplies raw values tuple).
78
+ */
34
79
  interface MultiListenerWithoutSelectorProps<T extends readonly Notifier<unknown>[]> extends BaseListenerProps {
80
+ /** A readonly tuple array of source notifiers to listen to. */
35
81
  notifiers: T;
36
82
  selector?: undefined;
83
+ /** Optional custom comparer for the state tuple. Defaults to a smart tuple-aware shallow equality checker. */
37
84
  areStatesEqual?: StatesComparer<InferNotifierValues<T>>;
85
+ /** A render-prop function receiving the resolved state tuple and optional stableChild. */
38
86
  children: ChildrenRenderer<InferNotifierValues<T>>;
39
87
  }
88
+ /**
89
+ * Overloaded component interface for `MultiListener` supporting both tuple and selector patterns.
90
+ */
40
91
  interface MultiListenerComponent {
41
92
  <const T extends readonly Notifier<unknown>[], const U>(props: MultiListenerWithSelectorProps<T, U>): ReactNode;
42
93
  <const T extends readonly Notifier<unknown>[]>(props: MultiListenerWithoutSelectorProps<T>): ReactNode;
43
94
  }
95
+ /**
96
+ * Props for the `IsolatedContainer` performance boundary component.
97
+ */
44
98
  interface IsolatedContainerProps {
45
99
  children: ReactNode;
46
100
  }
101
+ /** A standard callback function returning no value. */
47
102
  type VoidCallback = () => void;
103
+ /** A callback function receiving a state update of type `T`. */
48
104
  type ListenerCallback<T> = (state: T) => void;
105
+ /** A function returning an initial state value. */
49
106
  type StateInitializer<T> = () => T;
107
+ /** A function taking previous state and returning new state. */
50
108
  type StateUpdater<T> = (prevState: T) => T;
109
+ /** A function selecting or transforming state from `T` to `U`. */
51
110
  type StateSelector<T, U> = (state: T) => U;
111
+ /** A comparison function returning true if previous and next states are equal. */
52
112
  type StatesComparer<T> = (prevState: T, nextState: T) => boolean;
113
+ /** Accepts either a raw state value or an updater function. */
53
114
  type SetState<T> = T | StateUpdater<T>;
115
+ /** Initial state value or a lazy initializer function (excluding functions as root state). */
54
116
  type InitialState<T> = (T extends Function ? never : (T | StateInitializer<T>)) & {};
117
+ /** A render-prop function signature for rendering reactive components. */
55
118
  type ChildrenRenderer<T> = (state: T, stableChild?: JSX.Element) => ReactNode;
119
+ /** Helper type to extract the underlying value type T from a Notifier<T>. */
56
120
  type InferNotifierValue<N> = N extends Notifier<infer T> ? T : never;
121
+ /** Mapped tuple type to transform `[Notifier<A>, Notifier<B>]` into `[A, B]`. */
57
122
  type InferNotifierValues<T extends readonly Notifier<unknown>[]> = {
58
123
  [K in keyof T]: InferNotifierValue<T[K]>;
59
124
  };
60
125
 
61
- declare function createNotifier<T>(initialState: InitialState<T>): Notifier<T>;
126
+ /**
127
+ * Creates a core, global root-level state container wrapped in a deeply reactive proxy tree.
128
+ *
129
+ * @remarks
130
+ * Serves as the primary entry point for global stores outside of React. Every nested property
131
+ * automatically becomes a sub-notifier. Raw functions cannot be notifiers and will throw an error.
132
+ *
133
+ * @see [Barrex Docs](./docs/utils/notifier.md)
134
+ *
135
+ * @template T The shape of the initial state.
136
+ * @param initialState The initial value of the state, or a lazy initialization function returning the state.
137
+ * @returns A root Notifier proxy instance.
138
+ *
139
+ * @example
140
+ * ```ts
141
+ * import { notifier } from "barrex";
142
+ *
143
+ * const authNotifier = notifier({
144
+ * user: null,
145
+ * isAuthenticated: false
146
+ * });
147
+ * ```
148
+ */
149
+ declare function notifier<T>(initialState: InitialState<T>): Notifier<T>;
62
150
 
151
+ /**
152
+ * Subscribes imperatively to a `barrex` notifier outside of React component trees.
153
+ *
154
+ * @remarks
155
+ * Executes the callback immediately upon registration with the current state value,
156
+ * and subsequently every time the notifier updates. Returns a cleanup function to unsubscribe.
157
+ *
158
+ * @see [Barrex Docs](./docs/utils/listener.md)
159
+ *
160
+ * @param notifier The source notifier instance to subscribe to.
161
+ * @param callback A callback function invoked immediately and on every state update.
162
+ * @returns A cleanup function to remove the subscription.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * import { listener } from "barrex";
167
+ *
168
+ * const unsubscribe = listener(themeNotifier, (theme) => {
169
+ * localStorage.setItem("theme", theme);
170
+ * });
171
+ * ```
172
+ */
63
173
  declare function listener<T>(notifier: Notifier<T>, callback: ListenerCallback<T>): VoidCallback;
64
174
 
175
+ /**
176
+ * Subscribes imperatively to multiple `barrex` notifiers simultaneously outside of React.
177
+ *
178
+ * @remarks
179
+ * Accepts a tuple array of notifiers, infers their types automatically, executes
180
+ * the callback immediately with the initial values tuple, and returns a single composite cleanup function.
181
+ *
182
+ * @see [Barrex Docs](./docs/utils/multi-listener.md)
183
+ *
184
+ * @param notifiers A readonly tuple array of source notifiers to subscribe to.
185
+ * @param callback A callback function invoked immediately with the values tuple and on subsequent updates.
186
+ * @returns A composite cleanup function to unsubscribe from all provided notifiers.
187
+ *
188
+ * @example
189
+ * ```ts
190
+ * import { multiListener } from "barrex";
191
+ *
192
+ * const unsubscribe = multiListener(
193
+ * [userNotifier.name, settingsNotifier.theme],
194
+ * ([name, theme]) => {
195
+ * console.log(`User: ${name}, Theme: ${theme}`);
196
+ * }
197
+ * );
198
+ * ```
199
+ */
65
200
  declare function multiListener<const T extends readonly Notifier<unknown>[]>(notifiers: T, callback: ListenerCallback<InferNotifierValues<T>>): VoidCallback;
66
201
 
202
+ /**
203
+ * Creates a component-scoped, lazy-initialized notifier with automatic
204
+ * proxy-driven nested reactivity.
205
+ *
206
+ * @remarks
207
+ * Tied directly to the component lifecycle. Every property on the initial state
208
+ * object automatically becomes a first-class sub-notifier.
209
+ *
210
+ * @see [Barrex Docs](./docs/hooks/use-notifier.md)
211
+ *
212
+ * @template T The shape of the initial state.
213
+ * @param initialState The initial value or a lazy initializer function returning the state.
214
+ * @returns A root Notifier proxy instance.
215
+ *
216
+ * @example
217
+ * ```tsx
218
+ * import { useNotifier, useListener } from "barrex";
219
+ *
220
+ * function Profile() {
221
+ * const userNotifier = useNotifier({ name: "Alice", age: 40 });
222
+ * const name = useListener(userNotifier.name);
223
+ * return <p>{name}</p>;
224
+ * }
225
+ * ```
226
+ */
67
227
  declare function useNotifier<T>(initialState: InitialState<T>): Notifier<T>;
68
228
 
229
+ /**
230
+ * Subscribes a React component to a `barrex` notifier, enabling precise,
231
+ * fine-grained reactivity using `useSyncExternalStore`.
232
+ *
233
+ * @remarks
234
+ * Components re-render only when the exact state slice or computed selector
235
+ * value updates, preventing unnecessary re-renders across the tree.
236
+ *
237
+ * @see [Barrex Docs](./docs/hooks/use-listener.md)
238
+ *
239
+ * @template T The raw type of the source notifier state.
240
+ * @template U The resolved type after applying the selector.
241
+ *
242
+ * @param notifier The source notifier instance to subscribe to.
243
+ * @param selector Optional function to transform or compute a specific slice from the notifier state.
244
+ * @param areStatesEqual Optional custom comparer. Returns true to reuse the previous state reference and skip re-renders.
245
+ * @returns The current resolved state (raw or selected).
246
+ *
247
+ * @example
248
+ * ```tsx
249
+ * import { useListener } from "barrex";
250
+ *
251
+ * function Counter() {
252
+ * const count = useListener(counterNotifier);
253
+ * return <span>Count: {count}</span>;
254
+ * }
255
+ * ```
256
+ */
69
257
  declare function useListener<T, U = T>(notifier: Notifier<T>, selector?: StateSelector<T, U>, areStatesEqual?: StatesComparer<U>): U;
70
258
 
259
+ /**
260
+ * Subscribes a React component to multiple notifiers simultaneously,
261
+ * offering complete type safety and optimized derivations.
262
+ *
263
+ * @remarks
264
+ * Bundles multiple notifier subscriptions into a single unified cleanup routine
265
+ * and includes smart array/tuple shallow comparison by default.
266
+ *
267
+ * @see [Barrex Docs](./docs/hooks/use-multi-listener.md)
268
+ *
269
+ * @param notifiers A readonly tuple array of source notifiers to subscribe to.
270
+ * @param selector Optional function to transform or combine the tuple values into a custom shape.
271
+ * @param areStatesEqual Optional custom comparator. Defaults to a smart tuple-aware shallow equality checker.
272
+ * @returns The resolved state tuple or the computed selector output.
273
+ *
274
+ * @example
275
+ * ```tsx
276
+ * import { useMultiListener } from "barrex";
277
+ *
278
+ * function Header() {
279
+ * const [name, theme] = useMultiListener([userNotifier.name, themeNotifier]);
280
+ * return <header className={theme}>Welcome, {name}</header>;
281
+ * }
282
+ * ```
283
+ */
71
284
  declare function useMultiListener<const T extends readonly Notifier<unknown>[], const U>(notifiers: T, selector: StateSelector<InferNotifierValues<T>, U>, areStatesEqual?: StatesComparer<U>): U;
72
285
  declare function useMultiListener<const T extends readonly Notifier<unknown>[]>(notifiers: T, selector?: undefined, areStatesEqual?: StatesComparer<InferNotifierValues<T>>): InferNotifierValues<T>;
73
286
 
74
287
  declare function ListenerComponent<T, U = T>({ notifier, selector, areStatesEqual, stableChild, children }: ListenerProps<T, U>): react.ReactNode;
288
+ /**
289
+ * A declarative component wrapper that brings fine-grained reactivity
290
+ * and granular performance optimization directly into your JSX tree.
291
+ *
292
+ * @remarks
293
+ * Confines updates strictly to its render-prop children without forcing
294
+ * an entire component function to re-render.
295
+ *
296
+ * @see [Barrex Docs](./docs/components/listener.md)
297
+ *
298
+ * @param props The component props.
299
+ *
300
+ * @example
301
+ * ```tsx
302
+ * import { Listener } from "barrex";
303
+ *
304
+ * <Listener notifier={userNotifier.name}>
305
+ * {(name) => <h1>Welcome, {name}!</h1>}
306
+ * </Listener>
307
+ * ```
308
+ */
75
309
  declare const Listener: typeof ListenerComponent;
76
310
 
311
+ /**
312
+ * An advanced declarative component wrapper that enables fine-grained
313
+ * subscriptions to multiple notifiers simultaneously with full type safety.
314
+ *
315
+ * @remarks
316
+ * Automatically infers tuple types from the provided notifiers array
317
+ * and includes smart array equality checking.
318
+ *
319
+ * @see [Barrex Docs](./docs/components/multi-listener.md)
320
+ *
321
+ * @param props The component props.
322
+ *
323
+ * @example
324
+ * ```tsx
325
+ * import { MultiListener } from "barrex";
326
+ *
327
+ * <MultiListener notifiers={[userNotifier.name, cartNotifier.count]}>
328
+ * {([name, count]) => <p>{name} has {count} items</p>}
329
+ * </MultiListener>
330
+ * ```
331
+ */
77
332
  declare const MultiListener: MultiListenerComponent;
78
333
 
79
334
  declare function IsolatedContainerComponent({ children }: IsolatedContainerProps): react.JSX.Element;
335
+ /**
336
+ * A boundary component designed to completely shield its child
337
+ * subtree from parent-driven re-render cascades.
338
+ *
339
+ * @remarks
340
+ * Children inside an `IsolatedContainer` can still re-render independently
341
+ * based on their own local state or external reactive bindings (like `barrex` notifiers).
342
+ *
343
+ * @see [Barrex Docs](./docs/components/isolated-container.md)
344
+ *
345
+ * @param props The component props.
346
+ *
347
+ * @example
348
+ * ```tsx
349
+ * import { IsolatedContainer } from "barrex";
350
+ *
351
+ * <IsolatedContainer>
352
+ * <StaticHeader/>
353
+ * <CounterWidget/>
354
+ * </IsolatedContainer>
355
+ * ```
356
+ */
80
357
  declare const IsolatedContainer: typeof IsolatedContainerComponent;
81
358
 
82
- export { IsolatedContainer, Listener, MultiListener, createNotifier, listener, multiListener, useListener, useMultiListener, useNotifier };
359
+ export { IsolatedContainer, Listener, MultiListener, listener, multiListener, notifier, useListener, useMultiListener, useNotifier };
83
360
  export type { ChildrenRenderer, InitialState, IsolatedContainerProps, ListenerCallback, ListenerProps, MultiListenerComponent, MultiListenerProps, MultiListenerWithSelectorProps, MultiListenerWithoutSelectorProps, Notifier, SetState, StateInitializer, StateSelector, StateUpdater, StatesComparer, VoidCallback };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import {memo,useRef,useSyncExternalStore,useCallback,useState}from'react';import {jsx,Fragment}from'react/jsx-runtime';function b(e){return typeof e=="object"&&e!==null}function u(e){return Array.isArray(e)}function c(e){return typeof e=="function"}var f=class{state;rootNode;constructor(t){this.state=t,this.rootNode=new h(this);}},p=class{cachedState;listeners;children;proxy;constructor(){this.cachedState=null,this.listeners=new Set,this.children=new Map,this.proxy=this.createProxy();}dispatchSet=t=>this.set(t,false);subscribe=t=>(this.listeners.add(t),()=>{this.listeners.delete(t);});notifyListeners(){this.listeners.forEach(t=>t());}notifySubtree(){this.cachedState=null,this.notifyListeners();for(let t of this.children.values())t.notifySubtree();}reconcileChildren(t,r){if(!(!u(t)||!u(r)))for(let[n,i]of this.children){let o=n,s=t[o];r[o]!==s&&i.notifySubtree();}}setTrap(){return false}createProxy(){return new Proxy({},{get:(t,r)=>{if(r==="get")return this.get;if(r==="set")return this.dispatchSet;if(r==="subscribe")return this.subscribe;let n=this.get();if(b(n)&&r in n){let i=n[r];if(c(i))return i;let o=this.children.get(r);return o||(o=new d(r,this),this.children.set(r,o)),o.proxy}},set:this.setTrap})}},h=class extends p{root;constructor(t){super(),this.root=t;}get=()=>this.root.state;set(t,r){let n=this.get(),i=c(t)?t(n):t;return i!==n?(this.root.state=i,this.notifyListeners(),r||this.reconcileChildren(n,i),true):false}},d=class extends p{key;parent;constructor(t,r){super(),this.key=t,this.parent=r;}get=()=>{if(this.cachedState!==null)return this.cachedState;let t=this.parent.get()[this.key];return this.cachedState=t,t};set(t,r){let n=this.parent.get(),i=n[this.key],o=c(t)?t(i):t;if(o!==i){let s=Object.assign(u(n)?[]:{},n);return s[this.key]=o,this.parent.set(s,true)?(this.cachedState=o,this.notifyListeners(),r||this.reconcileChildren(i,o),true):false}return false}};var C="A function cannot be a notifier. Replace it with other value, or add it as a property/method inside an object, or don't wrap it with notifier.";function m(e){let t=c(e)?e():e;if(c(t))throw new Error(C);return k(t)}function k(e){return new f(e).rootNode.proxy}function N(e,t){let r=e.subscribe(()=>{t(e.get());});return t(e.get()),r}function w(e,t){let r=()=>{let o=e.map(s=>s.get());t(o);},n=e.map(o=>o.subscribe(r)),i=()=>n.forEach(o=>o());return r(),i}function L(e){let[t]=useState(()=>m(e));return t}function y(e,t,r){let n=useRef(null);return useSyncExternalStore(e.subscribe,()=>{let i=t?t(e.get()):e.get();return r&&n.current!==null&&r(n.current,i)?n.current:(n.current=i,i)},()=>t?t(e.get()):e.get())}function V(e,t){return u(e)&&u(t)&&e.length===t.length?e.every((r,n)=>{let i=t[n];return r===i}):e===t}function S(e,t,r=V){let n=useRef(null),i=useCallback(s=>{let a=e.map(l=>l.subscribe(s));return ()=>a.forEach(l=>l())},e),o=()=>{let s=e.map(l=>l.get()),a=t?t(s):s;return n.current!==null&&r(n.current,a)?n.current:(n.current=a,a)};return useSyncExternalStore(i,o,o)}function q({notifier:e,selector:t,areStatesEqual:r,stableChild:n,children:i}){let o=y(e,t,r);return i(o,n)}var A=memo(q,(e,t)=>t.isolated?true:e.notifier===t.notifier&&e.selector===t.selector&&e.areStatesEqual===t.areStatesEqual&&e.stableChild===t.stableChild&&e.children===t.children);function F({notifiers:e,selector:t,areStatesEqual:r,stableChild:n,children:i}){let o=S(e,t,r);return i(o,n)}var j=memo(F,(e,t)=>t.isolated?true:e.notifiers===t.notifiers&&e.selector===t.selector&&e.areStatesEqual===t.areStatesEqual&&e.stableChild===t.stableChild&&e.children===t.children);function O({children:e}){return jsx(Fragment,{children:e})}var K=memo(O,()=>true);export{K as IsolatedContainer,A as Listener,j as MultiListener,m as createNotifier,N as listener,w as multiListener,y as useListener,S as useMultiListener,L as useNotifier};//# sourceMappingURL=index.js.map
1
+ import {memo,useRef,useSyncExternalStore,useCallback,useState}from'react';import {jsx,Fragment}from'react/jsx-runtime';function b(e){return typeof e=="object"&&e!==null}function u(e){return Array.isArray(e)}function c(e){return typeof e=="function"}var f=class{state;rootNode;constructor(t){this.state=t,this.rootNode=new h(this);}},p=class{cachedState;listeners;children;proxy;constructor(){this.cachedState=null,this.listeners=new Set,this.children=new Map,this.proxy=this.createProxy();}dispatchSet=t=>this.set(t,false);subscribe=t=>(this.listeners.add(t),()=>{this.listeners.delete(t);});notifyListeners(){this.listeners.forEach(t=>t());}notifySubtree(){this.cachedState=null,this.notifyListeners();for(let t of this.children.values())t.notifySubtree();}reconcileChildren(t,r){if(!(!u(t)||!u(r)))for(let[n,i]of this.children){let o=n,s=t[o];r[o]!==s&&i.notifySubtree();}}setTrap(){return false}createProxy(){return new Proxy({},{get:(t,r)=>{if(r==="get")return this.get;if(r==="set")return this.dispatchSet;if(r==="subscribe")return this.subscribe;let n=this.get();if(b(n)&&r in n){let i=n[r];if(c(i))return i;let o=this.children.get(r);return o||(o=new d(r,this),this.children.set(r,o)),o.proxy}},set:this.setTrap})}},h=class extends p{root;constructor(t){super(),this.root=t;}get=()=>this.root.state;set(t,r){let n=this.get(),i=c(t)?t(n):t;return i!==n?(this.root.state=i,this.notifyListeners(),r||this.reconcileChildren(n,i),true):false}},d=class extends p{key;parent;constructor(t,r){super(),this.key=t,this.parent=r;}get=()=>{if(this.cachedState!==null)return this.cachedState;let t=this.parent.get()[this.key];return this.cachedState=t,t};set(t,r){let n=this.parent.get(),i=n[this.key],o=c(t)?t(i):t;if(o!==i){let s=Object.assign(u(n)?[]:{},n);return s[this.key]=o,this.parent.set(s,true)?(this.cachedState=o,this.notifyListeners(),r||this.reconcileChildren(i,o),true):false}return false}};var C="A function cannot be a notifier. Replace it with other value, or add it as a property/method inside an object, or don't wrap it with notifier.";function m(e){let t=c(e)?e():e;if(c(t))throw new Error(C);return k(t)}function k(e){return new f(e).rootNode.proxy}function w(e,t){let r=e.subscribe(()=>{t(e.get());});return t(e.get()),r}function N(e,t){let r=()=>{let o=e.map(s=>s.get());t(o);},n=e.map(o=>o.subscribe(r)),i=()=>n.forEach(o=>o());return r(),i}function L(e){let[t]=useState(()=>m(e));return t}function y(e,t,r){let n=useRef(null);return useSyncExternalStore(e.subscribe,()=>{let i=t?t(e.get()):e.get();return r&&n.current!==null&&r(n.current,i)?n.current:(n.current=i,i)},()=>t?t(e.get()):e.get())}function V(e,t){return u(e)&&u(t)&&e.length===t.length?e.every((r,n)=>{let i=t[n];return r===i}):e===t}function S(e,t,r=V){let n=useRef(null),i=useCallback(s=>{let a=e.map(l=>l.subscribe(s));return ()=>a.forEach(l=>l())},e),o=()=>{let s=e.map(l=>l.get()),a=t?t(s):s;return n.current!==null&&r(n.current,a)?n.current:(n.current=a,a)};return useSyncExternalStore(i,o,o)}function q({notifier:e,selector:t,areStatesEqual:r,stableChild:n,children:i}){let o=y(e,t,r);return i(o,n)}var A=memo(q,(e,t)=>t.isolated?true:e.notifier===t.notifier&&e.selector===t.selector&&e.areStatesEqual===t.areStatesEqual&&e.stableChild===t.stableChild&&e.children===t.children);function F({notifiers:e,selector:t,areStatesEqual:r,stableChild:n,children:i}){let o=S(e,t,r);return i(o,n)}var j=memo(F,(e,t)=>t.isolated?true:e.notifiers===t.notifiers&&e.selector===t.selector&&e.areStatesEqual===t.areStatesEqual&&e.stableChild===t.stableChild&&e.children===t.children);function O({children:e}){return jsx(Fragment,{children:e})}var K=memo(O,()=>true);export{K as IsolatedContainer,A as Listener,j as MultiListener,w as listener,N as multiListener,m as notifier,y as useListener,S as useMultiListener,L as useNotifier};//# sourceMappingURL=index.js.map
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/utils/helpers.ts","../src/notifier.ts","../src/utils/create-notifier.ts","../src/utils/listener.ts","../src/utils/multi-listener.ts","../src/hooks/useNotifier.ts","../src/hooks/useListener.ts","../src/hooks/useMultiListener.ts","../src/components/Listener.tsx","../src/components/MultiListener.tsx","../src/components/IsolatedContainer.tsx"],"names":["isObjectOrArray","value","isArray","isFunction","NotifierRoot","state","RootNode","Node","callback","listener","child","prevState","newState","key","index","prevChildState","_","prop","childNode","ChildNode","root","fromChild","parent","parentState","newParentState","functionCannotBeNotifierMessage","createNotifier","initialState","createNotifierInternal","notifier","unsubscribe","multiListener","notifiers","unsubscribeFns","fn","useNotifier","useState","useListener","selector","areStatesEqual","useRef","useSyncExternalStore","nextState","areStatesEqualFn","prevValue","nextValue","useMultiListener","subscribe","useCallback","getSnapshot","ListenerComponent","stableChild","children","Listener","memo","prevProps","nextProps","MultiListenerComponent","MultiListener","IsolatedContainerComponent","jsx","Fragment","IsolatedContainer"],"mappings":"uHAAO,SAASA,CAAAA,CAAgBC,CAAAA,CAAiC,CAC7D,OAAO,OAAOA,GAAU,QAAA,EAAYA,CAAAA,GAAU,IAClD,CAEO,SAASC,CAAAA,CAAQD,EAAyC,CAC7D,OAAO,KAAA,CAAM,OAAA,CAAQA,CAAK,CAC9B,CAEO,SAASE,CAAAA,CAAWF,CAAAA,CAAmC,CAC1D,OAAO,OAAOA,GAAU,UAC5B,CCFO,IAAMG,CAAAA,CAAN,KAAmB,CACf,MACA,QAAA,CAEP,WAAA,CAAYC,CAAAA,CAAgB,CACxB,IAAA,CAAK,KAAA,CAAQA,EACb,IAAA,CAAK,QAAA,CAAW,IAAIC,CAAAA,CAAS,IAAI,EACrC,CACJ,CAAA,CAEeC,CAAAA,CAAf,KAAoB,CACN,WAAA,CACF,S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AA,CACA,cAAA,CAAAC,CAAAA,CACA,WAAA,CAAAY,CAAAA,CACA,QAAA,CAAAC,CACJ,CAAA,CAAQ,CACJ,IAAM/C,CAAAA,CAAQyC,CAAAA,CAAiBd,CAAAA,CAAWM,EAAUC,CAAc,CAAA,CAClE,OAAOa,CAAAA,CAAS/C,CAAAA,CAAO8C,CAAW,CACtC,CAEO,IAAMO,CAAAA,CAA4CJ,IAAAA,CACrDG,CAAAA,CACA,CAACF,EAAWC,CAAAA,GACJA,CAAAA,CAAU,QAAA,CAAiB,IAAA,CAE3BD,CAAAA,CAAU,SAAA,GAAcC,EAAU,SAAA,EAClCD,CAAAA,CAAU,QAAA,GAAaC,CAAAA,CAAU,QAAA,EACjCD,CAAAA,CAAU,iBAAmBC,CAAAA,CAAU,cAAA,EACvCD,CAAAA,CAAU,WAAA,GAAgBC,CAAAA,CAAU,WAAA,EACpCD,EAAU,QAAA,GAAaC,CAAAA,CAAU,QAG7C,ECxBA,SAASG,CAAAA,CAA2B,CAAE,QAAA,CAAAP,CAAS,CAAA,CAA2B,CACtE,OAAOQ,GAAAA,CAAAC,QAAAA,CAAA,CAAG,QAAA,CAAAT,CAAAA,CAAS,CACvB,CAEO,IAAMU,CAAAA,CAAoBR,IAAAA,CAC7BK,CAAAA,CACA,IAAM,IACV","file":"index.js","sourcesContent":["export function isObjectOrArray(value: unknown): value is object {\r\n return typeof value === \"object\" && value !== null;\r\n}\r\n\r\nexport function isArray(value: unknown): value is Array<unknown> {\r\n return Array.isArray(value);\r\n}\r\n\r\nexport function isFunction(value: unknown): value is Function {\r\n return typeof value === \"function\";\r\n}","import type {\r\n NodeKey,\r\n SetState,\r\n StateUpdater,\r\n VoidCallback\r\n} from \"./types\";\r\nimport { isArray, isFunction, isObjectOrArray } from \"./utils/helpers\";\r\n\r\nexport class NotifierRoot {\r\n public state: unknown;\r\n public rootNode: RootNode;\r\n\r\n constructor(state: unknown) {\r\n this.state = state;\r\n this.rootNode = new RootNode(this);\r\n }\r\n}\r\n\r\nabstract class Node {\r\n protected cachedState: unknown;\r\n private listeners: Set<VoidCallback>;\r\n private children: Map<NodeKey, ChildNode>;\r\n public proxy: object;\r\n\r\n constructor() {\r\n this.cachedState = null;\r\n this.listeners = new Set();\r\n this.children = new Map();\r\n this.proxy = this.createProxy();\r\n }\r\n\r\n public abstract get(): unknown;\r\n public abstract set(state: SetState<unknown>, fromChild: boolean): boolean;\r\n\r\n public dispatchSet = (state: SetState<unknown>) => {\r\n return this.set(state, false);\r\n };\r\n\r\n private subscribe = (callback: VoidCallback) => {\r\n this.listeners.add(callback);\r\n return () => {\r\n this.listeners.delete(callback);\r\n };\r\n };\r\n\r\n protected notifyListeners() {\r\n this.listeners.forEach((listener) => listener());\r\n }\r\n\r\n private notifySubtree() {\r\n this.cachedState = null;\r\n this.notifyListeners();\r\n for (const child of this.children.values()) {\r\n child.notifySubtree();\r\n }\r\n }\r\n\r\n protected reconcileChildren(prevState: unknown, newState: unknown) {\r\n if (!isArray(prevState) || !isArray(newState)) {\r\n return;\r\n }\r\n for (const [key, child] of this.children) {\r\n const index = key as `${number}`;\r\n const prevChildState = prevState[index];\r\n const newChildState = newState[index];\r\n if (newChildState !== prevChildState) {\r\n child.notifySubtree();\r\n }\r\n }\r\n }\r\n\r\n private setTrap() { return false; }\r\n\r\n private createProxy() {\r\n return new Proxy(\r\n {},\r\n {\r\n get: (_, prop) => {\r\n if (prop === \"get\") return this.get;\r\n if (prop === \"set\") return this.dispatchSet;\r\n if (prop === \"subscribe\") return this.subscribe;\r\n\r\n const state = this.get();\r\n if (isObjectOrArray(state) && prop in state) {\r\n const value = (state as any)[prop];\r\n if (isFunction(value)) return value;\r\n\r\n let childNode = this.children.get(prop);\r\n if (!childNode) {\r\n childNode = new ChildNode(prop, this);\r\n this.children.set(prop, childNode);\r\n }\r\n return childNode.proxy;\r\n }\r\n },\r\n set: this.setTrap\r\n }\r\n );\r\n }\r\n}\r\n\r\nclass RootNode extends Node {\r\n private root: NotifierRoot;\r\n\r\n constructor(root: NotifierRoot) {\r\n super();\r\n this.root = root;\r\n }\r\n\r\n public get = () => this.root.state;\r\n\r\n public set(state: SetState<unknown>, fromChild: boolean) {\r\n const prevState = this.get();\r\n const newState = isFunction(state)\r\n ? (state as StateUpdater<unknown>)(prevState)\r\n : state;\r\n if (newState !== prevState) {\r\n this.root.state = newState;\r\n this.notifyListeners();\r\n if (!fromChild) {\r\n this.reconcileChildren(prevState, newState);\r\n }\r\n return true;\r\n }\r\n return false;\r\n }\r\n}\r\n\r\nclass ChildNode extends Node {\r\n private key: NodeKey;\r\n private parent: Node;\r\n\r\n constructor(key: NodeKey, parent: Node) {\r\n super();\r\n this.key = key;\r\n this.parent = parent;\r\n }\r\n\r\n public get = (): unknown => {\r\n if (this.cachedState !== null) {\r\n return this.cachedState;\r\n }\r\n const state = (this.parent.get() as any)[this.key];\r\n this.cachedState = state;\r\n return state;\r\n }\r\n\r\n public set(state: SetState<unknown>, fromChild: boolean) {\r\n const parentState = this.parent.get();\r\n const prevState = (parentState as any)[this.key];\r\n const newState = isFunction(state)\r\n ? (state as StateUpdater<unknown>)(prevState)\r\n : state;\r\n if (newState !== prevState) {\r\n const newParentState = Object.assign(\r\n isArray(parentState) ? [] : {},\r\n parentState\r\n );\r\n (newParentState as any)[this.key] = newState;\r\n const isSet = this.parent.set(newParentState, true);\r\n if (isSet) {\r\n this.cachedState = newState;\r\n this.notifyListeners();\r\n if (!fromChild) {\r\n this.reconcileChildren(prevState, newState);\r\n }\r\n return true;\r\n }\r\n return false;\r\n }\r\n return false;\r\n }\r\n}","import { isFunction } from \"./helpers\";\r\nimport { NotifierRoot } from \"../notifier\";\r\nimport type { InitialState, Notifier } from \"../types\";\r\n\r\nconst functionCannotBeNotifierMessage =\r\n \"A function cannot be a notifier. Replace it with other value, or add it as a property/method inside an object, or don't wrap it with notifier.\";\r\n\r\nexport function createNotifier<T>(initialState: InitialState<T>): Notifier<T> {\r\n const state = (isFunction(initialState)\r\n ? (initialState as () => T)()\r\n : initialState) as T;\r\n if (isFunction(state)) {\r\n throw new Error(functionCannotBeNotifierMessage);\r\n }\r\n return createNotifierInternal(state);\r\n}\r\n\r\nfunction createNotifierInternal<T>(state: T): Notifier<T> {\r\n const root = new NotifierRoot(state);\r\n return root.rootNode.proxy as Notifier<T>;\r\n}","import type { ListenerCallback, Notifier, VoidCallback } from \"../types\";\r\n\r\nexport function listener<T>(\r\n notifier: Notifier<T>,\r\n callback: ListenerCallback<T>\r\n): VoidCallback {\r\n const unsubscribe = notifier.subscribe(() => {\r\n callback(notifier.get());\r\n });\r\n callback(notifier.get());\r\n return unsubscribe;\r\n}","import type {\r\n InferNotifierValues,\r\n ListenerCallback,\r\n Notifier,\r\n VoidCallback\r\n} from \"../types\";\r\n\r\nexport function multiListener<\r\n const T extends readonly Notifier<unknown>[]\r\n>(\r\n notifiers: T,\r\n callback: ListenerCallback<InferNotifierValues<T>>\r\n): VoidCallback {\r\n const listener = () => {\r\n const state = notifiers.map((notifier) => notifier.get()) as any;\r\n callback(state);\r\n };\r\n const unsubscribeFns = notifiers.map((notifier) => {\r\n return notifier.subscribe(listener);\r\n });\r\n const unsubscribe = () => unsubscribeFns.forEach((fn) => fn());\r\n listener();\r\n return unsubscribe;\r\n}","import { useState } from \"react\";\r\n\r\nimport type { InitialState, Notifier } from \"../types\";\r\nimport { createNotifier } from \"../utils/create-notifier\";\r\n\r\nexport function useNotifier<T>(initialState: InitialState<T>): Notifier<T> {\r\n const [notifier] = useState(() => createNotifier(initialState));\r\n return notifier;\r\n}","import { useRef, useSyncExternalStore } from \"react\";\r\n\r\nimport type { Notifier, StatesComparer, StateSelector } from \"../types\";\r\n\r\nexport function useListener<T, U = T>(\r\n notifier: Notifier<T>,\r\n selector?: StateSelector<T, U>,\r\n areStatesEqual?: StatesComparer<U>\r\n): U {\r\n const prevState = useRef<U>(null);\r\n\r\n return useSyncExternalStore(\r\n notifier.subscribe,\r\n () => {\r\n const nextState = selector\r\n ? selector(notifier.get())\r\n : notifier.get() as unknown as U;\r\n if (areStatesEqual && prevState.current !== null) {\r\n const areEqual = areStatesEqual(prevState.current, nextState);\r\n if (areEqual) return prevState.current;\r\n }\r\n prevState.current = nextState;\r\n return nextState;\r\n },\r\n () => (selector ? selector(notifier.get()) : notifier.get() as unknown as U)\r\n );\r\n}","import { useCallback, useRef, useSyncExternalStore } from \"react\";\r\n\r\nimport type {\r\n InferNotifierValues,\r\n Notifier,\r\n StatesComparer,\r\n StateSelector,\r\n VoidCallback\r\n} from \"../types\";\r\nimport { isArray } from \"../utils/helpers\";\r\n\r\nfunction areStatesEqualFn<T>(prevState: T, nextState: T) {\r\n if (\r\n isArray(prevState) &&\r\n isArray(nextState) &&\r\n prevState.length === nextState.length\r\n ) {\r\n return prevState.every((prevValue, index) => {\r\n const nextValue = nextState[index];\r\n return prevValue === nextValue;\r\n });\r\n }\r\n return prevState === nextState;\r\n};\r\n\r\nexport function useMultiListener<\r\n const T extends readonly Notifier<unknown>[],\r\n const U\r\n>(\r\n notifiers: T,\r\n selector: StateSelector<InferNotifierValues<T>, U>,\r\n areStatesEqual?: StatesComparer<U>\r\n): U;\r\n\r\nexport function useMultiListener<\r\n const T extends readonly Notifier<unknown>[]\r\n>(\r\n notifiers: T,\r\n selector?: undefined,\r\n areStatesEqual?: StatesComparer<InferNotifierValues<T>>\r\n): InferNotifierValues<T>;\r\n\r\nexport function useMultiListener(\r\n notifiers: Notifier<unknown>[],\r\n selector?: StateSelector<any, any>,\r\n areStatesEqual: StatesComparer<any> = areStatesEqualFn\r\n): unknown {\r\n const prevState = useRef<unknown>(null);\r\n\r\n const subscribe = useCallback((callback: VoidCallback) => {\r\n const unsubscribeFns = notifiers.map((notifier) => {\r\n return notifier.subscribe(callback);\r\n });\r\n return () => unsubscribeFns.forEach((fn) => fn());\r\n }, notifiers);\r\n\r\n const getSnapshot = () => {\r\n const state = notifiers.map((notifier) => notifier.get());\r\n const nextState = selector ? selector(state) : state;\r\n if (prevState.current !== null) {\r\n const areEqual = areStatesEqual(prevState.current, nextState);\r\n if (areEqual) return prevState.current;\r\n }\r\n prevState.current = nextState;\r\n return nextState;\r\n };\r\n\r\n return useSyncExternalStore(\r\n subscribe,\r\n getSnapshot,\r\n getSnapshot\r\n );\r\n}","import { memo } from \"react\";\r\n\r\nimport type { ListenerProps } from \"../types\";\r\nimport { useListener } from \"../hooks/useListener\";\r\n\r\nfunction ListenerComponent<T, U = T>({\r\n notifier,\r\n selector,\r\n areStatesEqual,\r\n stableChild,\r\n children\r\n}: ListenerProps<T, U>) {\r\n const state = useListener(notifier, selector, areStatesEqual);\r\n return children(state, stableChild);\r\n}\r\n\r\nexport const Listener = memo(\r\n ListenerComponent,\r\n (prevProps, nextProps) => {\r\n if (nextProps.isolated) return true;\r\n return (\r\n prevProps.notifier === nextProps.notifier &&\r\n prevProps.selector === nextProps.selector &&\r\n prevProps.areStatesEqual === nextProps.areStatesEqual &&\r\n prevProps.stableChild === nextProps.stableChild &&\r\n prevProps.children === nextProps.children\r\n );\r\n }\r\n) as typeof ListenerComponent;","import { memo } from \"react\";\r\n\r\nimport { useMultiListener } from \"../hooks/useMultiListener\";\r\nimport type { MultiListenerComponent as MultiListenerComponentType } from \"../types\";\r\n\r\nfunction MultiListenerComponent({\r\n notifiers,\r\n selector,\r\n areStatesEqual,\r\n stableChild,\r\n children\r\n}: any) {\r\n const state = useMultiListener(notifiers, selector, areStatesEqual);\r\n return children(state, stableChild);\r\n}\r\n\r\nexport const MultiListener: MultiListenerComponentType = memo(\r\n MultiListenerComponent,\r\n (prevProps, nextProps) => {\r\n if (nextProps.isolated) return true;\r\n return (\r\n prevProps.notifiers === nextProps.notifiers &&\r\n prevProps.selector === nextProps.selector &&\r\n prevProps.areStatesEqual === nextProps.areStatesEqual &&\r\n prevProps.stableChild === nextProps.stableChild &&\r\n prevProps.children === nextProps.children\r\n );\r\n }\r\n);","import { memo } from \"react\";\r\n\r\nimport type { IsolatedContainerProps } from \"../types\";\r\n\r\nfunction IsolatedContainerComponent({ children }: IsolatedContainerProps) {\r\n return <>{children}</>;\r\n}\r\n\r\nexport const IsolatedContainer = memo(\r\n IsolatedContainerComponent,\r\n () => true\r\n) as typeof IsolatedContainerComponent;"]}
1
+ {"version":3,"sources":["../src/utils/helpers.ts","../src/notifier.ts","../src/utils/notifier.ts","../src/utils/listener.ts","../src/utils/multi-listener.ts","../src/hooks/useNotifier.ts","../src/hooks/useListener.ts","../src/hooks/useMultiListener.ts","../src/components/Listener.tsx","../src/components/MultiListener.tsx","../src/components/IsolatedContainer.tsx"],"names":["isObjectOrArray","value","isArray","isFunction","NotifierRoot","state","RootNode","Node","callback","listener","child","prevState","newState","key","index","prevChildState","_","prop","childNode","ChildNode","root","fromChild","parent","parentState","newParentState","functionCannotBeNotifierMessage","notifier","initialState","createNotifier","unsubscribe","multiListener","notifiers","unsubscribeFns","fn","useNotifier","useState","useListener","selector","areStatesEqual","useRef","useSyncExternalStore","nextState","areStatesEqualFn","prevValue","nextValue","useMultiListener","subscribe","useCallback","getSnapshot","ListenerComponent","stableChild","children","Listener","memo","prevProps","nextProps","MultiListenerComponent","MultiListener","IsolatedContainerComponent","jsx","Fragment","IsolatedContainer"],"mappings":"uHAAO,SAASA,CAAAA,CAAgBC,CAAAA,CAAiC,CAC7D,OAAO,OAAOA,GAAU,QAAA,EAAYA,CAAAA,GAAU,IAClD,CAEO,SAASC,CAAAA,CAAQD,EAAyC,CAC7D,OAAO,KAAA,CAAM,OAAA,CAAQA,CAAK,CAC9B,CAEO,SAASE,CAAAA,CAAWF,CAAAA,CAAmC,CAC1D,OAAO,OAAOA,GAAU,UAC5B,CCFO,IAAMG,CAAAA,CAAN,KAAmB,CACf,MACA,QAAA,CAEP,WAAA,CAAYC,CAAAA,CAAgB,CACxB,IAAA,CAAK,KAAA,CAAQA,EACb,IAAA,CAAK,QAAA,CAAW,IAAIC,CAAAA,CAAS,IAAI,EACrC,CACJ,CAAA,CAEeC,CAAAA,CAAf,KAAoB,CACN,WAAA,CACF,SAAA,CACA,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A,cAAA,CAAAC,CAAAA,CACA,WAAA,CAAAY,CAAAA,CACA,QAAA,CAAAC,CACJ,CAAA,CAAQ,CACJ,IAAM9C,CAAAA,CAAQwC,CAAAA,CAAiBd,CAAAA,CAAWM,EAAUC,CAAc,CAAA,CAClE,OAAOa,CAAAA,CAAS9C,CAAAA,CAAO6C,CAAW,CACtC,CAuBO,IAAMO,CAAAA,CAA4CJ,IAAAA,CACrDG,CAAAA,CACA,CAACF,EAAWC,CAAAA,GACJA,CAAAA,CAAU,QAAA,CAAiB,IAAA,CAE3BD,CAAAA,CAAU,SAAA,GAAcC,EAAU,SAAA,EAClCD,CAAAA,CAAU,QAAA,GAAaC,CAAAA,CAAU,QAAA,EACjCD,CAAAA,CAAU,iBAAmBC,CAAAA,CAAU,cAAA,EACvCD,CAAAA,CAAU,WAAA,GAAgBC,CAAAA,CAAU,WAAA,EACpCD,EAAU,QAAA,GAAaC,CAAAA,CAAU,QAG7C,EC7CA,SAASG,CAAAA,CAA2B,CAAE,QAAA,CAAAP,CAAS,CAAA,CAA2B,CACtE,OAAOQ,GAAAA,CAAAC,QAAAA,CAAA,CAAG,QAAA,CAAAT,CAAAA,CAAS,CACvB,CAwBO,IAAMU,CAAAA,CAAoBR,IAAAA,CAC7BK,CAAAA,CACA,IAAM,IACV","file":"index.js","sourcesContent":["export function isObjectOrArray(value: unknown): value is object {\r\n return typeof value === \"object\" && value !== null;\r\n}\r\n\r\nexport function isArray(value: unknown): value is Array<unknown> {\r\n return Array.isArray(value);\r\n}\r\n\r\nexport function isFunction(value: unknown): value is Function {\r\n return typeof value === \"function\";\r\n}","import type {\r\n NodeKey,\r\n SetState,\r\n StateUpdater,\r\n VoidCallback\r\n} from \"./types\";\r\nimport { isArray, isFunction, isObjectOrArray } from \"./utils/helpers\";\r\n\r\nexport class NotifierRoot {\r\n public state: unknown;\r\n public rootNode: RootNode;\r\n\r\n constructor(state: unknown) {\r\n this.state = state;\r\n this.rootNode = new RootNode(this);\r\n }\r\n}\r\n\r\nabstract class Node {\r\n protected cachedState: unknown;\r\n private listeners: Set<VoidCallback>;\r\n private children: Map<NodeKey, ChildNode>;\r\n public proxy: object;\r\n\r\n constructor() {\r\n this.cachedState = null;\r\n this.listeners = new Set();\r\n this.children = new Map();\r\n this.proxy = this.createProxy();\r\n }\r\n\r\n public abstract get(): unknown;\r\n public abstract set(state: SetState<unknown>, fromChild: boolean): boolean;\r\n\r\n public dispatchSet = (state: SetState<unknown>) => {\r\n return this.set(state, false);\r\n };\r\n\r\n private subscribe = (callback: VoidCallback) => {\r\n this.listeners.add(callback);\r\n return () => {\r\n this.listeners.delete(callback);\r\n };\r\n };\r\n\r\n protected notifyListeners() {\r\n this.listeners.forEach((listener) => listener());\r\n }\r\n\r\n private notifySubtree() {\r\n this.cachedState = null;\r\n this.notifyListeners();\r\n for (const child of this.children.values()) {\r\n child.notifySubtree();\r\n }\r\n }\r\n\r\n protected reconcileChildren(prevState: unknown, newState: unknown) {\r\n if (!isArray(prevState) || !isArray(newState)) {\r\n return;\r\n }\r\n for (const [key, child] of this.children) {\r\n const index = key as `${number}`;\r\n const prevChildState = prevState[index];\r\n const newChildState = newState[index];\r\n if (newChildState !== prevChildState) {\r\n child.notifySubtree();\r\n }\r\n }\r\n }\r\n\r\n private setTrap() { return false; }\r\n\r\n private createProxy() {\r\n return new Proxy(\r\n {},\r\n {\r\n get: (_, prop) => {\r\n if (prop === \"get\") return this.get;\r\n if (prop === \"set\") return this.dispatchSet;\r\n if (prop === \"subscribe\") return this.subscribe;\r\n\r\n const state = this.get();\r\n if (isObjectOrArray(state) && prop in state) {\r\n const value = (state as any)[prop];\r\n if (isFunction(value)) return value;\r\n\r\n let childNode = this.children.get(prop);\r\n if (!childNode) {\r\n childNode = new ChildNode(prop, this);\r\n this.children.set(prop, childNode);\r\n }\r\n return childNode.proxy;\r\n }\r\n },\r\n set: this.setTrap\r\n }\r\n );\r\n }\r\n}\r\n\r\nclass RootNode extends Node {\r\n private root: NotifierRoot;\r\n\r\n constructor(root: NotifierRoot) {\r\n super();\r\n this.root = root;\r\n }\r\n\r\n public get = () => this.root.state;\r\n\r\n public set(state: SetState<unknown>, fromChild: boolean) {\r\n const prevState = this.get();\r\n const newState = isFunction(state)\r\n ? (state as StateUpdater<unknown>)(prevState)\r\n : state;\r\n if (newState !== prevState) {\r\n this.root.state = newState;\r\n this.notifyListeners();\r\n if (!fromChild) {\r\n this.reconcileChildren(prevState, newState);\r\n }\r\n return true;\r\n }\r\n return false;\r\n }\r\n}\r\n\r\nclass ChildNode extends Node {\r\n private key: NodeKey;\r\n private parent: Node;\r\n\r\n constructor(key: NodeKey, parent: Node) {\r\n super();\r\n this.key = key;\r\n this.parent = parent;\r\n }\r\n\r\n public get = (): unknown => {\r\n if (this.cachedState !== null) {\r\n return this.cachedState;\r\n }\r\n const state = (this.parent.get() as any)[this.key];\r\n this.cachedState = state;\r\n return state;\r\n }\r\n\r\n public set(state: SetState<unknown>, fromChild: boolean) {\r\n const parentState = this.parent.get();\r\n const prevState = (parentState as any)[this.key];\r\n const newState = isFunction(state)\r\n ? (state as StateUpdater<unknown>)(prevState)\r\n : state;\r\n if (newState !== prevState) {\r\n const newParentState = Object.assign(\r\n isArray(parentState) ? [] : {},\r\n parentState\r\n );\r\n (newParentState as any)[this.key] = newState;\r\n const isSet = this.parent.set(newParentState, true);\r\n if (isSet) {\r\n this.cachedState = newState;\r\n this.notifyListeners();\r\n if (!fromChild) {\r\n this.reconcileChildren(prevState, newState);\r\n }\r\n return true;\r\n }\r\n return false;\r\n }\r\n return false;\r\n }\r\n}","import { isFunction } from \"./helpers\";\r\nimport { NotifierRoot } from \"../notifier\";\r\nimport type { InitialState, Notifier } from \"../types\";\r\n\r\nconst functionCannotBeNotifierMessage =\r\n \"A function cannot be a notifier. Replace it with other value, or add it as a property/method inside an object, or don't wrap it with notifier.\";\r\n\r\n/**\r\n * Creates a core, global root-level state container wrapped in a deeply reactive proxy tree.\r\n * \r\n * @remarks\r\n * Serves as the primary entry point for global stores outside of React. Every nested property \r\n * automatically becomes a sub-notifier. Raw functions cannot be notifiers and will throw an error.\r\n * \r\n * @see [Barrex Docs](./docs/utils/notifier.md)\r\n * \r\n * @template T The shape of the initial state.\r\n * @param initialState The initial value of the state, or a lazy initialization function returning the state.\r\n * @returns A root Notifier proxy instance.\r\n * \r\n * @example\r\n * ```ts\r\n * import { notifier } from \"barrex\";\r\n * \r\n * const authNotifier = notifier({\r\n * user: null,\r\n * isAuthenticated: false\r\n * });\r\n * ```\r\n */\r\nexport function notifier<T>(initialState: InitialState<T>): Notifier<T> {\r\n const state = (isFunction(initialState)\r\n ? (initialState as () => T)()\r\n : initialState) as T;\r\n if (isFunction(state)) {\r\n throw new Error(functionCannotBeNotifierMessage);\r\n }\r\n return createNotifier(state);\r\n}\r\n\r\nfunction createNotifier<T>(state: T): Notifier<T> {\r\n const root = new NotifierRoot(state);\r\n return root.rootNode.proxy as Notifier<T>;\r\n}","import type { ListenerCallback, Notifier, VoidCallback } from \"../types\";\r\n\r\n/**\r\n * Subscribes imperatively to a `barrex` notifier outside of React component trees.\r\n * \r\n * @remarks\r\n * Executes the callback immediately upon registration with the current state value, \r\n * and subsequently every time the notifier updates. Returns a cleanup function to unsubscribe.\r\n * \r\n * @see [Barrex Docs](./docs/utils/listener.md)\r\n * \r\n * @param notifier The source notifier instance to subscribe to.\r\n * @param callback A callback function invoked immediately and on every state update.\r\n * @returns A cleanup function to remove the subscription.\r\n * \r\n * @example\r\n * ```ts\r\n * import { listener } from \"barrex\";\r\n * \r\n * const unsubscribe = listener(themeNotifier, (theme) => {\r\n * localStorage.setItem(\"theme\", theme);\r\n * });\r\n * ```\r\n */\r\nexport function listener<T>(\r\n notifier: Notifier<T>,\r\n callback: ListenerCallback<T>\r\n): VoidCallback {\r\n const unsubscribe = notifier.subscribe(() => {\r\n callback(notifier.get());\r\n });\r\n callback(notifier.get());\r\n return unsubscribe;\r\n}","import type {\r\n InferNotifierValues,\r\n ListenerCallback,\r\n Notifier,\r\n VoidCallback\r\n} from \"../types\";\r\n\r\n/**\r\n * Subscribes imperatively to multiple `barrex` notifiers simultaneously outside of React.\r\n * \r\n * @remarks\r\n * Accepts a tuple array of notifiers, infers their types automatically, executes \r\n * the callback immediately with the initial values tuple, and returns a single composite cleanup function.\r\n * \r\n * @see [Barrex Docs](./docs/utils/multi-listener.md)\r\n * \r\n * @param notifiers A readonly tuple array of source notifiers to subscribe to.\r\n * @param callback A callback function invoked immediately with the values tuple and on subsequent updates.\r\n * @returns A composite cleanup function to unsubscribe from all provided notifiers.\r\n * \r\n * @example\r\n * ```ts\r\n * import { multiListener } from \"barrex\";\r\n * \r\n * const unsubscribe = multiListener(\r\n * [userNotifier.name, settingsNotifier.theme],\r\n * ([name, theme]) => {\r\n * console.log(`User: ${name}, Theme: ${theme}`);\r\n * }\r\n * );\r\n * ```\r\n */\r\nexport function multiListener<\r\n const T extends readonly Notifier<unknown>[]\r\n>(\r\n notifiers: T,\r\n callback: ListenerCallback<InferNotifierValues<T>>\r\n): VoidCallback {\r\n const listener = () => {\r\n const state = notifiers.map((notifier) => notifier.get()) as any;\r\n callback(state);\r\n };\r\n const unsubscribeFns = notifiers.map((notifier) => {\r\n return notifier.subscribe(listener);\r\n });\r\n const unsubscribe = () => unsubscribeFns.forEach((fn) => fn());\r\n listener();\r\n return unsubscribe;\r\n}","import { useState } from \"react\";\r\n\r\nimport type { InitialState, Notifier } from \"../types\";\r\nimport { notifier as createNotifier } from \"../utils/notifier\";\r\n\r\n/**\r\n * Creates a component-scoped, lazy-initialized notifier with automatic \r\n * proxy-driven nested reactivity.\r\n * \r\n * @remarks\r\n * Tied directly to the component lifecycle. Every property on the initial state \r\n * object automatically becomes a first-class sub-notifier.\r\n * \r\n * @see [Barrex Docs](./docs/hooks/use-notifier.md)\r\n * \r\n * @template T The shape of the initial state.\r\n * @param initialState The initial value or a lazy initializer function returning the state.\r\n * @returns A root Notifier proxy instance.\r\n * \r\n * @example\r\n * ```tsx\r\n * import { useNotifier, useListener } from \"barrex\";\r\n * \r\n * function Profile() {\r\n * const userNotifier = useNotifier({ name: \"Alice\", age: 40 });\r\n * const name = useListener(userNotifier.name);\r\n * return <p>{name}</p>;\r\n * }\r\n * ```\r\n */\r\nexport function useNotifier<T>(initialState: InitialState<T>): Notifier<T> {\r\n const [notifier] = useState(() => createNotifier(initialState));\r\n return notifier;\r\n}","import { useRef, useSyncExternalStore } from \"react\";\r\n\r\nimport type { Notifier, StatesComparer, StateSelector } from \"../types\";\r\n\r\n/**\r\n * Subscribes a React component to a `barrex` notifier, enabling precise, \r\n * fine-grained reactivity using `useSyncExternalStore`.\r\n * \r\n * @remarks\r\n * Components re-render only when the exact state slice or computed selector \r\n * value updates, preventing unnecessary re-renders across the tree.\r\n * \r\n * @see [Barrex Docs](./docs/hooks/use-listener.md)\r\n * \r\n * @template T The raw type of the source notifier state.\r\n * @template U The resolved type after applying the selector.\r\n * \r\n * @param notifier The source notifier instance to subscribe to.\r\n * @param selector Optional function to transform or compute a specific slice from the notifier state.\r\n * @param areStatesEqual Optional custom comparer. Returns true to reuse the previous state reference and skip re-renders.\r\n * @returns The current resolved state (raw or selected).\r\n * \r\n * @example\r\n * ```tsx\r\n * import { useListener } from \"barrex\";\r\n * \r\n * function Counter() {\r\n * const count = useListener(counterNotifier);\r\n * return <span>Count: {count}</span>;\r\n * }\r\n * ```\r\n */\r\nexport function useListener<T, U = T>(\r\n notifier: Notifier<T>,\r\n selector?: StateSelector<T, U>,\r\n areStatesEqual?: StatesComparer<U>\r\n): U {\r\n const prevState = useRef<U>(null);\r\n\r\n return useSyncExternalStore(\r\n notifier.subscribe,\r\n () => {\r\n const nextState = selector\r\n ? selector(notifier.get())\r\n : notifier.get() as unknown as U;\r\n if (areStatesEqual && prevState.current !== null) {\r\n const areEqual = areStatesEqual(prevState.current, nextState);\r\n if (areEqual) return prevState.current;\r\n }\r\n prevState.current = nextState;\r\n return nextState;\r\n },\r\n () => (selector ? selector(notifier.get()) : notifier.get() as unknown as U)\r\n );\r\n}","import { useCallback, useRef, useSyncExternalStore } from \"react\";\r\n\r\nimport type {\r\n InferNotifierValues,\r\n Notifier,\r\n StatesComparer,\r\n StateSelector,\r\n VoidCallback\r\n} from \"../types\";\r\nimport { isArray } from \"../utils/helpers\";\r\n\r\nfunction areStatesEqualFn<T>(prevState: T, nextState: T) {\r\n if (\r\n isArray(prevState) &&\r\n isArray(nextState) &&\r\n prevState.length === nextState.length\r\n ) {\r\n return prevState.every((prevValue, index) => {\r\n const nextValue = nextState[index];\r\n return prevValue === nextValue;\r\n });\r\n }\r\n return prevState === nextState;\r\n};\r\n\r\n/**\r\n * Subscribes a React component to multiple notifiers simultaneously, \r\n * offering complete type safety and optimized derivations.\r\n * \r\n * @remarks\r\n * Bundles multiple notifier subscriptions into a single unified cleanup routine \r\n * and includes smart array/tuple shallow comparison by default.\r\n * \r\n * @see [Barrex Docs](./docs/hooks/use-multi-listener.md)\r\n * \r\n * @param notifiers A readonly tuple array of source notifiers to subscribe to.\r\n * @param selector Optional function to transform or combine the tuple values into a custom shape.\r\n * @param areStatesEqual Optional custom comparator. Defaults to a smart tuple-aware shallow equality checker.\r\n * @returns The resolved state tuple or the computed selector output.\r\n * \r\n * @example\r\n * ```tsx\r\n * import { useMultiListener } from \"barrex\";\r\n * \r\n * function Header() {\r\n * const [name, theme] = useMultiListener([userNotifier.name, themeNotifier]);\r\n * return <header className={theme}>Welcome, {name}</header>;\r\n * }\r\n * ```\r\n */\r\nexport function useMultiListener<\r\n const T extends readonly Notifier<unknown>[],\r\n const U\r\n>(\r\n notifiers: T,\r\n selector: StateSelector<InferNotifierValues<T>, U>,\r\n areStatesEqual?: StatesComparer<U>\r\n): U;\r\n\r\nexport function useMultiListener<\r\n const T extends readonly Notifier<unknown>[]\r\n>(\r\n notifiers: T,\r\n selector?: undefined,\r\n areStatesEqual?: StatesComparer<InferNotifierValues<T>>\r\n): InferNotifierValues<T>;\r\n\r\nexport function useMultiListener(\r\n notifiers: Notifier<unknown>[],\r\n selector?: StateSelector<any, any>,\r\n areStatesEqual: StatesComparer<any> = areStatesEqualFn\r\n): unknown {\r\n const prevState = useRef<unknown>(null);\r\n\r\n const subscribe = useCallback((callback: VoidCallback) => {\r\n const unsubscribeFns = notifiers.map((notifier) => {\r\n return notifier.subscribe(callback);\r\n });\r\n return () => unsubscribeFns.forEach((fn) => fn());\r\n }, notifiers);\r\n\r\n const getSnapshot = () => {\r\n const state = notifiers.map((notifier) => notifier.get());\r\n const nextState = selector ? selector(state) : state;\r\n if (prevState.current !== null) {\r\n const areEqual = areStatesEqual(prevState.current, nextState);\r\n if (areEqual) return prevState.current;\r\n }\r\n prevState.current = nextState;\r\n return nextState;\r\n };\r\n\r\n return useSyncExternalStore(\r\n subscribe,\r\n getSnapshot,\r\n getSnapshot\r\n );\r\n}","import { memo } from \"react\";\r\n\r\nimport type { ListenerProps } from \"../types\";\r\nimport { useListener } from \"../hooks/useListener\";\r\n\r\nfunction ListenerComponent<T, U = T>({\r\n notifier,\r\n selector,\r\n areStatesEqual,\r\n stableChild,\r\n children\r\n}: ListenerProps<T, U>) {\r\n const state = useListener(notifier, selector, areStatesEqual);\r\n return children(state, stableChild);\r\n}\r\n\r\n/**\r\n * A declarative component wrapper that brings fine-grained reactivity \r\n * and granular performance optimization directly into your JSX tree.\r\n * \r\n * @remarks\r\n * Confines updates strictly to its render-prop children without forcing \r\n * an entire component function to re-render.\r\n * \r\n * @see [Barrex Docs](./docs/components/listener.md)\r\n * \r\n * @param props The component props.\r\n * \r\n * @example\r\n * ```tsx\r\n * import { Listener } from \"barrex\";\r\n * \r\n * <Listener notifier={userNotifier.name}>\r\n * {(name) => <h1>Welcome, {name}!</h1>}\r\n * </Listener>\r\n * ```\r\n */\r\nexport const Listener = memo(\r\n ListenerComponent,\r\n (prevProps, nextProps) => {\r\n if (nextProps.isolated) return true;\r\n return (\r\n prevProps.notifier === nextProps.notifier &&\r\n prevProps.selector === nextProps.selector &&\r\n prevProps.areStatesEqual === nextProps.areStatesEqual &&\r\n prevProps.stableChild === nextProps.stableChild &&\r\n prevProps.children === nextProps.children\r\n );\r\n }\r\n) as typeof ListenerComponent;","import { memo } from \"react\";\r\n\r\nimport { useMultiListener } from \"../hooks/useMultiListener\";\r\nimport type { MultiListenerComponent as MultiListenerComponentType } from \"../types\";\r\n\r\nfunction MultiListenerComponent({\r\n notifiers,\r\n selector,\r\n areStatesEqual,\r\n stableChild,\r\n children\r\n}: any) {\r\n const state = useMultiListener(notifiers, selector, areStatesEqual);\r\n return children(state, stableChild);\r\n}\r\n\r\n/**\r\n * An advanced declarative component wrapper that enables fine-grained \r\n * subscriptions to multiple notifiers simultaneously with full type safety.\r\n * \r\n * @remarks\r\n * Automatically infers tuple types from the provided notifiers array \r\n * and includes smart array equality checking.\r\n * \r\n * @see [Barrex Docs](./docs/components/multi-listener.md)\r\n * \r\n * @param props The component props.\r\n * \r\n * @example\r\n * ```tsx\r\n * import { MultiListener } from \"barrex\";\r\n * \r\n * <MultiListener notifiers={[userNotifier.name, cartNotifier.count]}>\r\n * {([name, count]) => <p>{name} has {count} items</p>}\r\n * </MultiListener>\r\n * ```\r\n */\r\nexport const MultiListener: MultiListenerComponentType = memo(\r\n MultiListenerComponent,\r\n (prevProps, nextProps) => {\r\n if (nextProps.isolated) return true;\r\n return (\r\n prevProps.notifiers === nextProps.notifiers &&\r\n prevProps.selector === nextProps.selector &&\r\n prevProps.areStatesEqual === nextProps.areStatesEqual &&\r\n prevProps.stableChild === nextProps.stableChild &&\r\n prevProps.children === nextProps.children\r\n );\r\n }\r\n);","import { memo } from \"react\";\r\n\r\nimport type { IsolatedContainerProps } from \"../types\";\r\n\r\nfunction IsolatedContainerComponent({ children }: IsolatedContainerProps) {\r\n return <>{children}</>;\r\n}\r\n\r\n/**\r\n * A boundary component designed to completely shield its child \r\n * subtree from parent-driven re-render cascades.\r\n * \r\n * @remarks\r\n * Children inside an `IsolatedContainer` can still re-render independently \r\n * based on their own local state or external reactive bindings (like `barrex` notifiers).\r\n * \r\n * @see [Barrex Docs](./docs/components/isolated-container.md)\r\n * \r\n * @param props The component props.\r\n * \r\n * @example\r\n * ```tsx\r\n * import { IsolatedContainer } from \"barrex\";\r\n * \r\n * <IsolatedContainer>\r\n * <StaticHeader/>\r\n * <CounterWidget/>\r\n * </IsolatedContainer>\r\n * ```\r\n */\r\nexport const IsolatedContainer = memo(\r\n IsolatedContainerComponent,\r\n () => true\r\n) as typeof IsolatedContainerComponent;"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "barrex",
3
- "version": "0.0.2",
4
- "description": "The bare essentials for state management in React",
3
+ "version": "0.1.1",
4
+ "description": "The bare essentials for state management in React.",
5
5
  "homepage": "https://github.com/bhuvanpddahal/barrex#readme",
6
6
  "repository": {
7
7
  "type": "git",
@@ -48,4 +48,4 @@
48
48
  "fine-grained-reactivity",
49
49
  "hooks"
50
50
  ]
51
- }
51
+ }