1- import type { App , Ref } from 'vue' ;
2- import { getCurrentInstance , inject , provide , isRef , shallowRef } from 'vue' ;
1+ import { type App , getCurrentScope , type Ref } from 'vue' ;
2+ import { inject , provide , isRef , shallowRef } from 'vue' ;
33import type { ClientOptions } from '@urql/core' ;
44import { Client } from '@urql/core' ;
55
6- const clientsPerInstance = new WeakMap < { } , Ref < Client > > ( ) ;
6+ // WeakMap to store client instances as fallback when client is provided and used in the same component
7+ const clientsPerScope = new WeakMap < { } , Ref < Client > > ( ) ;
78
8- /** Provides a {@link Client} to a component’s children.
9+ /** Provides a {@link Client} to a component and it ’s children.
910 *
1011 * @param opts - {@link ClientOptions}, a {@link Client}, or a reactive ref object of a `Client`.
1112 *
@@ -19,18 +20,16 @@ const clientsPerInstance = new WeakMap<{}, Ref<Client>>();
1920 *
2021 * @example
2122 * ```ts
22- * import { provideClient } from '@urql/vue';
23- * // All of `@urql/core` is also re-exported by `@urql/vue`:
24- * import { Client, cacheExchange, fetchExchange } from '@urql/core';
23+ * <script setup>
24+ * import { provideClient } from '@urql/vue';
25+ * // All of `@urql/core` is also re-exported by `@urql/vue`:
26+ * import { Client, cacheExchange, fetchExchange } from '@urql/core';
2527 *
26- * export default {
27- * setup() {
28- * provideClient(new Client({
29- * url: 'https://API',
30- * exchanges: [cacheExchange, fetchExchange],
31- * }));
32- * },
33- * };
28+ * provideClient(new Client({
29+ * url: 'https://API',
30+ * exchanges: [cacheExchange, fetchExchange],
31+ * }));
32+ * </script>
3433 * ```
3534 */
3635export function provideClient ( opts : ClientOptions | Client | Ref < Client > ) {
@@ -41,9 +40,9 @@ export function provideClient(opts: ClientOptions | Client | Ref<Client>) {
4140 client = opts ;
4241 }
4342
44- const instance = getCurrentInstance ( ) ;
45- if ( instance ) {
46- clientsPerInstance . set ( instance , client ) ;
43+ const scope = getCurrentScope ( ) ;
44+ if ( scope ) {
45+ clientsPerScope . set ( scope , client ) ;
4746 }
4847
4948 provide ( '$urql' , client ) ;
@@ -88,26 +87,26 @@ export function install(app: App, opts: ClientOptions | Client | Ref<Client>) {
8887/** Returns a provided reactive ref object of a {@link Client}.
8988 *
9089 * @remarks
91- * `useClient` may be called in Vue `setup` functions to retrieve a
92- * reactive rev object of a {@link Client} that’s previously been
90+ * `useClient` may be called in a reactive context to retrieve a
91+ * reactive ref object of a {@link Client} that’s previously been
9392 * provided with {@link provideClient} in the current or a parent’s
9493 * `setup` function.
9594 *
9695 * @throws
97- * In development, if `useClient` is called outside of a Vue `setup`
98- * function or no {@link Client} was provided, an error will be thrown.
96+ * In development, if `useClient` is called outside of a reactive context
97+ * or no {@link Client} was provided, an error will be thrown.
9998 */
10099export function useClient ( ) : Ref < Client > {
101- const instance = getCurrentInstance ( ) ;
102- if ( process . env . NODE_ENV !== 'production' && ! instance ) {
100+ const scope = getCurrentScope ( ) ;
101+ if ( process . env . NODE_ENV !== 'production' && ! scope ) {
103102 throw new Error (
104- 'use* functions may only be called during the `setup()` or other lifecycle hooks .'
103+ 'use* function must be called within a reactive context (component setup, composable, or effect scope) .'
105104 ) ;
106105 }
107106
108107 let client = inject ( '$urql' ) as Ref < Client > | undefined ;
109- if ( ! client && instance ) {
110- client = clientsPerInstance . get ( instance ) ;
108+ if ( ! client ) {
109+ client = clientsPerScope . get ( scope ! ) ;
111110 }
112111
113112 if ( process . env . NODE_ENV !== 'production' && ! client ) {
0 commit comments