UNPKG

promisify-dom-selector

Version:

Promisify any DOM selector so that it returns a promise which resolves when the element appears in the DOM

14 lines (9 loc) 1.01 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.promisifyDOMSelector = factory()); }(this, (function () { 'use strict'; function isArrayLike(a){return Array.isArray(a)||a instanceof NodeList||a instanceof HTMLCollection} function promisifyDOMSelector(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:document;return function(c){return new Promise(function(d,e){if(b!==document&&(a===document.getElementById||a===document.getElementsByName))return e('Cannot call '+a.name+'() with another context besides document.');b!==document&&(a=Element.prototype[a.name]);var f=a.call(b,c);if(f||isArrayLike(f)&&0<f.length)return d(f);var g=new MutationObserver(function(e,f){var g=a.call(b,c);!g||isArrayLike(g)&&0===g.length||(f.disconnect(),d(g));});g.observe(b===document?document.body:b,{childList:!0,subtree:!0});})}} return promisifyDOMSelector; })));