@metamask-previews/base-data-service 0.0.0-preview-1c2b324fd → 0.0.0-preview-00245ea
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/BaseDataService.cjs +91 -0
- package/dist/BaseDataService.cjs.map +1 -0
- package/dist/BaseDataService.d.cts +33 -0
- package/dist/BaseDataService.d.cts.map +1 -0
- package/dist/BaseDataService.d.mts +33 -0
- package/dist/BaseDataService.d.mts.map +1 -0
- package/dist/BaseDataService.mjs +91 -0
- package/dist/BaseDataService.mjs.map +1 -0
- package/dist/createUIQueryClient.cjs +85 -0
- package/dist/createUIQueryClient.cjs.map +1 -0
- package/dist/createUIQueryClient.d.cts +18 -0
- package/dist/createUIQueryClient.d.cts.map +1 -0
- package/dist/createUIQueryClient.d.mts +18 -0
- package/dist/createUIQueryClient.d.mts.map +1 -0
- package/dist/createUIQueryClient.mjs +81 -0
- package/dist/createUIQueryClient.mjs.map +1 -0
- package/dist/hooks.cjs +13 -0
- package/dist/hooks.cjs.map +1 -0
- package/dist/hooks.d.cts +6 -0
- package/dist/hooks.d.cts.map +1 -0
- package/dist/hooks.d.mts +6 -0
- package/dist/hooks.d.mts.map +1 -0
- package/dist/hooks.mjs +8 -0
- package/dist/hooks.mjs.map +1 -0
- package/dist/index.cjs +17 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -7
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -7
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -1
|
@@ -0,0 +1,91 @@
|
|
|
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_registerMessageHandlers, _BaseDataService_setupCacheListener, _BaseDataService_getDehydratedState, _BaseDataService_broadcastCacheUpdate;
|
|
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
|
+
this.name = name;
|
|
28
|
+
__classPrivateFieldSet(this, _BaseDataService_messenger, messenger, "f");
|
|
29
|
+
__classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_registerMessageHandlers).call(this);
|
|
30
|
+
__classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_setupCacheListener).call(this);
|
|
31
|
+
}
|
|
32
|
+
async fetchQuery(options) {
|
|
33
|
+
return __classPrivateFieldGet(this, _BaseDataService_client, "f").fetchQuery(options);
|
|
34
|
+
}
|
|
35
|
+
async fetchInfiniteQuery(options, pageParam) {
|
|
36
|
+
const query = __classPrivateFieldGet(this, _BaseDataService_client, "f")
|
|
37
|
+
.getQueryCache()
|
|
38
|
+
.find({ queryKey: options.queryKey });
|
|
39
|
+
if (!query || !pageParam) {
|
|
40
|
+
const result = await __classPrivateFieldGet(this, _BaseDataService_client, "f").fetchInfiniteQuery({
|
|
41
|
+
...options,
|
|
42
|
+
queryFn: (context) => options.queryFn({
|
|
43
|
+
...context,
|
|
44
|
+
pageParam: context.pageParam ?? pageParam,
|
|
45
|
+
}),
|
|
46
|
+
});
|
|
47
|
+
return result.pages[0];
|
|
48
|
+
}
|
|
49
|
+
const { pages } = query.state.data;
|
|
50
|
+
const previous = options.getPreviousPageParam?.(pages[0], pages);
|
|
51
|
+
const direction = (0, fast_deep_equal_1.default)(pageParam, previous) ? 'backward' : 'forward';
|
|
52
|
+
const result = (await query.fetch(undefined, {
|
|
53
|
+
meta: {
|
|
54
|
+
fetchMore: {
|
|
55
|
+
direction,
|
|
56
|
+
pageParam,
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
}));
|
|
60
|
+
const pageIndex = result.pageParams.indexOf(pageParam);
|
|
61
|
+
return result.pages[pageIndex];
|
|
62
|
+
}
|
|
63
|
+
async invalidateQueries(filters, options) {
|
|
64
|
+
return __classPrivateFieldGet(this, _BaseDataService_client, "f").invalidateQueries(filters, options);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.BaseDataService = BaseDataService;
|
|
68
|
+
_BaseDataService_messenger = new WeakMap(), _BaseDataService_client = new WeakMap(), _BaseDataService_instances = new WeakSet(), _BaseDataService_registerMessageHandlers = function _BaseDataService_registerMessageHandlers() {
|
|
69
|
+
__classPrivateFieldGet(this, _BaseDataService_messenger, "f").registerActionHandler(`${this.name}:invalidateQueries`, (filters, options) => this.invalidateQueries(filters, options));
|
|
70
|
+
}, _BaseDataService_setupCacheListener = function _BaseDataService_setupCacheListener() {
|
|
71
|
+
__classPrivateFieldGet(this, _BaseDataService_client, "f").getQueryCache().subscribe((event) => {
|
|
72
|
+
if (['added', 'updated', 'removed'].includes(event.type)) {
|
|
73
|
+
__classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_broadcastCacheUpdate).call(this, event.query.queryKey);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}, _BaseDataService_getDehydratedState = function _BaseDataService_getDehydratedState(queryKey) {
|
|
77
|
+
const hash = (0, query_core_1.hashQueryKey)(queryKey);
|
|
78
|
+
return (0, query_core_1.dehydrate)(__classPrivateFieldGet(this, _BaseDataService_client, "f"), {
|
|
79
|
+
shouldDehydrateQuery: (query) => query.queryHash === hash,
|
|
80
|
+
});
|
|
81
|
+
}, _BaseDataService_broadcastCacheUpdate = function _BaseDataService_broadcastCacheUpdate(queryKey) {
|
|
82
|
+
const hash = (0, query_core_1.hashQueryKey)(queryKey);
|
|
83
|
+
const state = __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_getDehydratedState).call(this, queryKey);
|
|
84
|
+
const payload = {
|
|
85
|
+
hash,
|
|
86
|
+
state,
|
|
87
|
+
};
|
|
88
|
+
__classPrivateFieldGet(this, _BaseDataService_messenger, "f").publish(`${this.name}:cacheUpdate`, payload);
|
|
89
|
+
__classPrivateFieldGet(this, _BaseDataService_messenger, "f").publish(`${this.name}:cacheUpdate:${hash}`, payload);
|
|
90
|
+
};
|
|
91
|
+
//# sourceMappingURL=BaseDataService.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseDataService.cjs","sourceRoot":"","sources":["../src/BaseDataService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,mDAI6B;AAE7B,qDAY8B;AAC9B,sEAAwC;AA6BxC,M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,IAAI,+BAAQ,EAAE;QAC7B,oBAAoB,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,KAAK,IAAI;KAC1D,CAAC,CAAC;AACL,CAAC,yFAEqB,QAAkB;IACtC,MAAM,IAAI,GAAG,IAAA,yBAAY,EAAC,QAAQ,CAAC,CAAC;IACpC,MAAM,KAAK,GAAG,uBAAA,IAAI,uEAAoB,MAAxB,IAAI,EAAqB,QAAQ,CAAC,CAAC;IAEjD,MAAM,OAAO,GAAG;QACd,IAAI;QACJ,KAAK;KACN,CAAC;IAEF,uBAAA,IAAI,kCAAW,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,IAAI,cAAuB,EAAE,OAAO,CAAC,CAAC;IACtE,uBAAA,IAAI,kCAAW,CAAC,OAAO,CACrB,GAAG,IAAI,CAAC,IAAI,gBAAgB,IAAI,EAAW,EAC3C,OAAO,CACR,CAAC;AACJ,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 CacheUpdatePayload = { hash: string; state: DehydratedState };\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 DataServiceInvalidateQueriesAction<ServiceName>;\n\nexport type DataServiceCacheUpdateEvent<ServiceName extends string> = {\n type: `${ServiceName}:cacheUpdate`;\n payload: [CacheUpdatePayload];\n};\n\nexport type DataServiceGranularCacheUpdateEvent<ServiceName extends string> = {\n type: `${ServiceName}:cacheUpdate:${string}`;\n payload: [CacheUpdatePayload];\n};\n\nexport type DataServiceEvents<ServiceName extends string> =\n | DataServiceCacheUpdateEvent<ServiceName>\n | DataServiceGranularCacheUpdateEvent<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 DataServiceEvents<ServiceName>\n >;\n\n readonly #client = new QueryClient();\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 DataServiceEvents<ServiceName>\n >;\n\n this.#registerMessageHandlers();\n this.#setupCacheListener();\n }\n\n #registerMessageHandlers(): void {\n this.#messenger.registerActionHandler(\n `${this.name}:invalidateQueries`,\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 (['added', 'updated', 'removed'].includes(event.type)) {\n this.#broadcastCacheUpdate(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 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 #getDehydratedState(queryKey: QueryKey): DehydratedState {\n const hash = hashQueryKey(queryKey);\n return dehydrate(this.#client, {\n shouldDehydrateQuery: (query) => query.queryHash === hash,\n });\n }\n\n #broadcastCacheUpdate(queryKey: QueryKey): void {\n const hash = hashQueryKey(queryKey);\n const state = this.#getDehydratedState(queryKey);\n\n const payload = {\n hash,\n state,\n };\n\n this.#messenger.publish(`${this.name}:cacheUpdate` as const, payload);\n this.#messenger.publish(\n `${this.name}:cacheUpdate:${hash}` as const,\n payload,\n );\n }\n}\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
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 CacheUpdatePayload = {
|
|
5
|
+
hash: string;
|
|
6
|
+
state: DehydratedState;
|
|
7
|
+
};
|
|
8
|
+
export type DataServiceInvalidateQueriesAction<ServiceName extends string> = {
|
|
9
|
+
type: `${ServiceName}:invalidateQueries`;
|
|
10
|
+
handler: (filters?: InvalidateQueryFilters<Json>, options?: InvalidateOptions) => Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
export type DataServiceActions<ServiceName extends string> = DataServiceInvalidateQueriesAction<ServiceName>;
|
|
13
|
+
export type DataServiceCacheUpdateEvent<ServiceName extends string> = {
|
|
14
|
+
type: `${ServiceName}:cacheUpdate`;
|
|
15
|
+
payload: [CacheUpdatePayload];
|
|
16
|
+
};
|
|
17
|
+
export type DataServiceGranularCacheUpdateEvent<ServiceName extends string> = {
|
|
18
|
+
type: `${ServiceName}:cacheUpdate:${string}`;
|
|
19
|
+
payload: [CacheUpdatePayload];
|
|
20
|
+
};
|
|
21
|
+
export type DataServiceEvents<ServiceName extends string> = DataServiceCacheUpdateEvent<ServiceName> | DataServiceGranularCacheUpdateEvent<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
|
+
invalidateQueries<TPageData extends Json>(filters?: InvalidateQueryFilters<TPageData>, options?: InvalidateOptions): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=BaseDataService.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,kBAAkB,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,eAAe,CAAA;CAAE,CAAC;AAE1E,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,OAAO,CAAC,EAAE,iBAAiB,KACxB,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,kBAAkB,CAAC,WAAW,SAAS,MAAM,IACvD,kCAAkC,CAAC,WAAW,CAAC,CAAC;AAElD,MAAM,MAAM,2BAA2B,CAAC,WAAW,SAAS,MAAM,IAAI;IACpE,IAAI,EAAE,GAAG,WAAW,cAAc,CAAC;IACnC,OAAO,EAAE,CAAC,kBAAkB,CAAC,CAAC;CAC/B,CAAC;AAEF,MAAM,MAAM,mCAAmC,CAAC,WAAW,SAAS,MAAM,IAAI;IAC5E,IAAI,EAAE,GAAG,WAAW,g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OAAO,CAAC,IAAI,CAAC;CA0BjB"}
|
|
@@ -0,0 +1,33 @@
|
|
|
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 CacheUpdatePayload = {
|
|
5
|
+
hash: string;
|
|
6
|
+
state: DehydratedState;
|
|
7
|
+
};
|
|
8
|
+
export type DataServiceInvalidateQueriesAction<ServiceName extends string> = {
|
|
9
|
+
type: `${ServiceName}:invalidateQueries`;
|
|
10
|
+
handler: (filters?: InvalidateQueryFilters<Json>, options?: InvalidateOptions) => Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
export type DataServiceActions<ServiceName extends string> = DataServiceInvalidateQueriesAction<ServiceName>;
|
|
13
|
+
export type DataServiceCacheUpdateEvent<ServiceName extends string> = {
|
|
14
|
+
type: `${ServiceName}:cacheUpdate`;
|
|
15
|
+
payload: [CacheUpdatePayload];
|
|
16
|
+
};
|
|
17
|
+
export type DataServiceGranularCacheUpdateEvent<ServiceName extends string> = {
|
|
18
|
+
type: `${ServiceName}:cacheUpdate:${string}`;
|
|
19
|
+
payload: [CacheUpdatePayload];
|
|
20
|
+
};
|
|
21
|
+
export type DataServiceEvents<ServiceName extends string> = DataServiceCacheUpdateEvent<ServiceName> | DataServiceGranularCacheUpdateEvent<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
|
+
invalidateQueries<TPageData extends Json>(filters?: InvalidateQueryFilters<TPageData>, options?: InvalidateOptions): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=BaseDataService.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,kBAAkB,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,eAAe,CAAA;CAAE,CAAC;AAE1E,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,OAAO,CAAC,EAAE,iBAAiB,KACxB,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,kBAAkB,CAAC,WAAW,SAAS,MAAM,IACvD,kCAAkC,CAAC,WAAW,CAAC,CAAC;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KAAK,EAAE,SAAS,CAAC,EACjE,UAAU,GAAG,SAAS,CACvB,EACD,SAAS,CAAC,EAAE,UAAU,GACrB,OAAO,CAAC,KAAK,CAAC;IAqCX,iBAAiB,CAAC,SAAS,SAAS,IAAI,EAC5C,OAAO,CAAC,EAAE,sBAAsB,CAAC,SAAS,CAAC,EAC3C,OAAO,CAAC,EAAE,iBAAiB,GAC1B,OAAO,CAAC,IAAI,CAAC;CA0BjB"}
|
|
@@ -0,0 +1,91 @@
|
|
|
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_registerMessageHandlers, _BaseDataService_setupCacheListener, _BaseDataService_getDehydratedState, _BaseDataService_broadcastCacheUpdate;
|
|
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
|
+
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 = deepEqual(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
|
+
_BaseDataService_messenger = new WeakMap(), _BaseDataService_client = new WeakMap(), _BaseDataService_instances = new WeakSet(), _BaseDataService_registerMessageHandlers = function _BaseDataService_registerMessageHandlers() {
|
|
69
|
+
__classPrivateFieldGet(this, _BaseDataService_messenger, "f").registerActionHandler(`${this.name}:invalidateQueries`, (filters, options) => this.invalidateQueries(filters, options));
|
|
70
|
+
}, _BaseDataService_setupCacheListener = function _BaseDataService_setupCacheListener() {
|
|
71
|
+
__classPrivateFieldGet(this, _BaseDataService_client, "f").getQueryCache().subscribe((event) => {
|
|
72
|
+
if (['added', 'updated', 'removed'].includes(event.type)) {
|
|
73
|
+
__classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_broadcastCacheUpdate).call(this, event.query.queryKey);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}, _BaseDataService_getDehydratedState = function _BaseDataService_getDehydratedState(queryKey) {
|
|
77
|
+
const hash = hashQueryKey(queryKey);
|
|
78
|
+
return dehydrate(__classPrivateFieldGet(this, _BaseDataService_client, "f"), {
|
|
79
|
+
shouldDehydrateQuery: (query) => query.queryHash === hash,
|
|
80
|
+
});
|
|
81
|
+
}, _BaseDataService_broadcastCacheUpdate = function _BaseDataService_broadcastCacheUpdate(queryKey) {
|
|
82
|
+
const hash = hashQueryKey(queryKey);
|
|
83
|
+
const state = __classPrivateFieldGet(this, _BaseDataService_instances, "m", _BaseDataService_getDehydratedState).call(this, queryKey);
|
|
84
|
+
const payload = {
|
|
85
|
+
hash,
|
|
86
|
+
state,
|
|
87
|
+
};
|
|
88
|
+
__classPrivateFieldGet(this, _BaseDataService_messenger, "f").publish(`${this.name}:cacheUpdate`, payload);
|
|
89
|
+
__classPrivateFieldGet(this, _BaseDataService_messenger, "f").publish(`${this.name}:cacheUpdate:${hash}`, payload);
|
|
90
|
+
};
|
|
91
|
+
//# sourceMappingURL=BaseDataService.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
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;;AA6BxC,MAAM,OAAO,eAAe;IAsB1B,YAAY,EACV,IAAI,EACJ,SAAS,GAIV;;QAdQ,6CAIP;QAEO,kCAAU,IAAI,WAAW,EAAE,EAAC;QASnC,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;IAkBS,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,IAAI,CAA8B,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;QAErE,IAAI,CAAC,KAAK,IAAI,CAAC,SAAS,EAAE,CAAC;YACzB,MAAM,MAAM,GAAG,MAAM,uBAAA,IAAI,+BAAQ,CAAC,kBAAkB,CAAC;gB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IAGC,uBAAA,IAAI,+BAAQ,CAAC,aAAa,EAAE,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE;QAC/C,IAAI,CAAC,OAAO,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;YACzD,uBAAA,IAAI,yEAAsB,MAA1B,IAAI,EAAuB,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QACnD,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC,qFAwEmB,QAAkB;IACpC,MAAM,IAAI,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;IACpC,OAAO,SAAS,CAAC,uBAAA,IAAI,+BAAQ,EAAE;QAC7B,oBAAoB,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,KAAK,IAAI;KAC1D,CAAC,CAAC;AACL,CAAC,yFAEqB,QAAkB;IACtC,MAAM,IAAI,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;IACpC,MAAM,KAAK,GAAG,uBAAA,IAAI,uEAAoB,MAAxB,IAAI,EAAqB,QAAQ,CAAC,CAAC;IAEjD,MAAM,OAAO,GAAG;QACd,IAAI;QACJ,KAAK;KACN,CAAC;IAEF,uBAAA,IAAI,kCAAW,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,IAAI,cAAuB,EAAE,OAAO,CAAC,CAAC;IACtE,uBAAA,IAAI,kCAAW,CAAC,OAAO,CACrB,GAAG,IAAI,CAAC,IAAI,gBAAgB,IAAI,EAAW,EAC3C,OAAO,CACR,CAAC;AACJ,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 CacheUpdatePayload = { hash: string; state: DehydratedState };\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 DataServiceInvalidateQueriesAction<ServiceName>;\n\nexport type DataServiceCacheUpdateEvent<ServiceName extends string> = {\n type: `${ServiceName}:cacheUpdate`;\n payload: [CacheUpdatePayload];\n};\n\nexport type DataServiceGranularCacheUpdateEvent<ServiceName extends string> = {\n type: `${ServiceName}:cacheUpdate:${string}`;\n payload: [CacheUpdatePayload];\n};\n\nexport type DataServiceEvents<ServiceName extends string> =\n | DataServiceCacheUpdateEvent<ServiceName>\n | DataServiceGranularCacheUpdateEvent<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 DataServiceEvents<ServiceName>\n >;\n\n readonly #client = new QueryClient();\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 DataServiceEvents<ServiceName>\n >;\n\n this.#registerMessageHandlers();\n this.#setupCacheListener();\n }\n\n #registerMessageHandlers(): void {\n this.#messenger.registerActionHandler(\n `${this.name}:invalidateQueries`,\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 (['added', 'updated', 'removed'].includes(event.type)) {\n this.#broadcastCacheUpdate(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 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 #getDehydratedState(queryKey: QueryKey): DehydratedState {\n const hash = hashQueryKey(queryKey);\n return dehydrate(this.#client, {\n shouldDehydrateQuery: (query) => query.queryHash === hash,\n });\n }\n\n #broadcastCacheUpdate(queryKey: QueryKey): void {\n const hash = hashQueryKey(queryKey);\n const state = this.#getDehydratedState(queryKey);\n\n const payload = {\n hash,\n state,\n };\n\n this.#messenger.publish(`${this.name}:cacheUpdate` as const, payload);\n this.#messenger.publish(\n `${this.name}:cacheUpdate:${hash}` as const,\n payload,\n );\n }\n}\n"]}
|
|
@@ -0,0 +1,85 @@
|
|
|
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
|
+
/**
|
|
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
|
+
function createUIQueryClient(dataServices, messenger) {
|
|
14
|
+
const subscriptions = new Map();
|
|
15
|
+
const getServiceFromQueryKey = (queryKey) => {
|
|
16
|
+
try {
|
|
17
|
+
const action = queryKey[0];
|
|
18
|
+
(0, utils_1.assert)(typeof action === 'string');
|
|
19
|
+
const service = action.split(':')[0];
|
|
20
|
+
(0, utils_1.assert)(dataServices.includes(service));
|
|
21
|
+
return service;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const client = new query_core_1.QueryClient({
|
|
28
|
+
defaultOptions: {
|
|
29
|
+
queries: {
|
|
30
|
+
queryFn: async (options) => {
|
|
31
|
+
const { queryKey } = options;
|
|
32
|
+
const action = queryKey?.[0];
|
|
33
|
+
(0, utils_1.assert)(typeof action === 'string', 'The first element of a query key must be a string.');
|
|
34
|
+
(0, utils_1.assert)(dataServices.includes(action.split(':')?.[0]), 'Queries must use data service actions.');
|
|
35
|
+
return (await messenger.call(action, ...options.queryKey.slice(1), options.pageParam));
|
|
36
|
+
},
|
|
37
|
+
staleTime: 0,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
client.getQueryCache().subscribe((event) => {
|
|
42
|
+
const { query } = event;
|
|
43
|
+
const hash = query.queryHash;
|
|
44
|
+
const hasSubscription = subscriptions.has(hash);
|
|
45
|
+
const observerCount = query.getObserversCount();
|
|
46
|
+
const service = getServiceFromQueryKey(query.queryKey);
|
|
47
|
+
if (!service) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (!hasSubscription &&
|
|
51
|
+
event.type === 'observerAdded' &&
|
|
52
|
+
observerCount === 1) {
|
|
53
|
+
const cacheListener = (payload) => {
|
|
54
|
+
(0, query_core_1.hydrate)(client, payload.state);
|
|
55
|
+
};
|
|
56
|
+
subscriptions.set(hash, cacheListener);
|
|
57
|
+
messenger.subscribe(`${service}:cacheUpdate:${hash}`, cacheListener);
|
|
58
|
+
}
|
|
59
|
+
else if (event.type === 'observerRemoved' &&
|
|
60
|
+
observerCount === 0 &&
|
|
61
|
+
hasSubscription) {
|
|
62
|
+
const subscriptionListener = subscriptions.get(hash);
|
|
63
|
+
messenger.unsubscribe(`${service}:cacheUpdate:${hash}`, subscriptionListener);
|
|
64
|
+
subscriptions.delete(hash);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
// Override invalidateQueries to ensure the data service is invalidated as well.
|
|
68
|
+
const originalInvalidate = client.invalidateQueries.bind(client);
|
|
69
|
+
// This function is defined in this way to have full support for all function overloads.
|
|
70
|
+
client.invalidateQueries = async (arg1, arg2, arg3) => {
|
|
71
|
+
const [filters, options] = (0, query_core_1.parseFilterArgs)(arg1, arg2, arg3);
|
|
72
|
+
const queries = client.getQueryCache().findAll(filters);
|
|
73
|
+
await Promise.all(queries.map(async (query) => {
|
|
74
|
+
const service = getServiceFromQueryKey(query.queryKey);
|
|
75
|
+
if (!service) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
return messenger.call(`${service}:invalidateQueries`, filters, options);
|
|
79
|
+
}));
|
|
80
|
+
return originalInvalidate(filters, options);
|
|
81
|
+
};
|
|
82
|
+
return client;
|
|
83
|
+
}
|
|
84
|
+
exports.createUIQueryClient = createUIQueryClient;
|
|
85
|
+
//# sourceMappingURL=createUIQueryClient.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createUIQueryClient.cjs","sourceRoot":"","sources":["../src/createUIQueryClient.ts"],"names":[],"mappings":";;;AAAA,2CAA+C;AAC/C,qDAQ8B;AAc9B;;;;;;GAMG;AACH,SAAgB,mBAAmB,CACjC,YAAsB,EACtB,SAA2B;IAE3B,MAAM,aAAa,GAAG,IAAI,GAAG,EAAgC,CAAC;IAE9D,MAAM,sBAAsB,GAAG,CAAC,QAAkB,EAAiB,EAAE;QACnE,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;YAC3B,IAAA,cAAM,EAAC,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC;YAEnC,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;YACrC,IAAA,cAAM,EAAC,YAAY,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;YAEvC,OAAO,OAAO,CAAC;QACjB,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC,CAAC;IAEF,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,Q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hydrate,\n QueryClient,\n InvalidateQueryFilters,\n InvalidateOptions,\n OmitKeyof,\n parseFilterArgs,\n QueryKey,\n} from '@tanstack/query-core';\n\nimport { CacheUpdatePayload } from './BaseDataService';\n\ntype SubscriptionCallback = (payload: CacheUpdatePayload) => void;\ntype JsonSubscriptionCallback = (data: Json) => void;\n\n// TODO: Figure out if we can replace with a better Messenger type\ntype MessengerAdapter = {\n call: (method: string, ...params: Json[]) => Promise<Json | void>;\n subscribe: (method: string, callback: JsonSubscriptionCallback) => void;\n unsubscribe: (method: string, callback: JsonSubscriptionCallback) => void;\n};\n\n/**\n * Create a QueryClient queries and subscribes to data services using the messenger.\n *\n * @param dataServices - A list of data services.\n * @param messenger - A messenger adapter.\n * @returns The QueryClient.\n */\nexport function createUIQueryClient(\n dataServices: string[],\n messenger: MessengerAdapter,\n): QueryClient {\n const subscriptions = new Map<string, SubscriptionCallback>();\n\n const getServiceFromQueryKey = (queryKey: QueryKey): string | null => {\n try {\n const action = queryKey[0];\n assert(typeof action === 'string');\n\n const service = action.split(':')[0];\n assert(dataServices.includes(service));\n\n return service;\n } catch {\n return null;\n }\n };\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\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 staleTime: 0,\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 (!service) {\n return;\n }\n\n if (\n !hasSubscription &&\n event.type === 'observerAdded' &&\n observerCount === 1\n ) {\n const cacheListener = (payload: CacheUpdatePayload): void => {\n hydrate(client, payload.state);\n };\n\n subscriptions.set(hash, cacheListener);\n messenger.subscribe(\n `${service}:cacheUpdate:${hash}`,\n cacheListener as unknown as JsonSubscriptionCallback,\n );\n } else if (\n event.type === 'observerRemoved' &&\n observerCount === 0 &&\n hasSubscription\n ) {\n const subscriptionListener = subscriptions.get(\n hash,\n ) as unknown as JsonSubscriptionCallback;\n\n messenger.unsubscribe(\n `${service}:cacheUpdate:${hash}`,\n subscriptionListener,\n );\n subscriptions.delete(hash);\n }\n });\n\n // Override invalidateQueries to ensure the data service is invalidated as well.\n const originalInvalidate = client.invalidateQueries.bind(client);\n\n // This function is defined in this way to have full support for all function overloads.\n client.invalidateQueries = async (\n arg1?: QueryKey | InvalidateQueryFilters,\n arg2?: OmitKeyof<InvalidateQueryFilters, 'queryKey'> | InvalidateOptions,\n arg3?: InvalidateOptions,\n ): Promise<void> => {\n const [filters, options] = parseFilterArgs(arg1, arg2, arg3);\n\n const queries = client.getQueryCache().findAll(filters);\n await Promise.all(\n queries.map(async (query) => {\n const service = getServiceFromQueryKey(query.queryKey);\n\n if (!service) {\n return null;\n }\n\n return messenger.call(\n `${service}:invalidateQueries`,\n filters as Json,\n options as Json,\n );\n }),\n );\n\n return originalInvalidate(filters, options);\n };\n\n return client;\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Json } from "@metamask/utils";
|
|
2
|
+
import { QueryClient } from "@tanstack/query-core";
|
|
3
|
+
type JsonSubscriptionCallback = (data: Json) => void;
|
|
4
|
+
type MessengerAdapter = {
|
|
5
|
+
call: (method: string, ...params: Json[]) => Promise<Json | void>;
|
|
6
|
+
subscribe: (method: string, callback: JsonSubscriptionCallback) => void;
|
|
7
|
+
unsubscribe: (method: string, callback: JsonSubscriptionCallback) => void;
|
|
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
|
+
export declare function createUIQueryClient(dataServices: string[], messenger: MessengerAdapter): QueryClient;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=createUIQueryClient.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,EAMZ,6BAA6B;AAK9B,KAAK,wBAAwB,GAAG,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;AAGrD,KAAK,gBAAgB,GAAG;IACtB,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,KAAK,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;IAClE,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,wBAAwB,KAAK,IAAI,CAAC;IACxE,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,wBAAwB,KAAK,IAAI,CAAC;CAC3E,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,gBAAgB,GAC1B,WAAW,CA0Hb"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Json } from "@metamask/utils";
|
|
2
|
+
import { QueryClient } from "@tanstack/query-core";
|
|
3
|
+
type JsonSubscriptionCallback = (data: Json) => void;
|
|
4
|
+
type MessengerAdapter = {
|
|
5
|
+
call: (method: string, ...params: Json[]) => Promise<Json | void>;
|
|
6
|
+
subscribe: (method: string, callback: JsonSubscriptionCallback) => void;
|
|
7
|
+
unsubscribe: (method: string, callback: JsonSubscriptionCallback) => void;
|
|
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
|
+
export declare function createUIQueryClient(dataServices: string[], messenger: MessengerAdapter): QueryClient;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=createUIQueryClient.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,EAMZ,6BAA6B;AAK9B,KAAK,wBAAwB,GAAG,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;AAGrD,KAAK,gBAAgB,GAAG;IACtB,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,KAAK,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;IAClE,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,wBAAwB,KAAK,IAAI,CAAC;IACxE,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,wBAAwB,KAAK,IAAI,CAAC;CAC3E,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,gBAAgB,GAC1B,WAAW,CA0Hb"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { assert } from "@metamask/utils";
|
|
2
|
+
import { hydrate, QueryClient, parseFilterArgs } from "@tanstack/query-core";
|
|
3
|
+
/**
|
|
4
|
+
* Create a QueryClient queries and subscribes to data services using the messenger.
|
|
5
|
+
*
|
|
6
|
+
* @param dataServices - A list of data services.
|
|
7
|
+
* @param messenger - A messenger adapter.
|
|
8
|
+
* @returns The QueryClient.
|
|
9
|
+
*/
|
|
10
|
+
export function createUIQueryClient(dataServices, messenger) {
|
|
11
|
+
const subscriptions = new Map();
|
|
12
|
+
const getServiceFromQueryKey = (queryKey) => {
|
|
13
|
+
try {
|
|
14
|
+
const action = queryKey[0];
|
|
15
|
+
assert(typeof action === 'string');
|
|
16
|
+
const service = action.split(':')[0];
|
|
17
|
+
assert(dataServices.includes(service));
|
|
18
|
+
return service;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const client = new QueryClient({
|
|
25
|
+
defaultOptions: {
|
|
26
|
+
queries: {
|
|
27
|
+
queryFn: async (options) => {
|
|
28
|
+
const { queryKey } = options;
|
|
29
|
+
const action = queryKey?.[0];
|
|
30
|
+
assert(typeof action === 'string', 'The first element of a query key must be a string.');
|
|
31
|
+
assert(dataServices.includes(action.split(':')?.[0]), 'Queries must use data service actions.');
|
|
32
|
+
return (await messenger.call(action, ...options.queryKey.slice(1), options.pageParam));
|
|
33
|
+
},
|
|
34
|
+
staleTime: 0,
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
client.getQueryCache().subscribe((event) => {
|
|
39
|
+
const { query } = event;
|
|
40
|
+
const hash = query.queryHash;
|
|
41
|
+
const hasSubscription = subscriptions.has(hash);
|
|
42
|
+
const observerCount = query.getObserversCount();
|
|
43
|
+
const service = getServiceFromQueryKey(query.queryKey);
|
|
44
|
+
if (!service) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (!hasSubscription &&
|
|
48
|
+
event.type === 'observerAdded' &&
|
|
49
|
+
observerCount === 1) {
|
|
50
|
+
const cacheListener = (payload) => {
|
|
51
|
+
hydrate(client, payload.state);
|
|
52
|
+
};
|
|
53
|
+
subscriptions.set(hash, cacheListener);
|
|
54
|
+
messenger.subscribe(`${service}:cacheUpdate:${hash}`, cacheListener);
|
|
55
|
+
}
|
|
56
|
+
else if (event.type === 'observerRemoved' &&
|
|
57
|
+
observerCount === 0 &&
|
|
58
|
+
hasSubscription) {
|
|
59
|
+
const subscriptionListener = subscriptions.get(hash);
|
|
60
|
+
messenger.unsubscribe(`${service}:cacheUpdate:${hash}`, subscriptionListener);
|
|
61
|
+
subscriptions.delete(hash);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
// Override invalidateQueries to ensure the data service is invalidated as well.
|
|
65
|
+
const originalInvalidate = client.invalidateQueries.bind(client);
|
|
66
|
+
// This function is defined in this way to have full support for all function overloads.
|
|
67
|
+
client.invalidateQueries = async (arg1, arg2, arg3) => {
|
|
68
|
+
const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
|
|
69
|
+
const queries = client.getQueryCache().findAll(filters);
|
|
70
|
+
await Promise.all(queries.map(async (query) => {
|
|
71
|
+
const service = getServiceFromQueryKey(query.queryKey);
|
|
72
|
+
if (!service) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
return messenger.call(`${service}:invalidateQueries`, filters, options);
|
|
76
|
+
}));
|
|
77
|
+
return originalInvalidate(filters, options);
|
|
78
|
+
};
|
|
79
|
+
return client;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=createUIQueryClient.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
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,EAIX,eAAe,EAEhB,6BAA6B;AAc9B;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CACjC,YAAsB,EACtB,SAA2B;IAE3B,MAAM,aAAa,GAAG,IAAI,GAAG,EAAgC,CAAC;IAE9D,MAAM,sBAAsB,GAAG,CAAC,QAAkB,EAAiB,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e,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAE7D,MAAM,OAAO,GAAG,MAAM,CAAC,aAAa,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QACxD,MAAM,OAAO,CAAC,GAAG,CACf,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;YAC1B,MAAM,OAAO,GAAG,sBAAsB,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;YAEvD,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,OAAO,IAAI,CAAC;YACd,CAAC;YAED,OAAO,SAAS,CAAC,IAAI,CACnB,GAAG,OAAO,oBAAoB,EAC9B,OAAe,EACf,OAAe,CAChB,CAAC;QACJ,CAAC,CAAC,CACH,CAAC;QAEF,OAAO,kBAAkB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { assert, Json } from '@metamask/utils';\nimport {\n hydrate,\n QueryClient,\n InvalidateQueryFilters,\n InvalidateOptions,\n OmitKeyof,\n parseFilterArgs,\n QueryKey,\n} from '@tanstack/query-core';\n\nimport { CacheUpdatePayload } from './BaseDataService';\n\ntype SubscriptionCallback = (payload: CacheUpdatePayload) => void;\ntype JsonSubscriptionCallback = (data: Json) => void;\n\n// TODO: Figure out if we can replace with a better Messenger type\ntype MessengerAdapter = {\n call: (method: string, ...params: Json[]) => Promise<Json | void>;\n subscribe: (method: string, callback: JsonSubscriptionCallback) => void;\n unsubscribe: (method: string, callback: JsonSubscriptionCallback) => void;\n};\n\n/**\n * Create a QueryClient queries and subscribes to data services using the messenger.\n *\n * @param dataServices - A list of data services.\n * @param messenger - A messenger adapter.\n * @returns The QueryClient.\n */\nexport function createUIQueryClient(\n dataServices: string[],\n messenger: MessengerAdapter,\n): QueryClient {\n const subscriptions = new Map<string, SubscriptionCallback>();\n\n const getServiceFromQueryKey = (queryKey: QueryKey): string | null => {\n try {\n const action = queryKey[0];\n assert(typeof action === 'string');\n\n const service = action.split(':')[0];\n assert(dataServices.includes(service));\n\n return service;\n } catch {\n return null;\n }\n };\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\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 staleTime: 0,\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 (!service) {\n return;\n }\n\n if (\n !hasSubscription &&\n event.type === 'observerAdded' &&\n observerCount === 1\n ) {\n const cacheListener = (payload: CacheUpdatePayload): void => {\n hydrate(client, payload.state);\n };\n\n subscriptions.set(hash, cacheListener);\n messenger.subscribe(\n `${service}:cacheUpdate:${hash}`,\n cacheListener as unknown as JsonSubscriptionCallback,\n );\n } else if (\n event.type === 'observerRemoved' &&\n observerCount === 0 &&\n hasSubscription\n ) {\n const subscriptionListener = subscriptions.get(\n hash,\n ) as unknown as JsonSubscriptionCallback;\n\n messenger.unsubscribe(\n `${service}:cacheUpdate:${hash}`,\n subscriptionListener,\n );\n subscriptions.delete(hash);\n }\n });\n\n // Override invalidateQueries to ensure the data service is invalidated as well.\n const originalInvalidate = client.invalidateQueries.bind(client);\n\n // This function is defined in this way to have full support for all function overloads.\n client.invalidateQueries = async (\n arg1?: QueryKey | InvalidateQueryFilters,\n arg2?: OmitKeyof<InvalidateQueryFilters, 'queryKey'> | InvalidateOptions,\n arg3?: InvalidateOptions,\n ): Promise<void> => {\n const [filters, options] = parseFilterArgs(arg1, arg2, arg3);\n\n const queries = client.getQueryCache().findAll(filters);\n await Promise.all(\n queries.map(async (query) => {\n const service = getServiceFromQueryKey(query.queryKey);\n\n if (!service) {\n return null;\n }\n\n return messenger.call(\n `${service}:invalidateQueries`,\n filters as Json,\n options as Json,\n );\n }),\n );\n\n return originalInvalidate(filters, options);\n };\n\n return client;\n}\n"]}
|
package/dist/hooks.cjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useInfiniteQuery = exports.useQuery = void 0;
|
|
4
|
+
const react_query_1 = require("@tanstack/react-query");
|
|
5
|
+
function useQuery(options) {
|
|
6
|
+
return (0, react_query_1.useQuery)(options);
|
|
7
|
+
}
|
|
8
|
+
exports.useQuery = useQuery;
|
|
9
|
+
function useInfiniteQuery(options) {
|
|
10
|
+
return (0, react_query_1.useInfiniteQuery)(options);
|
|
11
|
+
}
|
|
12
|
+
exports.useInfiniteQuery = useInfiniteQuery;
|
|
13
|
+
//# sourceMappingURL=hooks.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.cjs","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":";;;AAAA,uDAS+B;AAE/B,SAAgB,QAAQ,CAMtB,OAQC;IAED,OAAO,IAAA,sBAAgB,EAAC,OAAO,CAAC,CAAC;AACnC,CAAC;AAjBD,4BAiBC;AAED,SAAgB,gBAAgB,CAM9B,OASC;IAED,OAAO,IAAA,8BAAwB,EAAC,OAAO,CAAC,CAAC;AAC3C,CAAC;AAlBD,4CAkBC","sourcesContent":["import {\n useQuery as useQueryTanStack,\n useInfiniteQuery as useInfiniteQueryTanStack,\n OmitKeyof,\n UseQueryOptions,\n QueryKey,\n InitialDataFunction,\n NonUndefinedGuard,\n UseInfiniteQueryOptions,\n} from '@tanstack/react-query';\n\nexport function useQuery<\n TQueryFnData = unknown,\n TError = unknown,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: OmitKeyof<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'initialData' | 'staleTime' | 'queryFn'\n > & {\n initialData?:\n | undefined\n | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>\n | NonUndefinedGuard<TQueryFnData>;\n },\n): ReturnType<typeof useQueryTanStack> {\n return useQueryTanStack(options);\n}\n\nexport function useInfiniteQuery<\n TQueryFnData = unknown,\n TError = unknown,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: OmitKeyof<\n UseInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryFnData,\n TQueryKey\n >,\n 'staleTime' | 'queryFn'\n >,\n): ReturnType<typeof useInfiniteQueryTanStack> {\n return useInfiniteQueryTanStack(options);\n}\n"]}
|
package/dist/hooks.d.cts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { useQuery as useQueryTanStack, useInfiniteQuery as useInfiniteQueryTanStack, OmitKeyof, UseQueryOptions, QueryKey, InitialDataFunction, NonUndefinedGuard, UseInfiniteQueryOptions } from "@tanstack/react-query";
|
|
2
|
+
export declare function useQuery<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: OmitKeyof<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'initialData' | 'staleTime' | 'queryFn'> & {
|
|
3
|
+
initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>> | NonUndefinedGuard<TQueryFnData>;
|
|
4
|
+
}): ReturnType<typeof useQueryTanStack>;
|
|
5
|
+
export declare function useInfiniteQuery<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: OmitKeyof<UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>, 'staleTime' | 'queryFn'>): ReturnType<typeof useInfiniteQueryTanStack>;
|
|
6
|
+
//# sourceMappingURL=hooks.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.cts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,IAAI,gBAAgB,EAC5B,gBAAgB,IAAI,wBAAwB,EAC5C,SAAS,EACT,eAAe,EACf,QAAQ,EACR,mBAAmB,EACnB,iBAAiB,EACjB,uBAAuB,EACxB,8BAA8B;AAE/B,wBAAgB,QAAQ,CACtB,YAAY,GAAG,OAAO,EACtB,MAAM,GAAG,OAAO,EAChB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,EAErC,OAAO,EAAE,SAAS,CAChB,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,EACvD,aAAa,GAAG,WAAW,GAAG,SAAS,CACxC,GAAG;IACF,WAAW,CAAC,EACR,SAAS,GACT,mBAAmB,CAAC,iBAAiB,CAAC,YAAY,CAAC,CAAC,GACpD,iBAAiB,CAAC,YAAY,CAAC,CAAC;CACrC,GACA,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAErC;AAED,wBAAgB,gBAAgB,CAC9B,YAAY,GAAG,OAAO,EACtB,MAAM,GAAG,OAAO,EAChB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,EAErC,OAAO,EAAE,SAAS,CAChB,uBAAuB,CACrB,YAAY,EACZ,MAAM,EACN,KAAK,EACL,YAAY,EACZ,SAAS,CACV,EACD,WAAW,GAAG,SAAS,CACxB,GACA,UAAU,CAAC,OAAO,wBAAwB,CAAC,CAE7C"}
|
package/dist/hooks.d.mts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { useQuery as useQueryTanStack, useInfiniteQuery as useInfiniteQueryTanStack, OmitKeyof, UseQueryOptions, QueryKey, InitialDataFunction, NonUndefinedGuard, UseInfiniteQueryOptions } from "@tanstack/react-query";
|
|
2
|
+
export declare function useQuery<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: OmitKeyof<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'initialData' | 'staleTime' | 'queryFn'> & {
|
|
3
|
+
initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>> | NonUndefinedGuard<TQueryFnData>;
|
|
4
|
+
}): ReturnType<typeof useQueryTanStack>;
|
|
5
|
+
export declare function useInfiniteQuery<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: OmitKeyof<UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>, 'staleTime' | 'queryFn'>): ReturnType<typeof useInfiniteQueryTanStack>;
|
|
6
|
+
//# sourceMappingURL=hooks.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.mts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,IAAI,gBAAgB,EAC5B,gBAAgB,IAAI,wBAAwB,EAC5C,SAAS,EACT,eAAe,EACf,QAAQ,EACR,mBAAmB,EACnB,iBAAiB,EACjB,uBAAuB,EACxB,8BAA8B;AAE/B,wBAAgB,QAAQ,CACtB,YAAY,GAAG,OAAO,EACtB,MAAM,GAAG,OAAO,EAChB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,EAErC,OAAO,EAAE,SAAS,CAChB,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,EACvD,aAAa,GAAG,WAAW,GAAG,SAAS,CACxC,GAAG;IACF,WAAW,CAAC,EACR,SAAS,GACT,mBAAmB,CAAC,iBAAiB,CAAC,YAAY,CAAC,CAAC,GACpD,iBAAiB,CAAC,YAAY,CAAC,CAAC;CACrC,GACA,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAErC;AAED,wBAAgB,gBAAgB,CAC9B,YAAY,GAAG,OAAO,EACtB,MAAM,GAAG,OAAO,EAChB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,EAErC,OAAO,EAAE,SAAS,CAChB,uBAAuB,CACrB,YAAY,EACZ,MAAM,EACN,KAAK,EACL,YAAY,EACZ,SAAS,CACV,EACD,WAAW,GAAG,SAAS,CACxB,GACA,UAAU,CAAC,OAAO,wBAAwB,CAAC,CAE7C"}
|
package/dist/hooks.mjs
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { useQuery as useQueryTanStack, useInfiniteQuery as useInfiniteQueryTanStack } from "@tanstack/react-query";
|
|
2
|
+
export function useQuery(options) {
|
|
3
|
+
return useQueryTanStack(options);
|
|
4
|
+
}
|
|
5
|
+
export function useInfiniteQuery(options) {
|
|
6
|
+
return useInfiniteQueryTanStack(options);
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=hooks.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.mjs","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,IAAI,gBAAgB,EAC5B,gBAAgB,IAAI,wBAAwB,EAO7C,8BAA8B;AAE/B,MAAM,UAAU,QAAQ,CAMtB,OAQC;IAED,OAAO,gBAAgB,CAAC,OAAO,CAAC,CAAC;AACnC,CAAC;AAED,MAAM,UAAU,gBAAgB,CAM9B,OASC;IAED,OAAO,wBAAwB,CAAC,OAAO,CAAC,CAAC;AAC3C,CAAC","sourcesContent":["import {\n useQuery as useQueryTanStack,\n useInfiniteQuery as useInfiniteQueryTanStack,\n OmitKeyof,\n UseQueryOptions,\n QueryKey,\n InitialDataFunction,\n NonUndefinedGuard,\n UseInfiniteQueryOptions,\n} from '@tanstack/react-query';\n\nexport function useQuery<\n TQueryFnData = unknown,\n TError = unknown,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: OmitKeyof<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'initialData' | 'staleTime' | 'queryFn'\n > & {\n initialData?:\n | undefined\n | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>\n | NonUndefinedGuard<TQueryFnData>;\n },\n): ReturnType<typeof useQueryTanStack> {\n return useQueryTanStack(options);\n}\n\nexport function useInfiniteQuery<\n TQueryFnData = unknown,\n TError = unknown,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: OmitKeyof<\n UseInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryFnData,\n TQueryKey\n >,\n 'staleTime' | 'queryFn'\n >,\n): ReturnType<typeof useInfiniteQueryTanStack> {\n return useInfiniteQueryTanStack(options);\n}\n"]}
|
package/dist/index.cjs
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
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
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
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;
|
|
17
|
+
__exportStar(require("./BaseDataService.cjs"), exports);
|
|
18
|
+
__exportStar(require("./createUIQueryClient.cjs"), exports);
|
|
19
|
+
__exportStar(require("./hooks.cjs"), exports);
|
|
13
20
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wDAAkC;AAClC,4DAAsC;AACtC,8CAAwB","sourcesContent":["export * from './BaseDataService';\nexport * from './createUIQueryClient';\nexport * from './hooks';\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @param name - The name to greet.
|
|
5
|
-
* @returns The greeting.
|
|
6
|
-
*/
|
|
7
|
-
export default function greeter(name: string): string;
|
|
1
|
+
export * from "./BaseDataService.cjs";
|
|
2
|
+
export * from "./createUIQueryClient.cjs";
|
|
3
|
+
export * from "./hooks.cjs";
|
|
8
4
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAkC;AAClC,0CAAsC;AACtC,4BAAwB"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @param name - The name to greet.
|
|
5
|
-
* @returns The greeting.
|
|
6
|
-
*/
|
|
7
|
-
export default function greeter(name: string): string;
|
|
1
|
+
export * from "./BaseDataService.mjs";
|
|
2
|
+
export * from "./createUIQueryClient.mjs";
|
|
3
|
+
export * from "./hooks.mjs";
|
|
8
4
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAkC;AAClC,0CAAsC;AACtC,4BAAwB"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @param name - The name to greet.
|
|
5
|
-
* @returns The greeting.
|
|
6
|
-
*/
|
|
7
|
-
export default function greeter(name) {
|
|
8
|
-
return `Hello, ${name}!`;
|
|
9
|
-
}
|
|
1
|
+
export * from "./BaseDataService.mjs";
|
|
2
|
+
export * from "./createUIQueryClient.mjs";
|
|
3
|
+
export * from "./hooks.mjs";
|
|
10
4
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAkC;AAClC,0CAAsC;AACtC,4BAAwB","sourcesContent":["export * from './BaseDataService';\nexport * from './createUIQueryClient';\nexport * from './hooks';\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-
|
|
3
|
+
"version": "0.0.0-preview-00245ea",
|
|
4
4
|
"description": "Provides utilities for building data services",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -47,12 +47,20 @@
|
|
|
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
|
+
"@tanstack/react-query": "^4.43.0",
|
|
55
|
+
"fast-deep-equal": "^3.1.3"
|
|
56
|
+
},
|
|
50
57
|
"devDependencies": {
|
|
51
58
|
"@metamask/auto-changelog": "^3.4.4",
|
|
52
59
|
"@ts-bridge/cli": "^0.6.4",
|
|
53
60
|
"@types/jest": "^29.5.14",
|
|
54
61
|
"deepmerge": "^4.2.2",
|
|
55
62
|
"jest": "^29.7.0",
|
|
63
|
+
"nock": "^13.3.1",
|
|
56
64
|
"ts-jest": "^29.2.5",
|
|
57
65
|
"typedoc": "^0.25.13",
|
|
58
66
|
"typedoc-plugin-missing-exports": "^2.0.0",
|