@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.
@@ -230,6 +230,38 @@ function createAccountSelector(key) {
230
230
  return (state) => key ? state.accounts[key] : void 0;
231
231
  }
232
232
  __name(createAccountSelector, "createAccountSelector");
233
+ function useSuspenseFetcher(config) {
234
+ const preference = useQuerySuspensePreference();
235
+ const suspenseEnabled = Boolean(preference) && config.enabled;
236
+ const pendingRef = useRef(null);
237
+ useEffect(() => {
238
+ if (!suspenseEnabled) {
239
+ pendingRef.current = null;
240
+ return;
241
+ }
242
+ if (pendingRef.current && pendingRef.current.key !== config.key) {
243
+ pendingRef.current = null;
244
+ }
245
+ }, [config.key, suspenseEnabled]);
246
+ if (pendingRef.current && pendingRef.current.key !== config.key) {
247
+ pendingRef.current = null;
248
+ }
249
+ if (suspenseEnabled && config.key && !config.ready) {
250
+ if (!pendingRef.current) {
251
+ const promise = config.fetcher();
252
+ pendingRef.current = {
253
+ key: config.key,
254
+ promise: promise.finally(() => {
255
+ if (pendingRef.current?.promise === promise) {
256
+ pendingRef.current = null;
257
+ }
258
+ })
259
+ };
260
+ }
261
+ throw pendingRef.current.promise;
262
+ }
263
+ }
264
+ __name(useSuspenseFetcher, "useSuspenseFetcher");
233
265
  function useClusterState() {
234
266
  const selector = useMemo(createClusterSelector, []);
235
267
  return useClientStore(selector);
@@ -309,6 +341,7 @@ __name(useSolTransfer, "useSolTransfer");
309
341
  function useSplToken(mint, options = {}) {
310
342
  const client = useSolanaClient();
311
343
  const session = useWalletSession();
344
+ const suspense = Boolean(useQuerySuspensePreference());
312
345
  const normalizedMint = useMemo(() => String(mint), [mint]);
313
346
  const helperConfig = useMemo(
314
347
  () => ({
@@ -329,7 +362,8 @@ function useSplToken(mint, options = {}) {
329
362
  return helper.fetchBalance(owner, options.commitment);
330
363
  }, [helper, owner, options.commitment]);
331
364
  const { data, error, isLoading, isValidating, mutate } = useSWR(balanceKey, fetchBalance, {
332
- revalidateOnFocus: options.revalidateOnFocus ?? false
365
+ revalidateOnFocus: options.revalidateOnFocus ?? false,
366
+ suspense
333
367
  });
334
368
  const sessionRef = useRef(session);
335
369
  useEffect(() => {
@@ -402,12 +436,23 @@ function useAccount(addressLike, options = {}) {
402
436
  const accountKey = useMemo(() => address2?.toString(), [address2]);
403
437
  const selector = useMemo(() => createAccountSelector(accountKey), [accountKey]);
404
438
  const account = useClientStore(selector);
439
+ useSuspenseFetcher({
440
+ enabled: options.fetch !== false && !shouldSkip && Boolean(address2),
441
+ fetcher: /* @__PURE__ */ __name(() => {
442
+ if (!address2) {
443
+ throw new Error("Provide an address before fetching account data.");
444
+ }
445
+ return client.actions.fetchAccount(address2, options.commitment);
446
+ }, "fetcher"),
447
+ key: accountKey ?? null,
448
+ ready: account !== void 0
449
+ });
405
450
  useEffect(() => {
406
451
  if (!address2) {
407
452
  return;
408
453
  }
409
454
  const commitment = options.commitment;
410
- if (options.fetch !== false) {
455
+ if (options.fetch !== false && account === void 0) {
411
456
  void client.actions.fetchAccount(address2, commitment).catch(() => void 0);
412
457
  }
413
458
  if (options.watch) {
@@ -417,7 +462,7 @@ function useAccount(addressLike, options = {}) {
417
462
  };
418
463
  }
419
464
  return void 0;
420
- }, [address2, client, options.commitment, options.fetch, options.watch]);
465
+ }, [account, address2, client, options.commitment, options.fetch, options.watch]);
421
466
  return account;
422
467
  }
423
468
  __name(useAccount, "useAccount");
@@ -442,12 +487,23 @@ function useBalance(addressLike, options = {}) {
442
487
  const accountKey = useMemo(() => address2?.toString(), [address2]);
443
488
  const selector = useMemo(() => createAccountSelector(accountKey), [accountKey]);
444
489
  const account = useClientStore(selector);
490
+ useSuspenseFetcher({
491
+ enabled: mergedOptions.fetch !== false && !shouldSkip && Boolean(address2),
492
+ fetcher: /* @__PURE__ */ __name(() => {
493
+ if (!address2) {
494
+ throw new Error("Provide an address before fetching balance.");
495
+ }
496
+ return client.actions.fetchBalance(address2, mergedOptions.commitment);
497
+ }, "fetcher"),
498
+ key: accountKey ?? null,
499
+ ready: account !== void 0
500
+ });
445
501
  useEffect(() => {
446
502
  if (!address2) {
447
503
  return;
448
504
  }
449
505
  const commitment = mergedOptions.commitment;
450
- if (mergedOptions.fetch !== false) {
506
+ if (mergedOptions.fetch !== false && account === void 0) {
451
507
  void client.actions.fetchBalance(address2, commitment).catch(() => void 0);
452
508
  }
453
509
  if (mergedOptions.watch) {
@@ -457,7 +513,7 @@ function useBalance(addressLike, options = {}) {
457
513
  };
458
514
  }
459
515
  return void 0;
460
- }, [address2, client, mergedOptions.commitment, mergedOptions.fetch, mergedOptions.watch]);
516
+ }, [account, address2, client, mergedOptions.commitment, mergedOptions.fetch, mergedOptions.watch]);
461
517
  const lamports = account?.lamports ?? null;
462
518
  const fetching = account?.fetching ?? false;
463
519
  const slot = account?.slot;