@metamask-previews/react-data-query 0.2.1-preview-9feca08d3 → 0.2.1-preview-9c0a7053d
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 +0 -2
- package/dist/createUIQueryClient.cjs +5 -28
- package/dist/createUIQueryClient.cjs.map +1 -1
- package/dist/createUIQueryClient.d.cts +8 -42
- package/dist/createUIQueryClient.d.cts.map +1 -1
- package/dist/createUIQueryClient.d.mts +8 -42
- package/dist/createUIQueryClient.d.mts.map +1 -1
- package/dist/createUIQueryClient.mjs +5 -28
- package/dist/createUIQueryClient.mjs.map +1 -1
- package/package.json +4 -8
package/CHANGELOG.md
CHANGED
|
@@ -9,8 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Changed
|
|
11
11
|
|
|
12
|
-
- **BREAKING:** Constrain `createUIQueryClient`'s messenger-like type to match the given data services ([#9475](https://github.com/MetaMask/core/pull/9475))
|
|
13
|
-
- The messenger that `createUIQueryClient` takes must now at least support calling actions or `:cacheUpdated:${hash}` events, as long as they are namespaced by the given data service names.
|
|
14
12
|
- Bump `@metamask/utils` from `^11.9.0` to `^11.11.0` ([#9074](https://github.com/MetaMask/core/pull/9074))
|
|
15
13
|
- Make `react-dom` and `react-native` peer dependencies optional ([#9295](https://github.com/MetaMask/core/pull/9295))
|
|
16
14
|
|
|
@@ -7,34 +7,12 @@ const query_core_1 = require("@tanstack/query-core");
|
|
|
7
7
|
* Create a QueryClient queries and subscribes to data services using the messenger.
|
|
8
8
|
*
|
|
9
9
|
* @param dataServices - A list of data services.
|
|
10
|
-
* @param messenger - A messenger
|
|
11
|
-
* 1. The messenger must at least support `call`, `subscribe` and `unsubscribe`.
|
|
12
|
-
* 2. Its actions are expected to be asynchronous.
|
|
13
|
-
* 3. Its action handler arguments and event payloads are expected to be JSON-compatible.
|
|
14
|
-
* 4. It must support actions and granular cache update events that belong to `dataServices`.
|
|
10
|
+
* @param messenger - A messenger adapter.
|
|
15
11
|
* @param config - Optional query client configuration options.
|
|
16
12
|
* @returns The QueryClient.
|
|
17
13
|
*/
|
|
18
14
|
function createUIQueryClient(dataServices, messenger, config = {}) {
|
|
19
15
|
const subscriptions = new Map();
|
|
20
|
-
/**
|
|
21
|
-
* Check whether a name is one of the provided data service names.
|
|
22
|
-
*
|
|
23
|
-
* @param service - The service name to check.
|
|
24
|
-
* @returns Whether the service name is configured.
|
|
25
|
-
*/
|
|
26
|
-
function isRecognizedDataService(service) {
|
|
27
|
-
return dataServices.some((dataService) => dataService === service);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Check whether an action belongs to one of the provided data services.
|
|
31
|
-
*
|
|
32
|
-
* @param action - The action name to check.
|
|
33
|
-
* @returns Whether the action belongs to a configured data service.
|
|
34
|
-
*/
|
|
35
|
-
function isRecognizedDataServiceAction(action) {
|
|
36
|
-
return isRecognizedDataService(action.split(':')[0]);
|
|
37
|
-
}
|
|
38
16
|
/**
|
|
39
17
|
* Parse a query key to detect a service name.
|
|
40
18
|
*
|
|
@@ -47,7 +25,7 @@ function createUIQueryClient(dataServices, messenger, config = {}) {
|
|
|
47
25
|
return null;
|
|
48
26
|
}
|
|
49
27
|
const service = action.split(':')[0];
|
|
50
|
-
if (!
|
|
28
|
+
if (!dataServices.includes(service)) {
|
|
51
29
|
return null;
|
|
52
30
|
}
|
|
53
31
|
return service;
|
|
@@ -60,7 +38,8 @@ function createUIQueryClient(dataServices, messenger, config = {}) {
|
|
|
60
38
|
queryFn: async (options) => {
|
|
61
39
|
const { queryKey } = options;
|
|
62
40
|
const action = queryKey[0];
|
|
63
|
-
(0, utils_1.assert)(typeof action === 'string' &&
|
|
41
|
+
(0, utils_1.assert)(typeof action === 'string' &&
|
|
42
|
+
dataServices.includes(action.split(':')?.[0]), "Queries must call actions on the messenger provided to createUIQueryClient, e.g. `queryKey: ['ExampleDataService:getAssets', ...]`.");
|
|
64
43
|
return await messenger.call(action, ...options.queryKey.slice(1), options.pageParam);
|
|
65
44
|
},
|
|
66
45
|
},
|
|
@@ -93,9 +72,7 @@ function createUIQueryClient(dataServices, messenger, config = {}) {
|
|
|
93
72
|
observerCount === 0 &&
|
|
94
73
|
hasSubscription) {
|
|
95
74
|
const subscriptionListener = subscriptions.get(hash);
|
|
96
|
-
|
|
97
|
-
messenger.unsubscribe(`${service}:cacheUpdated:${hash}`, subscriptionListener);
|
|
98
|
-
}
|
|
75
|
+
messenger.unsubscribe(`${service}:cacheUpdated:${hash}`, subscriptionListener);
|
|
99
76
|
subscriptions.delete(hash);
|
|
100
77
|
}
|
|
101
78
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createUIQueryClient.cjs","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":";;;AAIA,2CAAyC;AACzC,qDAS8B;AAqD9B;;;;;;;;;;;GAWG;AACH,SAAgB,mBAAmB,CACjC,YAA8B,EAC9B,SAAqD,EACrD,SAA4B,EAAE;IAE9B,MAAM,aAAa,GAAG,IAAI,GAAG,EAG1B,CAAC;IAEJ;;;;;OAKG;IACH,SAAS,uBAAuB,CAC9B,OAAe;QAEf,OAAO,YAAY,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,WAAW,KAAK,OAAO,CAAC,CAAC;IACrE,CAAC;IAED;;;;;OAKG;IACH,SAAS,6BAA6B,CACpC,MAAc;QAEd,OAAO,uBAAuB,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACvD,CAAC;IAED;;;;;OAKG;IACH,SAAS,aAAa,CAAC,QAAkB;QACvC,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;QAE3B,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;YAC/B,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,OAAO,GAAG,MAAM,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AAM,CAAC,aAAa,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAExD,MAAM,QAAQ,GAAG;YACf,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC;SAClE,CAAC;QAEF,MAAM,OAAO,CAAC,GAAG,CACf,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;YAC7B,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,OAAO,IAAI,CAAC;YACd,CAAC;YAED,OAAO,SAAS,CAAC,IAAI,CAAC,GAAG,OAAO,oBAAoB,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QAC1E,CAAC,CAAC,CACH,CAAC;QAEF,OAAO,kBAAkB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC;AAlKD,kDAkKC","sourcesContent":["import type {\n DataServiceGranularCacheUpdatedEvent,\n DataServiceGranularCacheUpdatedPayload,\n} from '@metamask/base-data-service';\nimport { assert } from '@metamask/utils';\nimport {\n hydrate,\n QueryClient,\n InvalidateQueryFilters,\n InvalidateOptions,\n OmitKeyof,\n parseFilterArgs,\n QueryKey,\n QueryClientConfig,\n} from '@tanstack/query-core';\n\n/**\n * Handles granular cache update events emitted by data services.\n */\ntype DataServiceGranularCacheUpdatedHandler = (\n payload: DataServiceGranularCacheUpdatedPayload,\n) => void;\n\n/**\n * The minimum messenger shape needed by `createUIQueryClient`. This messenger\n * has some restrictions:\n *\n * 1. The messenger must at least support `call`, `subscribe` and `unsubscribe`.\n * 2. Its actions are expected to be asynchronous.\n * 3. Its action handler arguments and event payloads are expected to be JSON-compatible.\n * 4. It must support actions and granular cache update events that belong to the designated data services.\n *\n * @template DataServiceName - A union of the configured data service names.\n */\ntype MessengerAdapter<DataServiceName extends string> = {\n /**\n * Call an action on one of the configured data services.\n *\n * Note: The parameters are typed as `unknown[]` rather than `Json[]`.\n * Concrete messengers declare each action's parameters as a fixed-length\n * tuple, and a variadic `Json[]` is not assignable to a fixed-length tuple,\n * so using `Json[]` here would reject otherwise valid messengers.\n */\n call(\n actionType: `${DataServiceName}:${string}`,\n ...params: unknown[]\n ): Promise<unknown>;\n\n /**\n * Subscribe to a granular cache update event on one of the configured data\n * services.\n */\n subscribe(\n eventType: DataServiceGranularCacheUpdatedEvent<DataServiceName>['type'],\n handler: DataServiceGranularCacheUpdatedHandler,\n ): void;\n\n /**\n * Unsubscribe from a granular cache update event on one of the configured\n * data services.\n */\n unsubscribe(\n eventType: DataServiceGranularCacheUpdatedEvent<DataServiceName>['type'],\n handler: DataServiceGranularCacheUpdatedHandler,\n ): void;\n};\n\n/**\n * Create a QueryClient queries and subscribes to data services using the messenger.\n *\n * @param dataServices - A list of data services.\n * @param messenger - A messenger-like object, with some restrictions:\n * 1. The messenger must at least support `call`, `subscribe` and `unsubscribe`.\n * 2. Its actions are expected to be asynchronous.\n * 3. Its action handler arguments and event payloads are expected to be JSON-compatible.\n * 4. It must support actions and granular cache update events that belong to `dataServices`.\n * @param config - Optional query client configuration options.\n * @returns The QueryClient.\n */\nexport function createUIQueryClient<DataServiceNames extends readonly string[]>(\n dataServices: DataServiceNames,\n messenger: MessengerAdapter<DataServiceNames[number]>,\n config: QueryClientConfig = {},\n): QueryClient {\n const subscriptions = new Map<\n string,\n DataServiceGranularCacheUpdatedHandler\n >();\n\n /**\n * Check whether a name is one of the provided data service names.\n *\n * @param service - The service name to check.\n * @returns Whether the service name is configured.\n */\n function isRecognizedDataService(\n service: string,\n ): service is DataServiceNames[number] {\n return dataServices.some((dataService) => dataService === service);\n }\n\n /**\n * Check whether an action belongs to one of the provided data services.\n *\n * @param action - The action name to check.\n * @returns Whether the action belongs to a configured data service.\n */\n function isRecognizedDataServiceAction(\n action: string,\n ): action is `${DataServiceNames[number]}:${string}` {\n return isRecognizedDataService(action.split(':')[0]);\n }\n\n /**\n * Parse a query key to detect a service name.\n *\n * @param queryKey - The query key.\n * @returns The service name if it parsing succeeded, otherwise null.\n */\n function parseQueryKey(queryKey: QueryKey): DataServiceNames[number] | null {\n const action = queryKey[0];\n\n if (typeof action !== 'string') {\n return null;\n }\n\n const service = action.split(':')[0];\n\n if (!isRecognizedDataService(service)) {\n return null;\n }\n\n return service;\n }\n\n const client: QueryClient = new QueryClient({\n ...config,\n defaultOptions: {\n queries: {\n ...config.defaultOptions?.queries,\n queryFn: async (options): Promise<unknown> => {\n const { queryKey } = options;\n\n const action = queryKey[0];\n\n assert(\n typeof action === 'string' && isRecognizedDataServiceAction(action),\n \"Queries must call actions on the messenger provided to createUIQueryClient, e.g. `queryKey: ['ExampleDataService:getAssets', ...]`.\",\n );\n\n return await messenger.call(\n action,\n ...options.queryKey.slice(1),\n options.pageParam,\n );\n },\n },\n mutations: config.defaultOptions?.mutations,\n },\n });\n\n const cache = client.getQueryCache();\n\n cache.subscribe((event) => {\n const { query } = event;\n\n const hash = query.queryHash;\n const hasSubscription = subscriptions.has(hash);\n const observerCount = query.getObserversCount();\n\n const service = parseQueryKey(query.queryKey);\n\n if (!service) {\n return;\n }\n\n if (\n !hasSubscription &&\n event.type === 'observerAdded' &&\n observerCount === 1\n ) {\n const cacheListener: DataServiceGranularCacheUpdatedHandler = (\n payload,\n ) => {\n if (payload.type === 'removed') {\n return;\n }\n\n hydrate(client, payload.state);\n };\n\n subscriptions.set(hash, cacheListener);\n messenger.subscribe(`${service}:cacheUpdated:${hash}`, cacheListener);\n } else if (\n event.type === 'observerRemoved' &&\n observerCount === 0 &&\n hasSubscription\n ) {\n const subscriptionListener = subscriptions.get(hash);\n\n if (subscriptionListener) {\n messenger.unsubscribe(\n `${service}:cacheUpdated:${hash}`,\n subscriptionListener,\n );\n }\n subscriptions.delete(hash);\n }\n });\n\n // Override invalidateQueries to ensure the data service is invalidated as well.\n const originalInvalidate = client.invalidateQueries.bind(client);\n\n // This function is defined in this way to have full support for all function overloads.\n client.invalidateQueries = async (\n arg1?: QueryKey | InvalidateQueryFilters,\n arg2?: OmitKeyof<InvalidateQueryFilters, 'queryKey'> | InvalidateOptions,\n arg3?: InvalidateOptions,\n ): Promise<void> => {\n const [filters, options] = parseFilterArgs(arg1, arg2, arg3);\n\n const queries = client.getQueryCache().findAll(filters);\n\n const services = [\n ...new Set(queries.map((query) => parseQueryKey(query.queryKey))),\n ];\n\n await Promise.all(\n services.map(async (service) => {\n if (!service) {\n return null;\n }\n\n return messenger.call(`${service}:invalidateQueries`, filters, options);\n }),\n );\n\n return originalInvalidate(filters, options);\n };\n\n return client;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"createUIQueryClient.cjs","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":";;;AACA,2CAA+C;AAC/C,qDAS8B;AAe9B;;;;;;;GAOG;AACH,SAAgB,mBAAmB,CACjC,YAAsB,EACtB,SAA2B,EAC3B,SAA4B,EAAE;IAE9B,MAAM,aAAa,GAAG,IAAI,GAAG,EAAgC,CAAC;IAE9D;;;;;OAKG;IACH,SAAS,aAAa,CAAC,QAAkB;QACvC,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;QAE3B,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;YAC/B,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QAErC,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;YACpC,OAAO,IAAI,CAAC;QACd,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,MAAM,MAAM,GAAgB,IAAI,wBAAW,CAAC;QAC1C,GAAG,MAAM;QACT,cAAc,EAAE;YACd,OAAO,EAAE;gBACP,GAAG,MAAM,CAAC,cAAc,EAAE,OAAO;gBACjC,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AO,EAAE,EAAE;YAC7B,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,OAAO,IAAI,CAAC;YACd,CAAC;YAED,OAAO,SAAS,CAAC,IAAI,CACnB,GAAG,OAAO,oBAAoB,EAC9B,OAAe,EACf,OAAe,CAChB,CAAC;QACJ,CAAC,CAAC,CACH,CAAC;QAEF,OAAO,kBAAkB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC;AA7ID,kDA6IC","sourcesContent":["import { DataServiceGranularCacheUpdatedPayload } from '@metamask/base-data-service';\nimport { assert, Json } from '@metamask/utils';\nimport {\n hydrate,\n QueryClient,\n InvalidateQueryFilters,\n InvalidateOptions,\n OmitKeyof,\n parseFilterArgs,\n QueryKey,\n QueryClientConfig,\n} from '@tanstack/query-core';\n\ntype SubscriptionCallback = (\n payload: DataServiceGranularCacheUpdatedPayload,\n) => void;\n\ntype JsonSubscriptionCallback = (data: Json) => void;\n\n// TODO: Figure out if we can replace with a better Messenger type\ntype MessengerAdapter = {\n call: (method: string, ...params: Json[]) => Promise<Json | void>;\n subscribe: (method: string, callback: JsonSubscriptionCallback) => void;\n unsubscribe: (method: string, callback: JsonSubscriptionCallback) => void;\n};\n\n/**\n * Create a QueryClient queries and subscribes to data services using the messenger.\n *\n * @param dataServices - A list of data services.\n * @param messenger - A messenger adapter.\n * @param config - Optional query client configuration options.\n * @returns The QueryClient.\n */\nexport function createUIQueryClient(\n dataServices: string[],\n messenger: MessengerAdapter,\n config: QueryClientConfig = {},\n): QueryClient {\n const subscriptions = new Map<string, SubscriptionCallback>();\n\n /**\n * Parse a query key to detect a service name.\n *\n * @param queryKey - The query key.\n * @returns The service name if it parsing succeeded, otherwise null.\n */\n function parseQueryKey(queryKey: QueryKey): string | null {\n const action = queryKey[0];\n\n if (typeof action !== 'string') {\n return null;\n }\n\n const service = action.split(':')[0];\n\n if (!dataServices.includes(service)) {\n return null;\n }\n\n return service;\n }\n\n const client: QueryClient = new QueryClient({\n ...config,\n defaultOptions: {\n queries: {\n ...config.defaultOptions?.queries,\n queryFn: async (options): Promise<unknown> => {\n const { queryKey } = options;\n\n const action = queryKey[0];\n\n assert(\n typeof action === 'string' &&\n dataServices.includes(action.split(':')?.[0]),\n \"Queries must call actions on the messenger provided to createUIQueryClient, e.g. `queryKey: ['ExampleDataService:getAssets', ...]`.\",\n );\n\n return await messenger.call(\n action,\n ...(options.queryKey.slice(1) as Json[]),\n options.pageParam,\n );\n },\n },\n mutations: config.defaultOptions?.mutations,\n },\n });\n\n const cache = client.getQueryCache();\n\n cache.subscribe((event) => {\n const { query } = event;\n\n const hash = query.queryHash;\n const hasSubscription = subscriptions.has(hash);\n const observerCount = query.getObserversCount();\n\n const service = parseQueryKey(query.queryKey);\n\n if (!service) {\n return;\n }\n\n if (\n !hasSubscription &&\n event.type === 'observerAdded' &&\n observerCount === 1\n ) {\n const cacheListener = (\n payload: DataServiceGranularCacheUpdatedPayload,\n ): void => {\n if (payload.type === 'removed') {\n return;\n }\n\n hydrate(client, payload.state);\n };\n\n subscriptions.set(hash, cacheListener);\n messenger.subscribe(\n `${service}:cacheUpdated:${hash}`,\n cacheListener as JsonSubscriptionCallback,\n );\n } else if (\n event.type === 'observerRemoved' &&\n observerCount === 0 &&\n hasSubscription\n ) {\n const subscriptionListener = subscriptions.get(hash);\n\n messenger.unsubscribe(\n `${service}:cacheUpdated:${hash}`,\n subscriptionListener as JsonSubscriptionCallback,\n );\n subscriptions.delete(hash);\n }\n });\n\n // Override invalidateQueries to ensure the data service is invalidated as well.\n const originalInvalidate = client.invalidateQueries.bind(client);\n\n // This function is defined in this way to have full support for all function overloads.\n client.invalidateQueries = async (\n arg1?: QueryKey | InvalidateQueryFilters,\n arg2?: OmitKeyof<InvalidateQueryFilters, 'queryKey'> | InvalidateOptions,\n arg3?: InvalidateOptions,\n ): Promise<void> => {\n const [filters, options] = parseFilterArgs(arg1, arg2, arg3);\n\n const queries = client.getQueryCache().findAll(filters);\n\n const services = [\n ...new Set(queries.map((query) => parseQueryKey(query.queryKey))),\n ];\n\n await Promise.all(\n services.map(async (service) => {\n if (!service) {\n return null;\n }\n\n return messenger.call(\n `${service}:invalidateQueries`,\n filters as Json,\n options as Json,\n );\n }),\n );\n\n return originalInvalidate(filters, options);\n };\n\n return client;\n}\n"]}
|
|
@@ -1,53 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Json } from "@metamask/utils";
|
|
2
2
|
import { QueryClient, QueryClientConfig } from "@tanstack/query-core";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* The minimum messenger shape needed by `createUIQueryClient`. This messenger
|
|
9
|
-
* has some restrictions:
|
|
10
|
-
*
|
|
11
|
-
* 1. The messenger must at least support `call`, `subscribe` and `unsubscribe`.
|
|
12
|
-
* 2. Its actions are expected to be asynchronous.
|
|
13
|
-
* 3. Its action handler arguments and event payloads are expected to be JSON-compatible.
|
|
14
|
-
* 4. It must support actions and granular cache update events that belong to the designated data services.
|
|
15
|
-
*
|
|
16
|
-
* @template DataServiceName - A union of the configured data service names.
|
|
17
|
-
*/
|
|
18
|
-
type MessengerAdapter<DataServiceName extends string> = {
|
|
19
|
-
/**
|
|
20
|
-
* Call an action on one of the configured data services.
|
|
21
|
-
*
|
|
22
|
-
* Note: The parameters are typed as `unknown[]` rather than `Json[]`.
|
|
23
|
-
* Concrete messengers declare each action's parameters as a fixed-length
|
|
24
|
-
* tuple, and a variadic `Json[]` is not assignable to a fixed-length tuple,
|
|
25
|
-
* so using `Json[]` here would reject otherwise valid messengers.
|
|
26
|
-
*/
|
|
27
|
-
call(actionType: `${DataServiceName}:${string}`, ...params: unknown[]): Promise<unknown>;
|
|
28
|
-
/**
|
|
29
|
-
* Subscribe to a granular cache update event on one of the configured data
|
|
30
|
-
* services.
|
|
31
|
-
*/
|
|
32
|
-
subscribe(eventType: DataServiceGranularCacheUpdatedEvent<DataServiceName>['type'], handler: DataServiceGranularCacheUpdatedHandler): void;
|
|
33
|
-
/**
|
|
34
|
-
* Unsubscribe from a granular cache update event on one of the configured
|
|
35
|
-
* data services.
|
|
36
|
-
*/
|
|
37
|
-
unsubscribe(eventType: DataServiceGranularCacheUpdatedEvent<DataServiceName>['type'], handler: DataServiceGranularCacheUpdatedHandler): void;
|
|
3
|
+
type JsonSubscriptionCallback = (data: Json) => void;
|
|
4
|
+
type MessengerAdapter = {
|
|
5
|
+
call: (method: string, ...params: Json[]) => Promise<Json | void>;
|
|
6
|
+
subscribe: (method: string, callback: JsonSubscriptionCallback) => void;
|
|
7
|
+
unsubscribe: (method: string, callback: JsonSubscriptionCallback) => void;
|
|
38
8
|
};
|
|
39
9
|
/**
|
|
40
10
|
* Create a QueryClient queries and subscribes to data services using the messenger.
|
|
41
11
|
*
|
|
42
12
|
* @param dataServices - A list of data services.
|
|
43
|
-
* @param messenger - A messenger
|
|
44
|
-
* 1. The messenger must at least support `call`, `subscribe` and `unsubscribe`.
|
|
45
|
-
* 2. Its actions are expected to be asynchronous.
|
|
46
|
-
* 3. Its action handler arguments and event payloads are expected to be JSON-compatible.
|
|
47
|
-
* 4. It must support actions and granular cache update events that belong to `dataServices`.
|
|
13
|
+
* @param messenger - A messenger adapter.
|
|
48
14
|
* @param config - Optional query client configuration options.
|
|
49
15
|
* @returns The QueryClient.
|
|
50
16
|
*/
|
|
51
|
-
export declare function createUIQueryClient
|
|
17
|
+
export declare function createUIQueryClient(dataServices: string[], messenger: MessengerAdapter, config?: QueryClientConfig): QueryClient;
|
|
52
18
|
export {};
|
|
53
19
|
//# sourceMappingURL=createUIQueryClient.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createUIQueryClient.d.cts","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createUIQueryClient.d.cts","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAU,IAAI,EAAE,wBAAwB;AAC/C,OAAO,EAEL,WAAW,EAMX,iBAAiB,EAClB,6BAA6B;AAM9B,KAAK,wBAAwB,GAAG,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;AAGrD,KAAK,gBAAgB,GAAG;IACtB,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,KAAK,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;IAClE,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,wBAAwB,KAAK,IAAI,CAAC;IACxE,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,wBAAwB,KAAK,IAAI,CAAC;CAC3E,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,gBAAgB,EAC3B,MAAM,GAAE,iBAAsB,GAC7B,WAAW,CAyIb"}
|
|
@@ -1,53 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Json } from "@metamask/utils";
|
|
2
2
|
import { QueryClient, QueryClientConfig } from "@tanstack/query-core";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* The minimum messenger shape needed by `createUIQueryClient`. This messenger
|
|
9
|
-
* has some restrictions:
|
|
10
|
-
*
|
|
11
|
-
* 1. The messenger must at least support `call`, `subscribe` and `unsubscribe`.
|
|
12
|
-
* 2. Its actions are expected to be asynchronous.
|
|
13
|
-
* 3. Its action handler arguments and event payloads are expected to be JSON-compatible.
|
|
14
|
-
* 4. It must support actions and granular cache update events that belong to the designated data services.
|
|
15
|
-
*
|
|
16
|
-
* @template DataServiceName - A union of the configured data service names.
|
|
17
|
-
*/
|
|
18
|
-
type MessengerAdapter<DataServiceName extends string> = {
|
|
19
|
-
/**
|
|
20
|
-
* Call an action on one of the configured data services.
|
|
21
|
-
*
|
|
22
|
-
* Note: The parameters are typed as `unknown[]` rather than `Json[]`.
|
|
23
|
-
* Concrete messengers declare each action's parameters as a fixed-length
|
|
24
|
-
* tuple, and a variadic `Json[]` is not assignable to a fixed-length tuple,
|
|
25
|
-
* so using `Json[]` here would reject otherwise valid messengers.
|
|
26
|
-
*/
|
|
27
|
-
call(actionType: `${DataServiceName}:${string}`, ...params: unknown[]): Promise<unknown>;
|
|
28
|
-
/**
|
|
29
|
-
* Subscribe to a granular cache update event on one of the configured data
|
|
30
|
-
* services.
|
|
31
|
-
*/
|
|
32
|
-
subscribe(eventType: DataServiceGranularCacheUpdatedEvent<DataServiceName>['type'], handler: DataServiceGranularCacheUpdatedHandler): void;
|
|
33
|
-
/**
|
|
34
|
-
* Unsubscribe from a granular cache update event on one of the configured
|
|
35
|
-
* data services.
|
|
36
|
-
*/
|
|
37
|
-
unsubscribe(eventType: DataServiceGranularCacheUpdatedEvent<DataServiceName>['type'], handler: DataServiceGranularCacheUpdatedHandler): void;
|
|
3
|
+
type JsonSubscriptionCallback = (data: Json) => void;
|
|
4
|
+
type MessengerAdapter = {
|
|
5
|
+
call: (method: string, ...params: Json[]) => Promise<Json | void>;
|
|
6
|
+
subscribe: (method: string, callback: JsonSubscriptionCallback) => void;
|
|
7
|
+
unsubscribe: (method: string, callback: JsonSubscriptionCallback) => void;
|
|
38
8
|
};
|
|
39
9
|
/**
|
|
40
10
|
* Create a QueryClient queries and subscribes to data services using the messenger.
|
|
41
11
|
*
|
|
42
12
|
* @param dataServices - A list of data services.
|
|
43
|
-
* @param messenger - A messenger
|
|
44
|
-
* 1. The messenger must at least support `call`, `subscribe` and `unsubscribe`.
|
|
45
|
-
* 2. Its actions are expected to be asynchronous.
|
|
46
|
-
* 3. Its action handler arguments and event payloads are expected to be JSON-compatible.
|
|
47
|
-
* 4. It must support actions and granular cache update events that belong to `dataServices`.
|
|
13
|
+
* @param messenger - A messenger adapter.
|
|
48
14
|
* @param config - Optional query client configuration options.
|
|
49
15
|
* @returns The QueryClient.
|
|
50
16
|
*/
|
|
51
|
-
export declare function createUIQueryClient
|
|
17
|
+
export declare function createUIQueryClient(dataServices: string[], messenger: MessengerAdapter, config?: QueryClientConfig): QueryClient;
|
|
52
18
|
export {};
|
|
53
19
|
//# sourceMappingURL=createUIQueryClient.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createUIQueryClient.d.mts","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createUIQueryClient.d.mts","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAU,IAAI,EAAE,wBAAwB;AAC/C,OAAO,EAEL,WAAW,EAMX,iBAAiB,EAClB,6BAA6B;AAM9B,KAAK,wBAAwB,GAAG,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;AAGrD,KAAK,gBAAgB,GAAG;IACtB,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,KAAK,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;IAClE,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,wBAAwB,KAAK,IAAI,CAAC;IACxE,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,wBAAwB,KAAK,IAAI,CAAC;CAC3E,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,gBAAgB,EAC3B,MAAM,GAAE,iBAAsB,GAC7B,WAAW,CAyIb"}
|
|
@@ -4,34 +4,12 @@ import { hydrate, QueryClient, parseFilterArgs } from "@tanstack/query-core";
|
|
|
4
4
|
* Create a QueryClient queries and subscribes to data services using the messenger.
|
|
5
5
|
*
|
|
6
6
|
* @param dataServices - A list of data services.
|
|
7
|
-
* @param messenger - A messenger
|
|
8
|
-
* 1. The messenger must at least support `call`, `subscribe` and `unsubscribe`.
|
|
9
|
-
* 2. Its actions are expected to be asynchronous.
|
|
10
|
-
* 3. Its action handler arguments and event payloads are expected to be JSON-compatible.
|
|
11
|
-
* 4. It must support actions and granular cache update events that belong to `dataServices`.
|
|
7
|
+
* @param messenger - A messenger adapter.
|
|
12
8
|
* @param config - Optional query client configuration options.
|
|
13
9
|
* @returns The QueryClient.
|
|
14
10
|
*/
|
|
15
11
|
export function createUIQueryClient(dataServices, messenger, config = {}) {
|
|
16
12
|
const subscriptions = new Map();
|
|
17
|
-
/**
|
|
18
|
-
* Check whether a name is one of the provided data service names.
|
|
19
|
-
*
|
|
20
|
-
* @param service - The service name to check.
|
|
21
|
-
* @returns Whether the service name is configured.
|
|
22
|
-
*/
|
|
23
|
-
function isRecognizedDataService(service) {
|
|
24
|
-
return dataServices.some((dataService) => dataService === service);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Check whether an action belongs to one of the provided data services.
|
|
28
|
-
*
|
|
29
|
-
* @param action - The action name to check.
|
|
30
|
-
* @returns Whether the action belongs to a configured data service.
|
|
31
|
-
*/
|
|
32
|
-
function isRecognizedDataServiceAction(action) {
|
|
33
|
-
return isRecognizedDataService(action.split(':')[0]);
|
|
34
|
-
}
|
|
35
13
|
/**
|
|
36
14
|
* Parse a query key to detect a service name.
|
|
37
15
|
*
|
|
@@ -44,7 +22,7 @@ export function createUIQueryClient(dataServices, messenger, config = {}) {
|
|
|
44
22
|
return null;
|
|
45
23
|
}
|
|
46
24
|
const service = action.split(':')[0];
|
|
47
|
-
if (!
|
|
25
|
+
if (!dataServices.includes(service)) {
|
|
48
26
|
return null;
|
|
49
27
|
}
|
|
50
28
|
return service;
|
|
@@ -57,7 +35,8 @@ export function createUIQueryClient(dataServices, messenger, config = {}) {
|
|
|
57
35
|
queryFn: async (options) => {
|
|
58
36
|
const { queryKey } = options;
|
|
59
37
|
const action = queryKey[0];
|
|
60
|
-
assert(typeof action === 'string' &&
|
|
38
|
+
assert(typeof action === 'string' &&
|
|
39
|
+
dataServices.includes(action.split(':')?.[0]), "Queries must call actions on the messenger provided to createUIQueryClient, e.g. `queryKey: ['ExampleDataService:getAssets', ...]`.");
|
|
61
40
|
return await messenger.call(action, ...options.queryKey.slice(1), options.pageParam);
|
|
62
41
|
},
|
|
63
42
|
},
|
|
@@ -90,9 +69,7 @@ export function createUIQueryClient(dataServices, messenger, config = {}) {
|
|
|
90
69
|
observerCount === 0 &&
|
|
91
70
|
hasSubscription) {
|
|
92
71
|
const subscriptionListener = subscriptions.get(hash);
|
|
93
|
-
|
|
94
|
-
messenger.unsubscribe(`${service}:cacheUpdated:${hash}`, subscriptionListener);
|
|
95
|
-
}
|
|
72
|
+
messenger.unsubscribe(`${service}:cacheUpdated:${hash}`, subscriptionListener);
|
|
96
73
|
subscriptions.delete(hash);
|
|
97
74
|
}
|
|
98
75
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createUIQueryClient.mjs","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAE,wBAAwB;AACzC,OAAO,EACL,OAAO,EACP,WAAW,EAIX,eAAe,EAGhB,6BAA6B;AAqD9B;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,mBAAmB,CACjC,YAA8B,EAC9B,SAAqD,EACrD,SAA4B,EAAE;IAE9B,MAAM,aAAa,GAAG,IAAI,GAAG,EAG1B,CAAC;IAEJ;;;;;OAKG;IACH,SAAS,uBAAuB,CAC9B,OAAe;QAEf,OAAO,YAAY,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,WAAW,KAAK,OAAO,CAAC,CAAC;IACrE,CAAC;IAED;;;;;OAKG;IACH,SAAS,6BAA6B,CACpC,MAAc;QAEd,OAAO,uBAAuB,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,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C,CAAC;IAEjE,wFAAwF;IACxF,MAAM,CAAC,iBAAiB,GAAG,KAAK,EAC9B,IAAwC,EACxC,IAAwE,EACxE,IAAwB,EACT,EAAE;QACjB,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAE7D,MAAM,OAAO,GAAG,MAAM,CAAC,aAAa,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAExD,MAAM,QAAQ,GAAG;YACf,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC;SAClE,CAAC;QAEF,MAAM,OAAO,CAAC,GAAG,CACf,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;YAC7B,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,OAAO,IAAI,CAAC;YACd,CAAC;YAED,OAAO,SAAS,CAAC,IAAI,CAAC,GAAG,OAAO,oBAAoB,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QAC1E,CAAC,CAAC,CACH,CAAC;QAEF,OAAO,kBAAkB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type {\n DataServiceGranularCacheUpdatedEvent,\n DataServiceGranularCacheUpdatedPayload,\n} from '@metamask/base-data-service';\nimport { assert } from '@metamask/utils';\nimport {\n hydrate,\n QueryClient,\n InvalidateQueryFilters,\n InvalidateOptions,\n OmitKeyof,\n parseFilterArgs,\n QueryKey,\n QueryClientConfig,\n} from '@tanstack/query-core';\n\n/**\n * Handles granular cache update events emitted by data services.\n */\ntype DataServiceGranularCacheUpdatedHandler = (\n payload: DataServiceGranularCacheUpdatedPayload,\n) => void;\n\n/**\n * The minimum messenger shape needed by `createUIQueryClient`. This messenger\n * has some restrictions:\n *\n * 1. The messenger must at least support `call`, `subscribe` and `unsubscribe`.\n * 2. Its actions are expected to be asynchronous.\n * 3. Its action handler arguments and event payloads are expected to be JSON-compatible.\n * 4. It must support actions and granular cache update events that belong to the designated data services.\n *\n * @template DataServiceName - A union of the configured data service names.\n */\ntype MessengerAdapter<DataServiceName extends string> = {\n /**\n * Call an action on one of the configured data services.\n *\n * Note: The parameters are typed as `unknown[]` rather than `Json[]`.\n * Concrete messengers declare each action's parameters as a fixed-length\n * tuple, and a variadic `Json[]` is not assignable to a fixed-length tuple,\n * so using `Json[]` here would reject otherwise valid messengers.\n */\n call(\n actionType: `${DataServiceName}:${string}`,\n ...params: unknown[]\n ): Promise<unknown>;\n\n /**\n * Subscribe to a granular cache update event on one of the configured data\n * services.\n */\n subscribe(\n eventType: DataServiceGranularCacheUpdatedEvent<DataServiceName>['type'],\n handler: DataServiceGranularCacheUpdatedHandler,\n ): void;\n\n /**\n * Unsubscribe from a granular cache update event on one of the configured\n * data services.\n */\n unsubscribe(\n eventType: DataServiceGranularCacheUpdatedEvent<DataServiceName>['type'],\n handler: DataServiceGranularCacheUpdatedHandler,\n ): void;\n};\n\n/**\n * Create a QueryClient queries and subscribes to data services using the messenger.\n *\n * @param dataServices - A list of data services.\n * @param messenger - A messenger-like object, with some restrictions:\n * 1. The messenger must at least support `call`, `subscribe` and `unsubscribe`.\n * 2. Its actions are expected to be asynchronous.\n * 3. Its action handler arguments and event payloads are expected to be JSON-compatible.\n * 4. It must support actions and granular cache update events that belong to `dataServices`.\n * @param config - Optional query client configuration options.\n * @returns The QueryClient.\n */\nexport function createUIQueryClient<DataServiceNames extends readonly string[]>(\n dataServices: DataServiceNames,\n messenger: MessengerAdapter<DataServiceNames[number]>,\n config: QueryClientConfig = {},\n): QueryClient {\n const subscriptions = new Map<\n string,\n DataServiceGranularCacheUpdatedHandler\n >();\n\n /**\n * Check whether a name is one of the provided data service names.\n *\n * @param service - The service name to check.\n * @returns Whether the service name is configured.\n */\n function isRecognizedDataService(\n service: string,\n ): service is DataServiceNames[number] {\n return dataServices.some((dataService) => dataService === service);\n }\n\n /**\n * Check whether an action belongs to one of the provided data services.\n *\n * @param action - The action name to check.\n * @returns Whether the action belongs to a configured data service.\n */\n function isRecognizedDataServiceAction(\n action: string,\n ): action is `${DataServiceNames[number]}:${string}` {\n return isRecognizedDataService(action.split(':')[0]);\n }\n\n /**\n * Parse a query key to detect a service name.\n *\n * @param queryKey - The query key.\n * @returns The service name if it parsing succeeded, otherwise null.\n */\n function parseQueryKey(queryKey: QueryKey): DataServiceNames[number] | null {\n const action = queryKey[0];\n\n if (typeof action !== 'string') {\n return null;\n }\n\n const service = action.split(':')[0];\n\n if (!isRecognizedDataService(service)) {\n return null;\n }\n\n return service;\n }\n\n const client: QueryClient = new QueryClient({\n ...config,\n defaultOptions: {\n queries: {\n ...config.defaultOptions?.queries,\n queryFn: async (options): Promise<unknown> => {\n const { queryKey } = options;\n\n const action = queryKey[0];\n\n assert(\n typeof action === 'string' && isRecognizedDataServiceAction(action),\n \"Queries must call actions on the messenger provided to createUIQueryClient, e.g. `queryKey: ['ExampleDataService:getAssets', ...]`.\",\n );\n\n return await messenger.call(\n action,\n ...options.queryKey.slice(1),\n options.pageParam,\n );\n },\n },\n mutations: config.defaultOptions?.mutations,\n },\n });\n\n const cache = client.getQueryCache();\n\n cache.subscribe((event) => {\n const { query } = event;\n\n const hash = query.queryHash;\n const hasSubscription = subscriptions.has(hash);\n const observerCount = query.getObserversCount();\n\n const service = parseQueryKey(query.queryKey);\n\n if (!service) {\n return;\n }\n\n if (\n !hasSubscription &&\n event.type === 'observerAdded' &&\n observerCount === 1\n ) {\n const cacheListener: DataServiceGranularCacheUpdatedHandler = (\n payload,\n ) => {\n if (payload.type === 'removed') {\n return;\n }\n\n hydrate(client, payload.state);\n };\n\n subscriptions.set(hash, cacheListener);\n messenger.subscribe(`${service}:cacheUpdated:${hash}`, cacheListener);\n } else if (\n event.type === 'observerRemoved' &&\n observerCount === 0 &&\n hasSubscription\n ) {\n const subscriptionListener = subscriptions.get(hash);\n\n if (subscriptionListener) {\n messenger.unsubscribe(\n `${service}:cacheUpdated:${hash}`,\n subscriptionListener,\n );\n }\n subscriptions.delete(hash);\n }\n });\n\n // Override invalidateQueries to ensure the data service is invalidated as well.\n const originalInvalidate = client.invalidateQueries.bind(client);\n\n // This function is defined in this way to have full support for all function overloads.\n client.invalidateQueries = async (\n arg1?: QueryKey | InvalidateQueryFilters,\n arg2?: OmitKeyof<InvalidateQueryFilters, 'queryKey'> | InvalidateOptions,\n arg3?: InvalidateOptions,\n ): Promise<void> => {\n const [filters, options] = parseFilterArgs(arg1, arg2, arg3);\n\n const queries = client.getQueryCache().findAll(filters);\n\n const services = [\n ...new Set(queries.map((query) => parseQueryKey(query.queryKey))),\n ];\n\n await Promise.all(\n services.map(async (service) => {\n if (!service) {\n return null;\n }\n\n return messenger.call(`${service}:invalidateQueries`, filters, options);\n }),\n );\n\n return originalInvalidate(filters, options);\n };\n\n return client;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"createUIQueryClient.mjs","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAQ,wBAAwB;AAC/C,OAAO,EACL,OAAO,EACP,WAAW,EAIX,eAAe,EAGhB,6BAA6B;AAe9B;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CACjC,YAAsB,EACtB,SAA2B,EAC3B,SAA4B,EAAE;IAE9B,MAAM,aAAa,GAAG,IAAI,GAAG,EAAgC,CAAC;IAE9D;;;;;OAKG;IACH,SAAS,aAAa,CAAC,QAAkB;QACvC,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;QAE3B,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;YAC/B,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC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CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAExD,MAAM,QAAQ,GAAG;YACf,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC;SAClE,CAAC;QAEF,MAAM,OAAO,CAAC,GAAG,CACf,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;YAC7B,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,OAAO,IAAI,CAAC;YACd,CAAC;YAED,OAAO,SAAS,CAAC,IAAI,CACnB,GAAG,OAAO,oBAAoB,EAC9B,OAAe,EACf,OAAe,CAChB,CAAC;QACJ,CAAC,CAAC,CACH,CAAC;QAEF,OAAO,kBAAkB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { DataServiceGranularCacheUpdatedPayload } from '@metamask/base-data-service';\nimport { assert, Json } from '@metamask/utils';\nimport {\n hydrate,\n QueryClient,\n InvalidateQueryFilters,\n InvalidateOptions,\n OmitKeyof,\n parseFilterArgs,\n QueryKey,\n QueryClientConfig,\n} from '@tanstack/query-core';\n\ntype SubscriptionCallback = (\n payload: DataServiceGranularCacheUpdatedPayload,\n) => void;\n\ntype JsonSubscriptionCallback = (data: Json) => void;\n\n// TODO: Figure out if we can replace with a better Messenger type\ntype MessengerAdapter = {\n call: (method: string, ...params: Json[]) => Promise<Json | void>;\n subscribe: (method: string, callback: JsonSubscriptionCallback) => void;\n unsubscribe: (method: string, callback: JsonSubscriptionCallback) => void;\n};\n\n/**\n * Create a QueryClient queries and subscribes to data services using the messenger.\n *\n * @param dataServices - A list of data services.\n * @param messenger - A messenger adapter.\n * @param config - Optional query client configuration options.\n * @returns The QueryClient.\n */\nexport function createUIQueryClient(\n dataServices: string[],\n messenger: MessengerAdapter,\n config: QueryClientConfig = {},\n): QueryClient {\n const subscriptions = new Map<string, SubscriptionCallback>();\n\n /**\n * Parse a query key to detect a service name.\n *\n * @param queryKey - The query key.\n * @returns The service name if it parsing succeeded, otherwise null.\n */\n function parseQueryKey(queryKey: QueryKey): string | null {\n const action = queryKey[0];\n\n if (typeof action !== 'string') {\n return null;\n }\n\n const service = action.split(':')[0];\n\n if (!dataServices.includes(service)) {\n return null;\n }\n\n return service;\n }\n\n const client: QueryClient = new QueryClient({\n ...config,\n defaultOptions: {\n queries: {\n ...config.defaultOptions?.queries,\n queryFn: async (options): Promise<unknown> => {\n const { queryKey } = options;\n\n const action = queryKey[0];\n\n assert(\n typeof action === 'string' &&\n dataServices.includes(action.split(':')?.[0]),\n \"Queries must call actions on the messenger provided to createUIQueryClient, e.g. `queryKey: ['ExampleDataService:getAssets', ...]`.\",\n );\n\n return await messenger.call(\n action,\n ...(options.queryKey.slice(1) as Json[]),\n options.pageParam,\n );\n },\n },\n mutations: config.defaultOptions?.mutations,\n },\n });\n\n const cache = client.getQueryCache();\n\n cache.subscribe((event) => {\n const { query } = event;\n\n const hash = query.queryHash;\n const hasSubscription = subscriptions.has(hash);\n const observerCount = query.getObserversCount();\n\n const service = parseQueryKey(query.queryKey);\n\n if (!service) {\n return;\n }\n\n if (\n !hasSubscription &&\n event.type === 'observerAdded' &&\n observerCount === 1\n ) {\n const cacheListener = (\n payload: DataServiceGranularCacheUpdatedPayload,\n ): void => {\n if (payload.type === 'removed') {\n return;\n }\n\n hydrate(client, payload.state);\n };\n\n subscriptions.set(hash, cacheListener);\n messenger.subscribe(\n `${service}:cacheUpdated:${hash}`,\n cacheListener as JsonSubscriptionCallback,\n );\n } else if (\n event.type === 'observerRemoved' &&\n observerCount === 0 &&\n hasSubscription\n ) {\n const subscriptionListener = subscriptions.get(hash);\n\n messenger.unsubscribe(\n `${service}:cacheUpdated:${hash}`,\n subscriptionListener as JsonSubscriptionCallback,\n );\n subscriptions.delete(hash);\n }\n });\n\n // Override invalidateQueries to ensure the data service is invalidated as well.\n const originalInvalidate = client.invalidateQueries.bind(client);\n\n // This function is defined in this way to have full support for all function overloads.\n client.invalidateQueries = async (\n arg1?: QueryKey | InvalidateQueryFilters,\n arg2?: OmitKeyof<InvalidateQueryFilters, 'queryKey'> | InvalidateOptions,\n arg3?: InvalidateOptions,\n ): Promise<void> => {\n const [filters, options] = parseFilterArgs(arg1, arg2, arg3);\n\n const queries = client.getQueryCache().findAll(filters);\n\n const services = [\n ...new Set(queries.map((query) => parseQueryKey(query.queryKey))),\n ];\n\n await Promise.all(\n services.map(async (service) => {\n if (!service) {\n return null;\n }\n\n return messenger.call(\n `${service}:invalidateQueries`,\n filters as Json,\n options as Json,\n );\n }),\n );\n\n return originalInvalidate(filters, options);\n };\n\n return client;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/react-data-query",
|
|
3
|
-
"version": "0.2.1-preview-
|
|
3
|
+
"version": "0.2.1-preview-9c0a7053d",
|
|
4
4
|
"description": "Provides React utilities for consuming data services",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ethereum",
|
|
@@ -47,13 +47,9 @@
|
|
|
47
47
|
"lint:tsconfigs": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts",
|
|
48
48
|
"lint:tsconfigs:fix": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts --fix",
|
|
49
49
|
"since-latest-release": "../../scripts/since-latest-release.sh",
|
|
50
|
-
"test": "
|
|
51
|
-
"test:clean": "
|
|
52
|
-
"test:
|
|
53
|
-
"test:jest:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
|
|
54
|
-
"test:jest:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
|
|
55
|
-
"test:types": "tsc --noEmit --project tsconfig.type-tests.json && echo \"Type tests pass\"",
|
|
56
|
-
"test:verbose": "yarn test:jest:verbose && yarn test:types",
|
|
50
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
|
|
51
|
+
"test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
|
|
52
|
+
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
|
|
57
53
|
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
|
|
58
54
|
},
|
|
59
55
|
"dependencies": {
|