react-query
Version:
Hooks for managing, caching and syncing asynchronous and remote data in React
122 lines (102 loc) • 2.66 kB
JavaScript
import { hashQueryKeyByOptions, matchQuery, parseFilterArgs } from './utils';
import { Query } from './query';
import { notifyManager } from './notifyManager';
import { Subscribable } from './subscribable';
// CLASS
export class QueryCache extends Subscribable {
constructor(config) {
super();
this.config = config || {};
this.queries = [];
this.queriesMap = {};
}
build(client, options, state) {
var _options$queryHash;
const queryKey = options.queryKey;
const queryHash = (_options$queryHash = options.queryHash) != null ? _options$queryHash : hashQueryKeyByOptions(queryKey, options);
let query = this.get(queryHash);
if (!query) {
query = new Query({
cache: this,
logger: client.getLogger(),
queryKey,
queryHash,
options: client.defaultQueryOptions(options),
state,
defaultOptions: client.getQueryDefaults(queryKey),
meta: options.meta
});
this.add(query);
}
return query;
}
add(query) {
if (!this.queriesMap[query.queryHash]) {
this.queriesMap[query.queryHash] = query;
this.queries.push(query);
this.notify({
type: 'added',
query
});
}
}
remove(query) {
const queryInMap = this.queriesMap[query.queryHash];
if (queryInMap) {
query.destroy();
this.queries = this.queries.filter(x => x !== query);
if (queryInMap === query) {
delete this.queriesMap[query.queryHash];
}
this.notify({
type: 'removed',
query
});
}
}
clear() {
notifyManager.batch(() => {
this.queries.forEach(query => {
this.remove(query);
});
});
}
get(queryHash) {
return this.queriesMap[queryHash];
}
getAll() {
return this.queries;
}
find(arg1, arg2) {
const [filters] = parseFilterArgs(arg1, arg2);
if (typeof filters.exact === 'undefined') {
filters.exact = true;
}
return this.queries.find(query => matchQuery(filters, query));
}
findAll(arg1, arg2) {
const [filters] = parseFilterArgs(arg1, arg2);
return Object.keys(filters).length > 0 ? this.queries.filter(query => matchQuery(filters, query)) : this.queries;
}
notify(event) {
notifyManager.batch(() => {
this.listeners.forEach(listener => {
listener(event);
});
});
}
onFocus() {
notifyManager.batch(() => {
this.queries.forEach(query => {
query.onFocus();
});
});
}
onOnline() {
notifyManager.batch(() => {
this.queries.forEach(query => {
query.onOnline();
});
});
}
}