@solana/react-hooks 0.2.0 → 0.2.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.
@@ -236,6 +236,38 @@ function createAccountSelector(key) {
236
236
  return (state) => key ? state.accounts[key] : void 0;
237
237
  }
238
238
  __name(createAccountSelector, "createAccountSelector");
239
+ function useSuspenseFetcher(config) {
240
+ const preference = useQuerySuspensePreference();
241
+ const suspenseEnabled = Boolean(preference) && config.enabled;
242
+ const pendingRef = react.useRef(null);
243
+ react.useEffect(() => {
244
+ if (!suspenseEnabled) {
245
+ pendingRef.current = null;
246
+ return;
247
+ }
248
+ if (pendingRef.current && pendingRef.current.key !== config.key) {
249
+ pendingRef.current = null;
250
+ }
251
+ }, [config.key, suspenseEnabled]);
252
+ if (pendingRef.current && pendingRef.current.key !== config.key) {
253
+ pendingRef.current = null;
254
+ }
255
+ if (suspenseEnabled && config.key && !config.ready) {
256
+ if (!pendingRef.current) {
257
+ const promise = config.fetcher();
258
+ pendingRef.current = {
259
+ key: config.key,
260
+ promise: promise.finally(() => {
261
+ if (pendingRef.current?.promise === promise) {
262
+ pendingRef.current = null;
263
+ }
264
+ })
265
+ };
266
+ }
267
+ throw pendingRef.current.promise;
268
+ }
269
+ }
270
+ __name(useSuspenseFetcher, "useSuspenseFetcher");
239
271
  function useClusterState() {
240
272
  const selector = react.useMemo(createClusterSelector, []);
241
273
  return useClientStore(selector);
@@ -315,6 +347,7 @@ __name(useSolTransfer, "useSolTransfer");
315
347
  function useSplToken(mint, options = {}) {
316
348
  const client$1 = useSolanaClient();
317
349
  const session = useWalletSession();
350
+ const suspense = Boolean(useQuerySuspensePreference());
318
351
  const normalizedMint = react.useMemo(() => String(mint), [mint]);
319
352
  const helperConfig = react.useMemo(
320
353
  () => ({
@@ -335,7 +368,8 @@ function useSplToken(mint, options = {}) {
335
368
  return helper.fetchBalance(owner, options.commitment);
336
369
  }, [helper, owner, options.commitment]);
337
370
  const { data, error, isLoading, isValidating, mutate } = useSWR__default.default(balanceKey, fetchBalance, {
338
- revalidateOnFocus: options.revalidateOnFocus ?? false
371
+ revalidateOnFocus: options.revalidateOnFocus ?? false,
372
+ suspense
339
373
  });
340
374
  const sessionRef = react.useRef(session);
341
375
  react.useEffect(() => {
@@ -408,12 +442,23 @@ function useAccount(addressLike, options = {}) {
408
442
  const accountKey = react.useMemo(() => address2?.toString(), [address2]);
409
443
  const selector = react.useMemo(() => createAccountSelector(accountKey), [accountKey]);
410
444
  const account = useClientStore(selector);
445
+ useSuspenseFetcher({
446
+ enabled: options.fetch !== false && !shouldSkip && Boolean(address2),
447
+ fetcher: /* @__PURE__ */ __name(() => {
448
+ if (!address2) {
449
+ throw new Error("Provide an address before fetching account data.");
450
+ }
451
+ return client$1.actions.fetchAccount(address2, options.commitment);
452
+ }, "fetcher"),
453
+ key: accountKey ?? null,
454
+ ready: account !== void 0
455
+ });
411
456
  react.useEffect(() => {
412
457
  if (!address2) {
413
458
  return;
414
459
  }
415
460
  const commitment = options.commitment;
416
- if (options.fetch !== false) {
461
+ if (options.fetch !== false && account === void 0) {
417
462
  void client$1.actions.fetchAccount(address2, commitment).catch(() => void 0);
418
463
  }
419
464
  if (options.watch) {
@@ -423,7 +468,7 @@ function useAccount(addressLike, options = {}) {
423
468
  };
424
469
  }
425
470
  return void 0;
426
- }, [address2, client$1, options.commitment, options.fetch, options.watch]);
471
+ }, [account, address2, client$1, options.commitment, options.fetch, options.watch]);
427
472
  return account;
428
473
  }
429
474
  __name(useAccount, "useAccount");
@@ -448,12 +493,23 @@ function useBalance(addressLike, options = {}) {
448
493
  const accountKey = react.useMemo(() => address2?.toString(), [address2]);
449
494
  const selector = react.useMemo(() => createAccountSelector(accountKey), [accountKey]);
450
495
  const account = useClientStore(selector);
496
+ useSuspenseFetcher({
497
+ enabled: mergedOptions.fetch !== false && !shouldSkip && Boolean(address2),
498
+ fetcher: /* @__PURE__ */ __name(() => {
499
+ if (!address2) {
500
+ throw new Error("Provide an address before fetching balance.");
501
+ }
502
+ return client$1.actions.fetchBalance(address2, mergedOptions.commitment);
503
+ }, "fetcher"),
504
+ key: accountKey ?? null,
505
+ ready: account !== void 0
506
+ });
451
507
  react.useEffect(() => {
452
508
  if (!address2) {
453
509
  return;
454
510
  }
455
511
  const commitment = mergedOptions.commitment;
456
- if (mergedOptions.fetch !== false) {
512
+ if (mergedOptions.fetch !== false && account === void 0) {
457
513
  void client$1.actions.fetchBalance(address2, commitment).catch(() => void 0);
458
514
  }
459
515
  if (mergedOptions.watch) {
@@ -463,7 +519,7 @@ function useBalance(addressLike, options = {}) {
463
519
  };
464
520
  }
465
521
  return void 0;
466
- }, [address2, client$1, mergedOptions.commitment, mergedOptions.fetch, mergedOptions.watch]);
522
+ }, [account, address2, client$1, mergedOptions.commitment, mergedOptions.fetch, mergedOptions.watch]);
467
523
  const lamports = account?.lamports ?? null;
468
524
  const fetching = account?.fetching ?? false;
469
525
  const slot = account?.slot;