Skip to content

Commit

Permalink
feat(js): add debounce-promise
Browse files Browse the repository at this point in the history
  • Loading branch information
seognil committed Aug 22, 2020
1 parent 8719dfa commit a9462e6
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions js/util/debounce-promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
// * ------------------------------------------------ debounceP

const debounceP = <T extends any[], K>(fn: (...args: T) => Promise<K>, duration = 0) => {
let tick: NodeJS.Timeout;

let singleP: Promise<K> | null;
let singleRes: ((value: K) => void) | null;
let singleRej: ((reason?: any) => void) | null;

return (...args: T) => {
if (singleP) {
clearTimeout(tick);
} else {
singleP = new Promise<K>((res, rej) => {
singleRes = res;
singleRej = rej;
});
}

tick = setTimeout(() => {
clearTimeout(tick);
fn(...args!).then(singleRes, singleRej);
singleP = singleRes = singleRej = null;
}, duration);

return singleP;
};
};

// * ------------------------------------------------ test

const delay = (n: number) => new Promise((res) => setTimeout(() => res(), n));

const fn = async (val: number) => {
console.log('run fn, args:', val);
await delay(100);
return val;
};

const dFn = debounceP(fn, 500);

// * ----------------

(async () => {
dFn(1).then((e) => (console.log(e), console.assert(e === 3)));
dFn(2).then((e) => (console.log(e), console.assert(e === 3)));
dFn(3).then((e) => (console.log(e), console.assert(e === 3)));

console.log('--------');
await delay(1000);
console.log('--------');

dFn(6).then((e) => (console.log(e), console.assert(e === 8)));
dFn(7).then((e) => (console.log(e), console.assert(e === 8)));
dFn(8).then((e) => (console.log(e), console.assert(e === 8)));
})();
}

0 comments on commit a9462e6

Please sign in to comment.