mirror of
https://github.com/hanzoai/agency.git
synced 2026-11-02 19:13:28 +00:00
97 lines
2.4 KiB
Plaintext
97 lines
2.4 KiB
Plaintext
// src/queryCache.ts
|
|
import { hashQueryKeyByOptions, matchQuery } from "./utils.js";
|
|
import { Query } from "./query.js";
|
|
import { notifyManager } from "./notifyManager.js";
|
|
import { Subscribable } from "./subscribable.js";
|
|
var QueryCache = class extends Subscribable {
|
|
constructor(config = {}) {
|
|
super();
|
|
this.config = config;
|
|
this.#queries = /* @__PURE__ */ new Map();
|
|
}
|
|
#queries;
|
|
build(client, options, state) {
|
|
const queryKey = options.queryKey;
|
|
const queryHash = options.queryHash ?? hashQueryKeyByOptions(queryKey, options);
|
|
let query = this.get(queryHash);
|
|
if (!query) {
|
|
query = new Query({
|
|
client,
|
|
queryKey,
|
|
queryHash,
|
|
options: client.defaultQueryOptions(options),
|
|
state,
|
|
defaultOptions: client.getQueryDefaults(queryKey)
|
|
});
|
|
this.add(query);
|
|
}
|
|
return query;
|
|
}
|
|
add(query) {
|
|
if (!this.#queries.has(query.queryHash)) {
|
|
this.#queries.set(query.queryHash, query);
|
|
this.notify({
|
|
type: "added",
|
|
query
|
|
});
|
|
}
|
|
}
|
|
remove(query) {
|
|
const queryInMap = this.#queries.get(query.queryHash);
|
|
if (queryInMap) {
|
|
query.destroy();
|
|
if (queryInMap === query) {
|
|
this.#queries.delete(query.queryHash);
|
|
}
|
|
this.notify({ type: "removed", query });
|
|
}
|
|
}
|
|
clear() {
|
|
notifyManager.batch(() => {
|
|
this.getAll().forEach((query) => {
|
|
this.remove(query);
|
|
});
|
|
});
|
|
}
|
|
get(queryHash) {
|
|
return this.#queries.get(queryHash);
|
|
}
|
|
getAll() {
|
|
return [...this.#queries.values()];
|
|
}
|
|
find(filters) {
|
|
const defaultedFilters = { exact: true, ...filters };
|
|
return this.getAll().find(
|
|
(query) => matchQuery(defaultedFilters, query)
|
|
);
|
|
}
|
|
findAll(filters = {}) {
|
|
const queries = this.getAll();
|
|
return Object.keys(filters).length > 0 ? queries.filter((query) => matchQuery(filters, query)) : queries;
|
|
}
|
|
notify(event) {
|
|
notifyManager.batch(() => {
|
|
this.listeners.forEach((listener) => {
|
|
listener(event);
|
|
});
|
|
});
|
|
}
|
|
onFocus() {
|
|
notifyManager.batch(() => {
|
|
this.getAll().forEach((query) => {
|
|
query.onFocus();
|
|
});
|
|
});
|
|
}
|
|
onOnline() {
|
|
notifyManager.batch(() => {
|
|
this.getAll().forEach((query) => {
|
|
query.onOnline();
|
|
});
|
|
});
|
|
}
|
|
};
|
|
export {
|
|
QueryCache
|
|
};
|
|
//# sourceMappingURL=queryCache.js.map |