UNPKG

2.51 kBJavaScriptView Raw
1System.register([], function (exports_1, context_1) {
2 "use strict";
3 var __assign = (this && this.__assign) || Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 var __moduleName = context_1 && context_1.id;
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_1("waitFor", waitFor);
44 function waitForDocumentElement(selector, options) {
45 return waitFor(function () { return document.querySelector(selector); }, options);
46 }
47 exports_1("waitForDocumentElement", waitForDocumentElement);
48 function waitForDocumentElements(selector, options) {
49 return waitFor(function () { return document.querySelectorAll(selector); }, options);
50 }
51 exports_1("waitForDocumentElements", waitForDocumentElements);
52 return {
53 setters: [],
54 execute: function () {
55 }
56 };
57});