@kheopskit/react 5.3.0 → 6.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +546 -0
- package/README.md +237 -9
- package/dist/index.d.mts +117 -10
- package/dist/index.d.ts +117 -10
- package/dist/index.js +372 -95
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +372 -98
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -4
package/dist/index.js
CHANGED
|
@@ -2,116 +2,265 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
let _kheopskit_core = require("@kheopskit/core");
|
|
4
4
|
let react = require("react");
|
|
5
|
+
let rxjs = require("rxjs");
|
|
5
6
|
let react_jsx_runtime = require("react/jsx-runtime");
|
|
6
7
|
//#region src/context.ts
|
|
7
8
|
const KheopskitContext = (0, react.createContext)(null);
|
|
9
|
+
const useKheopskitExternalStore = (hookName) => {
|
|
10
|
+
const store = (0, react.useContext)(KheopskitContext);
|
|
11
|
+
if (!store) throw new Error(`${hookName} can't be used without a KheopskitProvider`);
|
|
12
|
+
return store;
|
|
13
|
+
};
|
|
8
14
|
//#endregion
|
|
9
15
|
//#region src/createExternalStore.ts
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
let
|
|
13
|
-
let
|
|
16
|
+
const afterThisCommitsLayoutEffects$1 = () => Promise.resolve();
|
|
17
|
+
const createExternalStore = (resolveClientSource, serverValue) => {
|
|
18
|
+
let source;
|
|
19
|
+
let latestValue;
|
|
20
|
+
let subscription;
|
|
14
21
|
const listeners = /* @__PURE__ */ new Set();
|
|
15
|
-
const
|
|
16
|
-
|
|
22
|
+
const getSource = () => {
|
|
23
|
+
source ?? (source = resolveClientSource());
|
|
24
|
+
return source;
|
|
25
|
+
};
|
|
26
|
+
const getSnapshot = () => latestValue ?? getSource().initialValue;
|
|
27
|
+
const getServerSnapshot = () => serverValue;
|
|
28
|
+
const releaseWhenUnobserved = () => {
|
|
29
|
+
if (listeners.size) return;
|
|
30
|
+
subscription?.unsubscribe();
|
|
31
|
+
subscription = void 0;
|
|
32
|
+
};
|
|
33
|
+
const subscribe = (listener) => {
|
|
34
|
+
if (!subscription || subscription.closed) subscription = getSource().pipeline.observable$.subscribe((value) => {
|
|
17
35
|
latestValue = value;
|
|
18
|
-
for (const
|
|
36
|
+
for (const notify of listeners) notify(value);
|
|
19
37
|
});
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Returns the server-side snapshot for SSR hydration.
|
|
24
|
-
* This prevents hydration mismatches by providing a consistent
|
|
25
|
-
* value during server rendering. Must return the same value as
|
|
26
|
-
* what the server rendered.
|
|
27
|
-
*/
|
|
28
|
-
const getServerSnapshot = () => serverValue ?? initialValue;
|
|
29
|
-
const subscribe = (callback) => {
|
|
30
|
-
subscriberCount++;
|
|
31
|
-
ensureSubscription();
|
|
32
|
-
listeners.add(callback);
|
|
33
|
-
callback(getSnapshot());
|
|
38
|
+
listeners.add(listener);
|
|
39
|
+
listener(getSnapshot());
|
|
34
40
|
return () => {
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
listeners.delete(listener);
|
|
42
|
+
queueMicrotask(releaseWhenUnobserved);
|
|
37
43
|
};
|
|
38
44
|
};
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
45
|
+
/** The shared pipeline's liveness once this commit's layout effects ran. */
|
|
46
|
+
const whenLive = async (signal) => {
|
|
47
|
+
await afterThisCommitsLayoutEffects$1();
|
|
48
|
+
return getSource().pipeline.whenLive(signal);
|
|
44
49
|
};
|
|
45
50
|
return {
|
|
46
51
|
getSnapshot,
|
|
47
52
|
getServerSnapshot,
|
|
48
53
|
subscribe,
|
|
49
|
-
|
|
54
|
+
whenLive
|
|
50
55
|
};
|
|
51
56
|
};
|
|
52
57
|
//#endregion
|
|
53
|
-
//#region src/
|
|
54
|
-
const
|
|
58
|
+
//#region src/liveActions.ts
|
|
59
|
+
const didNotRun = {
|
|
60
|
+
aborted: "was aborted",
|
|
61
|
+
unheld: "was called while nothing holds the pipeline (no KheopskitProvider of this instance is mounted and visible, and nothing subscribes its kheopskit$)",
|
|
62
|
+
errored: "did not run because the pipeline stopped with an error",
|
|
63
|
+
completed: "did not run because the pipeline completed"
|
|
64
|
+
};
|
|
65
|
+
const abortedError = (walletId, operation, end) => new _kheopskit_core.KheopskitError("ABORTED", `${operation} of wallet ${walletId} ${didNotRun[end]}`, { walletId });
|
|
66
|
+
/**
|
|
67
|
+
* `connect` and `disconnect` for one wallet id that act on the instance's
|
|
68
|
+
* pipeline at call time: they wait for the restarted pipeline's first emission
|
|
69
|
+
* after an `<Activity>` reveal and reject `ABORTED` while nothing holds it.
|
|
70
|
+
*/
|
|
71
|
+
const walletActions = (store, walletId) => {
|
|
72
|
+
const liveWallet = async (operation, signal) => {
|
|
73
|
+
const liveness = await store.whenLive(signal);
|
|
74
|
+
if (liveness.end) throw abortedError(walletId, operation, liveness.end);
|
|
75
|
+
const wallet = liveness.state.wallets.find((candidate) => candidate.id === walletId);
|
|
76
|
+
if (!wallet) throw new Error(`[kheopskit] no wallet ${walletId}`);
|
|
77
|
+
return wallet;
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
connect: async (options) => (await liveWallet("connect", options?.signal)).connect(options),
|
|
81
|
+
disconnect: async () => (await liveWallet("disconnect")).disconnect()
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
const withLiveActions = (store) => {
|
|
85
|
+
const presented = /* @__PURE__ */ new WeakMap();
|
|
86
|
+
const present = (state) => {
|
|
87
|
+
let live = presented.get(state);
|
|
88
|
+
if (!live) {
|
|
89
|
+
live = {
|
|
90
|
+
...state,
|
|
91
|
+
wallets: state.wallets.map((wallet) => ({
|
|
92
|
+
...wallet,
|
|
93
|
+
...walletActions(store, wallet.id)
|
|
94
|
+
}))
|
|
95
|
+
};
|
|
96
|
+
presented.set(state, live);
|
|
97
|
+
}
|
|
98
|
+
return live;
|
|
99
|
+
};
|
|
100
|
+
return {
|
|
101
|
+
...store,
|
|
102
|
+
getSnapshot: () => present(store.getSnapshot()),
|
|
103
|
+
getServerSnapshot: () => present(store.getServerSnapshot())
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/KheopskitSourceProvider.tsx
|
|
108
|
+
const keepPipelineRunning = () => {};
|
|
109
|
+
const useIsomorphicLayoutEffect$1 = typeof window === "undefined" ? react.useEffect : react.useLayoutEffect;
|
|
110
|
+
const hydrating = (config, snapshot) => ({
|
|
111
|
+
...snapshot,
|
|
112
|
+
config,
|
|
113
|
+
isHydrating: true,
|
|
114
|
+
isDiscovering: true
|
|
115
|
+
});
|
|
116
|
+
const serverSnapshot = (config, ssrCookies) => hydrating(config, ssrCookies === void 0 ? {
|
|
117
|
+
wallets: [],
|
|
118
|
+
accounts: []
|
|
119
|
+
} : (0, _kheopskit_core.getHydratedSnapshot)((0, _kheopskit_core.createKheopskitStore)({
|
|
120
|
+
ssrCookies,
|
|
121
|
+
storageKey: config.storageKey
|
|
122
|
+
}), config, { icons: false }));
|
|
123
|
+
const KheopskitSourceProvider = ({ children, config, ssrCookies, resolveClientSource }) => {
|
|
55
124
|
const resolvedConfig = (0, react.useMemo)(() => (0, _kheopskit_core.resolveConfig)(config), [config]);
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
(0, react.useEffect)(() => {
|
|
59
|
-
if (hasMountedRef.current && lastConfigRef.current !== config && (typeof process === "undefined" || process.env?.NODE_ENV !== "production")) console.warn("[kheopskit] KheopskitProvider received a new `config` reference; this recreates the store and re-subscribes on every render. Pass a referentially stable config (module scope, useMemo, or createKheopskit()).");
|
|
60
|
-
hasMountedRef.current = true;
|
|
61
|
-
lastConfigRef.current = config;
|
|
62
|
-
}, [config]);
|
|
63
|
-
const kheopskitStore = (0, react.useMemo)(() => (0, _kheopskit_core.createKheopskitStore)({
|
|
64
|
-
ssrCookies,
|
|
65
|
-
storageKey: resolvedConfig.storageKey
|
|
66
|
-
}), [ssrCookies, resolvedConfig.storageKey]);
|
|
67
|
-
const serverValue = (0, react.useMemo)(() => {
|
|
68
|
-
if (ssrCookies === void 0) return {
|
|
69
|
-
wallets: [],
|
|
70
|
-
accounts: [],
|
|
71
|
-
config: resolvedConfig,
|
|
72
|
-
isHydrating: true
|
|
73
|
-
};
|
|
125
|
+
const externalStore = (0, react.useMemo)(() => withLiveActions(createExternalStore(() => {
|
|
126
|
+
const { store, pipeline } = resolveClientSource(resolvedConfig);
|
|
74
127
|
return {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
isHydrating: true
|
|
128
|
+
pipeline,
|
|
129
|
+
initialValue: hydrating(resolvedConfig, (0, _kheopskit_core.getHydratedSnapshot)(store, resolvedConfig, { icons: true }))
|
|
78
130
|
};
|
|
79
|
-
}, [
|
|
131
|
+
}, serverSnapshot(resolvedConfig, ssrCookies))), [
|
|
132
|
+
resolvedConfig,
|
|
80
133
|
ssrCookies,
|
|
81
|
-
|
|
82
|
-
resolvedConfig
|
|
134
|
+
resolveClientSource
|
|
83
135
|
]);
|
|
84
|
-
|
|
85
|
-
return {
|
|
86
|
-
...(0, _kheopskit_core.getHydratedSnapshot)(kheopskitStore, resolvedConfig.platforms, { icons: true }),
|
|
87
|
-
config: resolvedConfig,
|
|
88
|
-
isHydrating: true
|
|
89
|
-
};
|
|
90
|
-
}, [kheopskitStore, resolvedConfig]);
|
|
91
|
-
const store = (0, react.useMemo)(() => createExternalStore((0, _kheopskit_core.getKheopskit$)(config, {
|
|
92
|
-
ssrCookies,
|
|
93
|
-
store: kheopskitStore
|
|
94
|
-
}), initialValue, serverValue), [
|
|
95
|
-
config,
|
|
96
|
-
ssrCookies,
|
|
97
|
-
kheopskitStore,
|
|
98
|
-
initialValue,
|
|
99
|
-
serverValue
|
|
100
|
-
]);
|
|
101
|
-
(0, react.useEffect)(() => {
|
|
102
|
-
return () => store.destroy();
|
|
103
|
-
}, [store]);
|
|
104
|
-
const state = (0, react.useSyncExternalStore)(store.subscribe, store.getSnapshot, store.getServerSnapshot);
|
|
105
|
-
const value = (0, react.useMemo)(() => ({ state }), [state]);
|
|
136
|
+
useIsomorphicLayoutEffect$1(() => externalStore.subscribe(keepPipelineRunning), [externalStore]);
|
|
106
137
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(KheopskitContext.Provider, {
|
|
107
|
-
value,
|
|
138
|
+
value: externalStore,
|
|
108
139
|
children
|
|
109
140
|
});
|
|
110
141
|
};
|
|
111
142
|
//#endregion
|
|
143
|
+
//#region src/livePipeline.ts
|
|
144
|
+
const createLivePipeline = (source$) => {
|
|
145
|
+
let holders = 0;
|
|
146
|
+
let run = 0;
|
|
147
|
+
let emittedRun = 0;
|
|
148
|
+
let latestValue;
|
|
149
|
+
let ended;
|
|
150
|
+
const firstEmissionWaiters = /* @__PURE__ */ new Set();
|
|
151
|
+
const settleFirstEmission = (liveness) => {
|
|
152
|
+
for (const waiter of firstEmissionWaiters) waiter(liveness);
|
|
153
|
+
};
|
|
154
|
+
const observable$ = new rxjs.Observable((subscriber) => {
|
|
155
|
+
if (holders++ === 0) {
|
|
156
|
+
run++;
|
|
157
|
+
ended = void 0;
|
|
158
|
+
}
|
|
159
|
+
const thisRun = run;
|
|
160
|
+
const subscription = source$.subscribe({
|
|
161
|
+
next: (value) => {
|
|
162
|
+
latestValue = value;
|
|
163
|
+
emittedRun = thisRun;
|
|
164
|
+
settleFirstEmission({ state: value });
|
|
165
|
+
subscriber.next(value);
|
|
166
|
+
},
|
|
167
|
+
error: (error) => {
|
|
168
|
+
ended = "errored";
|
|
169
|
+
subscriber.error(error);
|
|
170
|
+
},
|
|
171
|
+
complete: () => {
|
|
172
|
+
ended = "completed";
|
|
173
|
+
subscriber.complete();
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
return () => {
|
|
177
|
+
subscription.unsubscribe();
|
|
178
|
+
if (--holders === 0) settleFirstEmission({ end: ended ?? "unheld" });
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
const whenLive = async (signal) => {
|
|
182
|
+
if (signal?.aborted) return { end: "aborted" };
|
|
183
|
+
if (ended) return { end: ended };
|
|
184
|
+
if (holders === 0) return { end: "unheld" };
|
|
185
|
+
if (emittedRun === run) return { state: latestValue };
|
|
186
|
+
return new Promise((resolve) => {
|
|
187
|
+
const settle = (liveness) => {
|
|
188
|
+
firstEmissionWaiters.delete(settle);
|
|
189
|
+
signal?.removeEventListener("abort", onAbort);
|
|
190
|
+
resolve(liveness);
|
|
191
|
+
};
|
|
192
|
+
const onAbort = () => settle({ end: "aborted" });
|
|
193
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
194
|
+
firstEmissionWaiters.add(settle);
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
return {
|
|
198
|
+
observable$,
|
|
199
|
+
whenLive
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
//#endregion
|
|
203
|
+
//#region src/useStateItem.ts
|
|
204
|
+
const isSameItemIgnoringClosures = (a, b) => {
|
|
205
|
+
const previous = a;
|
|
206
|
+
const next = b;
|
|
207
|
+
const keys = Object.keys(previous);
|
|
208
|
+
return keys.length === Object.keys(next).length && keys.every((key) => Object.hasOwn(next, key) && (Object.is(previous[key], next[key]) || typeof previous[key] === "function" && typeof next[key] === "function"));
|
|
209
|
+
};
|
|
210
|
+
const useStateItem = (hookName, key, select) => {
|
|
211
|
+
const store = useKheopskitExternalStore(hookName);
|
|
212
|
+
const snapshots = (0, react.useMemo)(() => {
|
|
213
|
+
let cached;
|
|
214
|
+
return {
|
|
215
|
+
getSnapshot: () => {
|
|
216
|
+
const item = select(store.getSnapshot(), key);
|
|
217
|
+
if (!(cached !== void 0 && item !== void 0 && isSameItemIgnoringClosures(cached, item))) cached = item;
|
|
218
|
+
return cached;
|
|
219
|
+
},
|
|
220
|
+
getServerSnapshot: () => select(store.getServerSnapshot(), key)
|
|
221
|
+
};
|
|
222
|
+
}, [
|
|
223
|
+
store,
|
|
224
|
+
key,
|
|
225
|
+
select
|
|
226
|
+
]);
|
|
227
|
+
return (0, react.useSyncExternalStore)(store.subscribe, snapshots.getSnapshot, snapshots.getServerSnapshot);
|
|
228
|
+
};
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/useAccount.ts
|
|
231
|
+
const selectAccount = (state, accountId) => state.accounts.find((account) => account.id === accountId);
|
|
232
|
+
/**
|
|
233
|
+
* Returns the account with this id, or `undefined` while the state lists none.
|
|
234
|
+
* Re-renders only when that account changes: its name, its placeholder flag,
|
|
235
|
+
* a platform field such as an Ethereum `chainId`, or an SDK handle.
|
|
236
|
+
*
|
|
237
|
+
* Pass the platform tuple as a type argument for the SDK-precise account type,
|
|
238
|
+
* or use the pre-typed hook from {@link createKheopskit}.
|
|
239
|
+
*/
|
|
240
|
+
const useAccount = (accountId) => useStateItem("useAccount", accountId, selectAccount);
|
|
241
|
+
//#endregion
|
|
242
|
+
//#region src/useWallet.ts
|
|
243
|
+
const selectWallet = (state, walletId) => state.wallets.find((wallet) => wallet.id === walletId);
|
|
244
|
+
/**
|
|
245
|
+
* Returns the wallet with this id, or `undefined` while the state lists none.
|
|
246
|
+
* Re-renders only when that wallet changes: its status, connection, error,
|
|
247
|
+
* name or any other field. Its `connect` and `disconnect` act on the instance's
|
|
248
|
+
* pipeline when they are called, also right after an `<Activity>` reveal
|
|
249
|
+
* restarts it and while another provider or a `kheopskit$` subscriber holds
|
|
250
|
+
* it, and reject `ABORTED` while nothing holds it.
|
|
251
|
+
*
|
|
252
|
+
* Pass the platform tuple as a type argument for the SDK-precise wallet type,
|
|
253
|
+
* or use the pre-typed hook from {@link createKheopskit}.
|
|
254
|
+
*/
|
|
255
|
+
const useWallet = (walletId) => useStateItem("useWallet", walletId, selectWallet);
|
|
256
|
+
//#endregion
|
|
112
257
|
//#region src/useWallets.ts
|
|
113
258
|
/**
|
|
114
|
-
* Returns the current kheopskit state (wallets, accounts, config, isHydrating)
|
|
259
|
+
* Returns the current kheopskit state (wallets, accounts, config, isHydrating)
|
|
260
|
+
* and re-renders on every change. To follow one wallet or account, prefer
|
|
261
|
+
* {@link useWallet} or {@link useAccount}. Each wallet's `connect` and
|
|
262
|
+
* `disconnect` act on the instance's pipeline when they are called, while
|
|
263
|
+
* anything holds it.
|
|
115
264
|
*
|
|
116
265
|
* Pass the platform tuple as a type argument to recover SDK-precise account and
|
|
117
266
|
* wallet types — `useWallets<typeof platforms>()`. React contexts can't be
|
|
@@ -119,44 +268,116 @@ const KheopskitProvider = ({ children, config, ssrCookies }) => {
|
|
|
119
268
|
* (SDK-free) wallet/account shapes.
|
|
120
269
|
*/
|
|
121
270
|
const useWallets = () => {
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
return ctx.state;
|
|
271
|
+
const store = useKheopskitExternalStore("useWallets");
|
|
272
|
+
return (0, react.useSyncExternalStore)(store.subscribe, store.getSnapshot, store.getServerSnapshot);
|
|
125
273
|
};
|
|
126
274
|
//#endregion
|
|
127
275
|
//#region src/createKheopskit.tsx
|
|
276
|
+
const deferredStore = (claim) => ({
|
|
277
|
+
observable: (0, rxjs.defer)(() => claim().observable),
|
|
278
|
+
rememberWallet: (walletId) => claim().rememberWallet(walletId),
|
|
279
|
+
forgetWallet: (walletId) => claim().forgetWallet(walletId),
|
|
280
|
+
getCachedState: () => claim().getCachedState(),
|
|
281
|
+
setCachedState: (wallets, accounts) => claim().setCachedState(wallets, accounts),
|
|
282
|
+
destroy: () => claim().destroy()
|
|
283
|
+
});
|
|
128
284
|
/**
|
|
129
|
-
* Binds a platform tuple once and returns
|
|
130
|
-
*
|
|
131
|
-
*
|
|
285
|
+
* Binds a platform tuple once and returns one kheopskit instance: a
|
|
286
|
+
* `KheopskitProvider`, hooks already typed to those platforms, and the
|
|
287
|
+
* `kheopskit$` stream and `store` the provider uses, for code outside React.
|
|
288
|
+
* Subscribing `kheopskit$` while the provider is mounted runs no second
|
|
289
|
+
* pipeline, and holds the one pipeline for every hook action of the instance.
|
|
290
|
+
*
|
|
291
|
+
* Without `options.store`, the store is created on first use in the browser:
|
|
292
|
+
* backed by cookies when the provider renders first with `ssrCookies`, by
|
|
293
|
+
* localStorage otherwise. The caller owns `store.destroy()`.
|
|
132
294
|
*
|
|
133
295
|
* @example
|
|
134
296
|
* ```tsx
|
|
135
297
|
* // kheopskit.ts
|
|
136
|
-
* export const { KheopskitProvider, useWallets,
|
|
137
|
-
* platforms: [polkadot(), ethereum(), solana()]
|
|
138
|
-
* });
|
|
298
|
+
* export const { KheopskitProvider, useWallets, useWallet, kheopskit$ } =
|
|
299
|
+
* createKheopskit({ platforms: [polkadot(), ethereum(), solana()] });
|
|
139
300
|
*
|
|
140
301
|
* // anywhere
|
|
141
302
|
* const { accounts } = useWallets(); // accounts are platform-precise, no generic
|
|
142
303
|
* ```
|
|
143
304
|
*/
|
|
144
|
-
const createKheopskit = (config) => {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
305
|
+
const createKheopskit = (config, options = {}) => {
|
|
306
|
+
let claimed;
|
|
307
|
+
const claimStore = (ssrCookies) => {
|
|
308
|
+
claimed ?? (claimed = options.store ?? (0, _kheopskit_core.createKheopskitStore)({
|
|
309
|
+
ssrCookies,
|
|
310
|
+
storageKey: (0, _kheopskit_core.resolveConfig)(config).storageKey
|
|
311
|
+
}));
|
|
312
|
+
return claimed;
|
|
313
|
+
};
|
|
314
|
+
const shared$ = (0, rxjs.defer)(() => (0, _kheopskit_core.getKheopskit$)(config, { store: claimStore() })).pipe((0, rxjs.shareReplay)({
|
|
315
|
+
refCount: true,
|
|
316
|
+
bufferSize: 1
|
|
317
|
+
}));
|
|
318
|
+
const pipeline = createLivePipeline(shared$);
|
|
319
|
+
const kheopskit$ = pipeline.observable$;
|
|
320
|
+
const Provider = ({ children, ssrCookies }) => {
|
|
321
|
+
if (typeof window !== "undefined") claimStore(ssrCookies);
|
|
322
|
+
const resolveClientSource = (0, react.useCallback)(() => ({
|
|
323
|
+
store: claimStore(ssrCookies),
|
|
324
|
+
pipeline
|
|
325
|
+
}), [ssrCookies]);
|
|
326
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(KheopskitSourceProvider, {
|
|
327
|
+
config,
|
|
328
|
+
ssrCookies,
|
|
329
|
+
resolveClientSource,
|
|
330
|
+
children
|
|
331
|
+
});
|
|
332
|
+
};
|
|
150
333
|
Provider.displayName = "KheopskitProvider";
|
|
151
334
|
return {
|
|
152
335
|
KheopskitProvider: Provider,
|
|
336
|
+
/** The state stream the provider subscribes to, shared with code outside React. */
|
|
337
|
+
kheopskit$,
|
|
338
|
+
/** The store `kheopskit$` reads the cache from and persists into. */
|
|
339
|
+
store: options.store ?? deferredStore(claimStore),
|
|
153
340
|
/** Current state, typed to the bound platform tuple. */
|
|
154
341
|
useWallets: () => useWallets(),
|
|
155
342
|
/** Current accounts, typed to the bound platform tuple. */
|
|
156
|
-
useAccounts: () => useWallets().accounts
|
|
343
|
+
useAccounts: () => useWallets().accounts,
|
|
344
|
+
/** The wallet with this id, typed to the bound platform tuple. */
|
|
345
|
+
useWallet: (walletId) => useWallet(walletId),
|
|
346
|
+
/** The account with this id, typed to the bound platform tuple. */
|
|
347
|
+
useAccount: (accountId) => useAccount(accountId)
|
|
157
348
|
};
|
|
158
349
|
};
|
|
159
350
|
//#endregion
|
|
351
|
+
//#region src/KheopskitProvider.tsx
|
|
352
|
+
const KheopskitProvider = ({ children, config, ssrCookies, store }) => {
|
|
353
|
+
const lastConfigRef = (0, react.useRef)(config);
|
|
354
|
+
const hasMountedRef = (0, react.useRef)(false);
|
|
355
|
+
(0, react.useEffect)(() => {
|
|
356
|
+
if (hasMountedRef.current && lastConfigRef.current !== config && (typeof process === "undefined" || process.env?.NODE_ENV !== "production")) console.warn("[kheopskit] KheopskitProvider received a new `config` reference; this recreates the store and re-subscribes on every render. Pass a referentially stable config (module scope, useMemo, or createKheopskit()).");
|
|
357
|
+
hasMountedRef.current = true;
|
|
358
|
+
lastConfigRef.current = config;
|
|
359
|
+
}, [config]);
|
|
360
|
+
const resolveClientSource = (0, react.useCallback)((resolvedConfig) => {
|
|
361
|
+
const sourceStore = store ?? (0, _kheopskit_core.createKheopskitStore)({
|
|
362
|
+
ssrCookies,
|
|
363
|
+
storageKey: resolvedConfig.storageKey
|
|
364
|
+
});
|
|
365
|
+
return {
|
|
366
|
+
store: sourceStore,
|
|
367
|
+
pipeline: createLivePipeline((0, _kheopskit_core.getKheopskit$)(resolvedConfig, {
|
|
368
|
+
ssrCookies,
|
|
369
|
+
store: sourceStore
|
|
370
|
+
}))
|
|
371
|
+
};
|
|
372
|
+
}, [ssrCookies, store]);
|
|
373
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(KheopskitSourceProvider, {
|
|
374
|
+
config,
|
|
375
|
+
ssrCookies,
|
|
376
|
+
resolveClientSource,
|
|
377
|
+
children
|
|
378
|
+
});
|
|
379
|
+
};
|
|
380
|
+
//#endregion
|
|
160
381
|
//#region src/useAccounts.ts
|
|
161
382
|
/**
|
|
162
383
|
* Convenience hook returning just the accounts from kheopskit state. Pass the
|
|
@@ -166,9 +387,65 @@ const createKheopskit = (config) => {
|
|
|
166
387
|
*/
|
|
167
388
|
const useAccounts = () => useWallets().accounts;
|
|
168
389
|
//#endregion
|
|
390
|
+
//#region src/useConnect.ts
|
|
391
|
+
const useIsomorphicLayoutEffect = typeof window === "undefined" ? react.useEffect : react.useLayoutEffect;
|
|
392
|
+
const afterThisCommitsLayoutEffects = () => Promise.resolve();
|
|
393
|
+
const createWalletActions = (store, walletId) => {
|
|
394
|
+
let scope = new AbortController();
|
|
395
|
+
const wallet = walletActions(store, walletId);
|
|
396
|
+
return {
|
|
397
|
+
connect: async () => {
|
|
398
|
+
if (scope.signal.aborted) await afterThisCommitsLayoutEffects();
|
|
399
|
+
const { signal } = scope;
|
|
400
|
+
if (signal.aborted) throw new _kheopskit_core.KheopskitError("ABORTED", `connect of wallet ${walletId} started after useConnect unmounted or switched wallet`, { walletId });
|
|
401
|
+
try {
|
|
402
|
+
await wallet.connect({ signal });
|
|
403
|
+
} catch (error) {
|
|
404
|
+
if (!(signal.aborted && error instanceof _kheopskit_core.KheopskitError && error.code === "ABORTED")) throw error;
|
|
405
|
+
}
|
|
406
|
+
},
|
|
407
|
+
disconnect: wallet.disconnect,
|
|
408
|
+
renew: () => {
|
|
409
|
+
if (scope.signal.aborted) scope = new AbortController();
|
|
410
|
+
},
|
|
411
|
+
abort: () => scope.abort()
|
|
412
|
+
};
|
|
413
|
+
};
|
|
414
|
+
/**
|
|
415
|
+
* Connect and disconnect actions for one wallet, with its status and error.
|
|
416
|
+
* `connect` and `disconnect` keep their identity while `walletId` is the same,
|
|
417
|
+
* so `useEffect(() => { connect() }, [connect])` connects on mount and after
|
|
418
|
+
* every `<Activity>` reveal.
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* ```tsx
|
|
422
|
+
* const ConnectButton = ({ walletId }: { walletId: WalletId }) => {
|
|
423
|
+
* const { connect, disconnect, status } = useConnect(walletId);
|
|
424
|
+
* if (status === "connected") return <button onClick={disconnect}>Disconnect</button>;
|
|
425
|
+
* return <button disabled={status === "connecting"} onClick={connect}>Connect</button>;
|
|
426
|
+
* };
|
|
427
|
+
* ```
|
|
428
|
+
*/
|
|
429
|
+
const useConnect = (walletId) => {
|
|
430
|
+
const store = useKheopskitExternalStore("useConnect");
|
|
431
|
+
const wallet = useWallet(walletId);
|
|
432
|
+
const actions = (0, react.useMemo)(() => createWalletActions(store, walletId), [store, walletId]);
|
|
433
|
+
useIsomorphicLayoutEffect(() => actions.renew(), [actions]);
|
|
434
|
+
(0, react.useEffect)(() => () => actions.abort(), [actions]);
|
|
435
|
+
return {
|
|
436
|
+
connect: actions.connect,
|
|
437
|
+
disconnect: actions.disconnect,
|
|
438
|
+
status: wallet?.status,
|
|
439
|
+
error: wallet?.error
|
|
440
|
+
};
|
|
441
|
+
};
|
|
442
|
+
//#endregion
|
|
169
443
|
exports.KheopskitProvider = KheopskitProvider;
|
|
170
444
|
exports.createKheopskit = createKheopskit;
|
|
445
|
+
exports.useAccount = useAccount;
|
|
171
446
|
exports.useAccounts = useAccounts;
|
|
447
|
+
exports.useConnect = useConnect;
|
|
448
|
+
exports.useWallet = useWallet;
|
|
172
449
|
exports.useWallets = useWallets;
|
|
173
450
|
|
|
174
451
|
//# sourceMappingURL=index.js.map
|