@solana/react 7.0.0 → 7.1.0-canary-20260812160611

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.
Files changed (63) hide show
  1. package/README.md +117 -39
  2. package/dist/index.browser.cjs +64 -0
  3. package/dist/index.browser.cjs.map +1 -1
  4. package/dist/index.browser.mjs +58 -1
  5. package/dist/index.browser.mjs.map +1 -1
  6. package/dist/index.native.mjs +58 -1
  7. package/dist/index.native.mjs.map +1 -1
  8. package/dist/index.node.cjs +64 -0
  9. package/dist/index.node.cjs.map +1 -1
  10. package/dist/index.node.mjs +58 -1
  11. package/dist/index.node.mjs.map +1 -1
  12. package/dist/query.browser.cjs +5 -52
  13. package/dist/query.browser.cjs.map +1 -1
  14. package/dist/query.browser.mjs +5 -52
  15. package/dist/query.browser.mjs.map +1 -1
  16. package/dist/query.native.mjs +5 -52
  17. package/dist/query.native.mjs.map +1 -1
  18. package/dist/query.node.cjs +5 -52
  19. package/dist/query.node.cjs.map +1 -1
  20. package/dist/query.node.mjs +5 -52
  21. package/dist/query.node.mjs.map +1 -1
  22. package/dist/types/index.d.ts +7 -0
  23. package/dist/types/index.d.ts.map +1 -1
  24. package/dist/types/query/useSubscriptionQuery.d.ts +5 -5
  25. package/dist/types/query/useSubscriptionQuery.d.ts.map +1 -1
  26. package/dist/types/query/useTrackedDataQuery.d.ts +2 -2
  27. package/dist/types/query/useTrackedDataQuery.d.ts.map +1 -1
  28. package/dist/types/useAirdrop.d.ts +40 -0
  29. package/dist/types/useAirdrop.d.ts.map +1 -0
  30. package/dist/types/useClient.d.ts +7 -6
  31. package/dist/types/useClient.d.ts.map +1 -1
  32. package/dist/types/useIdentity.d.ts +28 -0
  33. package/dist/types/useIdentity.d.ts.map +1 -0
  34. package/dist/types/usePayer.d.ts +27 -0
  35. package/dist/types/usePayer.d.ts.map +1 -0
  36. package/dist/types/usePlanTransaction.d.ts +25 -0
  37. package/dist/types/usePlanTransaction.d.ts.map +1 -0
  38. package/dist/types/usePlanTransactions.d.ts +26 -0
  39. package/dist/types/usePlanTransactions.d.ts.map +1 -0
  40. package/dist/types/useSendTransaction.d.ts +29 -0
  41. package/dist/types/useSendTransaction.d.ts.map +1 -0
  42. package/dist/types/useSendTransactions.d.ts +28 -0
  43. package/dist/types/useSendTransactions.d.ts.map +1 -0
  44. package/package.json +10 -10
  45. package/src/index.ts +7 -0
  46. package/src/query/__tests__/useRequestQuery-test.browser.tsx +2 -2
  47. package/src/query/__tests__/useSubscriptionQuery-test.browser.tsx +7 -9
  48. package/src/query/__tests__/useTrackedDataQuery-test.browser.tsx +3 -3
  49. package/src/query/useSubscriptionQuery.ts +9 -7
  50. package/src/query/useTrackedDataQuery.ts +9 -4
  51. package/src/swr/__tests__/useRequestSWR-test.browser.tsx +4 -6
  52. package/src/useAirdrop.ts +45 -0
  53. package/src/useClient.ts +7 -6
  54. package/src/useIdentity.ts +47 -0
  55. package/src/usePayer.ts +44 -0
  56. package/src/usePlanTransaction.ts +30 -0
  57. package/src/usePlanTransactions.ts +31 -0
  58. package/src/useSendTransaction.ts +34 -0
  59. package/src/useSendTransactions.ts +33 -0
  60. package/dist/types/query/bridgeStoreToAsyncIterable.d.ts +0 -39
  61. package/dist/types/query/bridgeStoreToAsyncIterable.d.ts.map +0 -1
  62. package/src/query/__tests__/bridgeStoreToAsyncIterable-test.ts +0 -188
  63. package/src/query/bridgeStoreToAsyncIterable.ts +0 -105
@@ -1,188 +0,0 @@
1
- import {
2
- isSolanaError,
3
- type ReactiveState,
4
- type ReactiveStreamStore,
5
- SOLANA_ERROR__REACT__SUBSCRIPTION_CLOSED_WITHOUT_ERROR,
6
- } from '@solana/kit';
7
-
8
- import { bridgeStoreToAsyncIterable } from '../bridgeStoreToAsyncIterable';
9
-
10
- function createFakeStore<T>(): {
11
- connectCount: () => number;
12
- emit: (state: ReactiveState<T>) => void;
13
- listenerCount: () => number;
14
- resetCount: () => number;
15
- store: ReactiveStreamStore<T>;
16
- withSignalArg: () => AbortSignal | undefined;
17
- } {
18
- let state: ReactiveState<T> = { data: undefined, error: undefined, status: 'idle' };
19
- const listeners = new Set<() => void>();
20
- let connects = 0;
21
- let resets = 0;
22
- let withSignalArg: AbortSignal | undefined;
23
- const store: ReactiveStreamStore<T> = {
24
- // The bridge connects via `withSignal(signal).connect()`, never the bare `connect()` — fail
25
- // loudly if it reaches for the unbound one.
26
- connect: jest.fn().mockImplementation(() => {
27
- throw new Error('not implemented');
28
- }),
29
- getState: () => state,
30
- reset: () => {
31
- resets++;
32
- },
33
- subscribe: (callback: () => void) => {
34
- listeners.add(callback);
35
- return () => {
36
- listeners.delete(callback);
37
- };
38
- },
39
- withSignal: (signal: AbortSignal) => {
40
- withSignalArg = signal;
41
- return {
42
- connect: () => {
43
- connects++;
44
- },
45
- };
46
- },
47
- };
48
- return {
49
- connectCount: () => connects,
50
- emit: (next: ReactiveState<T>) => {
51
- state = next;
52
- listeners.forEach(l => l());
53
- },
54
- listenerCount: () => listeners.size,
55
- resetCount: () => resets,
56
- store,
57
- withSignalArg: () => withSignalArg,
58
- };
59
- }
60
-
61
- describe('bridgeStoreToAsyncIterable', () => {
62
- it('subscribes to and connects the store bound to the signal', () => {
63
- const fake = createFakeStore<number>();
64
- const ctrl = new AbortController();
65
- const it = bridgeStoreToAsyncIterable(fake.store, ctrl.signal)[Symbol.asyncIterator]();
66
- // The generator body runs synchronously up to its first `await`, so subscribe + connect have
67
- // happened by the time `next()` returns its (still-pending) promise.
68
- void it.next();
69
- expect(fake.listenerCount()).toBe(1);
70
- expect(fake.connectCount()).toBe(1);
71
- expect(fake.withSignalArg()).toBe(ctrl.signal);
72
- });
73
-
74
- it('yields a loaded value', async () => {
75
- expect.assertions(1);
76
- const fake = createFakeStore<number>();
77
- const it = bridgeStoreToAsyncIterable(fake.store, new AbortController().signal)[Symbol.asyncIterator]();
78
- const pull = it.next();
79
- fake.emit({ data: 42, error: undefined, status: 'loaded' });
80
- await expect(pull).resolves.toEqual({ done: false, value: 42 });
81
- });
82
-
83
- it('yields successive loaded values across pulls', async () => {
84
- expect.assertions(2);
85
- const fake = createFakeStore<number>();
86
- const it = bridgeStoreToAsyncIterable(fake.store, new AbortController().signal)[Symbol.asyncIterator]();
87
-
88
- const first = it.next();
89
- fake.emit({ data: 1, error: undefined, status: 'loaded' });
90
- await expect(first).resolves.toEqual({ done: false, value: 1 });
91
-
92
- const second = it.next();
93
- fake.emit({ data: 2, error: undefined, status: 'loaded' });
94
- await expect(second).resolves.toEqual({ done: false, value: 2 });
95
- });
96
-
97
- it('drops intermediate values (latest-wins) when several land between pulls', async () => {
98
- expect.assertions(1);
99
- const fake = createFakeStore<number>();
100
- const it = bridgeStoreToAsyncIterable(fake.store, new AbortController().signal)[Symbol.asyncIterator]();
101
- const pull = it.next();
102
- // Three notifications arrive before the consumer pulls — only the freshest survives.
103
- fake.emit({ data: 1, error: undefined, status: 'loaded' });
104
- fake.emit({ data: 2, error: undefined, status: 'loaded' });
105
- fake.emit({ data: 3, error: undefined, status: 'loaded' });
106
- await expect(pull).resolves.toEqual({ done: false, value: 3 });
107
- });
108
-
109
- it('ignores idle and loading states', async () => {
110
- expect.assertions(1);
111
- const fake = createFakeStore<number>();
112
- const it = bridgeStoreToAsyncIterable(fake.store, new AbortController().signal)[Symbol.asyncIterator]();
113
- const pull = it.next();
114
- fake.emit({ data: undefined, error: undefined, status: 'loading' });
115
- fake.emit({ data: undefined, error: undefined, status: 'idle' });
116
- // Neither carried a value; the first *loaded* is what the consumer sees.
117
- fake.emit({ data: 7, error: undefined, status: 'loaded' });
118
- await expect(pull).resolves.toEqual({ done: false, value: 7 });
119
- });
120
-
121
- it('throws on a store error and resets the store', async () => {
122
- expect.assertions(2);
123
- const fake = createFakeStore<number>();
124
- const it = bridgeStoreToAsyncIterable(fake.store, new AbortController().signal)[Symbol.asyncIterator]();
125
- const pull = it.next();
126
- const boom = new Error('boom');
127
- fake.emit({ data: undefined, error: boom, status: 'error' });
128
- await expect(pull).rejects.toBe(boom);
129
- expect(fake.resetCount()).toBe(1);
130
- });
131
-
132
- it('drops a buffered value when an error arrives before the next pull', async () => {
133
- expect.assertions(2);
134
- const fake = createFakeStore<number>();
135
- const it = bridgeStoreToAsyncIterable(fake.store, new AbortController().signal)[Symbol.asyncIterator]();
136
- const pull = it.next();
137
- // A value lands, then an error arrives before the consumer pulls again. Error wins: the
138
- // buffered value is dropped (once errored, stop yielding). Pins the failure-before-latest
139
- // precedence so a refactor can't silently invert it.
140
- const boom = new Error('boom');
141
- fake.emit({ data: 1, error: undefined, status: 'loaded' });
142
- fake.emit({ data: undefined, error: boom, status: 'error' });
143
- await expect(pull).rejects.toBe(boom);
144
- expect(fake.resetCount()).toBe(1);
145
- });
146
-
147
- it('substitutes a sentinel when the store errors with a nullish payload', async () => {
148
- expect.assertions(1);
149
- const fake = createFakeStore<number>();
150
- const it = bridgeStoreToAsyncIterable(fake.store, new AbortController().signal)[Symbol.asyncIterator]();
151
- const pull = it.next();
152
- fake.emit({ data: undefined, error: undefined, status: 'error' });
153
- await pull.then(
154
- () => {
155
- throw new Error('expected the pull to reject');
156
- },
157
- error => {
158
- expect(isSolanaError(error, SOLANA_ERROR__REACT__SUBSCRIPTION_CLOSED_WITHOUT_ERROR)).toBe(true);
159
- },
160
- );
161
- });
162
-
163
- it('ends cleanly and resets the store when the signal aborts', async () => {
164
- expect.assertions(3);
165
- const fake = createFakeStore<number>();
166
- const ctrl = new AbortController();
167
- const it = bridgeStoreToAsyncIterable(fake.store, ctrl.signal)[Symbol.asyncIterator]();
168
- const pull = it.next();
169
- ctrl.abort();
170
- // An abort is teardown, not failure: the iterable completes (`done`) rather than rejecting.
171
- await expect(pull).resolves.toEqual({ done: true, value: undefined });
172
- expect(fake.resetCount()).toBe(1);
173
- expect(fake.listenerCount()).toBe(0);
174
- });
175
-
176
- it('unsubscribes and resets the store when the consumer stops early', async () => {
177
- expect.assertions(2);
178
- const fake = createFakeStore<number>();
179
- const it = bridgeStoreToAsyncIterable(fake.store, new AbortController().signal)[Symbol.asyncIterator]();
180
- const pull = it.next();
181
- fake.emit({ data: 1, error: undefined, status: 'loaded' });
182
- await pull;
183
-
184
- await it.return!(undefined);
185
- expect(fake.resetCount()).toBe(1);
186
- expect(fake.listenerCount()).toBe(0);
187
- });
188
- });
@@ -1,105 +0,0 @@
1
- import {
2
- type ReactiveStreamStore,
3
- SOLANA_ERROR__REACT__SUBSCRIPTION_CLOSED_WITHOUT_ERROR,
4
- SolanaError,
5
- } from '@solana/kit';
6
-
7
- /**
8
- * Bridges a {@link ReactiveStreamStore} into the `AsyncIterable` contract that TanStack Query's
9
- * `experimental_streamedQuery` consumes. The TanStack twin of `bridgeStoreToSwr`: where the SWR
10
- * bridge pushes each store update into a `next` callback, this one exposes a *pull*-based async
11
- * iterable, since `streamedQuery` drives the stream by `for await`-ing it.
12
- *
13
- * Subscribes to the store, `connect()`s it bound to `signal`, and yields its unified lifecycle:
14
- * - `loaded` → yields the value, unless an optional `shouldYield` predicate rejects it. Latest-wins:
15
- * if several notifications land between pulls, only the most recent unconsumed value is yielded (a
16
- * subscription consumer wants the freshest state, not a backlog).
17
- * - `error` → throws, so the `streamedQuery` queryFn rejects and the query surfaces `error`.
18
- * Substitutes a {@link SOLANA_ERROR__REACT__SUBSCRIPTION_CLOSED_WITHOUT_ERROR} sentinel when the
19
- * store reports an error with a nullish payload (mirrors `bridgeStoreToSwr`). An error takes
20
- * precedence over a buffered value: if a `loaded` value is still pending when an `error` arrives,
21
- * that value is dropped and the error propagates (once errored, stop yielding).
22
- * - `signal` aborts → ends the iterable cleanly (no error). An abort means teardown — unmount, a
23
- * superseding refetch, or a caller-supplied deadline — so the subscription stops rather than
24
- * failing.
25
- *
26
- * Whichever way the iterable ends — value exhaustion, error, or abort — the `finally` block
27
- * unsubscribes and `reset()`s the store, aborting the active connection. The store the caller hands
28
- * in is owned by this bridge for the iterable's lifetime.
29
- *
30
- * @typeParam T - The notification type emitted by the store.
31
- *
32
- * @param store - An idle stream store to drive.
33
- * @param signal - The abort signal to bind the connection to (TanStack's query-cancellation signal,
34
- * optionally combined with a caller-supplied one). Aborting it tears the stream down.
35
- * @param shouldYield - Optional gate run against each `loaded` value before it is yielded. Return
36
- * `false` to drop the value. When omitted, every loaded value is yielded.
37
- *
38
- * @returns An `AsyncIterable<T>` that yields each store value until the store errors or `signal`
39
- * aborts.
40
- *
41
- * @internal
42
- */
43
- export function bridgeStoreToAsyncIterable<T>(
44
- store: ReactiveStreamStore<T>,
45
- signal: AbortSignal,
46
- shouldYield?: (value: T) => boolean,
47
- ): AsyncIterable<T> {
48
- return {
49
- async *[Symbol.asyncIterator](): AsyncIterator<T> {
50
- // Latest-wins single-slot buffer plus a one-shot "something changed" deferred the loop
51
- // parks on. `wake()` resolves the current deferred and arms a fresh one for the next park.
52
- let latest: { readonly value: T } | undefined;
53
- let failure: { readonly error: unknown } | undefined;
54
- let deferred = Promise.withResolvers<void>();
55
- const wake = () => {
56
- const { resolve } = deferred;
57
- deferred = Promise.withResolvers<void>();
58
- resolve();
59
- };
60
-
61
- const onChange = () => {
62
- const state = store.getState();
63
- if (state.status === 'loaded') {
64
- // Drop a value the gate rejects. The stream parks again rather than yielding it.
65
- if (shouldYield && !shouldYield(state.data)) return;
66
- latest = { value: state.data };
67
- wake();
68
- } else if (state.status === 'error') {
69
- // A nullish error would otherwise surface as a value-less success; substitute a
70
- // sentinel so the failure propagates, matching `bridgeStoreToSwr`.
71
- failure = {
72
- error: state.error ?? new SolanaError(SOLANA_ERROR__REACT__SUBSCRIPTION_CLOSED_WITHOUT_ERROR),
73
- };
74
- wake();
75
- }
76
- // `idle` / `loading` carry no value and no error — nothing to yield.
77
- };
78
-
79
- const onAbort = () => wake();
80
- signal.addEventListener('abort', onAbort, { once: true });
81
- const unsubscribe = store.subscribe(onChange);
82
- // Bind the connection to `signal` so an abort tears down the underlying stream too.
83
- store.withSignal(signal).connect();
84
- try {
85
- while (true) {
86
- // Abort wins over everything: an abort is teardown, so end cleanly without
87
- // surfacing the store's incidental abort-driven error state.
88
- if (signal.aborted) return;
89
- if (failure) throw failure.error;
90
- if (latest) {
91
- const { value } = latest;
92
- latest = undefined;
93
- yield value;
94
- continue;
95
- }
96
- await deferred.promise;
97
- }
98
- } finally {
99
- signal.removeEventListener('abort', onAbort);
100
- unsubscribe();
101
- store.reset();
102
- }
103
- },
104
- };
105
- }