@tuwaio/pulsar-react 1.0.0-fix-orbit-integration-alpha.6.76dee15 → 1.0.0-fix-orbit-integration-alpha.3.86c8fdb

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,15 +1,37 @@
1
- import * as react from 'react';
2
- import { PropsWithChildren } from 'react';
3
- import { Transaction, ITxTrackingStore, PulsarAdapter } from '@tuwaio/pulsar-core';
1
+ import { Transaction, ITxTrackingStore } from '@tuwaio/pulsar-core';
2
+ import React, { PropsWithChildren } from 'react';
4
3
  import { StoreApi } from 'zustand';
5
4
  import * as react_jsx_runtime from 'react/jsx-runtime';
5
+ import { PulsarAdapter, ITxTrackingStore as ITxTrackingStore$1 } from '@tuwaio/pulsar-core/src';
6
6
  import { PersistOptions } from 'zustand/middleware';
7
7
 
8
- type PulsarStoreState<TTransaction extends Transaction> = ITxTrackingStore<TTransaction>;
9
- type PulsarSelector<TTransaction extends Transaction, TSelected> = (state: PulsarStoreState<TTransaction>) => TSelected;
10
- declare const PulsarStoreContext: react.Context<StoreApi<PulsarStoreState<Transaction>> | null>;
11
- declare function usePulsarStore<TTransaction extends Transaction, TRet>(selector: PulsarSelector<TTransaction, TRet>): TRet;
12
- declare function usePulsarStore<TSelected>(selector: PulsarSelector<Transaction, TSelected>): TSelected;
8
+ /**
9
+ * Type alias for the core store state (ITxTrackingStore<TTransaction>).
10
+ */
11
+ type PulsarStore<TTransaction extends Transaction> = ITxTrackingStore<TTransaction>;
12
+ interface BoundedUseStore<T extends Transaction> {
13
+ <TRet>(selector: (state: PulsarStore<T>) => TRet): TRet;
14
+ <TTransaction extends T, TRet>(selector: (state: PulsarStore<TTransaction>) => TRet): TRet;
15
+ }
16
+ declare const PulsarStoreBindings: {
17
+ usePulsarStore: BoundedUseStore<any>;
18
+ };
19
+ type StoreContext<T extends Transaction> = React.Context<StoreApi<PulsarStore<T>> | null>;
20
+ /**
21
+ * Factory function to create a unique React Context and a strictly typed Bounded Hook for a specific T.
22
+ */
23
+ declare function createPulsarStoreContextAndHook<T extends Transaction>(): {
24
+ StoreContext: StoreContext<T>;
25
+ usePulsarStore: BoundedUseStore<T>;
26
+ };
27
+ /**
28
+ * Public hook to access the Pulsar Store.
29
+ * @template T The specific Transaction type (inferred from usage or specified).
30
+ * @template TSelected The type of the value returned by the selector (inferred).
31
+ * * NOTE: The T = Transaction default helps with base type hints, but the actual hook
32
+ * resolves T via the Provider-bound instance.
33
+ */
34
+ declare function usePulsarStore<T extends Transaction = Transaction, TSelected = unknown>(selector: (state: PulsarStore<T>) => TSelected): TSelected;
13
35
 
14
36
  /**
15
37
  * @file This file defines a React hook for initializing the transaction pool.
@@ -48,8 +70,8 @@ declare const useInitializeTransactionsPool: ({ initializeTransactionsPool, onEr
48
70
  onError?: (error: Error) => void;
49
71
  }) => void;
50
72
 
51
- interface PulsarCoreProviderProps<T extends Transaction> extends PropsWithChildren, PulsarAdapter<T>, Partial<PersistOptions<ITxTrackingStore<T>>> {
73
+ interface PulsarCoreProviderProps<T extends Transaction> extends PropsWithChildren, PulsarAdapter<T>, Partial<PersistOptions<ITxTrackingStore$1<T>>> {
52
74
  }
53
75
  declare function PulsarCoreProvider<T extends Transaction>({ children, ...params }: PulsarCoreProviderProps<T>): react_jsx_runtime.JSX.Element;
54
76
 
55
- export { PulsarCoreProvider, PulsarStoreContext, type PulsarStoreState, useInitializeTransactionsPool, usePulsarStore };
77
+ export { type BoundedUseStore, PulsarCoreProvider, type PulsarStore, PulsarStoreBindings, type StoreContext, createPulsarStoreContextAndHook, useInitializeTransactionsPool, usePulsarStore };
package/dist/index.d.ts CHANGED
@@ -1,15 +1,37 @@
1
- import * as react from 'react';
2
- import { PropsWithChildren } from 'react';
3
- import { Transaction, ITxTrackingStore, PulsarAdapter } from '@tuwaio/pulsar-core';
1
+ import { Transaction, ITxTrackingStore } from '@tuwaio/pulsar-core';
2
+ import React, { PropsWithChildren } from 'react';
4
3
  import { StoreApi } from 'zustand';
5
4
  import * as react_jsx_runtime from 'react/jsx-runtime';
5
+ import { PulsarAdapter, ITxTrackingStore as ITxTrackingStore$1 } from '@tuwaio/pulsar-core/src';
6
6
  import { PersistOptions } from 'zustand/middleware';
7
7
 
8
- type PulsarStoreState<TTransaction extends Transaction> = ITxTrackingStore<TTransaction>;
9
- type PulsarSelector<TTransaction extends Transaction, TSelected> = (state: PulsarStoreState<TTransaction>) => TSelected;
10
- declare const PulsarStoreContext: react.Context<StoreApi<PulsarStoreState<Transaction>> | null>;
11
- declare function usePulsarStore<TTransaction extends Transaction, TRet>(selector: PulsarSelector<TTransaction, TRet>): TRet;
12
- declare function usePulsarStore<TSelected>(selector: PulsarSelector<Transaction, TSelected>): TSelected;
8
+ /**
9
+ * Type alias for the core store state (ITxTrackingStore<TTransaction>).
10
+ */
11
+ type PulsarStore<TTransaction extends Transaction> = ITxTrackingStore<TTransaction>;
12
+ interface BoundedUseStore<T extends Transaction> {
13
+ <TRet>(selector: (state: PulsarStore<T>) => TRet): TRet;
14
+ <TTransaction extends T, TRet>(selector: (state: PulsarStore<TTransaction>) => TRet): TRet;
15
+ }
16
+ declare const PulsarStoreBindings: {
17
+ usePulsarStore: BoundedUseStore<any>;
18
+ };
19
+ type StoreContext<T extends Transaction> = React.Context<StoreApi<PulsarStore<T>> | null>;
20
+ /**
21
+ * Factory function to create a unique React Context and a strictly typed Bounded Hook for a specific T.
22
+ */
23
+ declare function createPulsarStoreContextAndHook<T extends Transaction>(): {
24
+ StoreContext: StoreContext<T>;
25
+ usePulsarStore: BoundedUseStore<T>;
26
+ };
27
+ /**
28
+ * Public hook to access the Pulsar Store.
29
+ * @template T The specific Transaction type (inferred from usage or specified).
30
+ * @template TSelected The type of the value returned by the selector (inferred).
31
+ * * NOTE: The T = Transaction default helps with base type hints, but the actual hook
32
+ * resolves T via the Provider-bound instance.
33
+ */
34
+ declare function usePulsarStore<T extends Transaction = Transaction, TSelected = unknown>(selector: (state: PulsarStore<T>) => TSelected): TSelected;
13
35
 
14
36
  /**
15
37
  * @file This file defines a React hook for initializing the transaction pool.
@@ -48,8 +70,8 @@ declare const useInitializeTransactionsPool: ({ initializeTransactionsPool, onEr
48
70
  onError?: (error: Error) => void;
49
71
  }) => void;
50
72
 
51
- interface PulsarCoreProviderProps<T extends Transaction> extends PropsWithChildren, PulsarAdapter<T>, Partial<PersistOptions<ITxTrackingStore<T>>> {
73
+ interface PulsarCoreProviderProps<T extends Transaction> extends PropsWithChildren, PulsarAdapter<T>, Partial<PersistOptions<ITxTrackingStore$1<T>>> {
52
74
  }
53
75
  declare function PulsarCoreProvider<T extends Transaction>({ children, ...params }: PulsarCoreProviderProps<T>): react_jsx_runtime.JSX.Element;
54
76
 
55
- export { PulsarCoreProvider, PulsarStoreContext, type PulsarStoreState, useInitializeTransactionsPool, usePulsarStore };
77
+ export { type BoundedUseStore, PulsarCoreProvider, type PulsarStore, PulsarStoreBindings, type StoreContext, createPulsarStoreContextAndHook, useInitializeTransactionsPool, usePulsarStore };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- 'use strict';var react=require('react'),zustand=require('zustand'),pulsarCore=require('@tuwaio/pulsar-core'),jsxRuntime=require('react/jsx-runtime');var a=react.createContext(null);function f(e){let r=react.useContext(a);if(!r)throw new Error("usePulsarStore must be used within a PulsarProvider");return zustand.useStore(r,e)}var s=({initializeTransactionsPool:e,onError:r})=>{react.useEffect(()=>{(async()=>{try{await e();}catch(o){(r??(i=>console.error("Failed to initialize transactions pool:",i)))(o);}})();},[e,r]);};function F({children:e,...r}){let t=react.useRef(null);t.current||(t.current=pulsarCore.createPulsarStore({...r,name:r.name??"pulsar-store"}));let o=t.current;return s({initializeTransactionsPool:t.current.getState().initializeTransactionsPool,onError:n=>console.error("Failed to initialize transactions pool:",n)}),jsxRuntime.jsx(a.Provider,{value:o,children:e})}exports.PulsarCoreProvider=F;exports.PulsarStoreContext=a;exports.useInitializeTransactionsPool=s;exports.usePulsarStore=f;//# sourceMappingURL=index.js.map
1
+ 'use strict';var react=require('react'),zustand=require('zustand'),pulsarCore=require('@tuwaio/pulsar-core'),S=require('lodash.once'),jsxRuntime=require('react/jsx-runtime');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var S__default=/*#__PURE__*/_interopDefault(S);var a={usePulsarStore:(r=>{throw new Error("usePulsarStore must be called within PulsarCoreProvider.")})};function i(){let r=react.createContext(null);return {StoreContext:r,usePulsarStore:(t=>{let e=react.useContext(r);if(!e)throw new Error("usePulsarStore must be used within a PulsarCoreProvider");return zustand.useStore(e,t)})}}function k(r){return a.usePulsarStore(r)}var u=({initializeTransactionsPool:r,onError:o})=>{react.useEffect(()=>{(async()=>{try{await r();}catch(e){(o??(s=>console.error("Failed to initialize transactions pool:",s)))(e);}})();},[r,o]);};var p=S__default.default(r=>{let{StoreContext:o,usePulsarStore:t}=i(),e=pulsarCore.createPulsarStore({...r,name:r.name??"tuwa:pulsar-store"});return {StoreContext:o,usePulsarStore:t,store:e}});function E({children:r,...o}){let{StoreContext:t,usePulsarStore:e,store:n}=p(o);return u({initializeTransactionsPool:n.getState().initializeTransactionsPool,onError:s=>console.error("Failed to initialize transactions pool:",s)}),a.usePulsarStore=e,jsxRuntime.jsx(t.Provider,{value:n,children:r})}exports.PulsarCoreProvider=E;exports.PulsarStoreBindings=a;exports.createPulsarStoreContextAndHook=i;exports.useInitializeTransactionsPool=u;exports.usePulsarStore=k;//# sourceMappingURL=index.js.map
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hooks/pulsarHook.ts","../src/hooks/useInitializeTransactionsPool.tsx","../src/providers/PulsarCoreProvider.tsx"],"names":["PulsarStoreContext","createContext","usePulsarStore","selector","store","useContext","useStore","useInitializeTransactionsPool","initializeTransactionsPool","onError","useEffect","error","e","PulsarCoreProvider","children","params","storeRef","useRef","createPulsarStore","storeToProvide","jsx"],"mappings":"yJAOaA,CAAAA,CAAqBC,mBAAAA,CAA8D,IAAI,EAQ7F,SAASC,CAAAA,CACdC,CAAAA,CACW,CACX,IAAMC,EAAQC,gBAAAA,CAAWL,CAAkB,CAAA,CAE3C,GAAI,CAACI,CAAAA,CACH,MAAM,IAAI,KAAA,CAAM,qDAAqD,CAAA,CAGvE,OAAOE,gBAAAA,CAASF,CAAAA,CAA8DD,CAAQ,CACxF,CCUO,IAAMI,CAAAA,CAAgC,CAAC,CAC5C,0BAAA,CAAAC,CAAAA,CACA,OAAA,CAAAC,CACF,CAAA,GAGM,CACJC,eAAAA,CAAU,IAAM,EACe,SAAY,CACvC,GAAI,CACF,MAAMF,CAAAA,GACR,CAAA,MAASG,CAAAA,CAAO,EACOF,CAAAA,GAAaG,CAAAA,EAAa,OAAA,CAAQ,KAAA,CAAM,0CAA2CA,CAAC,CAAA,CAAA,EAC5FD,CAAc,EAC7B,CACF,CAAA,IAOF,CAAA,CAAG,CAACH,CAAAA,CAA4BC,CAAO,CAAC,EAC1C,EC7CO,SAASI,CAAAA,CAA0C,CAAE,SAAAC,CAAAA,CAAU,GAAGC,CAAO,CAAA,CAA+B,CAC7G,IAAMC,CAAAA,CAAWC,YAAAA,CAA6C,IAAI,EAE7DD,CAAAA,CAAS,OAAA,GACZA,CAAAA,CAAS,OAAA,CAAUE,6BAAqB,CACtC,GAAGH,CAAAA,CACH,IAAA,CAAMA,CAAAA,CAAO,IAAA,EAAQ,cACvB,CAAC,GAGH,IAAMI,CAAAA,CAAiBH,CAAAA,CAAS,OAAA,CAEhC,OAAAT,CAAAA,CAA8B,CAC5B,0BAAA,CAA4BS,CAAAA,CAAS,QAAQ,QAAA,EAAS,CAAE,0BAAA,CACxD,OAAA,CAAUL,GAAU,OAAA,CAAQ,KAAA,CAAM,yCAAA,CAA2CA,CAAK,CACpF,CAAC,CAAA,CAEMS,cAAAA,CAACpB,CAAAA,CAAmB,SAAnB,CAA4B,KAAA,CAAOmB,CAAAA,CAAiB,QAAA,CAAAL,EAAS,CACvE","file":"index.js","sourcesContent":["import { ITxTrackingStore, Transaction } from '@tuwaio/pulsar-core';\nimport { createContext, useContext } from 'react';\nimport { StoreApi, useStore } from 'zustand';\n\nexport type PulsarStoreState<TTransaction extends Transaction> = ITxTrackingStore<TTransaction>;\ntype PulsarSelector<TTransaction extends Transaction, TSelected> = (state: PulsarStoreState<TTransaction>) => TSelected;\n\nexport const PulsarStoreContext = createContext<StoreApi<PulsarStoreState<Transaction>> | null>(null);\n\nexport function usePulsarStore<TTransaction extends Transaction, TRet>(\n selector: PulsarSelector<TTransaction, TRet>,\n): TRet;\n\nexport function usePulsarStore<TSelected>(selector: PulsarSelector<Transaction, TSelected>): TSelected;\n\nexport function usePulsarStore<TSelected, TTransaction extends Transaction = Transaction>(\n selector: PulsarSelector<TTransaction, TSelected>,\n): TSelected {\n const store = useContext(PulsarStoreContext);\n\n if (!store) {\n throw new Error('usePulsarStore must be used within a PulsarProvider');\n }\n\n return useStore(store as unknown as StoreApi<PulsarStoreState<TTransaction>>, selector);\n}\n","/**\n * @file This file defines a React hook for initializing the transaction pool.\n * This hook is crucial for resuming the tracking of pending transactions after a page reload.\n */\n\nimport { useEffect } from 'react';\n\n/**\n * A React hook that triggers the initialization of the transaction pool when the component mounts.\n *\n * This should be used once in your application's layout or root component. It ensures that any\n * pending transactions from a previous session (stored in `localStorage`) are picked up and\n\n * their trackers are re-activated.\n *\n * @param {object} params - The parameters for the hook.\n * @param {() => Promise<void>} params.initializeTransactionsPool - The `initializeTransactionsPool` function from your Pulsar store instance.\n * @param {(error: Error) => void} [params.onError] - An optional custom function to handle any errors that occur during initialization. Defaults to `console.error`.\n *\n * @example\n * ```tsx\n * import { useInitializeTransactionsPool } from '@tuwaio/pulsar-react';\n * import { pulsarStore } from './path/to/your/store';\n *\n * function AppLayout({ children }) {\n * // This hook will run once when the layout mounts.\n * useInitializeTransactionsPool({\n * initializeTransactionsPool: pulsarStore.getState().initializeTransactionsPool,\n * onError: (err) => console.warn('Failed to re-initialize trackers:', err),\n * });\n *\n * return <div>{children}</div>;\n * }\n * ```\n */\nexport const useInitializeTransactionsPool = ({\n initializeTransactionsPool,\n onError,\n}: {\n initializeTransactionsPool: () => Promise<void>;\n onError?: (error: Error) => void;\n}) => {\n useEffect(() => {\n const reinitializeTrackers = async () => {\n try {\n await initializeTransactionsPool();\n } catch (error) {\n const errorHandler = onError ?? ((e: Error) => console.error('Failed to initialize transactions pool:', e));\n errorHandler(error as Error);\n }\n };\n\n // Run the initialization process.\n reinitializeTrackers();\n\n // The dependency array is empty to ensure this effect runs only once on mount.\n // The functions from a vanilla Zustand store are stable and do not need to be in the array.\n }, [initializeTransactionsPool, onError]);\n};\n","import { createPulsarStore, ITxTrackingStore, PulsarAdapter, Transaction } from '@tuwaio/pulsar-core';\nimport { PropsWithChildren, useRef } from 'react';\nimport { StoreApi } from 'zustand';\nimport { PersistOptions } from 'zustand/middleware';\n\nimport { PulsarStoreContext, PulsarStoreState } from '../hooks/pulsarHook';\nimport { useInitializeTransactionsPool } from '../hooks/useInitializeTransactionsPool';\n\ninterface PulsarCoreProviderProps<T extends Transaction>\n extends PropsWithChildren,\n PulsarAdapter<T>,\n Partial<PersistOptions<ITxTrackingStore<T>>> {}\n\nexport function PulsarCoreProvider<T extends Transaction>({ children, ...params }: PulsarCoreProviderProps<T>) {\n const storeRef = useRef<StoreApi<PulsarStoreState<T>> | null>(null);\n\n if (!storeRef.current) {\n storeRef.current = createPulsarStore<T>({\n ...params,\n name: params.name ?? 'pulsar-store',\n });\n }\n\n const storeToProvide = storeRef.current as unknown as StoreApi<PulsarStoreState<Transaction>>;\n\n useInitializeTransactionsPool({\n initializeTransactionsPool: storeRef.current.getState().initializeTransactionsPool,\n onError: (error) => console.error('Failed to initialize transactions pool:', error),\n });\n\n return <PulsarStoreContext.Provider value={storeToProvide}>{children}</PulsarStoreContext.Provider>;\n}\n"]}
1
+ {"version":3,"sources":["../src/hooks/pulsarHook.ts","../src/hooks/useInitializeTransactionsPool.tsx","../src/providers/PulsarCoreProvider.tsx"],"names":["PulsarStoreBindings","selector","createPulsarStoreContextAndHook","StoreContext","createContext","store","useContext","useStore","usePulsarStore","useInitializeTransactionsPool","initializeTransactionsPool","onError","useEffect","error","e","getBoundedStoreInstance","once","params","useBoundedPulsarStore","createPulsarStore","PulsarCoreProvider","children","jsx"],"mappings":"8RA2BO,IAAMA,CAAAA,CAAsB,CAGjC,cAAA,EAAkBC,CAAAA,EAAuB,CACvC,MAAM,IAAI,MAAM,0DAA0D,CAC5E,CAAA,CACF,EAUO,SAASC,CAAAA,EAAyD,CAEvE,IAAMC,CAAAA,CAAgCC,oBAA+C,IAAI,CAAA,CAezF,OAAO,CAAE,YAAA,CAAAD,CAAAA,CAAc,cAAA,EAX6BF,CAAAA,EAAuB,CACzE,IAAMI,CAAAA,CAAQC,gBAAAA,CAAWH,CAAY,CAAA,CAErC,GAAI,CAACE,EACH,MAAM,IAAI,KAAA,CAAM,yDAAyD,CAAA,CAI3E,OAAOE,iBAASF,CAAAA,CAAOJ,CAAQ,CACjC,CAAA,CAE6D,CAC/D,CAWO,SAASO,CAAAA,CACdP,CAAAA,CACW,CAGX,OAAOD,CAAAA,CAAoB,eAA6BC,CAAQ,CAClE,CC3CO,IAAMQ,CAAAA,CAAgC,CAAC,CAC5C,0BAAA,CAAAC,EACA,OAAA,CAAAC,CACF,IAGM,CACJC,eAAAA,CAAU,IAAM,CAAA,CACe,SAAY,CACvC,GAAI,CACF,MAAMF,IACR,CAAA,MAASG,CAAAA,CAAO,CAAA,CACOF,CAAAA,GAAaG,CAAAA,EAAa,QAAQ,KAAA,CAAM,yCAAA,CAA2CA,CAAC,CAAA,CAAA,EAC5FD,CAAc,EAC7B,CACF,CAAA,IAOF,EAAG,CAACH,CAAAA,CAA4BC,CAAO,CAAC,EAC1C,EC3CA,IAAMI,CAAAA,CAA0BC,kBAAAA,CAA6BC,CAAAA,EAAuC,CAClG,GAAM,CAAE,YAAA,CAAAd,CAAAA,CAAc,eAAgBe,CAAsB,CAAA,CAAIhB,GAAmC,CAE7FG,CAAAA,CAAQc,4BAAAA,CAAqB,CACjC,GAAGF,CAAAA,CACH,KAAMA,CAAAA,CAAO,IAAA,EAAQ,mBACvB,CAAC,CAAA,CAED,OAAO,CAAE,YAAA,CAAAd,CAAAA,CAAc,cAAA,CAAgBe,CAAAA,CAAuB,KAAA,CAAAb,CAAM,CACtE,CAAC,CAAA,CAEM,SAASe,CAAAA,CAA0C,CAAE,SAAAC,CAAAA,CAAU,GAAGJ,CAAO,CAAA,CAA+B,CAC7G,GAAM,CAAE,YAAA,CAAAd,CAAAA,CAAc,cAAA,CAAgBe,CAAAA,CAAuB,KAAA,CAAAb,CAAM,EAAIU,CAAAA,CAAwBE,CAAM,CAAA,CAErG,OAAAR,CAAAA,CAA8B,CAC5B,2BAA4BJ,CAAAA,CAAM,QAAA,GAAW,0BAAA,CAC7C,OAAA,CAAUQ,GAAU,OAAA,CAAQ,KAAA,CAAM,yCAAA,CAA2CA,CAAK,CACpF,CAAC,EAEAb,CAAAA,CAAoB,cAAA,CAAwCkB,CAAAA,CAEtDI,cAAAA,CAACnB,CAAAA,CAAa,QAAA,CAAb,CAAsB,KAAA,CAAOE,CAAAA,CAAQ,QAAA,CAAAgB,CAAAA,CAAS,CACxD","file":"index.js","sourcesContent":["// packages/pulsar-react/src/hooks/usePulsarStore.ts\n\nimport { ITxTrackingStore, Transaction } from '@tuwaio/pulsar-core';\nimport React, { createContext, useContext } from 'react';\nimport { StoreApi, useStore } from 'zustand';\n\n// --- CORE TYPES ---\n\n/**\n * Type alias for the core store state (ITxTrackingStore<TTransaction>).\n */\nexport type PulsarStore<TTransaction extends Transaction> = ITxTrackingStore<TTransaction>;\n\n// --- GLOBAL HOOK BINDINGS ---\n\n// Signature for the Bounded Hook assigned by the Provider.\n// T must be explicitly provided in the declaration to allow TSelected to be inferred from the selector.\nexport interface BoundedUseStore<T extends Transaction> {\n // Overload 1: Simplest call. T is the type bound to the context. TSelected (TRet) is inferred.\n <TRet>(selector: (state: PulsarStore<T>) => TRet): TRet;\n\n // Overload 2: Call with an explicit subtype TTransaction. TSelected (TRet) is inferred.\n <TTransaction extends T, TRet>(selector: (state: PulsarStore<TTransaction>) => TRet): TRet;\n}\n\n// Global object to hold the *actual* Bounded Hook assigned by the Provider.\n// This is the object that the Provider modifies.\nexport const PulsarStoreBindings = {\n // We use a temporary non-functional placeholder that satisfies the widest type signature.\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n usePulsarStore: ((selector: any): any => {\n throw new Error('usePulsarStore must be called within PulsarCoreProvider.');\n }) as BoundedUseStore<any>, // BoundedUseStore<any> is the only way to satisfy TS globally.\n};\n\n// --- FACTORY LOGIC (Used by the Provider) ---\n\n// This must be exported so the Provider can use it.\nexport type StoreContext<T extends Transaction> = React.Context<StoreApi<PulsarStore<T>> | null>;\n\n/**\n * Factory function to create a unique React Context and a strictly typed Bounded Hook for a specific T.\n */\nexport function createPulsarStoreContextAndHook<T extends Transaction>() {\n // Context created with the specific T from the Provider's generic.\n const StoreContext: StoreContext<T> = createContext<StoreApi<PulsarStore<T>> | null>(null);\n\n // The Bounded Hook implementation. It knows T from the factory's closure.\n // The signature must be the most general one from the BoundedUseStore interface.\n const useBoundedPulsarStore: BoundedUseStore<T> = ((selector: any): any => {\n const store = useContext(StoreContext); // Uses the strictly typed local Context\n\n if (!store) {\n throw new Error('usePulsarStore must be used within a PulsarCoreProvider');\n }\n\n // TSelected (TRet) is correctly inferred here from the selector. No casting needed.\n return useStore(store, selector);\n }) as BoundedUseStore<T>;\n\n return { StoreContext, usePulsarStore: useBoundedPulsarStore };\n}\n\n// --- CONSUMER API (Hook for components to import) ---\n\n/**\n * Public hook to access the Pulsar Store.\n * @template T The specific Transaction type (inferred from usage or specified).\n * @template TSelected The type of the value returned by the selector (inferred).\n * * NOTE: The T = Transaction default helps with base type hints, but the actual hook\n * resolves T via the Provider-bound instance.\n */\nexport function usePulsarStore<T extends Transaction = Transaction, TSelected = unknown>(\n selector: (state: PulsarStore<T>) => TSelected,\n): TSelected {\n // Delegate the call to the actual Bounded Hook stored in the global bindings.\n // This is the necessary indirection to make the generic typing work across modules.\n return PulsarStoreBindings.usePulsarStore<T, TSelected>(selector);\n}\n","/**\n * @file This file defines a React hook for initializing the transaction pool.\n * This hook is crucial for resuming the tracking of pending transactions after a page reload.\n */\n\nimport { useEffect } from 'react';\n\n/**\n * A React hook that triggers the initialization of the transaction pool when the component mounts.\n *\n * This should be used once in your application's layout or root component. It ensures that any\n * pending transactions from a previous session (stored in `localStorage`) are picked up and\n\n * their trackers are re-activated.\n *\n * @param {object} params - The parameters for the hook.\n * @param {() => Promise<void>} params.initializeTransactionsPool - The `initializeTransactionsPool` function from your Pulsar store instance.\n * @param {(error: Error) => void} [params.onError] - An optional custom function to handle any errors that occur during initialization. Defaults to `console.error`.\n *\n * @example\n * ```tsx\n * import { useInitializeTransactionsPool } from '@tuwaio/pulsar-react';\n * import { pulsarStore } from './path/to/your/store';\n *\n * function AppLayout({ children }) {\n * // This hook will run once when the layout mounts.\n * useInitializeTransactionsPool({\n * initializeTransactionsPool: pulsarStore.getState().initializeTransactionsPool,\n * onError: (err) => console.warn('Failed to re-initialize trackers:', err),\n * });\n *\n * return <div>{children}</div>;\n * }\n * ```\n */\nexport const useInitializeTransactionsPool = ({\n initializeTransactionsPool,\n onError,\n}: {\n initializeTransactionsPool: () => Promise<void>;\n onError?: (error: Error) => void;\n}) => {\n useEffect(() => {\n const reinitializeTrackers = async () => {\n try {\n await initializeTransactionsPool();\n } catch (error) {\n const errorHandler = onError ?? ((e: Error) => console.error('Failed to initialize transactions pool:', e));\n errorHandler(error as Error);\n }\n };\n\n // Run the initialization process.\n reinitializeTrackers();\n\n // The dependency array is empty to ensure this effect runs only once on mount.\n // The functions from a vanilla Zustand store are stable and do not need to be in the array.\n }, [initializeTransactionsPool, onError]);\n};\n","import { createPulsarStore, Transaction } from '@tuwaio/pulsar-core';\nimport { ITxTrackingStore, PulsarAdapter } from '@tuwaio/pulsar-core/src';\nimport once from 'lodash.once';\nimport { PropsWithChildren } from 'react';\nimport { PersistOptions } from 'zustand/middleware';\n\nimport type { BoundedUseStore } from '../hooks/pulsarHook';\nimport { createPulsarStoreContextAndHook, PulsarStoreBindings } from '../hooks/pulsarHook';\nimport { useInitializeTransactionsPool } from '../hooks/useInitializeTransactionsPool';\n\ninterface PulsarCoreProviderProps<T extends Transaction>\n extends PropsWithChildren,\n PulsarAdapter<T>,\n Partial<PersistOptions<ITxTrackingStore<T>>> {}\n\nconst getBoundedStoreInstance = once(<T extends Transaction>(params: PulsarCoreProviderProps<T>) => {\n const { StoreContext, usePulsarStore: useBoundedPulsarStore } = createPulsarStoreContextAndHook<T>();\n\n const store = createPulsarStore<T>({\n ...params,\n name: params.name ?? 'tuwa:pulsar-store',\n });\n\n return { StoreContext, usePulsarStore: useBoundedPulsarStore, store };\n});\n\nexport function PulsarCoreProvider<T extends Transaction>({ children, ...params }: PulsarCoreProviderProps<T>) {\n const { StoreContext, usePulsarStore: useBoundedPulsarStore, store } = getBoundedStoreInstance(params);\n\n useInitializeTransactionsPool({\n initializeTransactionsPool: store.getState().initializeTransactionsPool,\n onError: (error) => console.error('Failed to initialize transactions pool:', error),\n });\n\n (PulsarStoreBindings.usePulsarStore as BoundedUseStore<T>) = useBoundedPulsarStore;\n\n return <StoreContext.Provider value={store}>{children}</StoreContext.Provider>;\n}\n"]}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import {createContext,useContext,useEffect,useRef}from'react';import {useStore}from'zustand';import {createPulsarStore}from'@tuwaio/pulsar-core';import {jsx}from'react/jsx-runtime';var a=createContext(null);function f(e){let r=useContext(a);if(!r)throw new Error("usePulsarStore must be used within a PulsarProvider");return useStore(r,e)}var s=({initializeTransactionsPool:e,onError:r})=>{useEffect(()=>{(async()=>{try{await e();}catch(o){(r??(i=>console.error("Failed to initialize transactions pool:",i)))(o);}})();},[e,r]);};function F({children:e,...r}){let t=useRef(null);t.current||(t.current=createPulsarStore({...r,name:r.name??"pulsar-store"}));let o=t.current;return s({initializeTransactionsPool:t.current.getState().initializeTransactionsPool,onError:n=>console.error("Failed to initialize transactions pool:",n)}),jsx(a.Provider,{value:o,children:e})}export{F as PulsarCoreProvider,a as PulsarStoreContext,s as useInitializeTransactionsPool,f as usePulsarStore};//# sourceMappingURL=index.mjs.map
1
+ import {createContext,useContext,useEffect}from'react';import {useStore}from'zustand';import {createPulsarStore}from'@tuwaio/pulsar-core';import S from'lodash.once';import {jsx}from'react/jsx-runtime';var a={usePulsarStore:(r=>{throw new Error("usePulsarStore must be called within PulsarCoreProvider.")})};function i(){let r=createContext(null);return {StoreContext:r,usePulsarStore:(t=>{let e=useContext(r);if(!e)throw new Error("usePulsarStore must be used within a PulsarCoreProvider");return useStore(e,t)})}}function k(r){return a.usePulsarStore(r)}var u=({initializeTransactionsPool:r,onError:o})=>{useEffect(()=>{(async()=>{try{await r();}catch(e){(o??(s=>console.error("Failed to initialize transactions pool:",s)))(e);}})();},[r,o]);};var p=S(r=>{let{StoreContext:o,usePulsarStore:t}=i(),e=createPulsarStore({...r,name:r.name??"tuwa:pulsar-store"});return {StoreContext:o,usePulsarStore:t,store:e}});function E({children:r,...o}){let{StoreContext:t,usePulsarStore:e,store:n}=p(o);return u({initializeTransactionsPool:n.getState().initializeTransactionsPool,onError:s=>console.error("Failed to initialize transactions pool:",s)}),a.usePulsarStore=e,jsx(t.Provider,{value:n,children:r})}export{E as PulsarCoreProvider,a as PulsarStoreBindings,i as createPulsarStoreContextAndHook,u as useInitializeTransactionsPool,k as usePulsarStore};//# sourceMappingURL=index.mjs.map
2
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hooks/pulsarHook.ts","../src/hooks/useInitializeTransactionsPool.tsx","../src/providers/PulsarCoreProvider.tsx"],"names":["PulsarStoreContext","createContext","usePulsarStore","selector","store","useContext","useStore","useInitializeTransactionsPool","initializeTransactionsPool","onError","useEffect","error","e","PulsarCoreProvider","children","params","storeRef","useRef","createPulsarStore","storeToProvide","jsx"],"mappings":"yLAOaA,CAAAA,CAAqBC,aAAAA,CAA8D,IAAI,EAQ7F,SAASC,CAAAA,CACdC,CAAAA,CACW,CACX,IAAMC,EAAQC,UAAAA,CAAWL,CAAkB,CAAA,CAE3C,GAAI,CAACI,CAAAA,CACH,MAAM,IAAI,KAAA,CAAM,qDAAqD,CAAA,CAGvE,OAAOE,QAAAA,CAASF,CAAAA,CAA8DD,CAAQ,CACxF,CCUO,IAAMI,CAAAA,CAAgC,CAAC,CAC5C,0BAAA,CAAAC,CAAAA,CACA,OAAA,CAAAC,CACF,CAAA,GAGM,CACJC,SAAAA,CAAU,IAAM,EACe,SAAY,CACvC,GAAI,CACF,MAAMF,CAAAA,GACR,CAAA,MAASG,CAAAA,CAAO,EACOF,CAAAA,GAAaG,CAAAA,EAAa,OAAA,CAAQ,KAAA,CAAM,0CAA2CA,CAAC,CAAA,CAAA,EAC5FD,CAAc,EAC7B,CACF,CAAA,IAOF,CAAA,CAAG,CAACH,CAAAA,CAA4BC,CAAO,CAAC,EAC1C,EC7CO,SAASI,CAAAA,CAA0C,CAAE,SAAAC,CAAAA,CAAU,GAAGC,CAAO,CAAA,CAA+B,CAC7G,IAAMC,CAAAA,CAAWC,MAAAA,CAA6C,IAAI,EAE7DD,CAAAA,CAAS,OAAA,GACZA,CAAAA,CAAS,OAAA,CAAUE,kBAAqB,CACtC,GAAGH,CAAAA,CACH,IAAA,CAAMA,CAAAA,CAAO,IAAA,EAAQ,cACvB,CAAC,GAGH,IAAMI,CAAAA,CAAiBH,CAAAA,CAAS,OAAA,CAEhC,OAAAT,CAAAA,CAA8B,CAC5B,0BAAA,CAA4BS,CAAAA,CAAS,QAAQ,QAAA,EAAS,CAAE,0BAAA,CACxD,OAAA,CAAUL,GAAU,OAAA,CAAQ,KAAA,CAAM,yCAAA,CAA2CA,CAAK,CACpF,CAAC,CAAA,CAEMS,GAAAA,CAACpB,CAAAA,CAAmB,SAAnB,CAA4B,KAAA,CAAOmB,CAAAA,CAAiB,QAAA,CAAAL,EAAS,CACvE","file":"index.mjs","sourcesContent":["import { ITxTrackingStore, Transaction } from '@tuwaio/pulsar-core';\nimport { createContext, useContext } from 'react';\nimport { StoreApi, useStore } from 'zustand';\n\nexport type PulsarStoreState<TTransaction extends Transaction> = ITxTrackingStore<TTransaction>;\ntype PulsarSelector<TTransaction extends Transaction, TSelected> = (state: PulsarStoreState<TTransaction>) => TSelected;\n\nexport const PulsarStoreContext = createContext<StoreApi<PulsarStoreState<Transaction>> | null>(null);\n\nexport function usePulsarStore<TTransaction extends Transaction, TRet>(\n selector: PulsarSelector<TTransaction, TRet>,\n): TRet;\n\nexport function usePulsarStore<TSelected>(selector: PulsarSelector<Transaction, TSelected>): TSelected;\n\nexport function usePulsarStore<TSelected, TTransaction extends Transaction = Transaction>(\n selector: PulsarSelector<TTransaction, TSelected>,\n): TSelected {\n const store = useContext(PulsarStoreContext);\n\n if (!store) {\n throw new Error('usePulsarStore must be used within a PulsarProvider');\n }\n\n return useStore(store as unknown as StoreApi<PulsarStoreState<TTransaction>>, selector);\n}\n","/**\n * @file This file defines a React hook for initializing the transaction pool.\n * This hook is crucial for resuming the tracking of pending transactions after a page reload.\n */\n\nimport { useEffect } from 'react';\n\n/**\n * A React hook that triggers the initialization of the transaction pool when the component mounts.\n *\n * This should be used once in your application's layout or root component. It ensures that any\n * pending transactions from a previous session (stored in `localStorage`) are picked up and\n\n * their trackers are re-activated.\n *\n * @param {object} params - The parameters for the hook.\n * @param {() => Promise<void>} params.initializeTransactionsPool - The `initializeTransactionsPool` function from your Pulsar store instance.\n * @param {(error: Error) => void} [params.onError] - An optional custom function to handle any errors that occur during initialization. Defaults to `console.error`.\n *\n * @example\n * ```tsx\n * import { useInitializeTransactionsPool } from '@tuwaio/pulsar-react';\n * import { pulsarStore } from './path/to/your/store';\n *\n * function AppLayout({ children }) {\n * // This hook will run once when the layout mounts.\n * useInitializeTransactionsPool({\n * initializeTransactionsPool: pulsarStore.getState().initializeTransactionsPool,\n * onError: (err) => console.warn('Failed to re-initialize trackers:', err),\n * });\n *\n * return <div>{children}</div>;\n * }\n * ```\n */\nexport const useInitializeTransactionsPool = ({\n initializeTransactionsPool,\n onError,\n}: {\n initializeTransactionsPool: () => Promise<void>;\n onError?: (error: Error) => void;\n}) => {\n useEffect(() => {\n const reinitializeTrackers = async () => {\n try {\n await initializeTransactionsPool();\n } catch (error) {\n const errorHandler = onError ?? ((e: Error) => console.error('Failed to initialize transactions pool:', e));\n errorHandler(error as Error);\n }\n };\n\n // Run the initialization process.\n reinitializeTrackers();\n\n // The dependency array is empty to ensure this effect runs only once on mount.\n // The functions from a vanilla Zustand store are stable and do not need to be in the array.\n }, [initializeTransactionsPool, onError]);\n};\n","import { createPulsarStore, ITxTrackingStore, PulsarAdapter, Transaction } from '@tuwaio/pulsar-core';\nimport { PropsWithChildren, useRef } from 'react';\nimport { StoreApi } from 'zustand';\nimport { PersistOptions } from 'zustand/middleware';\n\nimport { PulsarStoreContext, PulsarStoreState } from '../hooks/pulsarHook';\nimport { useInitializeTransactionsPool } from '../hooks/useInitializeTransactionsPool';\n\ninterface PulsarCoreProviderProps<T extends Transaction>\n extends PropsWithChildren,\n PulsarAdapter<T>,\n Partial<PersistOptions<ITxTrackingStore<T>>> {}\n\nexport function PulsarCoreProvider<T extends Transaction>({ children, ...params }: PulsarCoreProviderProps<T>) {\n const storeRef = useRef<StoreApi<PulsarStoreState<T>> | null>(null);\n\n if (!storeRef.current) {\n storeRef.current = createPulsarStore<T>({\n ...params,\n name: params.name ?? 'pulsar-store',\n });\n }\n\n const storeToProvide = storeRef.current as unknown as StoreApi<PulsarStoreState<Transaction>>;\n\n useInitializeTransactionsPool({\n initializeTransactionsPool: storeRef.current.getState().initializeTransactionsPool,\n onError: (error) => console.error('Failed to initialize transactions pool:', error),\n });\n\n return <PulsarStoreContext.Provider value={storeToProvide}>{children}</PulsarStoreContext.Provider>;\n}\n"]}
1
+ {"version":3,"sources":["../src/hooks/pulsarHook.ts","../src/hooks/useInitializeTransactionsPool.tsx","../src/providers/PulsarCoreProvider.tsx"],"names":["PulsarStoreBindings","selector","createPulsarStoreContextAndHook","StoreContext","createContext","store","useContext","useStore","usePulsarStore","useInitializeTransactionsPool","initializeTransactionsPool","onError","useEffect","error","e","getBoundedStoreInstance","once","params","useBoundedPulsarStore","createPulsarStore","PulsarCoreProvider","children","jsx"],"mappings":"yMA2BO,IAAMA,CAAAA,CAAsB,CAGjC,cAAA,EAAkBC,CAAAA,EAAuB,CACvC,MAAM,IAAI,MAAM,0DAA0D,CAC5E,CAAA,CACF,EAUO,SAASC,CAAAA,EAAyD,CAEvE,IAAMC,CAAAA,CAAgCC,cAA+C,IAAI,CAAA,CAezF,OAAO,CAAE,YAAA,CAAAD,CAAAA,CAAc,cAAA,EAX6BF,CAAAA,EAAuB,CACzE,IAAMI,CAAAA,CAAQC,UAAAA,CAAWH,CAAY,CAAA,CAErC,GAAI,CAACE,EACH,MAAM,IAAI,KAAA,CAAM,yDAAyD,CAAA,CAI3E,OAAOE,SAASF,CAAAA,CAAOJ,CAAQ,CACjC,CAAA,CAE6D,CAC/D,CAWO,SAASO,CAAAA,CACdP,CAAAA,CACW,CAGX,OAAOD,CAAAA,CAAoB,eAA6BC,CAAQ,CAClE,CC3CO,IAAMQ,CAAAA,CAAgC,CAAC,CAC5C,0BAAA,CAAAC,EACA,OAAA,CAAAC,CACF,IAGM,CACJC,SAAAA,CAAU,IAAM,CAAA,CACe,SAAY,CACvC,GAAI,CACF,MAAMF,IACR,CAAA,MAASG,CAAAA,CAAO,CAAA,CACOF,CAAAA,GAAaG,CAAAA,EAAa,QAAQ,KAAA,CAAM,yCAAA,CAA2CA,CAAC,CAAA,CAAA,EAC5FD,CAAc,EAC7B,CACF,CAAA,IAOF,EAAG,CAACH,CAAAA,CAA4BC,CAAO,CAAC,EAC1C,EC3CA,IAAMI,CAAAA,CAA0BC,CAAAA,CAA6BC,CAAAA,EAAuC,CAClG,GAAM,CAAE,YAAA,CAAAd,CAAAA,CAAc,eAAgBe,CAAsB,CAAA,CAAIhB,GAAmC,CAE7FG,CAAAA,CAAQc,iBAAAA,CAAqB,CACjC,GAAGF,CAAAA,CACH,KAAMA,CAAAA,CAAO,IAAA,EAAQ,mBACvB,CAAC,CAAA,CAED,OAAO,CAAE,YAAA,CAAAd,CAAAA,CAAc,cAAA,CAAgBe,CAAAA,CAAuB,KAAA,CAAAb,CAAM,CACtE,CAAC,CAAA,CAEM,SAASe,CAAAA,CAA0C,CAAE,SAAAC,CAAAA,CAAU,GAAGJ,CAAO,CAAA,CAA+B,CAC7G,GAAM,CAAE,YAAA,CAAAd,CAAAA,CAAc,cAAA,CAAgBe,CAAAA,CAAuB,KAAA,CAAAb,CAAM,EAAIU,CAAAA,CAAwBE,CAAM,CAAA,CAErG,OAAAR,CAAAA,CAA8B,CAC5B,2BAA4BJ,CAAAA,CAAM,QAAA,GAAW,0BAAA,CAC7C,OAAA,CAAUQ,GAAU,OAAA,CAAQ,KAAA,CAAM,yCAAA,CAA2CA,CAAK,CACpF,CAAC,EAEAb,CAAAA,CAAoB,cAAA,CAAwCkB,CAAAA,CAEtDI,GAAAA,CAACnB,CAAAA,CAAa,QAAA,CAAb,CAAsB,KAAA,CAAOE,CAAAA,CAAQ,QAAA,CAAAgB,CAAAA,CAAS,CACxD","file":"index.mjs","sourcesContent":["// packages/pulsar-react/src/hooks/usePulsarStore.ts\n\nimport { ITxTrackingStore, Transaction } from '@tuwaio/pulsar-core';\nimport React, { createContext, useContext } from 'react';\nimport { StoreApi, useStore } from 'zustand';\n\n// --- CORE TYPES ---\n\n/**\n * Type alias for the core store state (ITxTrackingStore<TTransaction>).\n */\nexport type PulsarStore<TTransaction extends Transaction> = ITxTrackingStore<TTransaction>;\n\n// --- GLOBAL HOOK BINDINGS ---\n\n// Signature for the Bounded Hook assigned by the Provider.\n// T must be explicitly provided in the declaration to allow TSelected to be inferred from the selector.\nexport interface BoundedUseStore<T extends Transaction> {\n // Overload 1: Simplest call. T is the type bound to the context. TSelected (TRet) is inferred.\n <TRet>(selector: (state: PulsarStore<T>) => TRet): TRet;\n\n // Overload 2: Call with an explicit subtype TTransaction. TSelected (TRet) is inferred.\n <TTransaction extends T, TRet>(selector: (state: PulsarStore<TTransaction>) => TRet): TRet;\n}\n\n// Global object to hold the *actual* Bounded Hook assigned by the Provider.\n// This is the object that the Provider modifies.\nexport const PulsarStoreBindings = {\n // We use a temporary non-functional placeholder that satisfies the widest type signature.\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n usePulsarStore: ((selector: any): any => {\n throw new Error('usePulsarStore must be called within PulsarCoreProvider.');\n }) as BoundedUseStore<any>, // BoundedUseStore<any> is the only way to satisfy TS globally.\n};\n\n// --- FACTORY LOGIC (Used by the Provider) ---\n\n// This must be exported so the Provider can use it.\nexport type StoreContext<T extends Transaction> = React.Context<StoreApi<PulsarStore<T>> | null>;\n\n/**\n * Factory function to create a unique React Context and a strictly typed Bounded Hook for a specific T.\n */\nexport function createPulsarStoreContextAndHook<T extends Transaction>() {\n // Context created with the specific T from the Provider's generic.\n const StoreContext: StoreContext<T> = createContext<StoreApi<PulsarStore<T>> | null>(null);\n\n // The Bounded Hook implementation. It knows T from the factory's closure.\n // The signature must be the most general one from the BoundedUseStore interface.\n const useBoundedPulsarStore: BoundedUseStore<T> = ((selector: any): any => {\n const store = useContext(StoreContext); // Uses the strictly typed local Context\n\n if (!store) {\n throw new Error('usePulsarStore must be used within a PulsarCoreProvider');\n }\n\n // TSelected (TRet) is correctly inferred here from the selector. No casting needed.\n return useStore(store, selector);\n }) as BoundedUseStore<T>;\n\n return { StoreContext, usePulsarStore: useBoundedPulsarStore };\n}\n\n// --- CONSUMER API (Hook for components to import) ---\n\n/**\n * Public hook to access the Pulsar Store.\n * @template T The specific Transaction type (inferred from usage or specified).\n * @template TSelected The type of the value returned by the selector (inferred).\n * * NOTE: The T = Transaction default helps with base type hints, but the actual hook\n * resolves T via the Provider-bound instance.\n */\nexport function usePulsarStore<T extends Transaction = Transaction, TSelected = unknown>(\n selector: (state: PulsarStore<T>) => TSelected,\n): TSelected {\n // Delegate the call to the actual Bounded Hook stored in the global bindings.\n // This is the necessary indirection to make the generic typing work across modules.\n return PulsarStoreBindings.usePulsarStore<T, TSelected>(selector);\n}\n","/**\n * @file This file defines a React hook for initializing the transaction pool.\n * This hook is crucial for resuming the tracking of pending transactions after a page reload.\n */\n\nimport { useEffect } from 'react';\n\n/**\n * A React hook that triggers the initialization of the transaction pool when the component mounts.\n *\n * This should be used once in your application's layout or root component. It ensures that any\n * pending transactions from a previous session (stored in `localStorage`) are picked up and\n\n * their trackers are re-activated.\n *\n * @param {object} params - The parameters for the hook.\n * @param {() => Promise<void>} params.initializeTransactionsPool - The `initializeTransactionsPool` function from your Pulsar store instance.\n * @param {(error: Error) => void} [params.onError] - An optional custom function to handle any errors that occur during initialization. Defaults to `console.error`.\n *\n * @example\n * ```tsx\n * import { useInitializeTransactionsPool } from '@tuwaio/pulsar-react';\n * import { pulsarStore } from './path/to/your/store';\n *\n * function AppLayout({ children }) {\n * // This hook will run once when the layout mounts.\n * useInitializeTransactionsPool({\n * initializeTransactionsPool: pulsarStore.getState().initializeTransactionsPool,\n * onError: (err) => console.warn('Failed to re-initialize trackers:', err),\n * });\n *\n * return <div>{children}</div>;\n * }\n * ```\n */\nexport const useInitializeTransactionsPool = ({\n initializeTransactionsPool,\n onError,\n}: {\n initializeTransactionsPool: () => Promise<void>;\n onError?: (error: Error) => void;\n}) => {\n useEffect(() => {\n const reinitializeTrackers = async () => {\n try {\n await initializeTransactionsPool();\n } catch (error) {\n const errorHandler = onError ?? ((e: Error) => console.error('Failed to initialize transactions pool:', e));\n errorHandler(error as Error);\n }\n };\n\n // Run the initialization process.\n reinitializeTrackers();\n\n // The dependency array is empty to ensure this effect runs only once on mount.\n // The functions from a vanilla Zustand store are stable and do not need to be in the array.\n }, [initializeTransactionsPool, onError]);\n};\n","import { createPulsarStore, Transaction } from '@tuwaio/pulsar-core';\nimport { ITxTrackingStore, PulsarAdapter } from '@tuwaio/pulsar-core/src';\nimport once from 'lodash.once';\nimport { PropsWithChildren } from 'react';\nimport { PersistOptions } from 'zustand/middleware';\n\nimport type { BoundedUseStore } from '../hooks/pulsarHook';\nimport { createPulsarStoreContextAndHook, PulsarStoreBindings } from '../hooks/pulsarHook';\nimport { useInitializeTransactionsPool } from '../hooks/useInitializeTransactionsPool';\n\ninterface PulsarCoreProviderProps<T extends Transaction>\n extends PropsWithChildren,\n PulsarAdapter<T>,\n Partial<PersistOptions<ITxTrackingStore<T>>> {}\n\nconst getBoundedStoreInstance = once(<T extends Transaction>(params: PulsarCoreProviderProps<T>) => {\n const { StoreContext, usePulsarStore: useBoundedPulsarStore } = createPulsarStoreContextAndHook<T>();\n\n const store = createPulsarStore<T>({\n ...params,\n name: params.name ?? 'tuwa:pulsar-store',\n });\n\n return { StoreContext, usePulsarStore: useBoundedPulsarStore, store };\n});\n\nexport function PulsarCoreProvider<T extends Transaction>({ children, ...params }: PulsarCoreProviderProps<T>) {\n const { StoreContext, usePulsarStore: useBoundedPulsarStore, store } = getBoundedStoreInstance(params);\n\n useInitializeTransactionsPool({\n initializeTransactionsPool: store.getState().initializeTransactionsPool,\n onError: (error) => console.error('Failed to initialize transactions pool:', error),\n });\n\n (PulsarStoreBindings.usePulsarStore as BoundedUseStore<T>) = useBoundedPulsarStore;\n\n return <StoreContext.Provider value={store}>{children}</StoreContext.Provider>;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tuwaio/pulsar-react",
3
- "version": "1.0.0-fix-orbit-integration-alpha.6.76dee15",
3
+ "version": "1.0.0-fix-orbit-integration-alpha.3.86c8fdb",
4
4
  "private": false,
5
5
  "author": "Oleksandr Tkach",
6
6
  "license": "Apache-2.0",
@@ -37,6 +37,10 @@
37
37
  "url": "https://github.com/Argeare5"
38
38
  }
39
39
  ],
40
+ "dependencies": {
41
+ "@types/lodash.once": "4.1.9",
42
+ "lodash.once": "4.1.1"
43
+ },
40
44
  "peerDependencies": {
41
45
  "@tuwaio/orbit-core": ">=0",
42
46
  "@tuwaio/pulsar-core": ">=0",
@@ -54,7 +58,7 @@
54
58
  "tsup": "^8.5.0",
55
59
  "typescript": "^5.9.2",
56
60
  "zustand": "^5.0.8",
57
- "@tuwaio/pulsar-core": "^1.0.0-fix-orbit-integration-alpha.6.76dee15"
61
+ "@tuwaio/pulsar-core": "^1.0.0-fix-orbit-integration-alpha.3.86c8fdb"
58
62
  },
59
63
  "scripts": {
60
64
  "start": "tsup src/index.ts --watch",