UNPKG

@web-atoms/core-docs

Version:
55 lines 2.03 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "./baseTypes"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const baseTypes_1 = require("./baseTypes"); const timerSymbol = Symbol(); const Once = (timeInMS = 100) => (target, key) => { baseTypes_1.registerInit(target, (vm) => { // tslint:disable-next-line: ban-types const oldMethod = vm[key]; let keyTimer = vm[timerSymbol]; if (typeof keyTimer === "undefined") { keyTimer = {}; vm[timerSymbol] = keyTimer; } let running = false; // tslint:disable-next-line:only-arrow-functions vm[key] = (...a) => { if (running) { return; } const pending = keyTimer[key]; if (pending) { clearTimeout(pending); } keyTimer[key] = setTimeout((...b) => { running = true; delete keyTimer[key]; const r = oldMethod.apply(vm, b); if (r && r.then) { r.then(() => { running = false; }, (e) => { running = false; // tslint:disable-next-line: no-console console.warn(e); }); } else { running = false; } }, timeInMS, ...a); }; }); }; exports.default = Once; }); //# sourceMappingURL=Once.js.map