react-query
Version:
Hooks for managing, caching and syncing asynchronous and remote data in React
161 lines (125 loc) • 4.46 kB
JavaScript
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
import { difference, replaceAt } from './utils';
import { notifyManager } from './notifyManager';
import { QueryObserver } from './queryObserver';
import { Subscribable } from './subscribable';
export var QueriesObserver = /*#__PURE__*/function (_Subscribable) {
_inheritsLoose(QueriesObserver, _Subscribable);
function QueriesObserver(client, queries) {
var _this;
_this = _Subscribable.call(this) || this;
_this.client = client;
_this.queries = [];
_this.result = [];
_this.observers = [];
_this.observersMap = {};
if (queries) {
_this.setQueries(queries);
}
return _this;
}
var _proto = QueriesObserver.prototype;
_proto.onSubscribe = function onSubscribe() {
var _this2 = this;
if (this.listeners.length === 1) {
this.observers.forEach(function (observer) {
observer.subscribe(function (result) {
_this2.onUpdate(observer, result);
});
});
}
};
_proto.onUnsubscribe = function onUnsubscribe() {
if (!this.listeners.length) {
this.destroy();
}
};
_proto.destroy = function destroy() {
this.listeners = [];
this.observers.forEach(function (observer) {
observer.destroy();
});
};
_proto.setQueries = function setQueries(queries, notifyOptions) {
this.queries = queries;
this.updateObservers(notifyOptions);
};
_proto.getCurrentResult = function getCurrentResult() {
return this.result;
};
_proto.getOptimisticResult = function getOptimisticResult(queries) {
var _this3 = this;
return queries.map(function (options, index) {
var defaultedOptions = _this3.client.defaultQueryObserverOptions(options);
return _this3.getObserver(defaultedOptions, index).getOptimisticResult(defaultedOptions);
});
};
_proto.getObserver = function getObserver(options, index) {
var _currentObserver;
var defaultedOptions = this.client.defaultQueryObserverOptions(options);
var currentObserver = this.observersMap[defaultedOptions.queryHash];
if (!currentObserver && defaultedOptions.keepPreviousData) {
currentObserver = this.observers[index];
}
return (_currentObserver = currentObserver) != null ? _currentObserver : new QueryObserver(this.client, defaultedOptions);
};
_proto.updateObservers = function updateObservers(notifyOptions) {
var _this4 = this;
notifyManager.batch(function () {
var hasIndexChange = false;
var prevObservers = _this4.observers;
var prevObserversMap = _this4.observersMap;
var newResult = [];
var newObservers = [];
var newObserversMap = {};
_this4.queries.forEach(function (options, i) {
var defaultedOptions = _this4.client.defaultQueryObserverOptions(options);
var queryHash = defaultedOptions.queryHash;
var observer = _this4.getObserver(defaultedOptions, i);
if (prevObserversMap[queryHash] || defaultedOptions.keepPreviousData) {
observer.setOptions(defaultedOptions, notifyOptions);
}
if (observer !== prevObservers[i]) {
hasIndexChange = true;
}
newObservers.push(observer);
newResult.push(observer.getCurrentResult());
newObserversMap[queryHash] = observer;
});
if (prevObservers.length === newObservers.length && !hasIndexChange) {
return;
}
_this4.observers = newObservers;
_this4.observersMap = newObserversMap;
_this4.result = newResult;
if (!_this4.hasListeners()) {
return;
}
difference(prevObservers, newObservers).forEach(function (observer) {
observer.destroy();
});
difference(newObservers, prevObservers).forEach(function (observer) {
observer.subscribe(function (result) {
_this4.onUpdate(observer, result);
});
});
_this4.notify();
});
};
_proto.onUpdate = function onUpdate(observer, result) {
var index = this.observers.indexOf(observer);
if (index !== -1) {
this.result = replaceAt(this.result, index, result);
this.notify();
}
};
_proto.notify = function notify() {
var _this5 = this;
notifyManager.batch(function () {
_this5.listeners.forEach(function (listener) {
listener(_this5.result);
});
});
};
return QueriesObserver;
}(Subscribable);