@vkontakte/vkjs
Advanced tools
Comparing version 0.22.2 to 0.23.0
@@ -6,5 +6,6 @@ export var noop = function noop() {}; // eslint-disable-line @typescript-eslint/no-empty-function | ||
var scope = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : window; | ||
var last; | ||
var deferTimer; | ||
return function () { | ||
var prevDate = Date.now() - threshold; | ||
var timeoutId; | ||
var throttledFn = function throttledFn() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
@@ -14,20 +15,26 @@ args[_key] = arguments[_key]; | ||
var context = scope; | ||
var now = Date.now(); | ||
var timeLeft = prevDate + threshold - Date.now(); | ||
clearTimeout(timeoutId); | ||
if (last && now < last + threshold) { | ||
clearTimeout(deferTimer); | ||
deferTimer = setTimeout(function () { | ||
last = now; | ||
fn.apply(context, args); | ||
}, threshold); | ||
} else { | ||
last = now; | ||
fn.apply(context, args); | ||
if (timeLeft > 0) { | ||
timeoutId = setTimeout(function () { | ||
prevDate = Date.now(); | ||
fn.apply(scope, args); | ||
}, timeLeft); | ||
return; | ||
} | ||
prevDate = Date.now(); | ||
fn.apply(scope, args); | ||
}; | ||
throttledFn.cancel = function () { | ||
clearTimeout(timeoutId); | ||
}; | ||
return throttledFn; | ||
} | ||
export function debounce(fn, delay) { | ||
var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : window; | ||
var timeout; | ||
var timeoutId; | ||
var args; | ||
@@ -39,3 +46,3 @@ | ||
return function () { | ||
var debouncedFn = function debouncedFn() { | ||
for (var _len2 = arguments.length, a = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
@@ -46,6 +53,12 @@ a[_key2] = arguments[_key2]; | ||
args = a; | ||
clearTimeout(timeout); | ||
timeout = setTimeout(later, delay); | ||
clearTimeout(timeoutId); | ||
timeoutId = setTimeout(later, delay); | ||
}; | ||
debouncedFn.cancel = function () { | ||
clearTimeout(timeoutId); | ||
}; | ||
return debouncedFn; | ||
} | ||
//# sourceMappingURL=functions.js.map |
export declare const noop: () => void; | ||
export declare function throttle<T extends any[]>(fn: (...args: T) => unknown, threshold?: number, scope?: Window & typeof globalThis): (...args: T) => void; | ||
export declare function debounce<T extends any[]>(fn: (...args: T) => unknown, delay: number, context?: Window & typeof globalThis): (...a: T) => void; | ||
export declare function throttle<T extends any[]>(fn: (...args: T) => unknown, threshold?: number, scope?: Window & typeof globalThis): { | ||
(...args: T): void; | ||
cancel(): void; | ||
}; | ||
export declare function debounce<T extends any[]>(fn: (...args: T) => unknown, delay: number, context?: Window & typeof globalThis): { | ||
(...a: T): void; | ||
cancel(): void; | ||
}; |
@@ -18,5 +18,6 @@ "use strict"; | ||
var scope = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : window; | ||
var last; | ||
var deferTimer; | ||
return function () { | ||
var prevDate = Date.now() - threshold; | ||
var timeoutId; | ||
var throttledFn = function throttledFn() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
@@ -26,16 +27,22 @@ args[_key] = arguments[_key]; | ||
var context = scope; | ||
var now = Date.now(); | ||
var timeLeft = prevDate + threshold - Date.now(); | ||
clearTimeout(timeoutId); | ||
if (last && now < last + threshold) { | ||
clearTimeout(deferTimer); | ||
deferTimer = setTimeout(function () { | ||
last = now; | ||
fn.apply(context, args); | ||
}, threshold); | ||
} else { | ||
last = now; | ||
fn.apply(context, args); | ||
if (timeLeft > 0) { | ||
timeoutId = setTimeout(function () { | ||
prevDate = Date.now(); | ||
fn.apply(scope, args); | ||
}, timeLeft); | ||
return; | ||
} | ||
prevDate = Date.now(); | ||
fn.apply(scope, args); | ||
}; | ||
throttledFn.cancel = function () { | ||
clearTimeout(timeoutId); | ||
}; | ||
return throttledFn; | ||
} | ||
@@ -45,3 +52,3 @@ | ||
var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : window; | ||
var timeout; | ||
var timeoutId; | ||
var args; | ||
@@ -53,3 +60,3 @@ | ||
return function () { | ||
var debouncedFn = function debouncedFn() { | ||
for (var _len2 = arguments.length, a = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
@@ -60,6 +67,12 @@ a[_key2] = arguments[_key2]; | ||
args = a; | ||
clearTimeout(timeout); | ||
timeout = setTimeout(later, delay); | ||
clearTimeout(timeoutId); | ||
timeoutId = setTimeout(later, delay); | ||
}; | ||
debouncedFn.cancel = function () { | ||
clearTimeout(timeoutId); | ||
}; | ||
return debouncedFn; | ||
} | ||
//# sourceMappingURL=functions.js.map |
{ | ||
"name": "@vkontakte/vkjs", | ||
"version": "0.22.2", | ||
"version": "0.23.0", | ||
"description": "VK shared JS libs", | ||
@@ -16,2 +16,5 @@ "main": "lib/index.js", | ||
], | ||
"engines": { | ||
"yarn": "^1.21.1" | ||
}, | ||
"scripts": { | ||
@@ -18,0 +21,0 @@ "clear": "shx rm -rf lib", |
@@ -1,35 +0,46 @@ | ||
export const noop = () => {}; // eslint-disable-line @typescript-eslint/no-empty-function | ||
export const noop = () => { }; // eslint-disable-line @typescript-eslint/no-empty-function | ||
export function throttle<T extends any[]>(fn: (...args: T) => unknown, threshold = 50, scope = window) { | ||
let last: number; | ||
let deferTimer: ReturnType<typeof setTimeout>; | ||
let prevDate: number = Date.now() - threshold; | ||
let timeoutId: ReturnType<typeof setTimeout>; | ||
return function(...args: T) { | ||
const context = scope; | ||
const now = Date.now(); | ||
const throttledFn = function(...args: T) { | ||
const timeLeft = prevDate + threshold - Date.now(); | ||
if (last && now < last + threshold) { | ||
clearTimeout(deferTimer); | ||
deferTimer = setTimeout(() => { | ||
last = now; | ||
fn.apply(context, args); | ||
}, threshold); | ||
} else { | ||
last = now; | ||
fn.apply(context, args); | ||
clearTimeout(timeoutId); | ||
if (timeLeft > 0) { | ||
timeoutId = setTimeout(() => { | ||
prevDate = Date.now(); | ||
fn.apply(scope, args); | ||
}, timeLeft); | ||
return; | ||
} | ||
prevDate = Date.now(); | ||
fn.apply(scope, args); | ||
}; | ||
throttledFn.cancel = () => { | ||
clearTimeout(timeoutId); | ||
}; | ||
return throttledFn; | ||
} | ||
export function debounce<T extends any[]>(fn: (...args: T) => unknown, delay: number, context = window) { | ||
let timeout: ReturnType<typeof setTimeout>; | ||
let timeoutId: ReturnType<typeof setTimeout>; | ||
let args: T; | ||
const later = () => fn.apply(context, args); | ||
return (...a: T) => { | ||
const debouncedFn = (...a: T) => { | ||
args = a; | ||
clearTimeout(timeout); | ||
timeout = setTimeout(later, delay); | ||
clearTimeout(timeoutId); | ||
timeoutId = setTimeout(later, delay); | ||
}; | ||
debouncedFn.cancel = () => { | ||
clearTimeout(timeoutId); | ||
}; | ||
return debouncedFn; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
294814
165
4364