@metamask-previews/react-data-query 2.0.0-preview-2ad7fb9 → 2.0.0-preview-55295a2

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 CHANGED
@@ -26,13 +26,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
26
26
  - Bump `@tanstack/react-query` from `^5.89.0` to `^5.103.2` ([#10511](https://github.com/MetaMask/core/pull/10511))
27
27
  - Bump `uuid` from `^11.1.1` to `^14.0.2` ([#10941](https://github.com/MetaMask/core/pull/10941), [#11179](https://github.com/MetaMask/core/pull/11179), [#11305](https://github.com/MetaMask/core/pull/11305))
28
28
 
29
- ### Fixed
30
-
31
- - UI queries now reflect `fetchStatus` and errors from fetches the data service starts in the background ([#11166](https://github.com/MetaMask/core/pull/11166))
32
- - While the data service refetches a query, the UI query is now `fetching` instead of staying `idle`, unless the UI is running its own fetch for that query.
33
- - When a background refetch fails, the UI query now moves to `error` with an `Error` built from the serialized error, keeping its existing data. Extra properties such as `code` or `httpStatus` are copied onto that `Error`, and a serialized `cause` is rebuilt as an `Error` too.
34
- - A UI query that mirrors a background fetch goes back to `idle` when its last observer is removed or when the data service removes the query.
35
-
36
29
  ## [2.0.0]
37
30
 
38
31
  ### Changed
@@ -1 +1 @@
1
- {"version":3,"file":"createUIQueryClient.d.ts","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oCAAoC,EACpC,sCAAsC,EAEvC,MAAM,6BAA6B,CAAC;AAErC,OAAO,EAGL,WAAW,EAIX,iBAAiB,EAKlB,MAAM,sBAAsB,CAAC;AAQ9B;;GAEG;AACH,KAAK,sCAAsC,GAAG,CAC5C,OAAO,EAAE,sCAAsC,KAC5C,IAAI,CAAC;AA0DV;;;GAGG;AACH,KAAK,gBAAgB,CAAC,eAAe,SAAS,MAAM,IAAI;IACtD;;;;;;;OAOG;IACH,IAAI,CACF,UA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ER;;;OAGG;IACH,WAAW,CACT,SAAS,EAAE,oCAAoC,CAAC,eAAe,CAAC,CAAC,MAAM,CAAC,EACxE,OAAO,EAAE,sCAAsC,GAC9C,IAAI,CAAC;CACT,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,gBAAgB,SAAS,SAAS,MAAM,EAAE,EAC5E,YAAY,EAAE,gBAAgB,EAC9B,SAAS,EAAE,gBAAgB,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC,EACrD,MAAM,GAAE,iBAAsB,GAC7B,WAAW,CA4Vb"}
@@ -1,57 +1,9 @@
1
- import { assert, isObject } from '@metamask/utils';
1
+ import { assert } from '@metamask/utils';
2
2
  import { hashKey, hydrate, QueryClient, } from '@tanstack/query-core';
3
3
  import { v4 as uuidV4 } from 'uuid';
4
4
  import { hydrateMutations, readGlobalId } from './hydrateMutations.js';
5
5
  import { createModuleLogger, projectLogger } from './loggers.js';
6
6
  const log = createModuleLogger(projectLogger, 'createUIQueryClient');
7
- /**
8
- * Serialized error keys that are not copied onto the rebuilt error as extra
9
- * properties. `name`, `message` and `cause` are set explicitly, and
10
- * `__proto__` must never be copied.
11
- */
12
- const RESERVED_ERROR_KEYS = new Set(['name', 'message', 'cause', '__proto__']);
13
- /**
14
- * Check whether a serialized `cause` is itself a serialized error.
15
- *
16
- * @param value - The serialized cause.
17
- * @returns Whether the cause has a string `name` and `message`.
18
- */
19
- function isSerializedQueryError(value) {
20
- return (isObject(value) &&
21
- typeof value.name === 'string' &&
22
- typeof value.message === 'string');
23
- }
24
- /**
25
- * Turn an error that came through a `:cacheUpdated` payload back into an
26
- * `Error`, so UI code can treat it like any other query error. Extra
27
- * properties such as `code` or `httpStatus` are copied onto the error, and a
28
- * serialized `cause` is rebuilt as an `Error` too.
29
- *
30
- * @param serializedError - The serialized error.
31
- * @returns The error.
32
- */
33
- function deserializeQueryError(serializedError) {
34
- const { name, message, cause } = serializedError;
35
- const error = cause === undefined
36
- ? new Error(message)
37
- : new Error(message, {
38
- cause: isSerializedQueryError(cause)
39
- ? deserializeQueryError(cause)
40
- : cause,
41
- });
42
- error.name = name;
43
- for (const [key, value] of Object.entries(serializedError)) {
44
- if (!RESERVED_ERROR_KEYS.has(key)) {
45
- Object.defineProperty(error, key, {
46
- value,
47
- enumerable: true,
48
- writable: true,
49
- configurable: true,
50
- });
51
- }
52
- }
53
- return error;
54
- }
55
7
  /**
56
8
  * Create a QueryClient that enables data services to power queries and mutations via a messenger adapter.
57
9
  *
@@ -155,21 +107,6 @@ export function createUIQueryClient(dataServices, messenger, config = {}) {
155
107
  },
156
108
  });
157
109
  const queryCache = client.getQueryCache();
158
- /**
159
- * Set a UI query back to `idle` if it is marked as fetching only because it
160
- * mirrors a fetch that the data service runs in the background. A fetch
161
- * that the UI started itself (and so has a promise) is left alone.
162
- *
163
- * @param hash - The hash of the query.
164
- */
165
- function resetBackgroundFetchStatus(hash) {
166
- const uiQuery = queryCache.get(hash);
167
- if (uiQuery &&
168
- uiQuery.state.fetchStatus !== 'idle' &&
169
- uiQuery.promise === undefined) {
170
- uiQuery.setState({ fetchStatus: 'idle' });
171
- }
172
- }
173
110
  queryCache.subscribe((event) => {
174
111
  const { query } = event;
175
112
  const hash = query.queryHash;
@@ -184,38 +121,10 @@ export function createUIQueryClient(dataServices, messenger, config = {}) {
184
121
  observerCount === 1) {
185
122
  const cacheListener = (payload) => {
186
123
  if (payload.type === 'removed') {
187
- // No more updates will arrive for this query, so a background fetch
188
- // we were mirroring will never be reported as finished.
189
- resetBackgroundFetchStatus(hash);
190
124
  return;
191
125
  }
192
126
  log('Hydrating with', payload.state);
193
127
  hydrate(client, payload.state);
194
- // `hydrate` leaves `fetchStatus` alone and only copies state when the
195
- // data is newer, so fetches and errors the service runs on its own
196
- // never show up. Mirror them here, unless the UI is running its own
197
- // fetch, which always wins.
198
- for (const { queryHash, state } of payload.state.queries) {
199
- const uiQuery = queryCache.get(queryHash);
200
- // `hydrate` builds any query that is missing, so this is a check
201
- // to appease TypeScript.
202
- // istanbul ignore next
203
- if (!uiQuery) {
204
- continue;
205
- }
206
- if (uiQuery.promise !== undefined) {
207
- continue;
208
- }
209
- uiQuery.setState({
210
- fetchStatus: state.fetchStatus,
211
- ...(state.status === 'error' &&
212
- state.error && {
213
- status: 'error',
214
- error: deserializeQueryError(state.error),
215
- errorUpdatedAt: state.errorUpdatedAt,
216
- }),
217
- });
218
- }
219
128
  };
220
129
  subscriptions.set(hash, cacheListener);
221
130
  messenger.subscribe(`${service}:cacheUpdated:${hash}`, cacheListener);
@@ -231,9 +140,6 @@ export function createUIQueryClient(dataServices, messenger, config = {}) {
231
140
  messenger.unsubscribe(`${service}:cacheUpdated:${hash}`, subscriptionListener);
232
141
  }
233
142
  subscriptions.delete(hash);
234
- // We stop listening here, so a background fetch we were mirroring would
235
- // otherwise stay `fetching` forever.
236
- resetBackgroundFetchStatus(hash);
237
143
  }
238
144
  });
239
145
  const mutationCache = client.getMutationCache();
@@ -1 +1 @@
1
- {"version":3,"file":"createUIQueryClient.js","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":"AAKA,OAAO,EAAQ,MAAM,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EACL,OAAO,EACP,OAAO,EACP,WAAW,GASZ,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AAEpC,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAEjE,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,qBAAqB,CAAC,CAAC;AASrE;;;;GAIG;AACH,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC,CAAC;AAE/E;;;;;GAKG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nB,IAAI,EAAE,EAAE,GAAG,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAE;SAC7C,CAAC,CAAC;QAEH,0EAA0E;QAC1E,wEAAwE;QACxE,wEAAwE;QACxE,oEAAoE;QACpE,QAAQ,CAAC,UAAU,GAAG,CAAC,WAAW,EAAQ,EAAE;YAC1C,0BAA0B,CAAC;gBACzB,GAAG,WAAW;gBACd,IAAI,EAAE,EAAE,GAAG,WAAW,CAAC,IAAI,EAAE,QAAQ,EAAE;aACxC,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type {\n DataServiceGranularCacheUpdatedEvent,\n DataServiceGranularCacheUpdatedPayload,\n SerializedQueryError,\n} from '@metamask/base-data-service';\nimport { Json, assert, isObject } from '@metamask/utils';\nimport {\n hashKey,\n hydrate,\n QueryClient,\n InvalidateQueryFilters,\n InvalidateOptions,\n QueryKey,\n QueryClientConfig,\n Mutation,\n MutationOptions,\n MutationState,\n MutationFunction,\n} from '@tanstack/query-core';\nimport { v4 as uuidV4 } from 'uuid';\n\nimport { hydrateMutations, readGlobalId } from './hydrateMutations.js';\nimport { createModuleLogger, projectLogger } from './loggers.js';\n\nconst log = createModuleLogger(projectLogger, 'createUIQueryClient');\n\n/**\n * Handles granular cache update events emitted by data services.\n */\ntype DataServiceGranularCacheUpdatedHandler = (\n payload: DataServiceGranularCacheUpdatedPayload,\n) => void;\n\n/**\n * Serialized error keys that are not copied onto the rebuilt error as extra\n * properties. `name`, `message` and `cause` are set explicitly, and\n * `__proto__` must never be copied.\n */\nconst RESERVED_ERROR_KEYS = new Set(['name', 'message', 'cause', '__proto__']);\n\n/**\n * Check whether a serialized `cause` is itself a serialized error.\n *\n * @param value - The serialized cause.\n * @returns Whether the cause has a string `name` and `message`.\n */\nfunction isSerializedQueryError(\n value: SerializedQueryError | Json,\n): value is SerializedQueryError {\n return (\n isObject(value) &&\n typeof value.name === 'string' &&\n typeof value.message === 'string'\n );\n}\n\n/**\n * Turn an error that came through a `:cacheUpdated` payload back into an\n * `Error`, so UI code can treat it like any other query error. Extra\n * properties such as `code` or `httpStatus` are copied onto the error, and a\n * serialized `cause` is rebuilt as an `Error` too.\n *\n * @param serializedError - The serialized error.\n * @returns The error.\n */\nfunction deserializeQueryError(serializedError: SerializedQueryError): Error {\n const { name, message, cause } = serializedError;\n const error =\n cause === undefined\n ? new Error(message)\n : new Error(message, {\n cause: isSerializedQueryError(cause)\n ? deserializeQueryError(cause)\n : cause,\n });\n error.name = name;\n for (const [key, value] of Object.entries(serializedError)) {\n if (!RESERVED_ERROR_KEYS.has(key)) {\n Object.defineProperty(error, key, {\n value,\n enumerable: true,\n writable: true,\n configurable: true,\n });\n }\n }\n return error;\n}\n\n/**\n * A narrower subset of the `Messenger` type, tailored to the messenger\n * that `createUIQueryClient` interacts with.\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[]`. For\n * concrete messengers, each action's parameters exist as fixed-length tuple,\n * and a variadic `Json[]` is not assignable to a fixed-length tuple, so using\n * `Json[]` here would reject otherwise valid messengers.\n */\n call(\n actionType: `${DataServiceName}:${string}`,\n ...params: unknown[]\n ): 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 that enables data services to power queries and mutations via a messenger adapter.\n *\n * This returns a wrapped version of TanStack Query's QueryClient interface. Note that while some methods such as `invalidateQueries` have support for data services, some such as `setQueryData` do not.\n *\n * @param dataServices - A list of data services.\n * @param messenger - A messenger-like object with the following constraints: 1) the messenger must support the `call`, `subscribe` and `unsubscribe` methods; 2) all action handler arguments and event payloads must be JSON-compatible; 3) the messenger must minimally support actions that are scoped to the designated data services and must minimally support the `:cacheUpdated:${hash}` event scoped to the designated data services.\n * @param config - Optional query client configuration options.\n * @returns The created 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 // Tracks how many mutation observers are currently relying on each mutation\n // key's cache subscription. Unlike queries, a `Mutation` does not expose its\n // observer count publicly, so we count observers ourselves and only tear down\n // the messenger subscription once the last observer for a key is removed.\n const mutationObserverCounts = new Map<string, number>();\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 /**\n * The default `mutationFn` for data-service mutations. It uses the\n * `mutationKey` to call an action on a data service through the messenger.\n *\n * This is a separate function so that we can use it to determine whether a\n * custom `mutationFn` was used when the mutation was executed.\n *\n * @param _variables - The variables with which the mutation is being\n * executed. Unused.\n * @param context - The context associated with the mutation. Holds the\n * `mutationKey`.\n * @returns The result of the data service action.\n */\n const defaultDataServiceMutationFn: MutationFunction = async (\n _variables,\n context,\n ): Promise<unknown> => {\n const { mutationKey } = context;\n\n assert(\n mutationKey !== undefined,\n \"You must pass a `mutationKey` that calls an action on the messenger provided to `createUIQueryClient`, e.g. `mutationKey: ['ExampleDataService:createOrder', ...]`.\",\n );\n\n const [action, ...params] = mutationKey;\n\n assert(\n typeof action === 'string' && isRecognizedDataServiceAction(action),\n \"You must pass a `mutationKey` that calls an action on the messenger provided to `createUIQueryClient`, e.g. `mutationKey: ['ExampleDataService:createOrder', ...]`.\",\n );\n\n log(`Detected mutation request, calling action: \"${action}\"`);\n\n // We assume that this mutation already has a global ID. (It was\n // added by our `MutationCache.build` override below.)\n const globalId = readGlobalId(context.meta);\n // We can't realistically test that it doesn't have a global ID, however,\n // since `MutationCache.build` always runs first.\n // istanbul ignore next\n assert(\n globalId !== undefined,\n 'Expected mutation to have a `globalId` in its `meta` by the time its `mutationFn` runs, but none was found. This is a bug in `createUIQueryClient`.',\n );\n\n return await messenger.call(action, ...(params as Json[]), globalId);\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 \"You must pass a `queryKey` that calls an action on the messenger provided to `createUIQueryClient`, e.g. `queryKey: ['ExampleDataService:getAssets', ...]`.\",\n );\n\n const params = options.queryKey.slice(1);\n if (options.pageParam !== undefined) {\n params.push(options.pageParam);\n }\n\n return await messenger.call(action, ...params);\n },\n },\n mutations: {\n ...config.defaultOptions?.mutations,\n mutationFn: defaultDataServiceMutationFn,\n },\n },\n });\n\n const queryCache = client.getQueryCache();\n\n /**\n * Set a UI query back to `idle` if it is marked as fetching only because it\n * mirrors a fetch that the data service runs in the background. A fetch\n * that the UI started itself (and so has a promise) is left alone.\n *\n * @param hash - The hash of the query.\n */\n function resetBackgroundFetchStatus(hash: string): void {\n const uiQuery = queryCache.get(hash);\n\n if (\n uiQuery &&\n uiQuery.state.fetchStatus !== 'idle' &&\n uiQuery.promise === undefined\n ) {\n uiQuery.setState({ fetchStatus: 'idle' });\n }\n }\n\n queryCache.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 // No more updates will arrive for this query, so a background fetch\n // we were mirroring will never be reported as finished.\n resetBackgroundFetchStatus(hash);\n return;\n }\n\n log('Hydrating with', payload.state);\n hydrate(client, payload.state);\n\n // `hydrate` leaves `fetchStatus` alone and only copies state when the\n // data is newer, so fetches and errors the service runs on its own\n // never show up. Mirror them here, unless the UI is running its own\n // fetch, which always wins.\n for (const { queryHash, state } of payload.state.queries) {\n const uiQuery = queryCache.get(queryHash);\n\n // `hydrate` builds any query that is missing, so this is a check\n // to appease TypeScript.\n // istanbul ignore next\n if (!uiQuery) {\n continue;\n }\n\n if (uiQuery.promise !== undefined) {\n continue;\n }\n\n uiQuery.setState({\n fetchStatus: state.fetchStatus,\n ...(state.status === 'error' &&\n state.error && {\n status: 'error',\n error: deserializeQueryError(state.error),\n errorUpdatedAt: state.errorUpdatedAt,\n }),\n });\n }\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 // We can't write a test for this, as it's unrealistic\n // (we just need a check to appease TypeScript).\n // istanbul ignore next\n if (subscriptionListener) {\n messenger.unsubscribe(\n `${service}:cacheUpdated:${hash}`,\n subscriptionListener,\n );\n }\n subscriptions.delete(hash);\n\n // We stop listening here, so a background fetch we were mirroring would\n // otherwise stay `fetching` forever.\n resetBackgroundFetchStatus(hash);\n }\n });\n\n const mutationCache = client.getMutationCache();\n mutationCache.subscribe((event) => {\n const { mutation } = event;\n\n if (!mutation?.options.mutationKey) {\n return;\n }\n\n const service = parseQueryKey(mutation.options.mutationKey);\n\n if (!service) {\n return;\n }\n\n const hash = hashKey(mutation.options.mutationKey);\n const hasSubscription = subscriptions.has(hash);\n\n log(\n `[mutationCache subscription] Received event \"${event.type}\". Details:`,\n event.mutation,\n );\n\n if (event.type === 'observerAdded') {\n mutationObserverCounts.set(\n hash,\n (mutationObserverCounts.get(hash) ?? 0) + 1,\n );\n\n log('[mutationCache subscription] hasSubscription =', hasSubscription);\n\n if (!hasSubscription) {\n const cacheListener = (\n payload: DataServiceGranularCacheUpdatedPayload,\n ): void => {\n log(\n `[mutationCache subscription] cacheUpdated:${hash} emitted`,\n payload,\n );\n\n // Only `updated` events have meaningful mutation state to sync.\n // When a mutation is first created, an `added` event is fired and the\n // state is `idle` with no result; hydrating that would clobber a UI\n // mutation that has already moved to `pending`, `success`, or\n // `error`. (And a `removed` event has no state at all.)\n if (payload.type !== 'updated') {\n return;\n }\n\n hydrateMutations(client, payload.state);\n };\n\n subscriptions.set(hash, cacheListener);\n messenger.subscribe(`${service}:cacheUpdated:${hash}`, cacheListener);\n }\n } else if (event.type === 'observerRemoved' && hasSubscription) {\n // We can assume that if an observed mutation is removed, it must have\n // first been added; and that when it was added, the observer count was\n // initialized. (There's no real way to test the alternative, anyway.)\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const remainingObservers = mutationObserverCounts.get(hash)! - 1;\n\n if (remainingObservers > 0) {\n mutationObserverCounts.set(hash, remainingObservers);\n return;\n }\n\n mutationObserverCounts.delete(hash);\n\n const subscriptionListener = subscriptions.get(hash);\n\n // A subscription always has a listener, since both are set together when\n // the first observer is added.\n // istanbul ignore next\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\n const originalInvalidate = client.invalidateQueries.bind(client);\n\n client.invalidateQueries = async (\n filters?: InvalidateQueryFilters,\n options?: InvalidateOptions,\n ): Promise<void> => {\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 // Override `build` to ensure that any data-service mutation created via\n // `executeMutation` or manually has a `globalId`.\n\n const originalBuildMutation = mutationCache.build.bind(mutationCache);\n\n mutationCache.build = function <TData, TError, TVariables, TOnMutateResult>(\n buildClient: QueryClient,\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,\n state?: MutationState<TData, TError, TVariables, TOnMutateResult>,\n ): Mutation<TData, TError, TVariables, TOnMutateResult> {\n const mutation = originalBuildMutation(buildClient, options, state);\n const { mutationFn } = mutation.options;\n\n if (\n // We know that a custom `mutationFn` was provided because it\n // is not the default (which is defined above).\n mutationFn !== defaultDataServiceMutationFn ||\n // No need to add a global ID if there's already one.\n readGlobalId(mutation.options.meta) !== undefined\n ) {\n return mutation;\n }\n\n const globalId = uuidV4();\n\n // Give the mutation its own `options`/`meta` objects rather than mutating\n // in place. When options are already defaulted, TanStack returns the shared\n // observer options as-is, so mutating them would leak this mutation's\n // `globalId` back onto the observer and cause the next mutation built from\n // that observer to reuse the same id.\n const originalSetMutationOptions = mutation.setOptions.bind(mutation);\n originalSetMutationOptions({\n ...mutation.options,\n meta: { ...mutation.options.meta, globalId },\n });\n\n // `MutationObserver.setOptions` (triggered on every re-render) replaces a\n // pending mutation's options wholesale, which would otherwise strip the\n // `globalId` and stop the mutation from matching its service-side cache\n // updates. Preserve the established `globalId` across such updates.\n mutation.setOptions = (nextOptions): void => {\n originalSetMutationOptions({\n ...nextOptions,\n meta: { ...nextOptions.meta, globalId },\n });\n };\n\n return mutation;\n };\n\n return client;\n}\n"]}
1
+ {"version":3,"file":"createUIQueryClient.js","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":"AAIA,OAAO,EAAQ,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EACL,OAAO,EACP,OAAO,EACP,WAAW,GASZ,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AAEpC,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAEjE,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,qBAAqB,CAAC,CAAC;AA8CrE;;;;;;;;;GASG;AACH,MAAM,UAAU,mBAAmB,CACjC,YAA8B,EAC9B,SAAqD,EACrD,MAAM,GAAsB,EAAE;IAE9B,MAAM,aAAa,GAAG,IAAI,GAAG,EAG1B,CAAC;IAEJ,4EAA4E;IAC5E,6EAA6E;IAC7E,8EAA8E;IAC9E,0EAA0E;IAC1E,MAAM,sBAAsB,GAAG,IAAI,GAAG,EAAkB,CAAC;IAEzD;;;;;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,CAA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,WAAW,CAAC,IAAI,EAAE,QAAQ,EAAE;aACxC,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type {\n DataServiceGranularCacheUpdatedEvent,\n DataServiceGranularCacheUpdatedPayload,\n} from '@metamask/base-data-service';\nimport { Json, assert } from '@metamask/utils';\nimport {\n hashKey,\n hydrate,\n QueryClient,\n InvalidateQueryFilters,\n InvalidateOptions,\n QueryKey,\n QueryClientConfig,\n Mutation,\n MutationOptions,\n MutationState,\n MutationFunction,\n} from '@tanstack/query-core';\nimport { v4 as uuidV4 } from 'uuid';\n\nimport { hydrateMutations, readGlobalId } from './hydrateMutations.js';\nimport { createModuleLogger, projectLogger } from './loggers.js';\n\nconst log = createModuleLogger(projectLogger, 'createUIQueryClient');\n\n/**\n * Handles granular cache update events emitted by data services.\n */\ntype DataServiceGranularCacheUpdatedHandler = (\n payload: DataServiceGranularCacheUpdatedPayload,\n) => void;\n\n/**\n * A narrower subset of the `Messenger` type, tailored to the messenger\n * that `createUIQueryClient` interacts with.\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[]`. For\n * concrete messengers, each action's parameters exist as fixed-length tuple,\n * and a variadic `Json[]` is not assignable to a fixed-length tuple, so using\n * `Json[]` here would reject otherwise valid messengers.\n */\n call(\n actionType: `${DataServiceName}:${string}`,\n ...params: unknown[]\n ): 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 that enables data services to power queries and mutations via a messenger adapter.\n *\n * This returns a wrapped version of TanStack Query's QueryClient interface. Note that while some methods such as `invalidateQueries` have support for data services, some such as `setQueryData` do not.\n *\n * @param dataServices - A list of data services.\n * @param messenger - A messenger-like object with the following constraints: 1) the messenger must support the `call`, `subscribe` and `unsubscribe` methods; 2) all action handler arguments and event payloads must be JSON-compatible; 3) the messenger must minimally support actions that are scoped to the designated data services and must minimally support the `:cacheUpdated:${hash}` event scoped to the designated data services.\n * @param config - Optional query client configuration options.\n * @returns The created 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 // Tracks how many mutation observers are currently relying on each mutation\n // key's cache subscription. Unlike queries, a `Mutation` does not expose its\n // observer count publicly, so we count observers ourselves and only tear down\n // the messenger subscription once the last observer for a key is removed.\n const mutationObserverCounts = new Map<string, number>();\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 /**\n * The default `mutationFn` for data-service mutations. It uses the\n * `mutationKey` to call an action on a data service through the messenger.\n *\n * This is a separate function so that we can use it to determine whether a\n * custom `mutationFn` was used when the mutation was executed.\n *\n * @param _variables - The variables with which the mutation is being\n * executed. Unused.\n * @param context - The context associated with the mutation. Holds the\n * `mutationKey`.\n * @returns The result of the data service action.\n */\n const defaultDataServiceMutationFn: MutationFunction = async (\n _variables,\n context,\n ): Promise<unknown> => {\n const { mutationKey } = context;\n\n assert(\n mutationKey !== undefined,\n \"You must pass a `mutationKey` that calls an action on the messenger provided to `createUIQueryClient`, e.g. `mutationKey: ['ExampleDataService:createOrder', ...]`.\",\n );\n\n const [action, ...params] = mutationKey;\n\n assert(\n typeof action === 'string' && isRecognizedDataServiceAction(action),\n \"You must pass a `mutationKey` that calls an action on the messenger provided to `createUIQueryClient`, e.g. `mutationKey: ['ExampleDataService:createOrder', ...]`.\",\n );\n\n log(`Detected mutation request, calling action: \"${action}\"`);\n\n // We assume that this mutation already has a global ID. (It was\n // added by our `MutationCache.build` override below.)\n const globalId = readGlobalId(context.meta);\n // We can't realistically test that it doesn't have a global ID, however,\n // since `MutationCache.build` always runs first.\n // istanbul ignore next\n assert(\n globalId !== undefined,\n 'Expected mutation to have a `globalId` in its `meta` by the time its `mutationFn` runs, but none was found. This is a bug in `createUIQueryClient`.',\n );\n\n return await messenger.call(action, ...(params as Json[]), globalId);\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 \"You must pass a `queryKey` that calls an action on the messenger provided to `createUIQueryClient`, e.g. `queryKey: ['ExampleDataService:getAssets', ...]`.\",\n );\n\n const params = options.queryKey.slice(1);\n if (options.pageParam !== undefined) {\n params.push(options.pageParam);\n }\n\n return await messenger.call(action, ...params);\n },\n },\n mutations: {\n ...config.defaultOptions?.mutations,\n mutationFn: defaultDataServiceMutationFn,\n },\n },\n });\n\n const queryCache = client.getQueryCache();\n queryCache.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 log('Hydrating with', payload.state);\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 // We can't write a test for this, as it's unrealistic\n // (we just need a check to appease TypeScript).\n // istanbul ignore next\n if (subscriptionListener) {\n messenger.unsubscribe(\n `${service}:cacheUpdated:${hash}`,\n subscriptionListener,\n );\n }\n subscriptions.delete(hash);\n }\n });\n\n const mutationCache = client.getMutationCache();\n mutationCache.subscribe((event) => {\n const { mutation } = event;\n\n if (!mutation?.options.mutationKey) {\n return;\n }\n\n const service = parseQueryKey(mutation.options.mutationKey);\n\n if (!service) {\n return;\n }\n\n const hash = hashKey(mutation.options.mutationKey);\n const hasSubscription = subscriptions.has(hash);\n\n log(\n `[mutationCache subscription] Received event \"${event.type}\". Details:`,\n event.mutation,\n );\n\n if (event.type === 'observerAdded') {\n mutationObserverCounts.set(\n hash,\n (mutationObserverCounts.get(hash) ?? 0) + 1,\n );\n\n log('[mutationCache subscription] hasSubscription =', hasSubscription);\n\n if (!hasSubscription) {\n const cacheListener = (\n payload: DataServiceGranularCacheUpdatedPayload,\n ): void => {\n log(\n `[mutationCache subscription] cacheUpdated:${hash} emitted`,\n payload,\n );\n\n // Only `updated` events have meaningful mutation state to sync.\n // When a mutation is first created, an `added` event is fired and the\n // state is `idle` with no result; hydrating that would clobber a UI\n // mutation that has already moved to `pending`, `success`, or\n // `error`. (And a `removed` event has no state at all.)\n if (payload.type !== 'updated') {\n return;\n }\n\n hydrateMutations(client, payload.state);\n };\n\n subscriptions.set(hash, cacheListener);\n messenger.subscribe(`${service}:cacheUpdated:${hash}`, cacheListener);\n }\n } else if (event.type === 'observerRemoved' && hasSubscription) {\n // We can assume that if an observed mutation is removed, it must have\n // first been added; and that when it was added, the observer count was\n // initialized. (There's no real way to test the alternative, anyway.)\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const remainingObservers = mutationObserverCounts.get(hash)! - 1;\n\n if (remainingObservers > 0) {\n mutationObserverCounts.set(hash, remainingObservers);\n return;\n }\n\n mutationObserverCounts.delete(hash);\n\n const subscriptionListener = subscriptions.get(hash);\n\n // A subscription always has a listener, since both are set together when\n // the first observer is added.\n // istanbul ignore next\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\n const originalInvalidate = client.invalidateQueries.bind(client);\n\n client.invalidateQueries = async (\n filters?: InvalidateQueryFilters,\n options?: InvalidateOptions,\n ): Promise<void> => {\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 // Override `build` to ensure that any data-service mutation created via\n // `executeMutation` or manually has a `globalId`.\n\n const originalBuildMutation = mutationCache.build.bind(mutationCache);\n\n mutationCache.build = function <TData, TError, TVariables, TOnMutateResult>(\n buildClient: QueryClient,\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,\n state?: MutationState<TData, TError, TVariables, TOnMutateResult>,\n ): Mutation<TData, TError, TVariables, TOnMutateResult> {\n const mutation = originalBuildMutation(buildClient, options, state);\n const { mutationFn } = mutation.options;\n\n if (\n // We know that a custom `mutationFn` was provided because it\n // is not the default (which is defined above).\n mutationFn !== defaultDataServiceMutationFn ||\n // No need to add a global ID if there's already one.\n readGlobalId(mutation.options.meta) !== undefined\n ) {\n return mutation;\n }\n\n const globalId = uuidV4();\n\n // Give the mutation its own `options`/`meta` objects rather than mutating\n // in place. When options are already defaulted, TanStack returns the shared\n // observer options as-is, so mutating them would leak this mutation's\n // `globalId` back onto the observer and cause the next mutation built from\n // that observer to reuse the same id.\n const originalSetMutationOptions = mutation.setOptions.bind(mutation);\n originalSetMutationOptions({\n ...mutation.options,\n meta: { ...mutation.options.meta, globalId },\n });\n\n // `MutationObserver.setOptions` (triggered on every re-render) replaces a\n // pending mutation's options wholesale, which would otherwise strip the\n // `globalId` and stop the mutation from matching its service-side cache\n // updates. Preserve the established `globalId` across such updates.\n mutation.setOptions = (nextOptions): void => {\n originalSetMutationOptions({\n ...nextOptions,\n meta: { ...nextOptions.meta, globalId },\n });\n };\n\n return mutation;\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": "2.0.0-preview-2ad7fb9",
3
+ "version": "2.0.0-preview-55295a2",
4
4
  "description": "Provides React utilities for consuming data services",
5
5
  "keywords": [
6
6
  "Ethereum",