UNPKG

11.1 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3 typeof define === 'function' && define.amd ? define(['exports'], factory) :
4 (global = global || self, factory(global.PollingObserver = {}));
5}(this, function (exports) { 'use strict';
6
7 /*! *****************************************************************************
8 Copyright (c) Microsoft Corporation. All rights reserved.
9 Licensed under the Apache License, Version 2.0 (the "License"); you may not use
10 this file except in compliance with the License. You may obtain a copy of the
11 License at http://www.apache.org/licenses/LICENSE-2.0
12
13 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 MERCHANTABLITY OR NON-INFRINGEMENT.
17
18 See the Apache Version 2.0 License for specific language governing permissions
19 and limitations under the License.
20 ***************************************************************************** */
21
22 function __awaiter(thisArg, _arguments, P, generator) {
23 return new (P || (P = Promise))(function (resolve, reject) {
24 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
27 step((generator = generator.apply(thisArg, _arguments || [])).next());
28 });
29 }
30
31 function __generator(thisArg, body) {
32 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
33 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
34 function verb(n) { return function (v) { return step([n, v]); }; }
35 function step(op) {
36 if (f) throw new TypeError("Generator is already executing.");
37 while (_) try {
38 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
39 if (y = 0, t) op = [op[0] & 2, t.value];
40 switch (op[0]) {
41 case 0: case 1: t = op; break;
42 case 4: _.label++; return { value: op[1], done: false };
43 case 5: _.label++; y = op[1]; op = [0]; continue;
44 case 7: op = _.ops.pop(); _.trys.pop(); continue;
45 default:
46 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
47 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
48 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
49 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
50 if (t[2]) _.ops.pop();
51 _.trys.pop(); continue;
52 }
53 op = body.call(thisArg, _);
54 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
55 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
56 }
57 }
58
59 function delayUntil(delay) {
60 if (delay === void 0) { delay = 0; }
61 return __awaiter(this, void 0, void 0, function () {
62 return __generator(this, function (_a) {
63 return [2, new Promise(function (yay) {
64 var delayNum = 'number' === typeof (delay) ? +delay : 0;
65 if (delayNum < 1)
66 yay();
67 else
68 setTimeout(yay, delay);
69 })];
70 });
71 });
72 }
73
74 function globalPerformance() {
75 return __awaiter(this, void 0, void 0, function () {
76 return __generator(this, function (_a) {
77 return [2, ('undefined' !== typeof (window) ? window : global).performance];
78 });
79 });
80 }
81
82 var PollingMeasure = (function () {
83 function PollingMeasure(name, duration, startTime) {
84 this.name = name;
85 this.duration = duration;
86 this.startTime = startTime;
87 this.entryType = 'polling-measure';
88 }
89 PollingMeasure.prototype.toJSON = function () {
90 return {
91 duration: this.duration,
92 entryType: this.entryType,
93 name: this.name,
94 startTime: this.startTime,
95 };
96 };
97 return PollingMeasure;
98 }());
99
100 function isPromise(r) {
101 return 'function' === typeof (r.then);
102 }
103 var PollingObserver = (function () {
104 function PollingObserver(conditionCallback) {
105 this.conditionCallback = conditionCallback;
106 this._forceStop = false;
107 this._records = [];
108 this._isPolling = false;
109 if ('function' !== typeof (conditionCallback)) {
110 throw new TypeError("'conditionCallback' is not defined");
111 }
112 }
113 PollingObserver.prototype.disconnect = function () {
114 this._forceStop = true;
115 if (!this._isPolling)
116 this._records = [];
117 };
118 PollingObserver.prototype.observe = function (callback, options) {
119 return __awaiter(this, void 0, void 0, function () {
120 var _a, interval, timeout, isValidInterval, obsTimeout, obsInterval, perf, isInfinitePolling, records, onfinishCallback, conditionCallback, totalTime, value, i, status, result, conditionResult, didConditionMeet, _b, didTimeout, startAt, r, _c, endAt, duration, timeLeft, e_1, recordsSlice;
121 return __generator(this, function (_d) {
122 switch (_d.label) {
123 case 0:
124 this._forceStop = false;
125 _a = options || {}, interval = _a.interval, timeout = _a.timeout;
126 isValidInterval = 'number' === typeof (interval) && interval > 0;
127 obsTimeout = 'number' === typeof (timeout) ? +timeout : -1;
128 obsInterval = isValidInterval ? +interval : -1;
129 return [4, globalPerformance()];
130 case 1:
131 perf = _d.sent();
132 isInfinitePolling = obsTimeout < 1;
133 records = this._records;
134 onfinishCallback = this.onfinish;
135 conditionCallback = this.conditionCallback;
136 totalTime = 0;
137 value = void 0;
138 i = 0;
139 status = 'finish';
140 result = {};
141 _d.label = 2;
142 case 2:
143 _d.trys.push([2, 13, 14, 15]);
144 _d.label = 3;
145 case 3:
146 if (this._forceStop)
147 return [3, 12];
148 this._isPolling = true;
149 conditionResult = conditionCallback(value, records, this);
150 if (!isPromise(conditionResult)) return [3, 5];
151 return [4, conditionResult];
152 case 4:
153 _b = _d.sent();
154 return [3, 6];
155 case 5:
156 _b = conditionResult;
157 _d.label = 6;
158 case 6:
159 didConditionMeet = _b;
160 didTimeout = isInfinitePolling ? false : totalTime >= obsTimeout;
161 if (didTimeout || didConditionMeet) {
162 status = didTimeout ? 'timeout' : status;
163 return [3, 12];
164 }
165 startAt = perf.now();
166 r = callback();
167 if (!isPromise(r)) return [3, 8];
168 return [4, r];
169 case 7:
170 _c = _d.sent();
171 return [3, 9];
172 case 8:
173 _c = r;
174 _d.label = 9;
175 case 9:
176 value = _c;
177 endAt = perf.now();
178 duration = endAt - startAt;
179 timeLeft = isValidInterval ? obsInterval - duration : 0;
180 records.push(new PollingMeasure("polling:" + i, duration, startAt));
181 totalTime += (duration > obsInterval ? duration : obsInterval);
182 i += 1;
183 if (!(timeLeft > 0)) return [3, 11];
184 return [4, delayUntil(timeLeft)];
185 case 10:
186 _d.sent();
187 _d.label = 11;
188 case 11: return [3, 3];
189 case 12:
190 result = { status: status, value: value };
191 return [3, 15];
192 case 13:
193 e_1 = _d.sent();
194 result = { status: 'error', reason: e_1 };
195 return [3, 15];
196 case 14:
197 recordsSlice = records.slice();
198 if (this._forceStop)
199 this._records = [];
200 this._isPolling = this._forceStop = false;
201 if ('function' === typeof (onfinishCallback)) {
202 onfinishCallback(result, recordsSlice, this);
203 }
204 return [7];
205 case 15: return [2];
206 }
207 });
208 });
209 };
210 PollingObserver.prototype.takeRecords = function () {
211 return this._records;
212 };
213 return PollingObserver;
214 }());
215
216 exports.PollingObserver = PollingObserver;
217 exports.default = PollingObserver;
218
219 Object.defineProperty(exports, '__esModule', { value: true });
220
221}));
222//# sourceMappingURL=polling-observer.umd.js.map