@metamask-previews/base-data-service 0.0.0-preview-ee1fa5d01 → 0.0.0-preview-7cbd7910e

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/dist/index.cjs CHANGED
@@ -1,19 +1,13 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
2
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./BaseDataService.cjs"), exports);
18
- __exportStar(require("./createUIQueryClient.cjs"), exports);
3
+ /**
4
+ * Example function that returns a greeting for the given name.
5
+ *
6
+ * @param name - The name to greet.
7
+ * @returns The greeting.
8
+ */
9
+ function greeter(name) {
10
+ return `Hello, ${name}!`;
11
+ }
12
+ exports.default = greeter;
19
13
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wDAAkC;AAClC,4DAAsC","sourcesContent":["export * from './BaseDataService';\nexport * from './createUIQueryClient';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA;;;;;GAKG;AACH,SAAwB,OAAO,CAAC,IAAY;IAC1C,OAAO,UAAU,IAAI,GAAG,CAAC;AAC3B,CAAC;AAFD,0BAEC","sourcesContent":["/**\n * Example function that returns a greeting for the given name.\n *\n * @param name - The name to greet.\n * @returns The greeting.\n */\nexport default function greeter(name: string): string {\n return `Hello, ${name}!`;\n}\n"]}
package/dist/index.d.cts CHANGED
@@ -1,3 +1,8 @@
1
- export * from "./BaseDataService.cjs";
2
- export * from "./createUIQueryClient.cjs";
1
+ /**
2
+ * Example function that returns a greeting for the given name.
3
+ *
4
+ * @param name - The name to greet.
5
+ * @returns The greeting.
6
+ */
7
+ export default function greeter(name: string): string;
3
8
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAkC;AAClC,0CAAsC"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD"}
package/dist/index.d.mts CHANGED
@@ -1,3 +1,8 @@
1
- export * from "./BaseDataService.mjs";
2
- export * from "./createUIQueryClient.mjs";
1
+ /**
2
+ * Example function that returns a greeting for the given name.
3
+ *
4
+ * @param name - The name to greet.
5
+ * @returns The greeting.
6
+ */
7
+ export default function greeter(name: string): string;
3
8
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAkC;AAClC,0CAAsC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD"}
package/dist/index.mjs CHANGED
@@ -1,3 +1,10 @@
1
- export * from "./BaseDataService.mjs";
2
- export * from "./createUIQueryClient.mjs";
1
+ /**
2
+ * Example function that returns a greeting for the given name.
3
+ *
4
+ * @param name - The name to greet.
5
+ * @returns The greeting.
6
+ */
7
+ export default function greeter(name) {
8
+ return `Hello, ${name}!`;
9
+ }
3
10
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAkC;AAClC,0CAAsC","sourcesContent":["export * from './BaseDataService';\nexport * from './createUIQueryClient';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,IAAY;IAC1C,OAAO,UAAU,IAAI,GAAG,CAAC;AAC3B,CAAC","sourcesContent":["/**\n * Example function that returns a greeting for the given name.\n *\n * @param name - The name to greet.\n * @returns The greeting.\n */\nexport default function greeter(name: string): string {\n return `Hello, ${name}!`;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/base-data-service",
3
- "version": "0.0.0-preview-ee1fa5d01",
3
+ "version": "0.0.0-preview-7cbd7910e",
4
4
  "description": "Provides utilities for building data services",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -47,19 +47,12 @@
47
47
  "test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
48
48
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
49
49
  },
50
- "dependencies": {
51
- "@metamask/messenger": "^0.3.0",
52
- "@metamask/utils": "^11.9.0",
53
- "@tanstack/query-core": "^4.43.0",
54
- "fast-deep-equal": "^3.1.3"
55
- },
56
50
  "devDependencies": {
57
51
  "@metamask/auto-changelog": "^3.4.4",
58
52
  "@ts-bridge/cli": "^0.6.4",
59
53
  "@types/jest": "^29.5.14",
60
54
  "deepmerge": "^4.2.2",
61
55
  "jest": "^29.7.0",
62
- "nock": "^13.3.1",
63
56
  "ts-jest": "^29.2.5",
64
57
  "typedoc": "^0.25.13",
65
58
  "typedoc-plugin-missing-exports": "^2.0.0",
@@ -1,115 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
- if (kind === "m") throw new TypeError("Private method is not writable");
4
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
- };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
- var _BaseDataService_instances, _BaseDataService_messenger, _BaseDataService_client, _BaseDataService_subscriptions, _BaseDataService_registerMessageHandlers, _BaseDataService_setupCacheListener, _BaseDataService_handleSubscribe, _BaseDataService_handleUnsubscribe, _BaseDataService_getDehydratedStateForQuery, _BaseDataService_broadcastQueryState;
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.BaseDataService = void 0;
19
- const messenger_1 = require("@metamask/messenger");
20
- const query_core_1 = require("@tanstack/query-core");
21
- const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
22
- class BaseDataService {
23
- constructor({ name, messenger, }) {
24
- _BaseDataService_instances.add(this);
25
- _BaseDataService_messenger.set(this, void 0);
26
- _BaseDataService_client.set(this, new query_core_1.QueryClient());
27
- _BaseDataService_subscriptions.set(this, new Map());
28
- this.name = name;
29
- __classPrivateFieldSet(this, _BaseDataService_messenger, messenger, "f");
30
- __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_registerMessageHandlers).call(this);
31
- __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_setupCacheListener).call(this);
32
- }
33
- async fetchQuery(options) {
34
- return __classPrivateFieldGet(this, _BaseDataService_client, "f").fetchQuery(options);
35
- }
36
- async fetchInfiniteQuery(options, pageParam) {
37
- const query = __classPrivateFieldGet(this, _BaseDataService_client, "f")
38
- .getQueryCache()
39
- .find({ queryKey: options.queryKey });
40
- if (!query || !pageParam) {
41
- const result = await __classPrivateFieldGet(this, _BaseDataService_client, "f").fetchInfiniteQuery({
42
- ...options,
43
- queryFn: (context) => options.queryFn({
44
- ...context,
45
- pageParam: context.pageParam ?? pageParam,
46
- }),
47
- });
48
- return result.pages[0];
49
- }
50
- const { pages } = query.state.data;
51
- const previous = options.getPreviousPageParam?.(pages[0], pages);
52
- const direction = (0, fast_deep_equal_1.default)(pageParam, previous) ? 'backward' : 'forward';
53
- const result = (await query.fetch(undefined, {
54
- meta: {
55
- fetchMore: {
56
- direction,
57
- pageParam,
58
- },
59
- },
60
- }));
61
- const pageIndex = result.pageParams.indexOf(pageParam);
62
- return result.pages[pageIndex];
63
- }
64
- async invalidateQueries(filters, options) {
65
- return __classPrivateFieldGet(this, _BaseDataService_client, "f").invalidateQueries(filters, options);
66
- }
67
- }
68
- exports.BaseDataService = BaseDataService;
69
- _BaseDataService_messenger = new WeakMap(), _BaseDataService_client = new WeakMap(), _BaseDataService_subscriptions = new WeakMap(), _BaseDataService_instances = new WeakSet(), _BaseDataService_registerMessageHandlers = function _BaseDataService_registerMessageHandlers() {
70
- __classPrivateFieldGet(this, _BaseDataService_messenger, "f").registerActionHandler(`${this.name}:subscribe`,
71
- // @ts-expect-error TODO.
72
- (queryKey, callback) => __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_handleSubscribe).call(this, queryKey, callback));
73
- __classPrivateFieldGet(this, _BaseDataService_messenger, "f").registerActionHandler(`${this.name}:unsubscribe`,
74
- // @ts-expect-error TODO.
75
- (queryKey, callback) => __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_handleUnsubscribe).call(this, queryKey, callback));
76
- __classPrivateFieldGet(this, _BaseDataService_messenger, "f").registerActionHandler(`${this.name}:invalidateQueries`,
77
- // @ts-expect-error TODO.
78
- (filters, options) => this.invalidateQueries(filters, options));
79
- }, _BaseDataService_setupCacheListener = function _BaseDataService_setupCacheListener() {
80
- __classPrivateFieldGet(this, _BaseDataService_client, "f").getQueryCache().subscribe((event) => {
81
- if (__classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").has(event.query.queryHash)) {
82
- __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_broadcastQueryState).call(this, event.query.queryKey);
83
- }
84
- });
85
- }, _BaseDataService_handleSubscribe = function _BaseDataService_handleSubscribe(queryKey, subscription) {
86
- const hash = (0, query_core_1.hashQueryKey)(queryKey);
87
- if (!__classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").has(hash)) {
88
- __classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").set(hash, new Set());
89
- }
90
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
91
- __classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").get(hash).add(subscription);
92
- return __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_getDehydratedStateForQuery).call(this, queryKey);
93
- }, _BaseDataService_handleUnsubscribe = function _BaseDataService_handleUnsubscribe(queryKey, subscription) {
94
- const hash = (0, query_core_1.hashQueryKey)(queryKey);
95
- const subscribers = __classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").get(hash);
96
- subscribers?.delete(subscription);
97
- if (subscribers?.size === 0) {
98
- __classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").delete(hash);
99
- }
100
- }, _BaseDataService_getDehydratedStateForQuery = function _BaseDataService_getDehydratedStateForQuery(queryKey) {
101
- const hash = (0, query_core_1.hashQueryKey)(queryKey);
102
- return (0, query_core_1.dehydrate)(__classPrivateFieldGet(this, _BaseDataService_client, "f"), {
103
- shouldDehydrateQuery: (query) => query.queryHash === hash,
104
- });
105
- }, _BaseDataService_broadcastQueryState = function _BaseDataService_broadcastQueryState(queryKey) {
106
- const hash = (0, query_core_1.hashQueryKey)(queryKey);
107
- const state = __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_getDehydratedStateForQuery).call(this, queryKey);
108
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
109
- const subscribers = __classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").get(hash);
110
- subscribers.forEach((subscriber) => subscriber({
111
- hash,
112
- state,
113
- }));
114
- };
115
- //# sourceMappingURL=BaseDataService.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BaseDataService.cjs","sourceRoot":"","sources":["../src/BaseDataService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,mDAI6B;AAE7B,qDAY8B;AAC9B,sEAAwC;AA+BxC,MAAa,eAAe;IAwB1B,YAAY,EACV,IAAI,EACJ,SAAS,GAIV;;QAhBQ,6CAIP;QAEO,kCAAU,IAAI,wBAAW,EAAE,EAAC;QAE5B,yCAAyD,IAAI,GAAG,EAAE,EAAC;QAS1E,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,uBAAA,IAAI,8BAAc,SAIjB,MAAA,CAAC;QAEF,uBAAA,IAAI,4EAAyB,MAA7B,IAAI,CAA2B,CAAC;QAChC,uBAAA,IAAI,uEAAoB,MAAxB,IAAI,CAAsB,CAAC;IAC7B,CAAC;IAiCS,KAAK,CAAC,UAAU,CAMxB,OAGC;QAED,OAAO,uBAAA,IAAI,+BAAQ,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAC1C,CAAC;IAES,KAAK,CAAC,kBAAkB,CAOhC,OAGC,EACD,SAAsB;QAEtB,MAAM,KAAK,GAAG,uBAAA,IAAI,+BAAQ;aACvB,aAAa,EAAE;aACf,IA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,CAAC","sourcesContent":["import {\n Messenger,\n ActionConstraint,\n EventConstraint,\n} from '@metamask/messenger';\nimport type { Json } from '@metamask/utils';\nimport {\n DehydratedState,\n FetchInfiniteQueryOptions,\n FetchQueryOptions,\n InfiniteData,\n InvalidateOptions,\n InvalidateQueryFilters,\n QueryClient,\n QueryKey,\n WithRequired,\n dehydrate,\n hashQueryKey,\n} from '@tanstack/query-core';\nimport deepEqual from 'fast-deep-equal';\n\nexport type SubscriptionPayload = { hash: string; state: DehydratedState };\nexport type SubscriptionCallback = (payload: SubscriptionPayload) => void;\n\nexport type DataServiceSubscribeAction<ServiceName extends string> = {\n type: `${ServiceName}:subscribe`;\n handler: (\n queryKey: QueryKey,\n callback: SubscriptionCallback,\n ) => DehydratedState;\n};\n\nexport type DataServiceUnsubscribeAction<ServiceName extends string> = {\n type: `${ServiceName}:unsubscribe`;\n handler: (queryKey: QueryKey, callback: SubscriptionCallback) => void;\n};\n\nexport type DataServiceInvalidateQueriesAction<ServiceName extends string> = {\n type: `${ServiceName}:invalidateQueries`;\n handler: (\n filters?: InvalidateQueryFilters<Json>,\n options?: InvalidateOptions,\n ) => Promise<void>;\n};\n\nexport type DataServiceActions<ServiceName extends string> =\n | DataServiceSubscribeAction<ServiceName>\n | DataServiceUnsubscribeAction<ServiceName>\n | DataServiceInvalidateQueriesAction<ServiceName>;\n\nexport class BaseDataService<\n ServiceName extends string,\n ServiceMessenger extends Messenger<\n ServiceName,\n ActionConstraint,\n EventConstraint,\n // Use `any` to allow any parent to be set. `any` is harmless in a type constraint anyway,\n // it's the one totally safe place to use it.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n >,\n> {\n public readonly name: ServiceName;\n\n readonly #messenger: Messenger<\n ServiceName,\n DataServiceActions<ServiceName>,\n never\n >;\n\n readonly #client = new QueryClient();\n\n readonly #subscriptions: Map<string, Set<SubscriptionCallback>> = new Map();\n\n constructor({\n name,\n messenger,\n }: {\n name: ServiceName;\n messenger: ServiceMessenger;\n }) {\n this.name = name;\n\n this.#messenger = messenger as unknown as Messenger<\n ServiceName,\n DataServiceActions<ServiceName>,\n never\n >;\n\n this.#registerMessageHandlers();\n this.#setupCacheListener();\n }\n\n #registerMessageHandlers(): void {\n this.#messenger.registerActionHandler(\n `${this.name}:subscribe`,\n // @ts-expect-error TODO.\n (queryKey: QueryKey, callback: SubscriptionCallback) =>\n this.#handleSubscribe(queryKey, callback),\n );\n\n this.#messenger.registerActionHandler(\n `${this.name}:unsubscribe`,\n // @ts-expect-error TODO.\n (queryKey: QueryKey, callback: SubscriptionCallback) =>\n this.#handleUnsubscribe(queryKey, callback),\n );\n\n this.#messenger.registerActionHandler(\n `${this.name}:invalidateQueries`,\n // @ts-expect-error TODO.\n (filters?: InvalidateQueryFilters<Json>, options?: InvalidateOptions) =>\n this.invalidateQueries(filters, options),\n );\n }\n\n #setupCacheListener(): void {\n this.#client.getQueryCache().subscribe((event) => {\n if (this.#subscriptions.has(event.query.queryHash)) {\n this.#broadcastQueryState(event.query.queryKey);\n }\n });\n }\n\n protected async fetchQuery<\n TQueryFnData extends Json,\n TError = unknown,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n options: WithRequired<\n FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'queryKey' | 'queryFn'\n >,\n ): Promise<TData> {\n return this.#client.fetchQuery(options);\n }\n\n protected async fetchInfiniteQuery<\n TQueryFnData extends Json,\n TError = unknown,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam extends Json = Json,\n >(\n options: WithRequired<\n FetchInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'queryKey' | 'queryFn'\n >,\n pageParam?: TPageParam,\n ): Promise<TData> {\n const query = this.#client\n .getQueryCache()\n .find<TQueryFnData, TError, TData>({ queryKey: options.queryKey });\n\n if (!query || !pageParam) {\n const result = await this.#client.fetchInfiniteQuery({\n ...options,\n queryFn: (context) =>\n options.queryFn({\n ...context,\n pageParam: context.pageParam ?? pageParam,\n }),\n });\n\n return result.pages[0];\n }\n\n const { pages } = query.state.data as InfiniteData<TQueryFnData>;\n const previous = options.getPreviousPageParam?.(pages[0], pages);\n\n const direction = deepEqual(pageParam, previous) ? 'backward' : 'forward';\n\n const result = (await query.fetch(undefined, {\n meta: {\n fetchMore: {\n direction,\n pageParam,\n },\n },\n })) as InfiniteData<TData>;\n\n const pageIndex = result.pageParams.indexOf(pageParam);\n\n return result.pages[pageIndex];\n }\n\n protected async invalidateQueries<TPageData extends Json>(\n filters?: InvalidateQueryFilters<TPageData>,\n options?: InvalidateOptions,\n ): Promise<void> {\n return this.#client.invalidateQueries(filters, options);\n }\n\n // TODO: Determine if this has a better fit with `messenger.publish`.\n #handleSubscribe(\n queryKey: QueryKey,\n subscription: SubscriptionCallback,\n ): DehydratedState {\n const hash = hashQueryKey(queryKey);\n\n if (!this.#subscriptions.has(hash)) {\n this.#subscriptions.set(hash, new Set());\n }\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.#subscriptions.get(hash)!.add(subscription);\n\n return this.#getDehydratedStateForQuery(queryKey);\n }\n\n #handleUnsubscribe(\n queryKey: QueryKey,\n subscription: SubscriptionCallback,\n ): void {\n const hash = hashQueryKey(queryKey);\n const subscribers = this.#subscriptions.get(hash);\n\n subscribers?.delete(subscription);\n if (subscribers?.size === 0) {\n this.#subscriptions.delete(hash);\n }\n }\n\n #getDehydratedStateForQuery(queryKey: QueryKey): DehydratedState {\n const hash = hashQueryKey(queryKey);\n return dehydrate(this.#client, {\n shouldDehydrateQuery: (query) => query.queryHash === hash,\n });\n }\n\n #broadcastQueryState(queryKey: QueryKey): void {\n const hash = hashQueryKey(queryKey);\n const state = this.#getDehydratedStateForQuery(queryKey);\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const subscribers = this.#subscriptions.get(hash)!;\n subscribers.forEach((subscriber) =>\n subscriber({\n hash,\n state,\n }),\n );\n }\n}\n"]}
@@ -1,33 +0,0 @@
1
- import { Messenger, ActionConstraint, EventConstraint } from "@metamask/messenger";
2
- import type { Json } from "@metamask/utils";
3
- import { DehydratedState, FetchInfiniteQueryOptions, FetchQueryOptions, InvalidateOptions, InvalidateQueryFilters, QueryKey, WithRequired } from "@tanstack/query-core";
4
- export type SubscriptionPayload = {
5
- hash: string;
6
- state: DehydratedState;
7
- };
8
- export type SubscriptionCallback = (payload: SubscriptionPayload) => void;
9
- export type DataServiceSubscribeAction<ServiceName extends string> = {
10
- type: `${ServiceName}:subscribe`;
11
- handler: (queryKey: QueryKey, callback: SubscriptionCallback) => DehydratedState;
12
- };
13
- export type DataServiceUnsubscribeAction<ServiceName extends string> = {
14
- type: `${ServiceName}:unsubscribe`;
15
- handler: (queryKey: QueryKey, callback: SubscriptionCallback) => void;
16
- };
17
- export type DataServiceInvalidateQueriesAction<ServiceName extends string> = {
18
- type: `${ServiceName}:invalidateQueries`;
19
- handler: (filters?: InvalidateQueryFilters<Json>, options?: InvalidateOptions) => Promise<void>;
20
- };
21
- export type DataServiceActions<ServiceName extends string> = DataServiceSubscribeAction<ServiceName> | DataServiceUnsubscribeAction<ServiceName> | DataServiceInvalidateQueriesAction<ServiceName>;
22
- export declare class BaseDataService<ServiceName extends string, ServiceMessenger extends Messenger<ServiceName, ActionConstraint, EventConstraint, any>> {
23
- #private;
24
- readonly name: ServiceName;
25
- constructor({ name, messenger, }: {
26
- name: ServiceName;
27
- messenger: ServiceMessenger;
28
- });
29
- protected fetchQuery<TQueryFnData extends Json, TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: WithRequired<FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey' | 'queryFn'>): Promise<TData>;
30
- protected fetchInfiniteQuery<TQueryFnData extends Json, TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam extends Json = Json>(options: WithRequired<FetchInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey' | 'queryFn'>, pageParam?: TPageParam): Promise<TData>;
31
- protected invalidateQueries<TPageData extends Json>(filters?: InvalidateQueryFilters<TPageData>, options?: InvalidateOptions): Promise<void>;
32
- }
33
- //# sourceMappingURL=BaseDataService.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BaseDataService.d.cts","sourceRoot":"","sources":["../src/BaseDataService.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,eAAe,EAChB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAC5C,OAAO,EACL,eAAe,EACf,yBAAyB,EACzB,iBAAiB,EAEjB,iBAAiB,EACjB,sBAAsB,EAEtB,QAAQ,EACR,YAAY,EAGb,6BAA6B;AAG9B,MAAM,MAAM,mBAAmB,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,eAAe,CAAA;CAAE,CAAC;AAC3E,MAAM,MAAM,oBAAoB,GAAG,CAAC,OAAO,EAAE,mBAAmB,KAAK,IAAI,CAAC;AAE1E,MAAM,MAAM,0BAA0B,CAAC,WAAW,SAAS,MAAM,IAAI;IACnE,IAAI,EAAE,GAAG,WAAW,YAAY,CAAC;IACjC,OAAO,EAAE,CACP,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,oBAAoB,KAC3B,eAAe,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,4BAA4B,CAAC,WAAW,SAAS,MAAM,IAAI;IACrE,IAAI,EAAE,GAAG,WAAW,cAAc,CAAC;IACnC,OAAO,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,oBAAoB,KAAK,IAAI,CAAC;CACvE,CAAC;AAEF,MAAM,MAAM,kCAAkC,CAAC,WAAW,SAAS,MAAM,IAAI;IAC3E,IAAI,EAAE,GAAG,WAAW,oBAAoB,CAAC;IACzC,OAAO,EAAE,CACP,OAAO,CAAC,EAAE,sBAAsB,CAAC,IAAI,CAAC,EACtC,OAA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AAE,iBAAiB,GAC1B,OAAO,CAAC,IAAI,CAAC;CAsDjB"}
@@ -1,33 +0,0 @@
1
- import { Messenger, ActionConstraint, EventConstraint } from "@metamask/messenger";
2
- import type { Json } from "@metamask/utils";
3
- import { DehydratedState, FetchInfiniteQueryOptions, FetchQueryOptions, InvalidateOptions, InvalidateQueryFilters, QueryKey, WithRequired } from "@tanstack/query-core";
4
- export type SubscriptionPayload = {
5
- hash: string;
6
- state: DehydratedState;
7
- };
8
- export type SubscriptionCallback = (payload: SubscriptionPayload) => void;
9
- export type DataServiceSubscribeAction<ServiceName extends string> = {
10
- type: `${ServiceName}:subscribe`;
11
- handler: (queryKey: QueryKey, callback: SubscriptionCallback) => DehydratedState;
12
- };
13
- export type DataServiceUnsubscribeAction<ServiceName extends string> = {
14
- type: `${ServiceName}:unsubscribe`;
15
- handler: (queryKey: QueryKey, callback: SubscriptionCallback) => void;
16
- };
17
- export type DataServiceInvalidateQueriesAction<ServiceName extends string> = {
18
- type: `${ServiceName}:invalidateQueries`;
19
- handler: (filters?: InvalidateQueryFilters<Json>, options?: InvalidateOptions) => Promise<void>;
20
- };
21
- export type DataServiceActions<ServiceName extends string> = DataServiceSubscribeAction<ServiceName> | DataServiceUnsubscribeAction<ServiceName> | DataServiceInvalidateQueriesAction<ServiceName>;
22
- export declare class BaseDataService<ServiceName extends string, ServiceMessenger extends Messenger<ServiceName, ActionConstraint, EventConstraint, any>> {
23
- #private;
24
- readonly name: ServiceName;
25
- constructor({ name, messenger, }: {
26
- name: ServiceName;
27
- messenger: ServiceMessenger;
28
- });
29
- protected fetchQuery<TQueryFnData extends Json, TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: WithRequired<FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey' | 'queryFn'>): Promise<TData>;
30
- protected fetchInfiniteQuery<TQueryFnData extends Json, TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam extends Json = Json>(options: WithRequired<FetchInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey' | 'queryFn'>, pageParam?: TPageParam): Promise<TData>;
31
- protected invalidateQueries<TPageData extends Json>(filters?: InvalidateQueryFilters<TPageData>, options?: InvalidateOptions): Promise<void>;
32
- }
33
- //# sourceMappingURL=BaseDataService.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BaseDataService.d.mts","sourceRoot":"","sources":["../src/BaseDataService.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,eAAe,EAChB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAC5C,OAAO,EACL,eAAe,EACf,yBAAyB,EACzB,iBAAiB,EAEjB,iBAAiB,EACjB,sBAAsB,EAEtB,QAAQ,EACR,YAAY,EAGb,6BAA6B;AAG9B,MAAM,MAAM,mBAAmB,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,eAAe,CAAA;CAAE,CAAC;AAC3E,MAAM,MAAM,oBAAoB,GAAG,CAAC,OAAO,EAAE,mBAAmB,KAAK,IAAI,CAAC;AAE1E,MAAM,MAAM,0BAA0B,CAAC,WAAW,SAAS,MAAM,IAAI;IACnE,IAAI,EAAE,GAAG,WAAW,YAAY,CAAC;IACjC,OAAO,EAAE,CACP,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,oBAAoB,KAC3B,eAAe,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,4BAA4B,CAAC,WAAW,SAAS,MAAM,IAAI;IACrE,IAAI,EAAE,GAAG,WAAW,cAAc,CAAC;IACnC,OAAO,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,oBAAoB,KAAK,IAAI,CAAC;CACvE,CAAC;AAEF,MAAM,MAAM,kCAAkC,CAAC,WAAW,SAAS,MAAM,IAAI;IAC3E,IAAI,EAAE,GAAG,WAAW,oBAAoB,CAAC;IACzC,OA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tD,OAAO,CAAC,EAAE,sBAAsB,CAAC,SAAS,CAAC,EAC3C,OAAO,CAAC,EAAE,iBAAiB,GAC1B,OAAO,CAAC,IAAI,CAAC;CAsDjB"}
@@ -1,115 +0,0 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _BaseDataService_instances, _BaseDataService_messenger, _BaseDataService_client, _BaseDataService_subscriptions, _BaseDataService_registerMessageHandlers, _BaseDataService_setupCacheListener, _BaseDataService_handleSubscribe, _BaseDataService_handleUnsubscribe, _BaseDataService_getDehydratedStateForQuery, _BaseDataService_broadcastQueryState;
13
- function $importDefault(module) {
14
- if (module?.__esModule) {
15
- return module.default;
16
- }
17
- return module;
18
- }
19
- import { Messenger } from "@metamask/messenger";
20
- import { QueryClient, dehydrate, hashQueryKey } from "@tanstack/query-core";
21
- import $deepEqual from "fast-deep-equal";
22
- const deepEqual = $importDefault($deepEqual);
23
- export class BaseDataService {
24
- constructor({ name, messenger, }) {
25
- _BaseDataService_instances.add(this);
26
- _BaseDataService_messenger.set(this, void 0);
27
- _BaseDataService_client.set(this, new QueryClient());
28
- _BaseDataService_subscriptions.set(this, new Map());
29
- this.name = name;
30
- __classPrivateFieldSet(this, _BaseDataService_messenger, messenger, "f");
31
- __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_registerMessageHandlers).call(this);
32
- __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_setupCacheListener).call(this);
33
- }
34
- async fetchQuery(options) {
35
- return __classPrivateFieldGet(this, _BaseDataService_client, "f").fetchQuery(options);
36
- }
37
- async fetchInfiniteQuery(options, pageParam) {
38
- const query = __classPrivateFieldGet(this, _BaseDataService_client, "f")
39
- .getQueryCache()
40
- .find({ queryKey: options.queryKey });
41
- if (!query || !pageParam) {
42
- const result = await __classPrivateFieldGet(this, _BaseDataService_client, "f").fetchInfiniteQuery({
43
- ...options,
44
- queryFn: (context) => options.queryFn({
45
- ...context,
46
- pageParam: context.pageParam ?? pageParam,
47
- }),
48
- });
49
- return result.pages[0];
50
- }
51
- const { pages } = query.state.data;
52
- const previous = options.getPreviousPageParam?.(pages[0], pages);
53
- const direction = deepEqual(pageParam, previous) ? 'backward' : 'forward';
54
- const result = (await query.fetch(undefined, {
55
- meta: {
56
- fetchMore: {
57
- direction,
58
- pageParam,
59
- },
60
- },
61
- }));
62
- const pageIndex = result.pageParams.indexOf(pageParam);
63
- return result.pages[pageIndex];
64
- }
65
- async invalidateQueries(filters, options) {
66
- return __classPrivateFieldGet(this, _BaseDataService_client, "f").invalidateQueries(filters, options);
67
- }
68
- }
69
- _BaseDataService_messenger = new WeakMap(), _BaseDataService_client = new WeakMap(), _BaseDataService_subscriptions = new WeakMap(), _BaseDataService_instances = new WeakSet(), _BaseDataService_registerMessageHandlers = function _BaseDataService_registerMessageHandlers() {
70
- __classPrivateFieldGet(this, _BaseDataService_messenger, "f").registerActionHandler(`${this.name}:subscribe`,
71
- // @ts-expect-error TODO.
72
- (queryKey, callback) => __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_handleSubscribe).call(this, queryKey, callback));
73
- __classPrivateFieldGet(this, _BaseDataService_messenger, "f").registerActionHandler(`${this.name}:unsubscribe`,
74
- // @ts-expect-error TODO.
75
- (queryKey, callback) => __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_handleUnsubscribe).call(this, queryKey, callback));
76
- __classPrivateFieldGet(this, _BaseDataService_messenger, "f").registerActionHandler(`${this.name}:invalidateQueries`,
77
- // @ts-expect-error TODO.
78
- (filters, options) => this.invalidateQueries(filters, options));
79
- }, _BaseDataService_setupCacheListener = function _BaseDataService_setupCacheListener() {
80
- __classPrivateFieldGet(this, _BaseDataService_client, "f").getQueryCache().subscribe((event) => {
81
- if (__classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").has(event.query.queryHash)) {
82
- __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_broadcastQueryState).call(this, event.query.queryKey);
83
- }
84
- });
85
- }, _BaseDataService_handleSubscribe = function _BaseDataService_handleSubscribe(queryKey, subscription) {
86
- const hash = hashQueryKey(queryKey);
87
- if (!__classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").has(hash)) {
88
- __classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").set(hash, new Set());
89
- }
90
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
91
- __classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").get(hash).add(subscription);
92
- return __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_getDehydratedStateForQuery).call(this, queryKey);
93
- }, _BaseDataService_handleUnsubscribe = function _BaseDataService_handleUnsubscribe(queryKey, subscription) {
94
- const hash = hashQueryKey(queryKey);
95
- const subscribers = __classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").get(hash);
96
- subscribers?.delete(subscription);
97
- if (subscribers?.size === 0) {
98
- __classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").delete(hash);
99
- }
100
- }, _BaseDataService_getDehydratedStateForQuery = function _BaseDataService_getDehydratedStateForQuery(queryKey) {
101
- const hash = hashQueryKey(queryKey);
102
- return dehydrate(__classPrivateFieldGet(this, _BaseDataService_client, "f"), {
103
- shouldDehydrateQuery: (query) => query.queryHash === hash,
104
- });
105
- }, _BaseDataService_broadcastQueryState = function _BaseDataService_broadcastQueryState(queryKey) {
106
- const hash = hashQueryKey(queryKey);
107
- const state = __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_getDehydratedStateForQuery).call(this, queryKey);
108
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
109
- const subscribers = __classPrivateFieldGet(this, _BaseDataService_subscriptions, "f").get(hash);
110
- subscribers.forEach((subscriber) => subscriber({
111
- hash,
112
- state,
113
- }));
114
- };
115
- //# sourceMappingURL=BaseDataService.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BaseDataService.mjs","sourceRoot":"","sources":["../src/BaseDataService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,EACL,SAAS,EAGV,4BAA4B;AAE7B,OAAO,EAOL,WAAW,EAGX,SAAS,EACT,YAAY,EACb,6BAA6B;AAC9B,OAAO,UAAS,wBAAwB;;AA+BxC,MAAM,OAAO,eAAe;IAwB1B,YAAY,EACV,IAAI,EACJ,SAAS,GAIV;;QAhBQ,6CAIP;QAEO,kCAAU,IAAI,WAAW,EAAE,EAAC;QAE5B,yCAAyD,IAAI,GAAG,EAAE,EAAC;QAS1E,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,uBAAA,IAAI,8BAAc,SAIjB,MAAA,CAAC;QAEF,uBAAA,IAAI,4EAAyB,MAA7B,IAAI,CAA2B,CAAC;QAChC,uBAAA,IAAI,uEAAoB,MAAxB,IAAI,CAAsB,CAAC;IAC7B,CAAC;IAiCS,KAAK,CAAC,UAAU,CAMxB,OAGC;QAED,OAAO,uBAAA,IAAI,+BAAQ,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAC1C,CAAC;IAES,KAAK,CAAC,kBAAkB,CAOhC,OAGC,EACD,SAAsB;QAEtB,MAAM,KAAK,GAAG,uBAAA,IAAI,+BAAQ;aACvB,aAAa,EAAE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\n} from '@metamask/messenger';\nimport type { Json } from '@metamask/utils';\nimport {\n DehydratedState,\n FetchInfiniteQueryOptions,\n FetchQueryOptions,\n InfiniteData,\n InvalidateOptions,\n InvalidateQueryFilters,\n QueryClient,\n QueryKey,\n WithRequired,\n dehydrate,\n hashQueryKey,\n} from '@tanstack/query-core';\nimport deepEqual from 'fast-deep-equal';\n\nexport type SubscriptionPayload = { hash: string; state: DehydratedState };\nexport type SubscriptionCallback = (payload: SubscriptionPayload) => void;\n\nexport type DataServiceSubscribeAction<ServiceName extends string> = {\n type: `${ServiceName}:subscribe`;\n handler: (\n queryKey: QueryKey,\n callback: SubscriptionCallback,\n ) => DehydratedState;\n};\n\nexport type DataServiceUnsubscribeAction<ServiceName extends string> = {\n type: `${ServiceName}:unsubscribe`;\n handler: (queryKey: QueryKey, callback: SubscriptionCallback) => void;\n};\n\nexport type DataServiceInvalidateQueriesAction<ServiceName extends string> = {\n type: `${ServiceName}:invalidateQueries`;\n handler: (\n filters?: InvalidateQueryFilters<Json>,\n options?: InvalidateOptions,\n ) => Promise<void>;\n};\n\nexport type DataServiceActions<ServiceName extends string> =\n | DataServiceSubscribeAction<ServiceName>\n | DataServiceUnsubscribeAction<ServiceName>\n | DataServiceInvalidateQueriesAction<ServiceName>;\n\nexport class BaseDataService<\n ServiceName extends string,\n ServiceMessenger extends Messenger<\n ServiceName,\n ActionConstraint,\n EventConstraint,\n // Use `any` to allow any parent to be set. `any` is harmless in a type constraint anyway,\n // it's the one totally safe place to use it.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n >,\n> {\n public readonly name: ServiceName;\n\n readonly #messenger: Messenger<\n ServiceName,\n DataServiceActions<ServiceName>,\n never\n >;\n\n readonly #client = new QueryClient();\n\n readonly #subscriptions: Map<string, Set<SubscriptionCallback>> = new Map();\n\n constructor({\n name,\n messenger,\n }: {\n name: ServiceName;\n messenger: ServiceMessenger;\n }) {\n this.name = name;\n\n this.#messenger = messenger as unknown as Messenger<\n ServiceName,\n DataServiceActions<ServiceName>,\n never\n >;\n\n this.#registerMessageHandlers();\n this.#setupCacheListener();\n }\n\n #registerMessageHandlers(): void {\n this.#messenger.registerActionHandler(\n `${this.name}:subscribe`,\n // @ts-expect-error TODO.\n (queryKey: QueryKey, callback: SubscriptionCallback) =>\n this.#handleSubscribe(queryKey, callback),\n );\n\n this.#messenger.registerActionHandler(\n `${this.name}:unsubscribe`,\n // @ts-expect-error TODO.\n (queryKey: QueryKey, callback: SubscriptionCallback) =>\n this.#handleUnsubscribe(queryKey, callback),\n );\n\n this.#messenger.registerActionHandler(\n `${this.name}:invalidateQueries`,\n // @ts-expect-error TODO.\n (filters?: InvalidateQueryFilters<Json>, options?: InvalidateOptions) =>\n this.invalidateQueries(filters, options),\n );\n }\n\n #setupCacheListener(): void {\n this.#client.getQueryCache().subscribe((event) => {\n if (this.#subscriptions.has(event.query.queryHash)) {\n this.#broadcastQueryState(event.query.queryKey);\n }\n });\n }\n\n protected async fetchQuery<\n TQueryFnData extends Json,\n TError = unknown,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n options: WithRequired<\n FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'queryKey' | 'queryFn'\n >,\n ): Promise<TData> {\n return this.#client.fetchQuery(options);\n }\n\n protected async fetchInfiniteQuery<\n TQueryFnData extends Json,\n TError = unknown,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam extends Json = Json,\n >(\n options: WithRequired<\n FetchInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'queryKey' | 'queryFn'\n >,\n pageParam?: TPageParam,\n ): Promise<TData> {\n const query = this.#client\n .getQueryCache()\n .find<TQueryFnData, TError, TData>({ queryKey: options.queryKey });\n\n if (!query || !pageParam) {\n const result = await this.#client.fetchInfiniteQuery({\n ...options,\n queryFn: (context) =>\n options.queryFn({\n ...context,\n pageParam: context.pageParam ?? pageParam,\n }),\n });\n\n return result.pages[0];\n }\n\n const { pages } = query.state.data as InfiniteData<TQueryFnData>;\n const previous = options.getPreviousPageParam?.(pages[0], pages);\n\n const direction = deepEqual(pageParam, previous) ? 'backward' : 'forward';\n\n const result = (await query.fetch(undefined, {\n meta: {\n fetchMore: {\n direction,\n pageParam,\n },\n },\n })) as InfiniteData<TData>;\n\n const pageIndex = result.pageParams.indexOf(pageParam);\n\n return result.pages[pageIndex];\n }\n\n protected async invalidateQueries<TPageData extends Json>(\n filters?: InvalidateQueryFilters<TPageData>,\n options?: InvalidateOptions,\n ): Promise<void> {\n return this.#client.invalidateQueries(filters, options);\n }\n\n // TODO: Determine if this has a better fit with `messenger.publish`.\n #handleSubscribe(\n queryKey: QueryKey,\n subscription: SubscriptionCallback,\n ): DehydratedState {\n const hash = hashQueryKey(queryKey);\n\n if (!this.#subscriptions.has(hash)) {\n this.#subscriptions.set(hash, new Set());\n }\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.#subscriptions.get(hash)!.add(subscription);\n\n return this.#getDehydratedStateForQuery(queryKey);\n }\n\n #handleUnsubscribe(\n queryKey: QueryKey,\n subscription: SubscriptionCallback,\n ): void {\n const hash = hashQueryKey(queryKey);\n const subscribers = this.#subscriptions.get(hash);\n\n subscribers?.delete(subscription);\n if (subscribers?.size === 0) {\n this.#subscriptions.delete(hash);\n }\n }\n\n #getDehydratedStateForQuery(queryKey: QueryKey): DehydratedState {\n const hash = hashQueryKey(queryKey);\n return dehydrate(this.#client, {\n shouldDehydrateQuery: (query) => query.queryHash === hash,\n });\n }\n\n #broadcastQueryState(queryKey: QueryKey): void {\n const hash = hashQueryKey(queryKey);\n const state = this.#getDehydratedStateForQuery(queryKey);\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const subscribers = this.#subscriptions.get(hash)!;\n subscribers.forEach((subscriber) =>\n subscriber({\n hash,\n state,\n }),\n );\n }\n}\n"]}
@@ -1,83 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createUIQueryClient = void 0;
4
- const utils_1 = require("@metamask/utils");
5
- const query_core_1 = require("@tanstack/query-core");
6
- function getServiceFromQueryKey(queryKey) {
7
- return queryKey[0].split(':')[0];
8
- }
9
- /**
10
- * Create a QueryClient queries and subscribes to data services using the messenger.
11
- *
12
- * @param dataServices - A list of data services.
13
- * @param messenger - A messenger adapter.
14
- * @returns The QueryClient.
15
- */
16
- function createUIQueryClient(dataServices, messenger) {
17
- const subscriptions = new Set();
18
- const client = new query_core_1.QueryClient({
19
- defaultOptions: {
20
- queries: {
21
- queryFn: async (options) => {
22
- const { queryKey } = options;
23
- const action = queryKey?.[0];
24
- (0, utils_1.assert)(typeof action === 'string', 'The first element of a query key must be a string.');
25
- (0, utils_1.assert)(dataServices.includes(action?.split(':')?.[0]), 'Queries must use data service actions.');
26
- return (await messenger.call(action, ...options.queryKey.slice(1), options.pageParam));
27
- },
28
- // TODO: Decide on values for these.
29
- staleTime: Infinity,
30
- refetchOnWindowFocus: false,
31
- refetchOnReconnect: false,
32
- refetchOnMount: false,
33
- },
34
- },
35
- });
36
- client.getQueryCache().subscribe((event) => {
37
- const { query } = event;
38
- const hash = query.queryHash;
39
- const hasSubscription = subscriptions.has(hash);
40
- const observerCount = query.getObserversCount();
41
- const service = getServiceFromQueryKey(query.queryKey);
42
- if (!hasSubscription &&
43
- event.type === 'observerAdded' &&
44
- observerCount === 1) {
45
- subscriptions.add(hash);
46
- // This is a bit of a mess because we can't pass functions across the process boundary, so we call subscribe
47
- // but also register listeners for :cacheUpdate which will be sent to subscribed processes
48
- // TODO: Unsubscribe
49
- messenger.subscribe(`${service}:cacheUpdate`, (data) => {
50
- const castData = data;
51
- if (subscriptions.has(castData.hash)) {
52
- (0, query_core_1.hydrate)(client, castData.state);
53
- }
54
- });
55
- messenger
56
- .call(`${service}:subscribe`, query.queryKey)
57
- .then((state) => (0, query_core_1.hydrate)(client, state))
58
- .catch(console.error);
59
- }
60
- else if (event.type === 'observerRemoved' &&
61
- observerCount === 0 &&
62
- hasSubscription) {
63
- subscriptions.delete(hash);
64
- messenger
65
- .call(`${service}:unsubscribe`, query.queryKey)
66
- .catch(console.error);
67
- }
68
- });
69
- // Override invalidateQueries to ensure the data service is invalidated as well.
70
- const originalInvalidate = client.invalidateQueries.bind(client);
71
- // @ts-expect-error TODO.
72
- client.invalidateQueries = async (filters, options) => {
73
- const queries = client.getQueryCache().findAll(filters);
74
- await Promise.all(queries.map((query) => {
75
- const service = getServiceFromQueryKey(query.queryKey);
76
- return messenger.call(`${service}:invalidateQueries`, filters, options);
77
- }));
78
- return originalInvalidate(filters, options);
79
- };
80
- return client;
81
- }
82
- exports.createUIQueryClient = createUIQueryClient;
83
- //# sourceMappingURL=createUIQueryClient.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createUIQueryClient.cjs","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":";;;AAAA,2CAA+C;AAC/C,qDAK8B;AAI9B,SAAS,sBAAsB,CAAC,QAAkB;IAChD,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;AACnC,CAAC;AAQD;;;;;;GAMG;AACH,SAAgB,mBAAmB,CACjC,YAAsB,EACtB,SAA2B;IAE3B,MAAM,aAAa,GAAG,IAAI,GAAG,EAAU,CAAC;IAExC,MAAM,MAAM,GAAgB,IAAI,wBAAW,CAAC;QAC1C,cAAc,EAAE;YACd,OAAO,EAAE;gBACP,OAAO,EAAE,KAAK,EAAE,OAAO,EAAiB,EAAE;oBACxC,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;oBAE7B,MAAM,MAAM,GAAG,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC;oBAE7B,IAAA,cAAM,EACJ,OAAO,MAAM,KAAK,QAAQ,EAC1B,oDAAoD,CACrD,CAAC;oBACF,IAAA,cAAM,EACJ,YAAY,CAAC,QAAQ,CAAC,MAAM,EAAE,KAA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GD,kDAyGC","sourcesContent":["import { assert, Json } from '@metamask/utils';\nimport {\n hydrate,\n QueryClient,\n InvalidateQueryFilters,\n InvalidateOptions,\n} from '@tanstack/query-core';\n\ntype QueryKey = readonly [string, ...Json[]];\n\nfunction getServiceFromQueryKey(queryKey: QueryKey): string {\n return queryKey[0].split(':')[0];\n}\n\n// When UI messengers are available this should simply be a proper messenger that allows access to DataServiceActions\ntype MessengerAdapter = {\n call: (method: string, ...params: Json[]) => Promise<Json | void>;\n subscribe: (method: string, callback: (data: Json) => void) => 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 * @returns The QueryClient.\n */\nexport function createUIQueryClient(\n dataServices: string[],\n messenger: MessengerAdapter,\n): QueryClient {\n const subscriptions = new Set<string>();\n\n const client: QueryClient = new QueryClient({\n defaultOptions: {\n queries: {\n queryFn: async (options): Promise<Json> => {\n const { queryKey } = options;\n\n const action = queryKey?.[0];\n\n assert(\n typeof action === 'string',\n 'The first element of a query key must be a string.',\n );\n assert(\n dataServices.includes(action?.split(':')?.[0]),\n 'Queries must use data service actions.',\n );\n\n return (await messenger.call(\n action,\n ...(options.queryKey.slice(1) as Json[]),\n options.pageParam,\n )) as Json;\n },\n // TODO: Decide on values for these.\n staleTime: Infinity,\n refetchOnWindowFocus: false,\n refetchOnReconnect: false,\n refetchOnMount: false,\n },\n },\n });\n\n client.getQueryCache().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 = getServiceFromQueryKey(query.queryKey);\n\n if (\n !hasSubscription &&\n event.type === 'observerAdded' &&\n observerCount === 1\n ) {\n subscriptions.add(hash);\n\n // This is a bit of a mess because we can't pass functions across the process boundary, so we call subscribe\n // but also register listeners for :cacheUpdate which will be sent to subscribed processes\n // TODO: Unsubscribe\n messenger.subscribe(`${service}:cacheUpdate`, (data) => {\n const castData = data as { hash: string; state: Json };\n if (subscriptions.has(castData.hash)) {\n hydrate(client, castData.state);\n }\n });\n\n messenger\n .call(`${service}:subscribe`, query.queryKey)\n .then((state) => hydrate(client, state))\n .catch(console.error);\n } else if (\n event.type === 'observerRemoved' &&\n observerCount === 0 &&\n hasSubscription\n ) {\n subscriptions.delete(hash);\n messenger\n .call(`${service}:unsubscribe`, query.queryKey)\n .catch(console.error);\n }\n });\n\n // Override invalidateQueries to ensure the data service is invalidated as well.\n const originalInvalidate = client.invalidateQueries.bind(client);\n\n // @ts-expect-error TODO.\n client.invalidateQueries = async (\n filters?: InvalidateQueryFilters<Json>,\n options?: InvalidateOptions,\n ): Promise<void> => {\n const queries = client.getQueryCache().findAll(filters);\n await Promise.all(\n queries.map((query) => {\n const service = getServiceFromQueryKey(query.queryKey as QueryKey);\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,16 +0,0 @@
1
- import { Json } from "@metamask/utils";
2
- import { QueryClient } from "@tanstack/query-core";
3
- type MessengerAdapter = {
4
- call: (method: string, ...params: Json[]) => Promise<Json | void>;
5
- subscribe: (method: string, callback: (data: Json) => void) => void;
6
- };
7
- /**
8
- * Create a QueryClient queries and subscribes to data services using the messenger.
9
- *
10
- * @param dataServices - A list of data services.
11
- * @param messenger - A messenger adapter.
12
- * @returns The QueryClient.
13
- */
14
- export declare function createUIQueryClient(dataServices: string[], messenger: MessengerAdapter): QueryClient;
15
- export {};
16
- //# sourceMappingURL=createUIQueryClient.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createUIQueryClient.d.cts","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,IAAI,EAAE,wBAAwB;AAC/C,OAAO,EAEL,WAAW,EAGZ,6BAA6B;AAS9B,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,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,KAAK,IAAI,CAAC;CACrE,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,gBAAgB,GAC1B,WAAW,CAsGb"}
@@ -1,16 +0,0 @@
1
- import { Json } from "@metamask/utils";
2
- import { QueryClient } from "@tanstack/query-core";
3
- type MessengerAdapter = {
4
- call: (method: string, ...params: Json[]) => Promise<Json | void>;
5
- subscribe: (method: string, callback: (data: Json) => void) => void;
6
- };
7
- /**
8
- * Create a QueryClient queries and subscribes to data services using the messenger.
9
- *
10
- * @param dataServices - A list of data services.
11
- * @param messenger - A messenger adapter.
12
- * @returns The QueryClient.
13
- */
14
- export declare function createUIQueryClient(dataServices: string[], messenger: MessengerAdapter): QueryClient;
15
- export {};
16
- //# sourceMappingURL=createUIQueryClient.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createUIQueryClient.d.mts","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,IAAI,EAAE,wBAAwB;AAC/C,OAAO,EAEL,WAAW,EAGZ,6BAA6B;AAS9B,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,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,KAAK,IAAI,CAAC;CACrE,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,gBAAgB,GAC1B,WAAW,CAsGb"}
@@ -1,79 +0,0 @@
1
- import { assert } from "@metamask/utils";
2
- import { hydrate, QueryClient } from "@tanstack/query-core";
3
- function getServiceFromQueryKey(queryKey) {
4
- return queryKey[0].split(':')[0];
5
- }
6
- /**
7
- * Create a QueryClient queries and subscribes to data services using the messenger.
8
- *
9
- * @param dataServices - A list of data services.
10
- * @param messenger - A messenger adapter.
11
- * @returns The QueryClient.
12
- */
13
- export function createUIQueryClient(dataServices, messenger) {
14
- const subscriptions = new Set();
15
- const client = new QueryClient({
16
- defaultOptions: {
17
- queries: {
18
- queryFn: async (options) => {
19
- const { queryKey } = options;
20
- const action = queryKey?.[0];
21
- assert(typeof action === 'string', 'The first element of a query key must be a string.');
22
- assert(dataServices.includes(action?.split(':')?.[0]), 'Queries must use data service actions.');
23
- return (await messenger.call(action, ...options.queryKey.slice(1), options.pageParam));
24
- },
25
- // TODO: Decide on values for these.
26
- staleTime: Infinity,
27
- refetchOnWindowFocus: false,
28
- refetchOnReconnect: false,
29
- refetchOnMount: false,
30
- },
31
- },
32
- });
33
- client.getQueryCache().subscribe((event) => {
34
- const { query } = event;
35
- const hash = query.queryHash;
36
- const hasSubscription = subscriptions.has(hash);
37
- const observerCount = query.getObserversCount();
38
- const service = getServiceFromQueryKey(query.queryKey);
39
- if (!hasSubscription &&
40
- event.type === 'observerAdded' &&
41
- observerCount === 1) {
42
- subscriptions.add(hash);
43
- // This is a bit of a mess because we can't pass functions across the process boundary, so we call subscribe
44
- // but also register listeners for :cacheUpdate which will be sent to subscribed processes
45
- // TODO: Unsubscribe
46
- messenger.subscribe(`${service}:cacheUpdate`, (data) => {
47
- const castData = data;
48
- if (subscriptions.has(castData.hash)) {
49
- hydrate(client, castData.state);
50
- }
51
- });
52
- messenger
53
- .call(`${service}:subscribe`, query.queryKey)
54
- .then((state) => hydrate(client, state))
55
- .catch(console.error);
56
- }
57
- else if (event.type === 'observerRemoved' &&
58
- observerCount === 0 &&
59
- hasSubscription) {
60
- subscriptions.delete(hash);
61
- messenger
62
- .call(`${service}:unsubscribe`, query.queryKey)
63
- .catch(console.error);
64
- }
65
- });
66
- // Override invalidateQueries to ensure the data service is invalidated as well.
67
- const originalInvalidate = client.invalidateQueries.bind(client);
68
- // @ts-expect-error TODO.
69
- client.invalidateQueries = async (filters, options) => {
70
- const queries = client.getQueryCache().findAll(filters);
71
- await Promise.all(queries.map((query) => {
72
- const service = getServiceFromQueryKey(query.queryKey);
73
- return messenger.call(`${service}:invalidateQueries`, filters, options);
74
- }));
75
- return originalInvalidate(filters, options);
76
- };
77
- return client;
78
- }
79
- //# sourceMappingURL=createUIQueryClient.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createUIQueryClient.mjs","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAQ,wBAAwB;AAC/C,OAAO,EACL,OAAO,EACP,WAAW,EAGZ,6BAA6B;AAI9B,SAAS,sBAAsB,CAAC,QAAkB;IAChD,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;AACnC,CAAC;AAQD;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CACjC,YAAsB,EACtB,SAA2B;IAE3B,MAAM,aAAa,GAAG,IAAI,GAAG,EAAU,CAAC;IAExC,MAAM,MAAM,GAAgB,IAAI,WAAW,CAAC;QAC1C,cAAc,EAAE;YACd,OAAO,EAAE;gBACP,OAAO,EAAE,KAAK,EAAE,OAAO,EAAiB,EAAE;oBACxC,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;oBAE7B,MAAM,MAAM,GAAG,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC;oBAE7B,MAAM,CACJ,OAAO,MAAM,KAAK,QAAQ,EAC1B,oDAAoD,CACrD,CAAC;oBACF,MAAM,CACJ,YAA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,CAAC;AAChB,CAAC","sourcesContent":["import { assert, Json } from '@metamask/utils';\nimport {\n hydrate,\n QueryClient,\n InvalidateQueryFilters,\n InvalidateOptions,\n} from '@tanstack/query-core';\n\ntype QueryKey = readonly [string, ...Json[]];\n\nfunction getServiceFromQueryKey(queryKey: QueryKey): string {\n return queryKey[0].split(':')[0];\n}\n\n// When UI messengers are available this should simply be a proper messenger that allows access to DataServiceActions\ntype MessengerAdapter = {\n call: (method: string, ...params: Json[]) => Promise<Json | void>;\n subscribe: (method: string, callback: (data: Json) => void) => 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 * @returns The QueryClient.\n */\nexport function createUIQueryClient(\n dataServices: string[],\n messenger: MessengerAdapter,\n): QueryClient {\n const subscriptions = new Set<string>();\n\n const client: QueryClient = new QueryClient({\n defaultOptions: {\n queries: {\n queryFn: async (options): Promise<Json> => {\n const { queryKey } = options;\n\n const action = queryKey?.[0];\n\n assert(\n typeof action === 'string',\n 'The first element of a query key must be a string.',\n );\n assert(\n dataServices.includes(action?.split(':')?.[0]),\n 'Queries must use data service actions.',\n );\n\n return (await messenger.call(\n action,\n ...(options.queryKey.slice(1) as Json[]),\n options.pageParam,\n )) as Json;\n },\n // TODO: Decide on values for these.\n staleTime: Infinity,\n refetchOnWindowFocus: false,\n refetchOnReconnect: false,\n refetchOnMount: false,\n },\n },\n });\n\n client.getQueryCache().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 = getServiceFromQueryKey(query.queryKey);\n\n if (\n !hasSubscription &&\n event.type === 'observerAdded' &&\n observerCount === 1\n ) {\n subscriptions.add(hash);\n\n // This is a bit of a mess because we can't pass functions across the process boundary, so we call subscribe\n // but also register listeners for :cacheUpdate which will be sent to subscribed processes\n // TODO: Unsubscribe\n messenger.subscribe(`${service}:cacheUpdate`, (data) => {\n const castData = data as { hash: string; state: Json };\n if (subscriptions.has(castData.hash)) {\n hydrate(client, castData.state);\n }\n });\n\n messenger\n .call(`${service}:subscribe`, query.queryKey)\n .then((state) => hydrate(client, state))\n .catch(console.error);\n } else if (\n event.type === 'observerRemoved' &&\n observerCount === 0 &&\n hasSubscription\n ) {\n subscriptions.delete(hash);\n messenger\n .call(`${service}:unsubscribe`, query.queryKey)\n .catch(console.error);\n }\n });\n\n // Override invalidateQueries to ensure the data service is invalidated as well.\n const originalInvalidate = client.invalidateQueries.bind(client);\n\n // @ts-expect-error TODO.\n client.invalidateQueries = async (\n filters?: InvalidateQueryFilters<Json>,\n options?: InvalidateOptions,\n ): Promise<void> => {\n const queries = client.getQueryCache().findAll(filters);\n await Promise.all(\n queries.map((query) => {\n const service = getServiceFromQueryKey(query.queryKey as QueryKey);\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"]}