UNPKG

4.61 kBSource Map (JSON)View Raw
1{"version":3,"file":"eventloop-d0571621.cjs","sources":["../eventloop.js"],"sourcesContent":["/* global requestIdleCallback, requestAnimationFrame, cancelIdleCallback, cancelAnimationFrame */\n\n/**\n * Utility module to work with EcmaScript's event loop.\n *\n * @module eventloop\n */\n\n/**\n * @type {Array<function>}\n */\nlet queue = []\n\nconst _runQueue = () => {\n for (let i = 0; i < queue.length; i++) {\n queue[i]()\n }\n queue = []\n}\n\n/**\n * @param {function():void} f\n */\nexport const enqueue = f => {\n queue.push(f)\n if (queue.length === 1) {\n setTimeout(_runQueue, 0)\n }\n}\n\n/**\n * @typedef {Object} TimeoutObject\n * @property {function} TimeoutObject.destroy\n */\n\n/**\n * @param {function(number):void} clearFunction\n */\nconst createTimeoutClass = clearFunction => class TT {\n /**\n * @param {number} timeoutId\n */\n constructor (timeoutId) {\n this._ = timeoutId\n }\n\n destroy () {\n clearFunction(this._)\n }\n}\n\nconst Timeout = createTimeoutClass(clearTimeout)\n\n/**\n * @param {number} timeout\n * @param {function} callback\n * @return {TimeoutObject}\n */\nexport const timeout = (timeout, callback) => new Timeout(setTimeout(callback, timeout))\n\nconst Interval = createTimeoutClass(clearInterval)\n\n/**\n * @param {number} timeout\n * @param {function} callback\n * @return {TimeoutObject}\n */\nexport const interval = (timeout, callback) => new Interval(setInterval(callback, timeout))\n\n/* c8 ignore next */\nexport const Animation = createTimeoutClass(arg => typeof requestAnimationFrame !== 'undefined' && cancelAnimationFrame(arg))\n\n/**\n * @param {function(number):void} cb\n * @return {TimeoutObject}\n */\n/* c8 ignore next */\nexport const animationFrame = cb => typeof requestAnimationFrame === 'undefined' ? timeout(0, cb) : new Animation(requestAnimationFrame(cb))\n\n/* c8 ignore next */\n// @ts-ignore\nconst Idle = createTimeoutClass(arg => typeof cancelIdleCallback !== 'undefined' && cancelIdleCallback(arg))\n\n/**\n * Note: this is experimental and is probably only useful in browsers.\n *\n * @param {function} cb\n * @return {TimeoutObject}\n */\n/* c8 ignore next 2 */\n// @ts-ignore\nexport const idleCallback = cb => typeof requestIdleCallback !== 'undefined' ? new Idle(requestIdleCallback(cb)) : timeout(1000, cb)\n\n/**\n * @param {number} timeout Timeout of the debounce action\n * @return {function(function():void):void}\n */\nexport const createDebouncer = timeout => {\n let timer = -1\n return f => {\n clearTimeout(timer)\n if (f) {\n timer = /** @type {any} */ (setTimeout(f, timeout))\n }\n }\n}\n"],"names":[],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAI,KAAK,GAAG,GAAE;AACd;AACA,MAAM,SAAS,GAAG,MAAM;AACxB,EAAE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;AACzC,IAAI,KAAK,CAAC,CAAC,CAAC,GAAE;AACd,GAAG;AACH,EAAE,KAAK,GAAG,GAAE;AACZ,EAAC;AACD;AACA;AACA;AACA;AACY,MAAC,OAAO,GAAG,CAAC,IAAI;AAC5B,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,EAAC;AACf,EAAE,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE;AAC1B,IAAI,UAAU,CAAC,SAAS,EAAE,CAAC,EAAC;AAC5B,GAAG;AACH,EAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,kBAAkB,GAAG,aAAa,IAAI,MAAM,EAAE,CAAC;AACrD;AACA;AACA;AACA,EAAE,WAAW,CAAC,CAAC,SAAS,EAAE;AAC1B,IAAI,IAAI,CAAC,CAAC,GAAG,UAAS;AACtB,GAAG;AACH;AACA,EAAE,OAAO,CAAC,GAAG;AACb,IAAI,aAAa,CAAC,IAAI,CAAC,CAAC,EAAC;AACzB,GAAG;AACH,EAAC;AACD;AACA,MAAM,OAAO,GAAG,kBAAkB,CAAC,YAAY,EAAC;AAChD;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,OAA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