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