1 | "use strict";
|
2 | var ObservableQuery_1 = require('./ObservableQuery');
|
3 | var assign = require('lodash.assign');
|
4 | var QueryScheduler = (function () {
|
5 | function QueryScheduler(_a) {
|
6 | var queryManager = _a.queryManager;
|
7 | this.queryManager = queryManager;
|
8 | this.pollingTimers = {};
|
9 | this.inFlightQueries = {};
|
10 | this.registeredQueries = {};
|
11 | this.intervalQueries = {};
|
12 | }
|
13 | QueryScheduler.prototype.checkInFlight = function (queryId) {
|
14 | return this.inFlightQueries.hasOwnProperty(queryId);
|
15 | };
|
16 | QueryScheduler.prototype.fetchQuery = function (queryId, options) {
|
17 | var _this = this;
|
18 | return new Promise(function (resolve, reject) {
|
19 | _this.queryManager.fetchQuery(queryId, options).then(function (result) {
|
20 | _this.removeInFlight(queryId);
|
21 | resolve(result);
|
22 | }).catch(function (error) {
|
23 | _this.removeInFlight(queryId);
|
24 | reject(error);
|
25 | });
|
26 | _this.addInFlight(queryId, options);
|
27 | });
|
28 | };
|
29 | QueryScheduler.prototype.startPollingQuery = function (options, queryId, firstFetch, listener) {
|
30 | if (firstFetch === void 0) { firstFetch = true; }
|
31 | if (!options.pollInterval) {
|
32 | throw new Error('Attempted to start a polling query without a polling interval.');
|
33 | }
|
34 | this.registeredQueries[queryId] = options;
|
35 | if (firstFetch) {
|
36 | this.fetchQuery(queryId, options);
|
37 | }
|
38 | if (listener) {
|
39 | this.queryManager.addQueryListener(queryId, listener);
|
40 | }
|
41 | this.addQueryOnInterval(queryId, options);
|
42 | return queryId;
|
43 | };
|
44 | QueryScheduler.prototype.stopPollingQuery = function (queryId) {
|
45 | delete this.registeredQueries[queryId];
|
46 | };
|
47 | QueryScheduler.prototype.fetchQueriesOnInterval = function (interval) {
|
48 | var _this = this;
|
49 | this.intervalQueries[interval] = this.intervalQueries[interval].filter(function (queryId) {
|
50 | if (!_this.registeredQueries.hasOwnProperty(queryId)) {
|
51 | return false;
|
52 | }
|
53 | if (_this.checkInFlight(queryId)) {
|
54 | return true;
|
55 | }
|
56 | var queryOptions = _this.registeredQueries[queryId];
|
57 | var pollingOptions = assign({}, queryOptions);
|
58 | pollingOptions.forceFetch = true;
|
59 | _this.fetchQuery(queryId, pollingOptions);
|
60 | return true;
|
61 | });
|
62 | if (this.intervalQueries[interval].length === 0) {
|
63 | clearInterval(this.pollingTimers[interval]);
|
64 | }
|
65 | };
|
66 | QueryScheduler.prototype.addQueryOnInterval = function (queryId, queryOptions) {
|
67 | var _this = this;
|
68 | var interval = queryOptions.pollInterval;
|
69 | if (this.intervalQueries.hasOwnProperty(interval.toString()) && this.intervalQueries[interval].length > 0) {
|
70 | this.intervalQueries[interval].push(queryId);
|
71 | }
|
72 | else {
|
73 | this.intervalQueries[interval] = [queryId];
|
74 | this.pollingTimers[interval] = setInterval(function () {
|
75 | _this.fetchQueriesOnInterval(interval);
|
76 | }, interval);
|
77 | }
|
78 | };
|
79 | QueryScheduler.prototype.registerPollingQuery = function (queryOptions) {
|
80 | if (!queryOptions.pollInterval) {
|
81 | throw new Error('Attempted to register a non-polling query with the scheduler.');
|
82 | }
|
83 | return new ObservableQuery_1.ObservableQuery({
|
84 | scheduler: this,
|
85 | options: queryOptions,
|
86 | });
|
87 | };
|
88 | QueryScheduler.prototype.addInFlight = function (queryId, options) {
|
89 | this.inFlightQueries[queryId] = options;
|
90 | };
|
91 | QueryScheduler.prototype.removeInFlight = function (queryId) {
|
92 | delete this.inFlightQueries[queryId];
|
93 | };
|
94 | return QueryScheduler;
|
95 | }());
|
96 | exports.QueryScheduler = QueryScheduler;
|
97 |
|
\ | No newline at end of file |