apollo-client
Version:
A simple yet functional GraphQL client.
97 lines • 4 kB
JavaScript
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
import { FetchType, } from '../core/types';
import { ObservableQuery } from '../core/ObservableQuery';
import { NetworkStatus } from '../queries/networkStatus';
var QueryScheduler = (function () {
function QueryScheduler(_a) {
var queryManager = _a.queryManager;
this.queryManager = queryManager;
this.pollingTimers = {};
this.inFlightQueries = {};
this.registeredQueries = {};
this.intervalQueries = {};
}
QueryScheduler.prototype.checkInFlight = function (queryId) {
var queries = this.queryManager.getApolloState().queries;
return queries[queryId] && queries[queryId].networkStatus !== NetworkStatus.ready;
};
QueryScheduler.prototype.fetchQuery = function (queryId, options, fetchType) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.queryManager.fetchQuery(queryId, options, fetchType).then(function (result) {
resolve(result);
}).catch(function (error) {
reject(error);
});
});
};
QueryScheduler.prototype.startPollingQuery = function (options, queryId, listener) {
if (!options.pollInterval) {
throw new Error('Attempted to start a polling query without a polling interval.');
}
this.registeredQueries[queryId] = options;
if (listener) {
this.queryManager.addQueryListener(queryId, listener);
}
this.addQueryOnInterval(queryId, options);
return queryId;
};
QueryScheduler.prototype.stopPollingQuery = function (queryId) {
delete this.registeredQueries[queryId];
};
QueryScheduler.prototype.fetchQueriesOnInterval = function (interval) {
var _this = this;
this.intervalQueries[interval] = this.intervalQueries[interval].filter(function (queryId) {
if (!_this.registeredQueries.hasOwnProperty(queryId)) {
return false;
}
if (_this.checkInFlight(queryId)) {
return true;
}
var queryOptions = _this.registeredQueries[queryId];
var pollingOptions = __assign({}, queryOptions);
pollingOptions.forceFetch = true;
_this.fetchQuery(queryId, pollingOptions, FetchType.poll);
return true;
});
if (this.intervalQueries[interval].length === 0) {
clearInterval(this.pollingTimers[interval]);
delete this.intervalQueries[interval];
}
};
QueryScheduler.prototype.addQueryOnInterval = function (queryId, queryOptions) {
var _this = this;
var interval = queryOptions.pollInterval;
if (!interval) {
throw new Error("A poll interval is required to start polling query with id '" + queryId + "'.");
}
if (this.intervalQueries.hasOwnProperty(interval.toString()) && this.intervalQueries[interval].length > 0) {
this.intervalQueries[interval].push(queryId);
}
else {
this.intervalQueries[interval] = [queryId];
this.pollingTimers[interval] = setInterval(function () {
_this.fetchQueriesOnInterval(interval);
}, interval);
}
};
QueryScheduler.prototype.registerPollingQuery = function (queryOptions) {
if (!queryOptions.pollInterval) {
throw new Error('Attempted to register a non-polling query with the scheduler.');
}
return new ObservableQuery({
scheduler: this,
options: queryOptions,
});
};
return QueryScheduler;
}());
export { QueryScheduler };
//# sourceMappingURL=scheduler.js.map