You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.6 KiB

5 years ago
export const tests = []
const t = (f) => tests.push(f)
const add = (arr, el) => arr?.push(el)
5 years ago
const run = (callback, callLimit, nbr) =>
new Promise((r) => {
let arr = []
let inter = setInterval(() => callback(arr, 1), callLimit)
setTimeout(() => {
clearInterval(inter)
r(arr.length)
}, callLimit * nbr)
})
t(async ({ eq }) =>
// wait 26ms and execute 4 times every 16ms, executes with a wait time of 26
eq(await run(throttle(add, 26), 16, 4), 2)
)
5 years ago
t(async ({ eq }) =>
// wait 20ms and execute 2 times every 10ms, executes with a wait time of 26
eq(await run(throttle(add, 20), 10, 2), 1)
)
5 years ago
t(async ({ eq }) =>
// wait 16ms and execute 5 times every 26ms, will execute with out waiting
eq(await run(throttle(add, 16), 26, 5), 4)
)
5 years ago
t(async ({ eq }) =>
// it works concurrently
5 years ago
eq(
await Promise.all([
run(throttle(add, 16), 26, 6),
run(throttle(add, 16), 26, 6),
5 years ago
]),
[5, 5]
)
5 years ago
)
t(async ({ eq }) =>
// tests the trailing option
eq(await run(opThrottle(add, 26, { trailing: true }), 16, 4), 1)
5 years ago
)
t(async ({ eq }) =>
// tests the leading option with wait time in the leading edge of the timeout
eq(await run(opThrottle(add, 15, { leading: true }), 10, 10), 5)
5 years ago
)
t(async ({ eq }) =>
// tests the leading option with wait time not in the leading edge of the timeout
eq(await run(opThrottle(add, 26, { leading: true }), 16, 4), 2)
5 years ago
)
t(async ({ eq }) =>
// tests without options
eq(await run(opThrottle(add, 10), 5, 2), 0)
)
5 years ago
t(async ({ eq }) =>
// tests with both options true
5 years ago
eq(
await run(opThrottle(add, 26, { trailing: true, leading: true }), 16, 4),
2
)
5 years ago
)
Object.freeze(tests)