Compare commits
No commits in common. "stable" and "1.0.0" have entirely different histories.
|
@ -1,36 +0,0 @@
|
||||||
"use strict";
|
|
||||||
/**
|
|
||||||
* @name Damper
|
|
||||||
*
|
|
||||||
* @description
|
|
||||||
* Execute multiple `function` calls in a `timeout` amount of time just once
|
|
||||||
*
|
|
||||||
* You may also know this technology as a "debouncer" (you are free to use any name)
|
|
||||||
* I prefer "damper", from the word "демпфер"
|
|
||||||
*
|
|
||||||
* @param {function} function Function to execute after damping
|
|
||||||
* @param {number} timeout Timer in milliseconds (ms)
|
|
||||||
* @param {number} force Argument number storing the status of enforcement execution (see @example)
|
|
||||||
*
|
|
||||||
* @return {Promise}
|
|
||||||
*
|
|
||||||
* @example <caption>Forced execution</caption>
|
|
||||||
* const dampered = damper(
|
|
||||||
* (
|
|
||||||
* a, // 0
|
|
||||||
* b, // 1
|
|
||||||
* c, // 2
|
|
||||||
* force = false, // 3
|
|
||||||
* d, // 4
|
|
||||||
* resolve,
|
|
||||||
* reject
|
|
||||||
* ) => {},
|
|
||||||
* 500,
|
|
||||||
* 3, // 3 -> the "force" argument
|
|
||||||
* );
|
|
||||||
*
|
|
||||||
* dampered('for a', 'for b', 'for c', true, 'for d'); // Enabled forced execution
|
|
||||||
*
|
|
||||||
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
|
||||||
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
|
|
||||||
*/export function damper(e,p=300,t){let s;return(...i)=>new Promise(((r,u)=>{clearTimeout(s),"number"==typeof t&&i[t]?("number"==typeof t&&(i=[...i.splice(0,t),...i.splice(t+1)]),i.push(r,u),e.apply(this,i)):("number"==typeof t&&(i=[...i.splice(0,t),...i.splice(t+1)]),i.push(r,u),s=setTimeout((()=>e.apply(this,i)),p))}))}
|
|
|
@ -35,7 +35,7 @@
|
||||||
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
|
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
|
||||||
*/
|
*/
|
||||||
export function damper(func, timeout = 300, force) {
|
export default function damper(func, timeout = 300, force) {
|
||||||
// Declaring the timer for executing the function
|
// Declaring the timer for executing the function
|
||||||
let timer;
|
let timer;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue