Skip to content

Commit 81792a1

Browse files
committed
Add tests
1 parent 6415ade commit 81792a1

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

async/unstable_throttle.ts

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -70,40 +70,19 @@ export interface ThrottledFunction<T extends Array<unknown>> {
7070
* assert(func.lastExecution > 0);
7171
* ```
7272
*
73-
* @example Using a dynamic timeframe
74-
* ```ts
75-
* import { throttle } from "@std/async/unstable-throttle";
76-
* import { delay } from "./delay.ts";
77-
* import { assertEquals } from "@std/assert";
78-
*
79-
* let timesCalled = 0;
80-
* const fn = throttle(async (ms: number) => {
81-
* await delay(ms);
82-
* ++timesCalled;
83-
* }, (previousExecution) => previousExecution * 2);
84-
* await fn(50); // takes ~50ms to execute, after which it will be throttled for 50ms * 2 = 100ms
85-
* assertEquals(timesCalled, 1);
86-
* await delay(50);
87-
* await fn(50);
88-
* assertEquals(timesCalled, 1); // still throttled
89-
* await delay(30);
90-
* await fn(50);
91-
* assertEquals(timesCalled, 1); // still throttled
92-
* await delay(30);
93-
* await fn(50);
94-
* assertEquals(timesCalled, 2); // not throttled this time
95-
* ```
96-
*
9773
* @typeParam T The arguments of the provided function.
9874
* @param fn The function to throttle.
99-
* @param timeframe The timeframe in milliseconds in which the function should be called at most once. If a callback function is supplied, it will be called with the duration of the previous execution and should return the next timeframe to use in milliseconds.
75+
* @param timeframe The timeframe in milliseconds in which the function should be called at most once.
76+
* If a callback function is supplied, it will be called with the duration of
77+
* the previous execution and should return the
78+
* next timeframe to use in milliseconds.
10079
* @param options Additional options.
10180
* @returns The throttled function.
10281
*/
10382
// deno-lint-ignore no-explicit-any
10483
export function throttle<T extends Array<any>>(
10584
fn: (this: ThrottledFunction<T>, ...args: T) => void,
106-
timeframe: number | ((previousExecution: number) => number),
85+
timeframe: number | ((previousDuration: number) => number),
10786
options?: ThrottleOptions,
10887
): ThrottledFunction<T> {
10988
const ensureLast = Boolean(options?.ensureLastCall);
@@ -118,22 +97,20 @@ export function throttle<T extends Array<any>>(
11897
flush = () => {
11998
const start = Date.now();
12099
let result: unknown;
100+
const done = () => {
101+
lastExecution = Date.now();
102+
if (typeof timeframe === "function") {
103+
tf = timeframe(lastExecution - start);
104+
}
105+
};
121106
try {
122107
clearTimeout(timeout);
123108
result = fn.call(throttled, ...args);
124109
} finally {
125-
lastExecution = Date.now();
126110
if (isPromiseLike(result)) {
127-
result.finally(() => {
128-
lastExecution = Date.now();
129-
if (typeof timeframe === "function") {
130-
tf = timeframe(lastExecution - start);
131-
}
132-
});
111+
result.finally(done);
133112
} else {
134-
if (typeof timeframe === "function") {
135-
tf = timeframe(lastExecution - start);
136-
}
113+
done();
137114
}
138115
flush = null;
139116
}

async/unstable_throttle_test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,29 @@ Deno.test("throttle() handles ensureLastCall option", async (t) => {
153153
});
154154
}
155155
});
156+
157+
Deno.test("throttle() handles dynamic timeframe", () => {
158+
using time = new FakeTime();
159+
let calls = 0;
160+
const fn = throttle(
161+
() => {
162+
time.tick(50 * ++calls);
163+
},
164+
(n) => n,
165+
);
166+
fn();
167+
assertEquals(calls, 1);
168+
assertEquals(fn.throttling, true);
169+
time.tick(49);
170+
assertEquals(fn.throttling, true);
171+
time.tick(2);
172+
assertEquals(fn.throttling, false);
173+
174+
fn();
175+
assertEquals(calls, 2);
176+
assertEquals(fn.throttling, true);
177+
time.tick(99);
178+
assertEquals(fn.throttling, true);
179+
time.tick(2);
180+
assertEquals(fn.throttling, false);
181+
});

0 commit comments

Comments
 (0)