UNPKG

angular2-data-table

Version:

angular2-data-table is a Angular2 component for presenting large and complex data.

78 lines (69 loc) 1.6 kB
/** * Throttle a function * * @export * @param {*} func * @param {number} wait * @param {*} [options] * @returns */ export function throttle(func: any, wait: number, options?: any) { options = options || {}; let context; let args; let result; let timeout = null; let previous = 0; function later() { previous = options.leading === false ? 0 : +new Date(); timeout = null; result = func.apply(context, args); } return function() { let now = +new Date(); if (!previous && options.leading === false) { previous = now; } let remaining = wait - (now - previous); context = this; args = arguments; if (remaining <= 0) { clearTimeout(timeout); timeout = null; previous = now; result = func.apply(context, args); } else if (!timeout && options.trailing !== false) { timeout = setTimeout(later, remaining); } return result; }; } /** * Throttle decorator * * class MyClass { * throttleable(10) * myFn() { ... } * } * * @export * @param {number} duration * @param {*} [options] * @returns */ export function throttleable(duration: number, options?: any) { return function innerDecorator (target, key, descriptor) { return { configurable: true, enumerable: descriptor.enumerable, get: function getter () { Object.defineProperty(this, key, { configurable: true, enumerable: descriptor.enumerable, value: throttle(descriptor.value, duration, options) }); return this[key]; } }; }; }