UNPKG

typescript-debounce-decorator

Version:
2 lines (1 loc) 1.7 kB
"use strict";function cancel(e){e&&e.options&&clearTimeout(e.options.timer)}function getWrapper(o,n,i,e){var a={timer:void 0,lastArgs:[]},t=function(){for(var e=this,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];a.lastArgs=t,a.timer?clearTimeout(a.timer):n&&i.apply(this,a.lastArgs),a.timer=setTimeout(function(){n||i.apply(e,a.lastArgs),a.timer=void 0},o)};return e&&(t=t.bind(e)),t.options=a,t}function defineProperty(t,r,e,o){var n;Object.defineProperty(e,o,{configurable:!0,enumerable:!1,get:function(){return n},set:function(e){n=getWrapper(t,r,e,this)}})}function modifyDescriptor(e,t,r){var o=r.value;return r.value=getWrapper(e,t,o),r}function createDebounce(e,t){for(var r=[],o=2;o<arguments.length;o++)r[o-2]=arguments[o];if(0===r.length)throw new Error("function applied debounce decorator should be a method");if(1===r.length)throw new Error("method applied debounce decorator should have valid name");var n=r[0],i=r[1],a=3===r.length&&r[2]?r[2]:Object.getOwnPropertyDescriptor(n,i);if(a)return modifyDescriptor(e,t,a);defineProperty(e,t,n,i)}function debounce(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var r=500,o=!1;if(e.length&&("number"==typeof e[0]||"object"==typeof e[0]&&void 0!==e[0].leading)){"number"==typeof e[0]&&(r=e[0]);var n=void 0;return"object"==typeof e[0]&&void 0!==e[0].leading&&(n=e[0]),1<e.length&&"object"==typeof e[1]&&void 0!==e[1].leading&&(n=e[1]),n&&(o=n.leading),function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return createDebounce.apply(void 0,[r,o].concat(e))}}return createDebounce.apply(void 0,[r,o].concat(e))}Object.defineProperty(exports,"__esModule",{value:!0}),exports.cancel=cancel,exports.debounce=debounce;