barrex 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- 'use strict';var react=require('react'),jsxRuntime=require('react/jsx-runtime');var f=Symbol("isNotifier");function C(e){return typeof e=="object"&&e!==null}function u(e){return Array.isArray(e)}function c(e){return typeof e=="function"}function N(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&e[f]===true}var p=class{state;rootNode;constructor(t){this.state=t,this.rootNode=new h(this);}},m=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===f)return true;if(r==="get")return this.get;if(r==="set")return this.dispatchSet;if(r==="subscribe")return this.subscribe;let n=this.get();if(C(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 m{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 m{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 k="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 y(e){let t=c(e)?e():e;if(c(t))throw new Error(k);return N(t)?t:T(t)}function T(e){return new p(e).rootNode.proxy}function w(e,t){let r=e.subscribe(()=>{t(e.get());});return t(e.get()),r}function L(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 I(e){let[t]=react.useState(()=>y(e));return t}function S(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 A(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 b(e,t,r=A){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=S(e,t,r);return i(o,n)}var F=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 j({notifiers:e,selector:t,areStatesEqual:r,stableChild:n,children:i}){let o=b(e,t,r);return i(o,n)}var v=react.memo(j,(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 K({children:e}){return jsxRuntime.jsx(jsxRuntime.Fragment,{children:e})}var _=react.memo(K,()=>true);exports.IsolatedContainer=_;exports.Listener=F;exports.MultiListener=v;exports.listener=w;exports.multiListener=L;exports.notifier=y;exports.useListener=S;exports.useMultiListener=b;exports.useNotifier=I;//# sourceMappingURL=index.cjs.map
1
+ 'use strict';var react=require('react'),jsxRuntime=require('react/jsx-runtime');var y=Symbol("isNull"),p=Symbol("isNotifier"),b=Symbol("getNode"),h=Symbol("dispose");function x(t){return typeof t=="object"&&t!==null}function f(t){return Array.isArray(t)}function u(t){return typeof t=="function"}function v(t){return t===null||(typeof t!="object"||F(t))&&typeof t!="function"}function F(t){return t instanceof Date}function c(t){return typeof t=="object"&&t!==null&&!Array.isArray(t)&&t[p]===true}function m(t,e){return f(t)&&f(e)&&t.length===e.length?t.every((i,n)=>{let r=e[n];return i===r}):t===e}function S(t){return Object.assign(f(t)?[]:{},t)}var N=class{state;resolved;listeners;children;proxy;linkedFromNodes;constructor(e,i){this.state=e,this.resolved=false,this.proxy=i??this.createProxy();}get=()=>{let e=this.peek();return this.resolved?e:(this.resolveSubtree(e),this.state)};dispatchSet=e=>this.set(e,false);subscribe=e=>(this.listeners||(this.listeners=new Set),this.listeners.add(e),()=>{this.listeners&&(this.listeners.delete(e),this.listeners.size||(this.listeners=void 0));});notifyListeners(){this.listeners?.forEach(e=>e());}notifyLinkedFromNodes(e){this.linkedFromNodes?.forEach(i=>{i.state=e,i.parent.peek()[i.key]=e,i.parent.resolved&&i.parent.set(S);});}addLinkedFromNode(e){this.linkedFromNodes||(this.linkedFromNodes=new Set),this.linkedFromNodes.add(e),this instanceof d&&this.root.addLinkedNode(this);}removeLinkedFromNode(e){this.linkedFromNodes&&(this.linkedFromNodes.delete(e),this.linkedFromNodes.size||(this.linkedFromNodes=void 0,this instanceof d&&!this.isLinkedToNode&&this.root.removeLinkedNode(this)));}bridgePath(e){let i=this,n=this instanceof d?this.root:this;for(let o=0;o<e.length-1;o++){let a=e[o],l=i.peek()[a],L=new d(a,n,i,l);i.children||(i.children=new Map),i.children.set(a,L),i=L;}let r=e[e.length-1];return new d(r,n,i,i.peek()[r])}resolveSubtree(e,i){if(v(e)){i||(this.resolved=true);return}if(c(e))if(i){let n=this.bridgePath(i);if(n.establishLink(),n.parent.peek()[n.key]=n.peek(),n.parent!==this)return n.parent}else this instanceof d&&this.establishLink();else {let n,r=(i?.length??0)===1,s=(o,a)=>{let l=r?this.children?.get(o):void 0;if(l){if(l.resolved)return;l.resolveSubtree(a);}else u(a)||(n?n.resolveSubtree(a,[o]):n=this.resolveSubtree(a,[...i??[],o]));};if(f(e))for(let o=0;o<e.length;o++)s(`${o}`,e[o]);else for(let o in e)s(o,e[o]);if(n&&(n.resolved=true,n.parent!==this))return n.parent;i||(this.resolved=true);}}reconcileChildren(e,i){if(!(!this.children||!f(e)||!f(i)))for(let[n,r]of this.children){let s=n,o=e[s];i[s]!==o&&r.notifySubtree();}}setTrap(){return false}createProxy(){return new Proxy({},{get:(e,i)=>{if(i==="get")return this.get;if(i==="set")return this.dispatchSet;if(i==="subscribe")return this.subscribe;if(i===p)return true;if(i===b)return this;if(i===h)return this.dispose;let n=this.peek();if(x(n)&&Object.hasOwn(n,i)){let r=n[i];if(u(r))return r;this.children||(this.children=new Map);let s=this.children.get(i);if(!s){let o=this instanceof d?this.root:this;s=new d(i,o,this,r),this.children.set(i,s);}return s.proxy}},set:this.setTrap})}},k=class extends N{linkedNodes;constructor(e){super(e);}dispose=()=>{this.linkedFromNodes&&(this.linkedFromNodes.forEach(e=>e.severLink(false)),this.linkedFromNodes=void 0),this.linkedNodes&&(this.linkedNodes.forEach(e=>e.dispose()),this.linkedNodes=void 0);};peek(){return this.state}set(e,i=true){let n=this.get(),r=u(e)?e(n):e;return r!==n?(this.state=r,this.notifyLinkedFromNodes(r),this.notifyListeners(),i||this.reconcileChildren(n,r),true):false}addLinkedNode(e){this.linkedNodes||(this.linkedNodes=new Set),this.linkedNodes.add(e);}removeLinkedNode(e){this.linkedNodes&&(this.linkedNodes.delete(e),this.linkedNodes.size||(this.linkedNodes=void 0));}},d=class extends N{key;root;parent;linkedToNode;constructor(e,i,n,r){super(r,c(r)?r:void 0),this.key=e,this.root=i,this.parent=n,this.resolved=n.resolved;}get isLinkedToNode(){return !!this.linkedToNode}dispose=()=>{this.linkedFromNodes&&(this.linkedFromNodes.forEach(e=>e.severLink(false)),this.linkedFromNodes=void 0),this.severLink();};peek(){return this.state!==y?this.state:this.state=this.parent.peek()[this.key]}set(e,i=true){let n=this.get(),r=u(e)?e(n):e;if(r!==n){let s=this.parent.peek(),o;if(this.parent.resolved){let a=S(s);a[this.key]=r,o=this.parent.set(a);}else s[this.key]=r,o=true;return o?(this.state=r,this.notifyLinkedFromNodes(r),this.notifyListeners(),i||this.reconcileChildren(n,r),true):false}return false}establishLink(){let e=this.proxy[b];e.addLinkedFromNode(this),this.linkedToNode=e,this.root.addLinkedNode(this);let i=e.get();this.parent.peek()[this.key]=i,this.state=i,this.resolved=true;}severLink(e=true){this.linkedToNode&&(e&&this.linkedToNode.removeLinkedFromNode(this),this.linkedToNode=void 0,this.proxy={});}notifySubtree(){if(this.state=y,this.notifyListeners(),this.children)for(let e of this.children.values())e.notifySubtree();}};var U="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 C(t){let e=u(t)?t():t;if(u(e))throw new Error(U);return c(e)?e:I(e)}function I(t){return new k(t).proxy}function M(t,e){let i=t.get(),n=t.subscribe(()=>{let r=t.get();r!==i&&(e(r),i=r);});return e(i),n}function V(t,e){let i=null,n=()=>{let o=t.map(a=>a.get());m(i,o)||(e(o),i=o);},r=t.map(o=>o.subscribe(n)),s=()=>r.forEach(o=>o());return n(),s}function E(t){if(process.env.NODE_ENV==="development"||process.env.NODE_ENV==="test"){let e=react.useRef(false);react.useEffect(()=>{if(e.current)return t;e.current=true;},[]);}else react.useEffect(()=>t,[]);}function j(t){let e=react.useRef(null);return e.current===null&&(e.current=C(t)),E(e.current[h]),e.current}function w(t,e,i){let n=react.useRef(null);return react.useSyncExternalStore(t.subscribe,()=>{let r=e?e(t.get()):t.get();return i&&n.current!==null&&i(n.current,r)?n.current:(n.current=r,r)},()=>e?e(t.get()):t.get())}function T(t,e,i=m){let n=react.useRef(null),r=react.useCallback(o=>{let a=t.map(l=>l.subscribe(o));return ()=>a.forEach(l=>l())},t),s=()=>{let o=t.map(l=>l.get()),a=e?e(o):o;return n.current!==null&&i(n.current,a)?n.current:(n.current=a,a)};return react.useSyncExternalStore(r,s,s)}function W({notifier:t,selector:e,areStatesEqual:i,stableChild:n,children:r}){let s=w(t,e,i);return r(s,n)}var $=react.memo(W,(t,e)=>e.isolated?true:t.notifier===e.notifier&&t.selector===e.selector&&t.areStatesEqual===e.areStatesEqual&&t.stableChild===e.stableChild&&t.children===e.children);function P({notifiers:t,selector:e,areStatesEqual:i,stableChild:n,children:r}){let s=T(t,e,i);return r(s,n)}var G=react.memo(P,(t,e)=>e.isolated?true:t.notifiers===e.notifiers&&t.selector===e.selector&&t.areStatesEqual===e.areStatesEqual&&t.stableChild===e.stableChild&&t.children===e.children);function J({children:t}){return jsxRuntime.jsx(jsxRuntime.Fragment,{children:t})}var Q=react.memo(J,()=>true);exports.IsolatedContainer=Q;exports.Listener=$;exports.MultiListener=G;exports.listener=M;exports.multiListener=V;exports.notifier=C;exports.useListener=w;exports.useMultiListener=T;exports.useNotifier=j;//# sourceMappingURL=index.cjs.map
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/symbols.ts","../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":["NOTIFIER_SYMBOL","isObjectOrArray","value","isArray","isFunction","isNotifier","NotifierRoot","state","RootNode","Node","callback","listener","child","prevState","newState","key","index","prevChildState","_","prop","childNode","ChildNode","root","fromChild","parent","parentState","newParentState","functionCannotBeNotifierMessage","notifier","initialState","resolvedState","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,IAAMA,CAAAA,CAAkB,MAAA,CAAO,YAAY,CAAA,CCG3C,SAASC,EAAgBC,CAAAA,CAAiC,CAC7D,OAAO,OAAOA,CAAAA,EAAU,QAAA,EAAYA,IAAU,IAClD,CAEO,SAASC,CAAAA,CAAQD,CAAAA,CAAyC,CAC7D,OAAO,KAAA,CAAM,OAAA,CAAQA,CAAK,CAC9B,CAEO,SAASE,CAAAA,CAAWF,EAAmC,CAC1D,OAAO,OAAOA,CAAAA,EAAU,UAC5B,CAEO,SAASG,CAAAA,CAAWH,CAAAA,CAA4C,CACnE,OACI,OAAOA,CAAAA,EAAU,UACjBA,CAAAA,GAAU,IAAA,EACV,CAAC,KAAA,CAAM,OAAA,CAAQA,CAAK,CAAA,EACnBA,CAAAA,CAAcF,CAAe,CAAA,GAAM,IAE5C,CCbO,IAAMM,CAAAA,CAAN,KAAmB,CACf,KAAA,CACA,QAAA,CAEP,WAAA,CAAYC,CAAAA,CAAgB,CACxB,KAAK,KAAA,CAAQA,CAAAA,CACb,IAAA,CAAK,QAAA,CAAW,IAAIC,CAAAA,CAAS,IAAI,EACrC,CACJ,CAAA,CAEeC,CAAAA,CAAf,KAAoB,CACN,WAAA,CACF,SAAA,CACA,QAAA,CACD,KAAA,CAEP,WAAA,EAAc,CACV,IAAA,CAAK,WAAA,CAAc,KACnB,IAAA,CAAK,SAAA,CAAY,IAAI,GAAA,CACrB,IAAA,CAAK,QAAA,CAAW,IAAI,GAAA,CACpB,IAAA,CAAK,KAAA,CAAQ,IAAA,CAAK,WAAA,GACtB,CAKO,WAAA,CAAeF,CAAAA,EACX,IAAA,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AAAA,CACA,QAAA,CAAAM,CAAAA,CACA,cAAA,CAAAC,CAAAA,CACA,YAAAY,CAAAA,CACA,QAAA,CAAAC,CACJ,CAAA,CAAQ,CACJ,IAAM/C,EAAQyC,CAAAA,CAAiBd,CAAAA,CAAWM,CAAAA,CAAUC,CAAc,CAAA,CAClE,OAAOa,CAAAA,CAAS/C,CAAAA,CAAO8C,CAAW,CACtC,CAuBO,IAAMO,CAAAA,CAA4CJ,UAAAA,CACrDG,EACA,CAACF,CAAAA,CAAWC,CAAAA,GACJA,CAAAA,CAAU,QAAA,CAAiB,IAAA,CAE3BD,EAAU,SAAA,GAAcC,CAAAA,CAAU,SAAA,EAClCD,CAAAA,CAAU,QAAA,GAAaC,CAAAA,CAAU,UACjCD,CAAAA,CAAU,cAAA,GAAmBC,CAAAA,CAAU,cAAA,EACvCD,CAAAA,CAAU,WAAA,GAAgBC,CAAAA,CAAU,WAAA,EACpCD,CAAAA,CAAU,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 const NOTIFIER_SYMBOL = Symbol(\"isNotifier\");","import type { Notifier } from \"../types\";\r\nimport { NOTIFIER_SYMBOL } from \"../symbols\";\r\n\r\nexport 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}\r\n\r\nexport function isNotifier(value: unknown): value is Notifier<unknown> {\r\n return (\r\n typeof value === \"object\" &&\r\n value !== null &&\r\n !Array.isArray(value) &&\r\n (value as any)[NOTIFIER_SYMBOL] === true\r\n );\r\n}","import type {\r\n NodeKey,\r\n SetState,\r\n StateUpdater,\r\n VoidCallback\r\n} from \"./types\";\r\nimport { NOTIFIER_SYMBOL } from \"./symbols\";\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 === NOTIFIER_SYMBOL) return true;\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 { NotifierRoot } from \"../notifier\";\r\nimport { isFunction, isNotifier } from \"./helpers\";\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](https://github.com/bhuvanpddahal/barrex/blob/main/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<Notifier<T>>): Notifier<T>;\r\nexport function notifier<T>(initialState: InitialState<T>): Notifier<T>;\r\nexport function notifier<T>(initialState: unknown): Notifier<T> {\r\n const resolvedState = isFunction(initialState)\r\n ? (initialState)()\r\n : initialState;\r\n if (isFunction(resolvedState)) {\r\n throw new Error(functionCannotBeNotifierMessage);\r\n }\r\n if (isNotifier(resolvedState)) {\r\n return resolvedState as Notifier<T>;\r\n }\r\n return createNotifier(resolvedState);\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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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.count);\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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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;"]}
1
+ {"version":3,"sources":["../src/symbols.ts","../src/utils/helpers.ts","../src/nodes.ts","../src/utils/notifier.ts","../src/utils/listener.ts","../src/utils/multi-listener.ts","../src/hooks/useUnmount.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":["nullSymbol","notifierSymbol","getNodeSymbol","disposeSymbol","isObjectOrArray","value","isArray","isFunction","isPrimitive","isDate","isNotifier","areStatesEqual","prevState","nextState","prevValue","index","nextValue","shallowCopy","Node","state","proxy","callback","listener","newState","node","ChildNode","keys","parentNode","rootNode","key","childNode","currentNode","isDirectChild","handleEntry","child","prevChildState","_","prop","RootNode","fromChild","root","parent","parentState","isSet","newParentState","linkedToNode","removeFromLinkedToNode","functionCannotBeNotifierMessage","notifier","initialState","resolvedState","createNotifier","unsubscribe","multiListener","notifiers","unsubscribeFns","fn","useUnmount","didRunStrictCleanupRef","useRef","useEffect","useNotifier","notifierRef","useListener","selector","useSyncExternalStore","useMultiListener","subscribe","useCallback","getSnapshot","ListenerComponent","stableChild","children","Listener","memo","prevProps","nextProps","MultiListenerComponent","MultiListener","IsolatedContainerComponent","jsx","Fragment","IsolatedContainer"],"mappings":"gFAAO,IAAMA,CAAAA,CAAa,OAAO,QAAQ,CAAA,CAC5BC,EAAiB,MAAA,CAAO,YAAY,EACpCC,CAAAA,CAAgB,MAAA,CAAO,SAAS,CAAA,CAChCC,CAAAA,CAAgB,OAAO,SAAS,CAAA,CCAtC,SAASC,CAAAA,CAAgBC,CAAAA,CAAiC,CAC7D,OAAO,OAAOA,CAAAA,EAAU,UAAYA,CAAAA,GAAU,IAClD,CAEO,SAASC,CAAAA,CAAQD,EAAyC,CAC7D,OAAO,MAAM,OAAA,CAAQA,CAAK,CAC9B,CAEO,SAASE,EAAWF,CAAAA,CAAmC,CAC1D,OAAO,OAAOA,CAAAA,EAAU,UAC5B,CAEO,SAASG,CAAAA,CAAYH,EAAoC,CAC5D,OACIA,IAAU,IAAA,EAAA,CACR,OAAOA,GAAU,QAAA,EAAYI,CAAAA,CAAOJ,CAAK,CAAA,GAAM,OAAOA,GAAU,UAE1E,CAEO,SAASI,CAAAA,CAAOJ,CAAAA,CAA+B,CAClD,OAA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aC,CAAAA,CAAU,QAAA,EACjCD,EAAU,QAAA,GAAaC,CAAAA,CAAU,UACjCD,CAAAA,CAAU,cAAA,GAAmBC,CAAAA,CAAU,cAAA,EACvCD,CAAAA,CAAU,WAAA,GAAgBC,EAAU,WAAA,EACpCD,CAAAA,CAAU,WAAaC,CAAAA,CAAU,QAG7C,EC5CA,SAASC,EAAuB,CAC5B,SAAA,CAAAvB,EACA,QAAA,CAAAU,CAAAA,CACA,eAAArD,CAAAA,CACA,WAAA,CAAA4D,CAAAA,CACA,QAAA,CAAAC,CACJ,CAAA,CAAQ,CACJ,IAAMrD,CAAAA,CAAQ+C,EAAiBZ,CAAAA,CAAWU,CAAAA,CAAUrD,CAAc,CAAA,CAClE,OAAO6D,EAASrD,CAAAA,CAAOoD,CAAW,CACtC,CAuBO,IAAMO,EAA4CJ,UAAAA,CACrDG,CAAAA,CACA,CAACF,CAAAA,CAAWC,CAAAA,GACJA,CAAAA,CAAU,QAAA,CAAiB,IAAA,CAE3BD,CAAAA,CAAU,YAAcC,CAAAA,CAAU,SAAA,EAClCD,EAAU,QAAA,GAAaC,CAAAA,CAAU,UACjCD,CAAAA,CAAU,cAAA,GAAmBC,EAAU,cAAA,EACvCD,CAAAA,CAAU,cAAgBC,CAAAA,CAAU,WAAA,EACpCD,EAAU,QAAA,GAAaC,CAAAA,CAAU,QAG7C,EC7CA,SAASG,CAAAA,CAA2B,CAAE,QAAA,CAAAP,CAAS,CAAA,CAA2B,CACtE,OAAOQ,cAAAA,CAAAC,oBAAA,CAAG,QAAA,CAAAT,EAAS,CACvB,KAwBaU,CAAAA,CAAoBR,UAAAA,CAC7BK,CAAAA,CACA,IAAM,IACV","file":"index.cjs","sourcesContent":["export const nullSymbol = Symbol(\"isNull\");\r\nexport const notifierSymbol = Symbol(\"isNotifier\");\r\nexport const getNodeSymbol = Symbol(\"getNode\");\r\nexport const disposeSymbol = Symbol(\"dispose\");","import { notifierSymbol } from \"../symbols\";\r\nimport type { Notifier, Primitive } from \"../types\";\r\n\r\nexport 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}\r\n\r\nexport function isPrimitive(value: unknown): value is Primitive {\r\n return (\r\n value === null ||\r\n ((typeof value !== \"object\" || isDate(value)) && typeof value !== \"function\")\r\n );\r\n}\r\n\r\nexport function isDate(value: unknown): value is Date {\r\n return value instanceof Date;\r\n}\r\n\r\nexport function isNotifier(value: unknown): value is Notifier<unknown> {\r\n return (\r\n typeof value === \"object\" &&\r\n value !== null &&\r\n !Array.isArray(value) &&\r\n (value as any)[notifierSymbol] === true\r\n );\r\n}\r\n\r\nexport function areStatesEqual<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 shallowCopy<T extends object>(value: T) {\r\n return Object.assign(isArray(value) ? [] : {}, value);\r\n}","import type {\r\n NodeKey,\r\n SetState,\r\n StateUpdater,\r\n VoidCallback\r\n} from \"./types\";\r\nimport {\r\n isArray,\r\n isFunction,\r\n isNotifier,\r\n isObjectOrArray,\r\n isPrimitive,\r\n shallowCopy\r\n} from \"./utils/helpers\";\r\nimport { disposeSymbol, getNodeSymbol, notifierSymbol, nullSymbol } from \"./symbols\";\r\n\r\nabstract class Node {\r\n protected state: unknown;\r\n public resolved: boolean;\r\n protected listeners?: Set<VoidCallback>;\r\n protected children?: Map<NodeKey, ChildNode>;\r\n public proxy: object;\r\n protected linkedFromNodes?: Set<ChildNode>;\r\n\r\n constructor(state: unknown, proxy?: object) {\r\n this.state = state;\r\n this.resolved = false;\r\n this.proxy = proxy ?? this.createProxy();\r\n }\r\n\r\n public abstract dispose(): void;\r\n public abstract peek(): unknown;\r\n public abstract set(state: SetState<unknown>, fromChild?: boolean): boolean;\r\n\r\n public get = (): unknown => {\r\n const state = this.peek();\r\n if (this.resolved) return state;\r\n this.resolveSubtree(state);\r\n return this.state;\r\n };\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 if (!this.listeners) {\r\n this.listeners = new Set();\r\n }\r\n this.listeners.add(callback);\r\n return () => {\r\n if (!this.listeners) return;\r\n this.listeners.delete(callback);\r\n if (!this.listeners.size) {\r\n this.listeners = undefined;\r\n }\r\n };\r\n };\r\n\r\n protected notifyListeners() {\r\n this.listeners?.forEach((listener) => listener());\r\n }\r\n\r\n protected notifyLinkedFromNodes(newState: unknown) {\r\n this.linkedFromNodes?.forEach((node) => {\r\n node.state = newState;\r\n (node.parent.peek() as any)[node.key] = newState;\r\n if (node.parent.resolved) {\r\n node.parent.set(shallowCopy);\r\n }\r\n });\r\n }\r\n\r\n public addLinkedFromNode(node: ChildNode) {\r\n if (!this.linkedFromNodes) {\r\n this.linkedFromNodes = new Set();\r\n }\r\n this.linkedFromNodes.add(node);\r\n if (this instanceof ChildNode) {\r\n this.root.addLinkedNode(this);\r\n }\r\n }\r\n\r\n public removeLinkedFromNode(node: ChildNode) {\r\n if (this.linkedFromNodes) {\r\n this.linkedFromNodes.delete(node);\r\n if (!this.linkedFromNodes.size) {\r\n this.linkedFromNodes = undefined;\r\n if (this instanceof ChildNode && !this.isLinkedToNode) {\r\n this.root.removeLinkedNode(this);\r\n }\r\n }\r\n }\r\n }\r\n\r\n private bridgePath(keys: [NodeKey, ...NodeKey[]]) {\r\n let parentNode = this as Node;\r\n const rootNode = (this instanceof ChildNode ? this.root : this) as RootNode;\r\n for (let index = 0; index < keys.length - 1; index++) {\r\n const key = keys[index];\r\n const state = (parentNode.peek() as any)[key];\r\n const childNode = new ChildNode(key, rootNode, parentNode, state);\r\n if (!parentNode.children) {\r\n parentNode.children = new Map();\r\n }\r\n parentNode.children.set(key, childNode);\r\n parentNode = childNode;\r\n }\r\n const key = keys[keys.length - 1];\r\n const currentNode = new ChildNode(\r\n key, rootNode, parentNode, (parentNode.peek() as any)[key]\r\n );\r\n return currentNode;\r\n }\r\n\r\n private resolveSubtree(state: unknown, keys?: [NodeKey, ...NodeKey[]]) {\r\n if (isPrimitive(state)) {\r\n if (!keys) this.resolved = true;\r\n return;\r\n }\r\n\r\n if (isNotifier(state)) {\r\n if (keys) {\r\n const currentNode = this.bridgePath(keys);\r\n currentNode.establishLink();\r\n (currentNode.parent.peek() as any)[currentNode.key] = currentNode.peek();\r\n if (currentNode.parent !== this) {\r\n return currentNode.parent as ChildNode;\r\n }\r\n } else if (this instanceof ChildNode) {\r\n this.establishLink();\r\n }\r\n } else {\r\n let currentNode: ChildNode | undefined;\r\n const isDirectChild = (keys?.length ?? 0) === 1;\r\n\r\n const handleEntry = (key: NodeKey, value: unknown) => {\r\n const childNode = isDirectChild ? this.children?.get(key) : undefined;\r\n if (childNode) {\r\n if (childNode.resolved) return;\r\n childNode.resolveSubtree(value);\r\n } else if (!isFunction(value)) {\r\n if (currentNode) {\r\n currentNode.resolveSubtree(value, [key]);\r\n } else {\r\n currentNode = this.resolveSubtree(value, [...(keys ?? []), key]);\r\n }\r\n }\r\n };\r\n\r\n if (isArray(state)) {\r\n for (let index = 0; index < state.length; index++) {\r\n handleEntry(`${index}`, state[index]);\r\n }\r\n } else {\r\n for (const key in state) {\r\n handleEntry(key, (state as any)[key]);\r\n }\r\n }\r\n if (currentNode) {\r\n currentNode.resolved = true;\r\n if (currentNode.parent !== this) {\r\n return currentNode.parent as ChildNode;\r\n }\r\n }\r\n if (!keys) this.resolved = true;\r\n }\r\n }\r\n\r\n protected reconcileChildren(prevState: unknown, newState: unknown) {\r\n if (!this.children || !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 if (prop === notifierSymbol) return true;\r\n if (prop === getNodeSymbol) return this;\r\n if (prop === disposeSymbol) return this.dispose;\r\n\r\n const state = this.peek();\r\n if (isObjectOrArray(state) && Object.hasOwn(state, prop)) {\r\n const value = (state as any)[prop];\r\n if (isFunction(value)) return value;\r\n\r\n if (!this.children) {\r\n this.children = new Map();\r\n }\r\n let childNode = this.children.get(prop);\r\n if (!childNode) {\r\n const rootNode = (this instanceof ChildNode ? this.root : this) as RootNode;\r\n childNode = new ChildNode(prop, rootNode, this, value);\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\nexport class RootNode extends Node {\r\n private linkedNodes?: Set<ChildNode>;\r\n\r\n constructor(state: unknown) {\r\n super(state);\r\n }\r\n\r\n public dispose = () => {\r\n if (this.linkedFromNodes) {\r\n this.linkedFromNodes.forEach((node) => node.severLink(false));\r\n this.linkedFromNodes = undefined;\r\n }\r\n if (this.linkedNodes) {\r\n this.linkedNodes.forEach((node) => node.dispose());\r\n this.linkedNodes = undefined;\r\n }\r\n };\r\n\r\n public peek() { return this.state; }\r\n\r\n public set(state: SetState<unknown>, fromChild = true) {\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.state = newState;\r\n this.notifyLinkedFromNodes(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 public addLinkedNode(node: ChildNode) {\r\n if (!this.linkedNodes) {\r\n this.linkedNodes = new Set();\r\n }\r\n this.linkedNodes.add(node);\r\n }\r\n\r\n public removeLinkedNode(node: ChildNode) {\r\n if (this.linkedNodes) {\r\n this.linkedNodes.delete(node);\r\n if (!this.linkedNodes.size) {\r\n this.linkedNodes = undefined;\r\n }\r\n }\r\n }\r\n}\r\n\r\nclass ChildNode extends Node {\r\n public key: NodeKey;\r\n public root: RootNode;\r\n public parent: Node;\r\n private linkedToNode?: Node;\r\n\r\n constructor(key: NodeKey, root: RootNode, parent: Node, state: unknown) {\r\n super(state, isNotifier(state) ? state : undefined);\r\n this.key = key;\r\n this.root = root;\r\n this.parent = parent;\r\n this.resolved = parent.resolved;\r\n }\r\n\r\n public get isLinkedToNode() { return !!this.linkedToNode; }\r\n\r\n public dispose = () => {\r\n if (this.linkedFromNodes) {\r\n this.linkedFromNodes.forEach((node) => node.severLink(false));\r\n this.linkedFromNodes = undefined;\r\n }\r\n this.severLink();\r\n }\r\n\r\n public peek(): unknown {\r\n return this.state !== nullSymbol\r\n ? this.state\r\n : (this.state = (this.parent.peek() as any)[this.key]);\r\n }\r\n\r\n public set(state: SetState<unknown>, fromChild = true) {\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 const parentState = this.parent.peek();\r\n let isSet: boolean;\r\n if (this.parent.resolved) {\r\n const newParentState = shallowCopy(parentState as object);\r\n (newParentState as any)[this.key] = newState;\r\n isSet = this.parent.set(newParentState);\r\n } else {\r\n (parentState as any)[this.key] = newState;\r\n isSet = true;\r\n }\r\n if (isSet) {\r\n this.state = newState;\r\n this.notifyLinkedFromNodes(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\r\n public establishLink() {\r\n const linkedToNode: Node = (this.proxy as any)[getNodeSymbol];\r\n linkedToNode.addLinkedFromNode(this);\r\n this.linkedToNode = linkedToNode;\r\n this.root.addLinkedNode(this);\r\n const state = linkedToNode.get();\r\n (this.parent.peek() as any)[this.key] = state;\r\n this.state = state;\r\n this.resolved = true;\r\n }\r\n\r\n public severLink(removeFromLinkedToNode = true) {\r\n if (this.linkedToNode) {\r\n if (removeFromLinkedToNode) {\r\n this.linkedToNode.removeLinkedFromNode(this);\r\n }\r\n this.linkedToNode = undefined;\r\n this.proxy = {};\r\n }\r\n }\r\n\r\n public notifySubtree() {\r\n this.state = nullSymbol;\r\n this.notifyListeners();\r\n if (this.children) {\r\n for (const child of this.children.values()) {\r\n child.notifySubtree();\r\n }\r\n }\r\n }\r\n}","import { RootNode } from \"../nodes\";\r\nimport { isFunction, isNotifier } from \"./helpers\";\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](https://github.com/bhuvanpddahal/barrex/blob/main/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<Notifier<T>>): Notifier<T>;\r\nexport function notifier<T>(initialState: InitialState<T>): Notifier<T>;\r\nexport function notifier<T>(initialState: unknown): Notifier<T> {\r\n const resolvedState = isFunction(initialState)\r\n ? (initialState)()\r\n : initialState;\r\n if (isFunction(resolvedState)) {\r\n throw new Error(functionCannotBeNotifierMessage);\r\n }\r\n if (isNotifier(resolvedState)) {\r\n return resolvedState as Notifier<T>;\r\n }\r\n return createNotifier(resolvedState);\r\n}\r\n\r\nfunction createNotifier<T>(state: T): Notifier<T> {\r\n const rootNode = new RootNode(state);\r\n return rootNode.proxy as Notifier<T>;\r\n}","import type {\r\n ListenerCallback,\r\n Notifier,\r\n UnwrapNotifierState,\r\n VoidCallback\r\n} 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](https://github.com/bhuvanpddahal/barrex/blob/main/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<UnwrapNotifierState<T>>\r\n): VoidCallback {\r\n let prevState = notifier.get();\r\n const unsubscribe = notifier.subscribe(() => {\r\n const nextState = notifier.get();\r\n if (nextState !== prevState) {\r\n callback(nextState);\r\n prevState = nextState;\r\n }\r\n });\r\n callback(prevState);\r\n return unsubscribe;\r\n}","import type {\r\n InferNotifierStates,\r\n ListenerCallback,\r\n Notifier,\r\n VoidCallback\r\n} from \"../types\";\r\nimport { areStatesEqual } from \"./helpers\";\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](https://github.com/bhuvanpddahal/barrex/blob/main/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<InferNotifierStates<T>>\r\n): VoidCallback {\r\n let prevState: any = null;\r\n const listener = () => {\r\n const nextState = notifiers.map((notifier) => notifier.get()) as any;\r\n if (!areStatesEqual(prevState, nextState)) {\r\n callback(nextState);\r\n prevState = nextState;\r\n }\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 { useEffect, useRef } from \"react\";\r\n\r\nimport type { VoidCallback } from \"../types\";\r\n\r\nexport function useUnmount(callback: VoidCallback) {\r\n if (process.env.NODE_ENV === \"development\" || process.env.NODE_ENV === \"test\") {\r\n const didRunStrictCleanupRef = useRef(false);\r\n useEffect(() => {\r\n if (didRunStrictCleanupRef.current) {\r\n return callback;\r\n } else {\r\n didRunStrictCleanupRef.current = true;\r\n }\r\n }, []);\r\n } else {\r\n useEffect(() => callback, []);\r\n }\r\n}","import { useRef } from \"react\";\r\n\r\nimport { useUnmount } from \"./useUnmount\";\r\nimport { disposeSymbol } from \"../symbols\";\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](https://github.com/bhuvanpddahal/barrex/blob/main/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<Notifier<T>>): Notifier<T>;\r\nexport function useNotifier<T>(initialState: InitialState<T>): Notifier<T>;\r\nexport function useNotifier<T>(initialState: any): Notifier<T> {\r\n const notifierRef = useRef<Notifier<T>>(null);\r\n if (notifierRef.current === null) {\r\n notifierRef.current = createNotifier(initialState);\r\n }\r\n useUnmount((notifierRef.current as any)[disposeSymbol]);\r\n return notifierRef.current;\r\n}","import { useRef, useSyncExternalStore } from \"react\";\r\n\r\nimport type {\r\n Notifier,\r\n StatesComparer,\r\n StateSelector,\r\n UnwrapNotifierState\r\n} 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](https://github.com/bhuvanpddahal/barrex/blob/main/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.count);\r\n * return <span>Count: {count}</span>;\r\n * }\r\n * ```\r\n */\r\nexport function useListener<T, U = UnwrapNotifierState<T>>(\r\n notifier: Notifier<T>,\r\n selector?: StateSelector<UnwrapNotifierState<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 InferNotifierStates,\r\n Notifier,\r\n StatesComparer,\r\n StateSelector,\r\n VoidCallback\r\n} from \"../types\";\r\nimport { areStatesEqual as areStatesEqualFn } from \"../utils/helpers\";\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](https://github.com/bhuvanpddahal/barrex/blob/main/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<InferNotifierStates<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<InferNotifierStates<T>>\r\n): InferNotifierStates<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 { useListener } from \"../hooks/useListener\";\r\nimport type { ListenerProps, UnwrapNotifierState } from \"../types\";\r\n\r\nfunction ListenerComponent<T, U = UnwrapNotifierState<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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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,14 +1,24 @@
1
1
  import * as react from 'react';
2
2
  import { JSX, ReactNode } from 'react';
3
3
 
4
+ /** Built-in types that satisfy the `object` type. */
5
+ type BuiltInObject = Date | RegExp | Map<unknown, unknown> | Set<unknown> | WeakMap<object, unknown> | WeakSet<object> | Promise<unknown> | Error | ArrayBuffer | SharedArrayBuffer | DataView | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
6
+ /** Helper type to detect if T is a plain object. */
7
+ type IsPlainObject<T> = [T] extends [BuiltInObject] ? false : true;
8
+ /** Recursively unwraps notifier's state, unwraps any notifiers inside it to its actual value. */
9
+ type UnwrapNotifierState<T> = [
10
+ T
11
+ ] extends [Notifier<infer U>] ? UnwrapNotifierState<U> : [T] extends [Array<infer Item>] ? Array<UnwrapNotifierState<Item>> : IsPlainObject<T> extends true ? {
12
+ [K in keyof T]: UnwrapNotifierState<T[K]>;
13
+ } : T;
4
14
  /**
5
15
  * The core methods available on every notifier node in the `barrex` tree.
6
16
  */
7
17
  type BaseNotifier<T> = {
8
18
  /** Retrieves the current value of the notifier state. */
9
- get(): T;
19
+ get(): UnwrapNotifierState<T>;
10
20
  /** Updates the notifier state with a new value or an updater function, returning true if updated. */
11
- set(state: SetState<T>): boolean;
21
+ set(state: SetState<UnwrapNotifierState<T>>): boolean;
12
22
  /** Subscribes a listener callback to state updates, returning an unsubscribe function. */
13
23
  subscribe(listener: VoidCallback): VoidCallback;
14
24
  };
@@ -19,9 +29,9 @@ type BaseNotifier<T> = {
19
29
  type NotifierNode<T> = ([
20
30
  T
21
31
  ] extends [Array<any>] ? {
22
- [K in Extract<keyof T, number | `${number}`>]: T[K] extends Function ? T[K] : Notifier<T[K]>;
32
+ [K in Extract<keyof T, number | `${number}`>]: T[K] extends (Function | Notifier<unknown>) ? T[K] : Notifier<T[K]>;
23
33
  } : [T] extends [object] ? {
24
- [K in keyof T]: T[K] extends Function ? T[K] : Notifier<T[K]>;
34
+ [K in keyof T]: T[K] extends (Function | Notifier<unknown>) ? T[K] : Notifier<T[K]>;
25
35
  } : object) & BaseNotifier<T>;
26
36
  /**
27
37
  * Represents a deeply reactive proxy container wrapping state of type `T`.
@@ -43,11 +53,11 @@ interface BaseListenerProps {
43
53
  /**
44
54
  * Props for the single `Listener` component wrapper.
45
55
  */
46
- interface ListenerProps<T, U = T> extends BaseListenerProps {
56
+ interface ListenerProps<T, U = UnwrapNotifierState<T>> extends BaseListenerProps {
47
57
  /** The source notifier instance to listen to. */
48
58
  notifier: Notifier<T>;
49
59
  /** Optional function to transform or compute a specific slice from the notifier state. */
50
- selector?: StateSelector<T, U>;
60
+ selector?: StateSelector<UnwrapNotifierState<T>, U>;
51
61
  /** Optional custom comparer. Returns true to skip re-rendering when states are equal. */
52
62
  areStatesEqual?: StatesComparer<U>;
53
63
  /** A render-prop function receiving the reactive state and optional stableChild. */
@@ -56,7 +66,7 @@ interface ListenerProps<T, U = T> extends BaseListenerProps {
56
66
  /**
57
67
  * Props for the `MultiListener` component.
58
68
  */
59
- type MultiListenerProps<T extends readonly Notifier<unknown>[], U = InferNotifierValues<T>> = MultiListenerWithSelectorProps<T, U> | MultiListenerWithoutSelectorProps<T>;
69
+ type MultiListenerProps<T extends readonly Notifier<unknown>[], U = InferNotifierStates<T>> = MultiListenerWithSelectorProps<T, U> | MultiListenerWithoutSelectorProps<T>;
60
70
  /**
61
71
  * Props for the `MultiListener` component when utilizing a selector transformation.
62
72
  */
@@ -64,7 +74,7 @@ interface MultiListenerWithSelectorProps<T extends readonly Notifier<unknown>[],
64
74
  /** A readonly tuple array of source notifiers to listen to. */
65
75
  notifiers: T;
66
76
  /** Function to transform or combine the tuple values into a custom shape U. */
67
- selector: StateSelector<InferNotifierValues<T>, U>;
77
+ selector: StateSelector<InferNotifierStates<T>, U>;
68
78
  /**
69
79
  * Optional custom comparer. Returns true to skip re-rendering when states are equal.
70
80
  * Defaults to a smart tuple-aware shallow equality checker.
@@ -81,9 +91,9 @@ interface MultiListenerWithoutSelectorProps<T extends readonly Notifier<unknown>
81
91
  notifiers: T;
82
92
  selector?: undefined;
83
93
  /** Optional custom comparer for the state tuple. Defaults to a smart tuple-aware shallow equality checker. */
84
- areStatesEqual?: StatesComparer<InferNotifierValues<T>>;
94
+ areStatesEqual?: StatesComparer<InferNotifierStates<T>>;
85
95
  /** A render-prop function receiving the resolved state tuple and optional stableChild. */
86
- children: ChildrenRenderer<InferNotifierValues<T>>;
96
+ children: ChildrenRenderer<InferNotifierStates<T>>;
87
97
  }
88
98
  /**
89
99
  * Overloaded component interface for `MultiListener` supporting both tuple and selector patterns.
@@ -116,11 +126,11 @@ type SetState<T> = T | StateUpdater<T>;
116
126
  type InitialState<T> = (T extends Function ? never : (T | StateInitializer<T>)) & {};
117
127
  /** A render-prop function signature for rendering reactive components. */
118
128
  type ChildrenRenderer<T> = (state: T, stableChild?: JSX.Element) => ReactNode;
119
- /** Helper type to extract the underlying value type T from a Notifier<T>. */
120
- type InferNotifierValue<N> = N extends Notifier<infer T> ? T : never;
129
+ /** Helper type to extract the underlying state type from T of a `Notifier<T>`. */
130
+ type InferNotifierState<T> = T extends Notifier<infer U> ? UnwrapNotifierState<U> : never;
121
131
  /** Mapped tuple type to transform `[Notifier<A>, Notifier<B>]` into `[A, B]`. */
122
- type InferNotifierValues<T extends readonly Notifier<unknown>[]> = {
123
- [K in keyof T]: InferNotifierValue<T[K]>;
132
+ type InferNotifierStates<T extends readonly Notifier<unknown>[]> = {
133
+ [K in keyof T]: InferNotifierState<T[K]>;
124
134
  };
125
135
 
126
136
  /**
@@ -171,7 +181,7 @@ declare function notifier<T>(initialState: InitialState<T>): Notifier<T>;
171
181
  * });
172
182
  * ```
173
183
  */
174
- declare function listener<T>(notifier: Notifier<T>, callback: ListenerCallback<T>): VoidCallback;
184
+ declare function listener<T>(notifier: Notifier<T>, callback: ListenerCallback<UnwrapNotifierState<T>>): VoidCallback;
175
185
 
176
186
  /**
177
187
  * Subscribes imperatively to multiple `barrex` notifiers simultaneously outside of React.
@@ -198,7 +208,7 @@ declare function listener<T>(notifier: Notifier<T>, callback: ListenerCallback<T
198
208
  * );
199
209
  * ```
200
210
  */
201
- declare function multiListener<const T extends readonly Notifier<unknown>[]>(notifiers: T, callback: ListenerCallback<InferNotifierValues<T>>): VoidCallback;
211
+ declare function multiListener<const T extends readonly Notifier<unknown>[]>(notifiers: T, callback: ListenerCallback<InferNotifierStates<T>>): VoidCallback;
202
212
 
203
213
  /**
204
214
  * Creates a component-scoped, lazy-initialized notifier with automatic
@@ -225,6 +235,7 @@ declare function multiListener<const T extends readonly Notifier<unknown>[]>(not
225
235
  * }
226
236
  * ```
227
237
  */
238
+ declare function useNotifier<T>(initialState: InitialState<Notifier<T>>): Notifier<T>;
228
239
  declare function useNotifier<T>(initialState: InitialState<T>): Notifier<T>;
229
240
 
230
241
  /**
@@ -255,7 +266,7 @@ declare function useNotifier<T>(initialState: InitialState<T>): Notifier<T>;
255
266
  * }
256
267
  * ```
257
268
  */
258
- declare function useListener<T, U = T>(notifier: Notifier<T>, selector?: StateSelector<T, U>, areStatesEqual?: StatesComparer<U>): U;
269
+ declare function useListener<T, U = UnwrapNotifierState<T>>(notifier: Notifier<T>, selector?: StateSelector<UnwrapNotifierState<T>, U>, areStatesEqual?: StatesComparer<U>): U;
259
270
 
260
271
  /**
261
272
  * Subscribes a React component to multiple notifiers simultaneously,
@@ -282,10 +293,10 @@ declare function useListener<T, U = T>(notifier: Notifier<T>, selector?: StateSe
282
293
  * }
283
294
  * ```
284
295
  */
285
- declare function useMultiListener<const T extends readonly Notifier<unknown>[], const U>(notifiers: T, selector: StateSelector<InferNotifierValues<T>, U>, areStatesEqual?: StatesComparer<U>): U;
286
- declare function useMultiListener<const T extends readonly Notifier<unknown>[]>(notifiers: T, selector?: undefined, areStatesEqual?: StatesComparer<InferNotifierValues<T>>): InferNotifierValues<T>;
296
+ declare function useMultiListener<const T extends readonly Notifier<unknown>[], const U>(notifiers: T, selector: StateSelector<InferNotifierStates<T>, U>, areStatesEqual?: StatesComparer<U>): U;
297
+ declare function useMultiListener<const T extends readonly Notifier<unknown>[]>(notifiers: T, selector?: undefined, areStatesEqual?: StatesComparer<InferNotifierStates<T>>): InferNotifierStates<T>;
287
298
 
288
- declare function ListenerComponent<T, U = T>({ notifier, selector, areStatesEqual, stableChild, children }: ListenerProps<T, U>): react.ReactNode;
299
+ declare function ListenerComponent<T, U = UnwrapNotifierState<T>>({ notifier, selector, areStatesEqual, stableChild, children }: ListenerProps<T, U>): react.ReactNode;
289
300
  /**
290
301
  * A declarative component wrapper that brings fine-grained reactivity
291
302
  * and granular performance optimization directly into your JSX tree.
package/dist/index.d.ts CHANGED
@@ -1,14 +1,24 @@
1
1
  import * as react from 'react';
2
2
  import { JSX, ReactNode } from 'react';
3
3
 
4
+ /** Built-in types that satisfy the `object` type. */
5
+ type BuiltInObject = Date | RegExp | Map<unknown, unknown> | Set<unknown> | WeakMap<object, unknown> | WeakSet<object> | Promise<unknown> | Error | ArrayBuffer | SharedArrayBuffer | DataView | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
6
+ /** Helper type to detect if T is a plain object. */
7
+ type IsPlainObject<T> = [T] extends [BuiltInObject] ? false : true;
8
+ /** Recursively unwraps notifier's state, unwraps any notifiers inside it to its actual value. */
9
+ type UnwrapNotifierState<T> = [
10
+ T
11
+ ] extends [Notifier<infer U>] ? UnwrapNotifierState<U> : [T] extends [Array<infer Item>] ? Array<UnwrapNotifierState<Item>> : IsPlainObject<T> extends true ? {
12
+ [K in keyof T]: UnwrapNotifierState<T[K]>;
13
+ } : T;
4
14
  /**
5
15
  * The core methods available on every notifier node in the `barrex` tree.
6
16
  */
7
17
  type BaseNotifier<T> = {
8
18
  /** Retrieves the current value of the notifier state. */
9
- get(): T;
19
+ get(): UnwrapNotifierState<T>;
10
20
  /** Updates the notifier state with a new value or an updater function, returning true if updated. */
11
- set(state: SetState<T>): boolean;
21
+ set(state: SetState<UnwrapNotifierState<T>>): boolean;
12
22
  /** Subscribes a listener callback to state updates, returning an unsubscribe function. */
13
23
  subscribe(listener: VoidCallback): VoidCallback;
14
24
  };
@@ -19,9 +29,9 @@ type BaseNotifier<T> = {
19
29
  type NotifierNode<T> = ([
20
30
  T
21
31
  ] extends [Array<any>] ? {
22
- [K in Extract<keyof T, number | `${number}`>]: T[K] extends Function ? T[K] : Notifier<T[K]>;
32
+ [K in Extract<keyof T, number | `${number}`>]: T[K] extends (Function | Notifier<unknown>) ? T[K] : Notifier<T[K]>;
23
33
  } : [T] extends [object] ? {
24
- [K in keyof T]: T[K] extends Function ? T[K] : Notifier<T[K]>;
34
+ [K in keyof T]: T[K] extends (Function | Notifier<unknown>) ? T[K] : Notifier<T[K]>;
25
35
  } : object) & BaseNotifier<T>;
26
36
  /**
27
37
  * Represents a deeply reactive proxy container wrapping state of type `T`.
@@ -43,11 +53,11 @@ interface BaseListenerProps {
43
53
  /**
44
54
  * Props for the single `Listener` component wrapper.
45
55
  */
46
- interface ListenerProps<T, U = T> extends BaseListenerProps {
56
+ interface ListenerProps<T, U = UnwrapNotifierState<T>> extends BaseListenerProps {
47
57
  /** The source notifier instance to listen to. */
48
58
  notifier: Notifier<T>;
49
59
  /** Optional function to transform or compute a specific slice from the notifier state. */
50
- selector?: StateSelector<T, U>;
60
+ selector?: StateSelector<UnwrapNotifierState<T>, U>;
51
61
  /** Optional custom comparer. Returns true to skip re-rendering when states are equal. */
52
62
  areStatesEqual?: StatesComparer<U>;
53
63
  /** A render-prop function receiving the reactive state and optional stableChild. */
@@ -56,7 +66,7 @@ interface ListenerProps<T, U = T> extends BaseListenerProps {
56
66
  /**
57
67
  * Props for the `MultiListener` component.
58
68
  */
59
- type MultiListenerProps<T extends readonly Notifier<unknown>[], U = InferNotifierValues<T>> = MultiListenerWithSelectorProps<T, U> | MultiListenerWithoutSelectorProps<T>;
69
+ type MultiListenerProps<T extends readonly Notifier<unknown>[], U = InferNotifierStates<T>> = MultiListenerWithSelectorProps<T, U> | MultiListenerWithoutSelectorProps<T>;
60
70
  /**
61
71
  * Props for the `MultiListener` component when utilizing a selector transformation.
62
72
  */
@@ -64,7 +74,7 @@ interface MultiListenerWithSelectorProps<T extends readonly Notifier<unknown>[],
64
74
  /** A readonly tuple array of source notifiers to listen to. */
65
75
  notifiers: T;
66
76
  /** Function to transform or combine the tuple values into a custom shape U. */
67
- selector: StateSelector<InferNotifierValues<T>, U>;
77
+ selector: StateSelector<InferNotifierStates<T>, U>;
68
78
  /**
69
79
  * Optional custom comparer. Returns true to skip re-rendering when states are equal.
70
80
  * Defaults to a smart tuple-aware shallow equality checker.
@@ -81,9 +91,9 @@ interface MultiListenerWithoutSelectorProps<T extends readonly Notifier<unknown>
81
91
  notifiers: T;
82
92
  selector?: undefined;
83
93
  /** Optional custom comparer for the state tuple. Defaults to a smart tuple-aware shallow equality checker. */
84
- areStatesEqual?: StatesComparer<InferNotifierValues<T>>;
94
+ areStatesEqual?: StatesComparer<InferNotifierStates<T>>;
85
95
  /** A render-prop function receiving the resolved state tuple and optional stableChild. */
86
- children: ChildrenRenderer<InferNotifierValues<T>>;
96
+ children: ChildrenRenderer<InferNotifierStates<T>>;
87
97
  }
88
98
  /**
89
99
  * Overloaded component interface for `MultiListener` supporting both tuple and selector patterns.
@@ -116,11 +126,11 @@ type SetState<T> = T | StateUpdater<T>;
116
126
  type InitialState<T> = (T extends Function ? never : (T | StateInitializer<T>)) & {};
117
127
  /** A render-prop function signature for rendering reactive components. */
118
128
  type ChildrenRenderer<T> = (state: T, stableChild?: JSX.Element) => ReactNode;
119
- /** Helper type to extract the underlying value type T from a Notifier<T>. */
120
- type InferNotifierValue<N> = N extends Notifier<infer T> ? T : never;
129
+ /** Helper type to extract the underlying state type from T of a `Notifier<T>`. */
130
+ type InferNotifierState<T> = T extends Notifier<infer U> ? UnwrapNotifierState<U> : never;
121
131
  /** Mapped tuple type to transform `[Notifier<A>, Notifier<B>]` into `[A, B]`. */
122
- type InferNotifierValues<T extends readonly Notifier<unknown>[]> = {
123
- [K in keyof T]: InferNotifierValue<T[K]>;
132
+ type InferNotifierStates<T extends readonly Notifier<unknown>[]> = {
133
+ [K in keyof T]: InferNotifierState<T[K]>;
124
134
  };
125
135
 
126
136
  /**
@@ -171,7 +181,7 @@ declare function notifier<T>(initialState: InitialState<T>): Notifier<T>;
171
181
  * });
172
182
  * ```
173
183
  */
174
- declare function listener<T>(notifier: Notifier<T>, callback: ListenerCallback<T>): VoidCallback;
184
+ declare function listener<T>(notifier: Notifier<T>, callback: ListenerCallback<UnwrapNotifierState<T>>): VoidCallback;
175
185
 
176
186
  /**
177
187
  * Subscribes imperatively to multiple `barrex` notifiers simultaneously outside of React.
@@ -198,7 +208,7 @@ declare function listener<T>(notifier: Notifier<T>, callback: ListenerCallback<T
198
208
  * );
199
209
  * ```
200
210
  */
201
- declare function multiListener<const T extends readonly Notifier<unknown>[]>(notifiers: T, callback: ListenerCallback<InferNotifierValues<T>>): VoidCallback;
211
+ declare function multiListener<const T extends readonly Notifier<unknown>[]>(notifiers: T, callback: ListenerCallback<InferNotifierStates<T>>): VoidCallback;
202
212
 
203
213
  /**
204
214
  * Creates a component-scoped, lazy-initialized notifier with automatic
@@ -225,6 +235,7 @@ declare function multiListener<const T extends readonly Notifier<unknown>[]>(not
225
235
  * }
226
236
  * ```
227
237
  */
238
+ declare function useNotifier<T>(initialState: InitialState<Notifier<T>>): Notifier<T>;
228
239
  declare function useNotifier<T>(initialState: InitialState<T>): Notifier<T>;
229
240
 
230
241
  /**
@@ -255,7 +266,7 @@ declare function useNotifier<T>(initialState: InitialState<T>): Notifier<T>;
255
266
  * }
256
267
  * ```
257
268
  */
258
- declare function useListener<T, U = T>(notifier: Notifier<T>, selector?: StateSelector<T, U>, areStatesEqual?: StatesComparer<U>): U;
269
+ declare function useListener<T, U = UnwrapNotifierState<T>>(notifier: Notifier<T>, selector?: StateSelector<UnwrapNotifierState<T>, U>, areStatesEqual?: StatesComparer<U>): U;
259
270
 
260
271
  /**
261
272
  * Subscribes a React component to multiple notifiers simultaneously,
@@ -282,10 +293,10 @@ declare function useListener<T, U = T>(notifier: Notifier<T>, selector?: StateSe
282
293
  * }
283
294
  * ```
284
295
  */
285
- declare function useMultiListener<const T extends readonly Notifier<unknown>[], const U>(notifiers: T, selector: StateSelector<InferNotifierValues<T>, U>, areStatesEqual?: StatesComparer<U>): U;
286
- declare function useMultiListener<const T extends readonly Notifier<unknown>[]>(notifiers: T, selector?: undefined, areStatesEqual?: StatesComparer<InferNotifierValues<T>>): InferNotifierValues<T>;
296
+ declare function useMultiListener<const T extends readonly Notifier<unknown>[], const U>(notifiers: T, selector: StateSelector<InferNotifierStates<T>, U>, areStatesEqual?: StatesComparer<U>): U;
297
+ declare function useMultiListener<const T extends readonly Notifier<unknown>[]>(notifiers: T, selector?: undefined, areStatesEqual?: StatesComparer<InferNotifierStates<T>>): InferNotifierStates<T>;
287
298
 
288
- declare function ListenerComponent<T, U = T>({ notifier, selector, areStatesEqual, stableChild, children }: ListenerProps<T, U>): react.ReactNode;
299
+ declare function ListenerComponent<T, U = UnwrapNotifierState<T>>({ notifier, selector, areStatesEqual, stableChild, children }: ListenerProps<T, U>): react.ReactNode;
289
300
  /**
290
301
  * A declarative component wrapper that brings fine-grained reactivity
291
302
  * and granular performance optimization directly into your JSX tree.
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';var f=Symbol("isNotifier");function C(e){return typeof e=="object"&&e!==null}function u(e){return Array.isArray(e)}function c(e){return typeof e=="function"}function N(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&e[f]===true}var p=class{state;rootNode;constructor(t){this.state=t,this.rootNode=new h(this);}},m=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===f)return true;if(r==="get")return this.get;if(r==="set")return this.dispatchSet;if(r==="subscribe")return this.subscribe;let n=this.get();if(C(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 m{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 m{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 k="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 y(e){let t=c(e)?e():e;if(c(t))throw new Error(k);return N(t)?t:T(t)}function T(e){return new p(e).rootNode.proxy}function w(e,t){let r=e.subscribe(()=>{t(e.get());});return t(e.get()),r}function L(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 I(e){let[t]=useState(()=>y(e));return t}function S(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 A(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 b(e,t,r=A){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=S(e,t,r);return i(o,n)}var F=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 j({notifiers:e,selector:t,areStatesEqual:r,stableChild:n,children:i}){let o=b(e,t,r);return i(o,n)}var v=memo(j,(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 K({children:e}){return jsx(Fragment,{children:e})}var _=memo(K,()=>true);export{_ as IsolatedContainer,F as Listener,v as MultiListener,w as listener,L as multiListener,y as notifier,S as useListener,b as useMultiListener,I as useNotifier};//# sourceMappingURL=index.js.map
1
+ import {memo,useRef,useSyncExternalStore,useCallback,useEffect}from'react';import {jsx,Fragment}from'react/jsx-runtime';var y=Symbol("isNull"),p=Symbol("isNotifier"),b=Symbol("getNode"),h=Symbol("dispose");function x(t){return typeof t=="object"&&t!==null}function f(t){return Array.isArray(t)}function u(t){return typeof t=="function"}function v(t){return t===null||(typeof t!="object"||F(t))&&typeof t!="function"}function F(t){return t instanceof Date}function c(t){return typeof t=="object"&&t!==null&&!Array.isArray(t)&&t[p]===true}function m(t,e){return f(t)&&f(e)&&t.length===e.length?t.every((i,n)=>{let r=e[n];return i===r}):t===e}function S(t){return Object.assign(f(t)?[]:{},t)}var N=class{state;resolved;listeners;children;proxy;linkedFromNodes;constructor(e,i){this.state=e,this.resolved=false,this.proxy=i??this.createProxy();}get=()=>{let e=this.peek();return this.resolved?e:(this.resolveSubtree(e),this.state)};dispatchSet=e=>this.set(e,false);subscribe=e=>(this.listeners||(this.listeners=new Set),this.listeners.add(e),()=>{this.listeners&&(this.listeners.delete(e),this.listeners.size||(this.listeners=void 0));});notifyListeners(){this.listeners?.forEach(e=>e());}notifyLinkedFromNodes(e){this.linkedFromNodes?.forEach(i=>{i.state=e,i.parent.peek()[i.key]=e,i.parent.resolved&&i.parent.set(S);});}addLinkedFromNode(e){this.linkedFromNodes||(this.linkedFromNodes=new Set),this.linkedFromNodes.add(e),this instanceof d&&this.root.addLinkedNode(this);}removeLinkedFromNode(e){this.linkedFromNodes&&(this.linkedFromNodes.delete(e),this.linkedFromNodes.size||(this.linkedFromNodes=void 0,this instanceof d&&!this.isLinkedToNode&&this.root.removeLinkedNode(this)));}bridgePath(e){let i=this,n=this instanceof d?this.root:this;for(let o=0;o<e.length-1;o++){let a=e[o],l=i.peek()[a],L=new d(a,n,i,l);i.children||(i.children=new Map),i.children.set(a,L),i=L;}let r=e[e.length-1];return new d(r,n,i,i.peek()[r])}resolveSubtree(e,i){if(v(e)){i||(this.resolved=true);return}if(c(e))if(i){let n=this.bridgePath(i);if(n.establishLink(),n.parent.peek()[n.key]=n.peek(),n.parent!==this)return n.parent}else this instanceof d&&this.establishLink();else {let n,r=(i?.length??0)===1,s=(o,a)=>{let l=r?this.children?.get(o):void 0;if(l){if(l.resolved)return;l.resolveSubtree(a);}else u(a)||(n?n.resolveSubtree(a,[o]):n=this.resolveSubtree(a,[...i??[],o]));};if(f(e))for(let o=0;o<e.length;o++)s(`${o}`,e[o]);else for(let o in e)s(o,e[o]);if(n&&(n.resolved=true,n.parent!==this))return n.parent;i||(this.resolved=true);}}reconcileChildren(e,i){if(!(!this.children||!f(e)||!f(i)))for(let[n,r]of this.children){let s=n,o=e[s];i[s]!==o&&r.notifySubtree();}}setTrap(){return false}createProxy(){return new Proxy({},{get:(e,i)=>{if(i==="get")return this.get;if(i==="set")return this.dispatchSet;if(i==="subscribe")return this.subscribe;if(i===p)return true;if(i===b)return this;if(i===h)return this.dispose;let n=this.peek();if(x(n)&&Object.hasOwn(n,i)){let r=n[i];if(u(r))return r;this.children||(this.children=new Map);let s=this.children.get(i);if(!s){let o=this instanceof d?this.root:this;s=new d(i,o,this,r),this.children.set(i,s);}return s.proxy}},set:this.setTrap})}},k=class extends N{linkedNodes;constructor(e){super(e);}dispose=()=>{this.linkedFromNodes&&(this.linkedFromNodes.forEach(e=>e.severLink(false)),this.linkedFromNodes=void 0),this.linkedNodes&&(this.linkedNodes.forEach(e=>e.dispose()),this.linkedNodes=void 0);};peek(){return this.state}set(e,i=true){let n=this.get(),r=u(e)?e(n):e;return r!==n?(this.state=r,this.notifyLinkedFromNodes(r),this.notifyListeners(),i||this.reconcileChildren(n,r),true):false}addLinkedNode(e){this.linkedNodes||(this.linkedNodes=new Set),this.linkedNodes.add(e);}removeLinkedNode(e){this.linkedNodes&&(this.linkedNodes.delete(e),this.linkedNodes.size||(this.linkedNodes=void 0));}},d=class extends N{key;root;parent;linkedToNode;constructor(e,i,n,r){super(r,c(r)?r:void 0),this.key=e,this.root=i,this.parent=n,this.resolved=n.resolved;}get isLinkedToNode(){return !!this.linkedToNode}dispose=()=>{this.linkedFromNodes&&(this.linkedFromNodes.forEach(e=>e.severLink(false)),this.linkedFromNodes=void 0),this.severLink();};peek(){return this.state!==y?this.state:this.state=this.parent.peek()[this.key]}set(e,i=true){let n=this.get(),r=u(e)?e(n):e;if(r!==n){let s=this.parent.peek(),o;if(this.parent.resolved){let a=S(s);a[this.key]=r,o=this.parent.set(a);}else s[this.key]=r,o=true;return o?(this.state=r,this.notifyLinkedFromNodes(r),this.notifyListeners(),i||this.reconcileChildren(n,r),true):false}return false}establishLink(){let e=this.proxy[b];e.addLinkedFromNode(this),this.linkedToNode=e,this.root.addLinkedNode(this);let i=e.get();this.parent.peek()[this.key]=i,this.state=i,this.resolved=true;}severLink(e=true){this.linkedToNode&&(e&&this.linkedToNode.removeLinkedFromNode(this),this.linkedToNode=void 0,this.proxy={});}notifySubtree(){if(this.state=y,this.notifyListeners(),this.children)for(let e of this.children.values())e.notifySubtree();}};var U="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 C(t){let e=u(t)?t():t;if(u(e))throw new Error(U);return c(e)?e:I(e)}function I(t){return new k(t).proxy}function M(t,e){let i=t.get(),n=t.subscribe(()=>{let r=t.get();r!==i&&(e(r),i=r);});return e(i),n}function V(t,e){let i=null,n=()=>{let o=t.map(a=>a.get());m(i,o)||(e(o),i=o);},r=t.map(o=>o.subscribe(n)),s=()=>r.forEach(o=>o());return n(),s}function E(t){if(process.env.NODE_ENV==="development"||process.env.NODE_ENV==="test"){let e=useRef(false);useEffect(()=>{if(e.current)return t;e.current=true;},[]);}else useEffect(()=>t,[]);}function j(t){let e=useRef(null);return e.current===null&&(e.current=C(t)),E(e.current[h]),e.current}function w(t,e,i){let n=useRef(null);return useSyncExternalStore(t.subscribe,()=>{let r=e?e(t.get()):t.get();return i&&n.current!==null&&i(n.current,r)?n.current:(n.current=r,r)},()=>e?e(t.get()):t.get())}function T(t,e,i=m){let n=useRef(null),r=useCallback(o=>{let a=t.map(l=>l.subscribe(o));return ()=>a.forEach(l=>l())},t),s=()=>{let o=t.map(l=>l.get()),a=e?e(o):o;return n.current!==null&&i(n.current,a)?n.current:(n.current=a,a)};return useSyncExternalStore(r,s,s)}function W({notifier:t,selector:e,areStatesEqual:i,stableChild:n,children:r}){let s=w(t,e,i);return r(s,n)}var $=memo(W,(t,e)=>e.isolated?true:t.notifier===e.notifier&&t.selector===e.selector&&t.areStatesEqual===e.areStatesEqual&&t.stableChild===e.stableChild&&t.children===e.children);function P({notifiers:t,selector:e,areStatesEqual:i,stableChild:n,children:r}){let s=T(t,e,i);return r(s,n)}var G=memo(P,(t,e)=>e.isolated?true:t.notifiers===e.notifiers&&t.selector===e.selector&&t.areStatesEqual===e.areStatesEqual&&t.stableChild===e.stableChild&&t.children===e.children);function J({children:t}){return jsx(Fragment,{children:t})}var Q=memo(J,()=>true);export{Q as IsolatedContainer,$ as Listener,G as MultiListener,M as listener,V as multiListener,C as notifier,w as useListener,T as useMultiListener,j 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/symbols.ts","../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":["NOTIFIER_SYMBOL","isObjectOrArray","value","isArray","isFunction","isNotifier","NotifierRoot","state","RootNode","Node","callback","listener","child","prevState","newState","key","index","prevChildState","_","prop","childNode","ChildNode","root","fromChild","parent","parentState","newParentState","functionCannotBeNotifierMessage","notifier","initialState","resolvedState","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,IAAMA,CAAAA,CAAkB,MAAA,CAAO,YAAY,CAAA,CCG3C,SAASC,EAAgBC,CAAAA,CAAiC,CAC7D,OAAO,OAAOA,CAAAA,EAAU,QAAA,EAAYA,IAAU,IAClD,CAEO,SAASC,CAAAA,CAAQD,CAAAA,CAAyC,CAC7D,OAAO,KAAA,CAAM,OAAA,CAAQA,CAAK,CAC9B,CAEO,SAASE,CAAAA,CAAWF,EAAmC,CAC1D,OAAO,OAAOA,CAAAA,EAAU,UAC5B,CAEO,SAASG,CAAAA,CAAWH,CAAAA,CAA4C,CACnE,OACI,OAAOA,CAAAA,EAAU,UACjBA,CAAAA,GAAU,IAAA,EACV,CAAC,KAAA,CAAM,OAAA,CAAQA,CAAK,CAAA,EACnBA,CAAAA,CAAcF,CAAe,CAAA,GAAM,IAE5C,CCbO,IAAMM,CAAAA,CAAN,KAAmB,CACf,KAAA,CACA,QAAA,CAEP,WAAA,CAAYC,CAAAA,CAAgB,CACxB,KAAK,KAAA,CAAQA,CAAAA,CACb,IAAA,CAAK,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\"isNotifier\");","import type { Notifier } from \"../types\";\r\nimport { NOTIFIER_SYMBOL } from \"../symbols\";\r\n\r\nexport 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}\r\n\r\nexport function isNotifier(value: unknown): value is Notifier<unknown> {\r\n return (\r\n typeof value === \"object\" &&\r\n value !== null &&\r\n !Array.isArray(value) &&\r\n (value as any)[NOTIFIER_SYMBOL] === true\r\n );\r\n}","import type {\r\n NodeKey,\r\n SetState,\r\n StateUpdater,\r\n VoidCallback\r\n} from \"./types\";\r\nimport { NOTIFIER_SYMBOL } from \"./symbols\";\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 === NOTIFIER_SYMBOL) return true;\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 { NotifierRoot } from \"../notifier\";\r\nimport { isFunction, isNotifier } from \"./helpers\";\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](https://github.com/bhuvanpddahal/barrex/blob/main/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<Notifier<T>>): Notifier<T>;\r\nexport function notifier<T>(initialState: InitialState<T>): Notifier<T>;\r\nexport function notifier<T>(initialState: unknown): Notifier<T> {\r\n const resolvedState = isFunction(initialState)\r\n ? (initialState)()\r\n : initialState;\r\n if (isFunction(resolvedState)) {\r\n throw new Error(functionCannotBeNotifierMessage);\r\n }\r\n if (isNotifier(resolvedState)) {\r\n return resolvedState as Notifier<T>;\r\n }\r\n return createNotifier(resolvedState);\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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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.count);\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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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;"]}
1
+ {"version":3,"sources":["../src/symbols.ts","../src/utils/helpers.ts","../src/nodes.ts","../src/utils/notifier.ts","../src/utils/listener.ts","../src/utils/multi-listener.ts","../src/hooks/useUnmount.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":["nullSymbol","notifierSymbol","getNodeSymbol","disposeSymbol","isObjectOrArray","value","isArray","isFunction","isPrimitive","isDate","isNotifier","areStatesEqual","prevState","nextState","prevValue","index","nextValue","shallowCopy","Node","state","proxy","callback","listener","newState","node","ChildNode","keys","parentNode","rootNode","key","childNode","currentNode","isDirectChild","handleEntry","child","prevChildState","_","prop","RootNode","fromChild","root","parent","parentState","isSet","newParentState","linkedToNode","removeFromLinkedToNode","functionCannotBeNotifierMessage","notifier","initialState","resolvedState","createNotifier","unsubscribe","multiListener","notifiers","unsubscribeFns","fn","useUnmount","didRunStrictCleanupRef","useRef","useEffect","useNotifier","notifierRef","useListener","selector","useSyncExternalStore","useMultiListener","subscribe","useCallback","getSnapshot","ListenerComponent","stableChild","children","Listener","memo","prevProps","nextProps","MultiListenerComponent","MultiListener","IsolatedContainerComponent","jsx","Fragment","IsolatedContainer"],"mappings":"wHAAO,IAAMA,CAAAA,CAAa,OAAO,QAAQ,CAAA,CAC5BC,EAAiB,MAAA,CAAO,YAAY,EACpCC,CAAAA,CAAgB,MAAA,CAAO,SAAS,CAAA,CAChCC,CAAAA,CAAgB,OAAO,SAAS,CAAA,CCAtC,SAASC,CAAAA,CAAgBC,CAAAA,CAAiC,CAC7D,OAAO,OAAOA,CAAAA,EAAU,UAAYA,CAAAA,GAAU,IAClD,CAEO,SAAS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,CAAS,CAAA,CAA2B,CACtE,OAAOQ,GAAAA,CAAAC,SAAA,CAAG,QAAA,CAAAT,EAAS,CACvB,KAwBaU,CAAAA,CAAoBR,IAAAA,CAC7BK,CAAAA,CACA,IAAM,IACV","file":"index.js","sourcesContent":["export const nullSymbol = Symbol(\"isNull\");\r\nexport const notifierSymbol = Symbol(\"isNotifier\");\r\nexport const getNodeSymbol = Symbol(\"getNode\");\r\nexport const disposeSymbol = Symbol(\"dispose\");","import { notifierSymbol } from \"../symbols\";\r\nimport type { Notifier, Primitive } from \"../types\";\r\n\r\nexport 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}\r\n\r\nexport function isPrimitive(value: unknown): value is Primitive {\r\n return (\r\n value === null ||\r\n ((typeof value !== \"object\" || isDate(value)) && typeof value !== \"function\")\r\n );\r\n}\r\n\r\nexport function isDate(value: unknown): value is Date {\r\n return value instanceof Date;\r\n}\r\n\r\nexport function isNotifier(value: unknown): value is Notifier<unknown> {\r\n return (\r\n typeof value === \"object\" &&\r\n value !== null &&\r\n !Array.isArray(value) &&\r\n (value as any)[notifierSymbol] === true\r\n );\r\n}\r\n\r\nexport function areStatesEqual<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 shallowCopy<T extends object>(value: T) {\r\n return Object.assign(isArray(value) ? [] : {}, value);\r\n}","import type {\r\n NodeKey,\r\n SetState,\r\n StateUpdater,\r\n VoidCallback\r\n} from \"./types\";\r\nimport {\r\n isArray,\r\n isFunction,\r\n isNotifier,\r\n isObjectOrArray,\r\n isPrimitive,\r\n shallowCopy\r\n} from \"./utils/helpers\";\r\nimport { disposeSymbol, getNodeSymbol, notifierSymbol, nullSymbol } from \"./symbols\";\r\n\r\nabstract class Node {\r\n protected state: unknown;\r\n public resolved: boolean;\r\n protected listeners?: Set<VoidCallback>;\r\n protected children?: Map<NodeKey, ChildNode>;\r\n public proxy: object;\r\n protected linkedFromNodes?: Set<ChildNode>;\r\n\r\n constructor(state: unknown, proxy?: object) {\r\n this.state = state;\r\n this.resolved = false;\r\n this.proxy = proxy ?? this.createProxy();\r\n }\r\n\r\n public abstract dispose(): void;\r\n public abstract peek(): unknown;\r\n public abstract set(state: SetState<unknown>, fromChild?: boolean): boolean;\r\n\r\n public get = (): unknown => {\r\n const state = this.peek();\r\n if (this.resolved) return state;\r\n this.resolveSubtree(state);\r\n return this.state;\r\n };\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 if (!this.listeners) {\r\n this.listeners = new Set();\r\n }\r\n this.listeners.add(callback);\r\n return () => {\r\n if (!this.listeners) return;\r\n this.listeners.delete(callback);\r\n if (!this.listeners.size) {\r\n this.listeners = undefined;\r\n }\r\n };\r\n };\r\n\r\n protected notifyListeners() {\r\n this.listeners?.forEach((listener) => listener());\r\n }\r\n\r\n protected notifyLinkedFromNodes(newState: unknown) {\r\n this.linkedFromNodes?.forEach((node) => {\r\n node.state = newState;\r\n (node.parent.peek() as any)[node.key] = newState;\r\n if (node.parent.resolved) {\r\n node.parent.set(shallowCopy);\r\n }\r\n });\r\n }\r\n\r\n public addLinkedFromNode(node: ChildNode) {\r\n if (!this.linkedFromNodes) {\r\n this.linkedFromNodes = new Set();\r\n }\r\n this.linkedFromNodes.add(node);\r\n if (this instanceof ChildNode) {\r\n this.root.addLinkedNode(this);\r\n }\r\n }\r\n\r\n public removeLinkedFromNode(node: ChildNode) {\r\n if (this.linkedFromNodes) {\r\n this.linkedFromNodes.delete(node);\r\n if (!this.linkedFromNodes.size) {\r\n this.linkedFromNodes = undefined;\r\n if (this instanceof ChildNode && !this.isLinkedToNode) {\r\n this.root.removeLinkedNode(this);\r\n }\r\n }\r\n }\r\n }\r\n\r\n private bridgePath(keys: [NodeKey, ...NodeKey[]]) {\r\n let parentNode = this as Node;\r\n const rootNode = (this instanceof ChildNode ? this.root : this) as RootNode;\r\n for (let index = 0; index < keys.length - 1; index++) {\r\n const key = keys[index];\r\n const state = (parentNode.peek() as any)[key];\r\n const childNode = new ChildNode(key, rootNode, parentNode, state);\r\n if (!parentNode.children) {\r\n parentNode.children = new Map();\r\n }\r\n parentNode.children.set(key, childNode);\r\n parentNode = childNode;\r\n }\r\n const key = keys[keys.length - 1];\r\n const currentNode = new ChildNode(\r\n key, rootNode, parentNode, (parentNode.peek() as any)[key]\r\n );\r\n return currentNode;\r\n }\r\n\r\n private resolveSubtree(state: unknown, keys?: [NodeKey, ...NodeKey[]]) {\r\n if (isPrimitive(state)) {\r\n if (!keys) this.resolved = true;\r\n return;\r\n }\r\n\r\n if (isNotifier(state)) {\r\n if (keys) {\r\n const currentNode = this.bridgePath(keys);\r\n currentNode.establishLink();\r\n (currentNode.parent.peek() as any)[currentNode.key] = currentNode.peek();\r\n if (currentNode.parent !== this) {\r\n return currentNode.parent as ChildNode;\r\n }\r\n } else if (this instanceof ChildNode) {\r\n this.establishLink();\r\n }\r\n } else {\r\n let currentNode: ChildNode | undefined;\r\n const isDirectChild = (keys?.length ?? 0) === 1;\r\n\r\n const handleEntry = (key: NodeKey, value: unknown) => {\r\n const childNode = isDirectChild ? this.children?.get(key) : undefined;\r\n if (childNode) {\r\n if (childNode.resolved) return;\r\n childNode.resolveSubtree(value);\r\n } else if (!isFunction(value)) {\r\n if (currentNode) {\r\n currentNode.resolveSubtree(value, [key]);\r\n } else {\r\n currentNode = this.resolveSubtree(value, [...(keys ?? []), key]);\r\n }\r\n }\r\n };\r\n\r\n if (isArray(state)) {\r\n for (let index = 0; index < state.length; index++) {\r\n handleEntry(`${index}`, state[index]);\r\n }\r\n } else {\r\n for (const key in state) {\r\n handleEntry(key, (state as any)[key]);\r\n }\r\n }\r\n if (currentNode) {\r\n currentNode.resolved = true;\r\n if (currentNode.parent !== this) {\r\n return currentNode.parent as ChildNode;\r\n }\r\n }\r\n if (!keys) this.resolved = true;\r\n }\r\n }\r\n\r\n protected reconcileChildren(prevState: unknown, newState: unknown) {\r\n if (!this.children || !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 if (prop === notifierSymbol) return true;\r\n if (prop === getNodeSymbol) return this;\r\n if (prop === disposeSymbol) return this.dispose;\r\n\r\n const state = this.peek();\r\n if (isObjectOrArray(state) && Object.hasOwn(state, prop)) {\r\n const value = (state as any)[prop];\r\n if (isFunction(value)) return value;\r\n\r\n if (!this.children) {\r\n this.children = new Map();\r\n }\r\n let childNode = this.children.get(prop);\r\n if (!childNode) {\r\n const rootNode = (this instanceof ChildNode ? this.root : this) as RootNode;\r\n childNode = new ChildNode(prop, rootNode, this, value);\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\nexport class RootNode extends Node {\r\n private linkedNodes?: Set<ChildNode>;\r\n\r\n constructor(state: unknown) {\r\n super(state);\r\n }\r\n\r\n public dispose = () => {\r\n if (this.linkedFromNodes) {\r\n this.linkedFromNodes.forEach((node) => node.severLink(false));\r\n this.linkedFromNodes = undefined;\r\n }\r\n if (this.linkedNodes) {\r\n this.linkedNodes.forEach((node) => node.dispose());\r\n this.linkedNodes = undefined;\r\n }\r\n };\r\n\r\n public peek() { return this.state; }\r\n\r\n public set(state: SetState<unknown>, fromChild = true) {\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.state = newState;\r\n this.notifyLinkedFromNodes(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 public addLinkedNode(node: ChildNode) {\r\n if (!this.linkedNodes) {\r\n this.linkedNodes = new Set();\r\n }\r\n this.linkedNodes.add(node);\r\n }\r\n\r\n public removeLinkedNode(node: ChildNode) {\r\n if (this.linkedNodes) {\r\n this.linkedNodes.delete(node);\r\n if (!this.linkedNodes.size) {\r\n this.linkedNodes = undefined;\r\n }\r\n }\r\n }\r\n}\r\n\r\nclass ChildNode extends Node {\r\n public key: NodeKey;\r\n public root: RootNode;\r\n public parent: Node;\r\n private linkedToNode?: Node;\r\n\r\n constructor(key: NodeKey, root: RootNode, parent: Node, state: unknown) {\r\n super(state, isNotifier(state) ? state : undefined);\r\n this.key = key;\r\n this.root = root;\r\n this.parent = parent;\r\n this.resolved = parent.resolved;\r\n }\r\n\r\n public get isLinkedToNode() { return !!this.linkedToNode; }\r\n\r\n public dispose = () => {\r\n if (this.linkedFromNodes) {\r\n this.linkedFromNodes.forEach((node) => node.severLink(false));\r\n this.linkedFromNodes = undefined;\r\n }\r\n this.severLink();\r\n }\r\n\r\n public peek(): unknown {\r\n return this.state !== nullSymbol\r\n ? this.state\r\n : (this.state = (this.parent.peek() as any)[this.key]);\r\n }\r\n\r\n public set(state: SetState<unknown>, fromChild = true) {\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 const parentState = this.parent.peek();\r\n let isSet: boolean;\r\n if (this.parent.resolved) {\r\n const newParentState = shallowCopy(parentState as object);\r\n (newParentState as any)[this.key] = newState;\r\n isSet = this.parent.set(newParentState);\r\n } else {\r\n (parentState as any)[this.key] = newState;\r\n isSet = true;\r\n }\r\n if (isSet) {\r\n this.state = newState;\r\n this.notifyLinkedFromNodes(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\r\n public establishLink() {\r\n const linkedToNode: Node = (this.proxy as any)[getNodeSymbol];\r\n linkedToNode.addLinkedFromNode(this);\r\n this.linkedToNode = linkedToNode;\r\n this.root.addLinkedNode(this);\r\n const state = linkedToNode.get();\r\n (this.parent.peek() as any)[this.key] = state;\r\n this.state = state;\r\n this.resolved = true;\r\n }\r\n\r\n public severLink(removeFromLinkedToNode = true) {\r\n if (this.linkedToNode) {\r\n if (removeFromLinkedToNode) {\r\n this.linkedToNode.removeLinkedFromNode(this);\r\n }\r\n this.linkedToNode = undefined;\r\n this.proxy = {};\r\n }\r\n }\r\n\r\n public notifySubtree() {\r\n this.state = nullSymbol;\r\n this.notifyListeners();\r\n if (this.children) {\r\n for (const child of this.children.values()) {\r\n child.notifySubtree();\r\n }\r\n }\r\n }\r\n}","import { RootNode } from \"../nodes\";\r\nimport { isFunction, isNotifier } from \"./helpers\";\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](https://github.com/bhuvanpddahal/barrex/blob/main/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<Notifier<T>>): Notifier<T>;\r\nexport function notifier<T>(initialState: InitialState<T>): Notifier<T>;\r\nexport function notifier<T>(initialState: unknown): Notifier<T> {\r\n const resolvedState = isFunction(initialState)\r\n ? (initialState)()\r\n : initialState;\r\n if (isFunction(resolvedState)) {\r\n throw new Error(functionCannotBeNotifierMessage);\r\n }\r\n if (isNotifier(resolvedState)) {\r\n return resolvedState as Notifier<T>;\r\n }\r\n return createNotifier(resolvedState);\r\n}\r\n\r\nfunction createNotifier<T>(state: T): Notifier<T> {\r\n const rootNode = new RootNode(state);\r\n return rootNode.proxy as Notifier<T>;\r\n}","import type {\r\n ListenerCallback,\r\n Notifier,\r\n UnwrapNotifierState,\r\n VoidCallback\r\n} 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](https://github.com/bhuvanpddahal/barrex/blob/main/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<UnwrapNotifierState<T>>\r\n): VoidCallback {\r\n let prevState = notifier.get();\r\n const unsubscribe = notifier.subscribe(() => {\r\n const nextState = notifier.get();\r\n if (nextState !== prevState) {\r\n callback(nextState);\r\n prevState = nextState;\r\n }\r\n });\r\n callback(prevState);\r\n return unsubscribe;\r\n}","import type {\r\n InferNotifierStates,\r\n ListenerCallback,\r\n Notifier,\r\n VoidCallback\r\n} from \"../types\";\r\nimport { areStatesEqual } from \"./helpers\";\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](https://github.com/bhuvanpddahal/barrex/blob/main/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<InferNotifierStates<T>>\r\n): VoidCallback {\r\n let prevState: any = null;\r\n const listener = () => {\r\n const nextState = notifiers.map((notifier) => notifier.get()) as any;\r\n if (!areStatesEqual(prevState, nextState)) {\r\n callback(nextState);\r\n prevState = nextState;\r\n }\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 { useEffect, useRef } from \"react\";\r\n\r\nimport type { VoidCallback } from \"../types\";\r\n\r\nexport function useUnmount(callback: VoidCallback) {\r\n if (process.env.NODE_ENV === \"development\" || process.env.NODE_ENV === \"test\") {\r\n const didRunStrictCleanupRef = useRef(false);\r\n useEffect(() => {\r\n if (didRunStrictCleanupRef.current) {\r\n return callback;\r\n } else {\r\n didRunStrictCleanupRef.current = true;\r\n }\r\n }, []);\r\n } else {\r\n useEffect(() => callback, []);\r\n }\r\n}","import { useRef } from \"react\";\r\n\r\nimport { useUnmount } from \"./useUnmount\";\r\nimport { disposeSymbol } from \"../symbols\";\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](https://github.com/bhuvanpddahal/barrex/blob/main/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<Notifier<T>>): Notifier<T>;\r\nexport function useNotifier<T>(initialState: InitialState<T>): Notifier<T>;\r\nexport function useNotifier<T>(initialState: any): Notifier<T> {\r\n const notifierRef = useRef<Notifier<T>>(null);\r\n if (notifierRef.current === null) {\r\n notifierRef.current = createNotifier(initialState);\r\n }\r\n useUnmount((notifierRef.current as any)[disposeSymbol]);\r\n return notifierRef.current;\r\n}","import { useRef, useSyncExternalStore } from \"react\";\r\n\r\nimport type {\r\n Notifier,\r\n StatesComparer,\r\n StateSelector,\r\n UnwrapNotifierState\r\n} 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](https://github.com/bhuvanpddahal/barrex/blob/main/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.count);\r\n * return <span>Count: {count}</span>;\r\n * }\r\n * ```\r\n */\r\nexport function useListener<T, U = UnwrapNotifierState<T>>(\r\n notifier: Notifier<T>,\r\n selector?: StateSelector<UnwrapNotifierState<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 InferNotifierStates,\r\n Notifier,\r\n StatesComparer,\r\n StateSelector,\r\n VoidCallback\r\n} from \"../types\";\r\nimport { areStatesEqual as areStatesEqualFn } from \"../utils/helpers\";\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](https://github.com/bhuvanpddahal/barrex/blob/main/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<InferNotifierStates<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<InferNotifierStates<T>>\r\n): InferNotifierStates<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 { useListener } from \"../hooks/useListener\";\r\nimport type { ListenerProps, UnwrapNotifierState } from \"../types\";\r\n\r\nfunction ListenerComponent<T, U = UnwrapNotifierState<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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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](https://github.com/bhuvanpddahal/barrex/blob/main/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,6 +1,6 @@
1
1
  {
2
2
  "name": "barrex",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
4
4
  "description": "The bare essentials for state management in React.",
5
5
  "homepage": "https://github.com/bhuvanpddahal/barrex#readme",
6
6
  "repository": {
@@ -35,6 +35,7 @@
35
35
  "react": "^18.0.0 || ^19.0.0"
36
36
  },
37
37
  "devDependencies": {
38
+ "@types/node": "^26.6.10",
38
39
  "@types/react": "^18.0.0 || ^19.0.0",
39
40
  "tsup": "^9.0.0",
40
41
  "typescript": "^5.9.3"