UNPKG

2.41 kBJavaScriptView Raw
1var __assign = (this && this.__assign) || Object.assign || function(t) {
2 for (var s, i = 1, n = arguments.length; i < n; i++) {
3 s = arguments[i];
4 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
5 t[p] = s[p];
6 }
7 return t;
8};
9define(["require", "exports"], function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 /**
13 * Generic function to wait for something to happen. Uses polling
14 * @param getter: a getter function that returns anything else than `null` or an
15 * empty array or an empty jQuery object when the
16 * condition is met
17 * @param options: lookup options, defaults to
18 * `{present: true, interval: 50, timeout: 5000}`
19 */
20 function waitFor(getter, options) {
21 if (options === void 0) { options = { present: true, interval: 50, timeout: 5000 }; }
22 // prevents infinite recursion if the request times out
23 var timedOut = false;
24 options = __assign({ present: true, interval: 50, timeout: 5000 }, options);
25 function wait() {
26 var element = getter();
27 // boolean is needed here, hence the length > 0
28 var found = element !== null && (!(element instanceof NodeList) &&
29 !element.jquery || element.length > 0);
30 if (!options.present === !found || timedOut) {
31 return Promise.resolve(element);
32 }
33 return new Promise(function (rs) { return setTimeout(rs, options.interval); }).then(wait);
34 }
35 return Promise.race([
36 new Promise(function (_, rj) { return setTimeout(function () {
37 timedOut = true;
38 rj(new Error(options.present ? 'Element not found' : 'Element not removed'));
39 }, options.timeout); }),
40 wait()
41 ]);
42 }
43 exports.waitFor = waitFor;
44 function waitForDocumentElement(selector, options) {
45 return waitFor(function () { return document.querySelector(selector); }, options);
46 }
47 exports.waitForDocumentElement = waitForDocumentElement;
48 function waitForDocumentElements(selector, options) {
49 return waitFor(function () { return document.querySelectorAll(selector); }, options);
50 }
51 exports.waitForDocumentElements = waitForDocumentElements;
52});