|
|
@ -3,26 +3,39 @@ const t = (f) => tests.push(f) |
|
|
|
|
|
|
|
|
|
|
|
const add = (arr, el) => arr.push(el) |
|
|
|
const add = (arr, el) => arr.push(el) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// async setInterval is not precise enough so I made this synchronous one
|
|
|
|
|
|
|
|
const setIntervalSync = (fn, delay, limit) => { |
|
|
|
|
|
|
|
let run = true |
|
|
|
|
|
|
|
let count = 1 |
|
|
|
|
|
|
|
const start = Date.now() |
|
|
|
|
|
|
|
while (run) { |
|
|
|
|
|
|
|
const tick = Date.now() |
|
|
|
|
|
|
|
const elapsed = tick - start |
|
|
|
|
|
|
|
if (elapsed > count * delay) { |
|
|
|
|
|
|
|
console.log({ elapsed, count }) |
|
|
|
|
|
|
|
fn() |
|
|
|
|
|
|
|
count++ |
|
|
|
|
|
|
|
if (count >= limit) return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// it uses the array to better test the leading and trailing edge of the time limit
|
|
|
|
// it uses the array to better test the leading and trailing edge of the time limit
|
|
|
|
// so if the leading edge is true it will execute the callback
|
|
|
|
// so if the leading edge is true it will execute the callback
|
|
|
|
// if the trailing edge is true it will execute the callback before returning the array
|
|
|
|
// if the trailing edge is true it will execute the callback before returning the array
|
|
|
|
const run = (callback, { delay, count }) => |
|
|
|
const run = (callback, { delay, count }) => { |
|
|
|
new Promise((r) => { |
|
|
|
const arr = [] |
|
|
|
const arr = [] |
|
|
|
setIntervalSync(() => callback(arr, 1), delay, count) |
|
|
|
const inter = setInterval(() => callback(arr, 1), delay) |
|
|
|
return arr.length |
|
|
|
setTimeout(() => { |
|
|
|
} |
|
|
|
clearInterval(inter) |
|
|
|
|
|
|
|
r(arr.length) |
|
|
|
|
|
|
|
}, delay * count) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t(async ({ eq }) => |
|
|
|
t(async ({ eq }) => |
|
|
|
// test with debounce wait limit inferior to wait time call (how much time we wait to the function be called again)
|
|
|
|
// test with debounce wait limit inferior to wait time call (how much time we wait to the function be called again)
|
|
|
|
// it works concurrently
|
|
|
|
// it works concurrently
|
|
|
|
eq( |
|
|
|
eq( |
|
|
|
await Promise.all([ |
|
|
|
await Promise.all([ |
|
|
|
run(debounce(add, 50), { delay: 100, count: 5 }), |
|
|
|
run(debounce(add, 5), { delay: 10, count: 5 }), |
|
|
|
run(debounce(add, 20), { delay: 50, count: 10 }), |
|
|
|
run(debounce(add, 2), { delay: 5, count: 10 }), |
|
|
|
]), |
|
|
|
]), |
|
|
|
[4, 9] |
|
|
|
[4, 9] |
|
|
|
) |
|
|
|
) |
|
|
@ -30,15 +43,15 @@ t(async ({ eq }) => |
|
|
|
t(async ({ eq }) => |
|
|
|
t(async ({ eq }) => |
|
|
|
// testing with wait limit superior to wait time call
|
|
|
|
// testing with wait limit superior to wait time call
|
|
|
|
// execution on the trailing edge, after wait limit has elapsed
|
|
|
|
// execution on the trailing edge, after wait limit has elapsed
|
|
|
|
eq(await run(debounce(add, 100), { delay: 50, count: 5 }), 0) |
|
|
|
eq(await run(debounce(add, 10), { delay: 5, count: 5 }), 0) |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
t(async ({ eq }) => |
|
|
|
t(async ({ eq }) => |
|
|
|
// it works concurrently
|
|
|
|
// it works concurrently
|
|
|
|
eq( |
|
|
|
eq( |
|
|
|
await Promise.all([ |
|
|
|
await Promise.all([ |
|
|
|
run(opDebounce(add, 40), { delay: 20, count: 5 }), |
|
|
|
run(opDebounce(add, 4), { delay: 2, count: 5 }), |
|
|
|
run(opDebounce(add, 40), { delay: 20, count: 2 }), |
|
|
|
run(opDebounce(add, 4), { delay: 2, count: 2 }), |
|
|
|
]), |
|
|
|
]), |
|
|
|
[0, 0] |
|
|
|
[0, 0] |
|
|
|
) |
|
|
|
) |
|
|
@ -49,8 +62,8 @@ t(async ({ eq }) => |
|
|
|
// it works concurrently
|
|
|
|
// it works concurrently
|
|
|
|
eq( |
|
|
|
eq( |
|
|
|
await Promise.all([ |
|
|
|
await Promise.all([ |
|
|
|
run(opDebounce(add, 200, { leading: true }), { delay: 70, count: 3 }), |
|
|
|
run(opDebounce(add, 20, { leading: true }), { delay: 7, count: 3 }), |
|
|
|
run(opDebounce(add, 100, { leading: true }), { delay: 140, count: 3 }), |
|
|
|
run(opDebounce(add, 10, { leading: true }), { delay: 14, count: 3 }), |
|
|
|
]), |
|
|
|
]), |
|
|
|
[1, 2] |
|
|
|
[1, 2] |
|
|
|
) |
|
|
|
) |
|
|
|