vue-use-query
Version:
vue use query
135 lines (134 loc) • 5.53 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { difference, replaceAt } from './utils';
import { notifyManager } from './notifyManager';
import { QueryObserver } from './queryObserver';
import { Subscribable } from './subscribable';
var QueriesObserver = /** @class */ (function (_super) {
__extends(QueriesObserver, _super);
function QueriesObserver(client, queries) {
var _this = _super.call(this) || this;
_this.client = client;
_this.queries = [];
_this.result = [];
_this.observers = [];
_this.observersMap = {};
if (queries) {
_this.setQueries(queries);
}
return _this;
}
QueriesObserver.prototype.onSubscribe = function () {
var _this = this;
if (this.listeners.length === 1) {
this.observers.forEach(function (observer) {
observer.subscribe(function (result) {
_this.onUpdate(observer, result);
});
});
}
};
QueriesObserver.prototype.onUnsubscribe = function () {
if (!this.listeners.length) {
this.destroy();
}
};
QueriesObserver.prototype.destroy = function () {
this.listeners = [];
this.observers.forEach(function (observer) {
observer.destroy();
});
};
QueriesObserver.prototype.setQueries = function (queries, notifyOptions) {
this.queries = queries;
this.updateObservers(notifyOptions);
};
QueriesObserver.prototype.getCurrentResult = function () {
return this.result;
};
QueriesObserver.prototype.getOptimisticResult = function (queries) {
var _this = this;
return queries.map(function (options) {
var defaultedOptions = _this.client.defaultQueryObserverOptions(options);
return _this.getObserver(defaultedOptions).getOptimisticResult(defaultedOptions);
});
};
QueriesObserver.prototype.getObserver = function (options) {
var defaultedOptions = this.client.defaultQueryObserverOptions(options);
return (this.observersMap[defaultedOptions.queryHash] ||
new QueryObserver(this.client, defaultedOptions));
};
QueriesObserver.prototype.updateObservers = function (notifyOptions) {
var _this = this;
notifyManager.batch(function () {
var hasIndexChange = false;
var prevObservers = _this.observers;
var prevOberversMap = _this.observersMap;
var newResult = [];
var newObservers = [];
var newObserversMap = {};
_this.queries.forEach(function (options, i) {
var defaultedOptions = _this.client.defaultQueryObserverOptions(options);
var queryHash = defaultedOptions.queryHash;
var observer = _this.getObserver(defaultedOptions);
if (prevOberversMap[queryHash]) {
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;
}
_this.observers = newObservers;
_this.observersMap = newObserversMap;
_this.result = newResult;
if (!_this.hasListeners()) {
return;
}
difference(prevObservers, newObservers).forEach(function (observer) {
observer.destroy();
});
difference(newObservers, prevObservers).forEach(function (observer) {
observer.subscribe(function (result) {
_this.onUpdate(observer, result);
});
});
_this.notify();
});
};
QueriesObserver.prototype.onUpdate = function (observer, result) {
var index = this.observers.indexOf(observer);
if (index !== -1) {
this.result = replaceAt(this.result, index, result);
this.notify();
}
};
QueriesObserver.prototype.notify = function () {
var _this = this;
notifyManager.batch(function () {
_this.listeners.forEach(function (listener) {
listener(_this.result);
});
});
};
return QueriesObserver;
}(Subscribable));
export { QueriesObserver };