Skip to content

Commit 8006363

Browse files
committed
feat(notify): throw RangeError for invalid n
1 parent ea131ba commit 8006363

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

notify.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ export class Notify {
3535
/**
3636
* Notifies `n` waiters that are waiting for notification. Resolves each of the notified waiters.
3737
* If there are fewer than `n` waiters, all waiters are notified.
38+
*
39+
* @param n The number of waiters to notify.
40+
* @throws {RangeError} if `n` is not a positive safe integer.
3841
*/
3942
notify(n = 1): void {
43+
if (n <= 0 || !Number.isSafeInteger(n)) {
44+
throw new RangeError(`n must be a positive safe integer, got ${n}`);
45+
}
4046
const it = iter(this.#waiters);
4147
for (const waiter of take(it, n)) {
4248
waiter.resolve();

notify_test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { delay } from "@std/async/delay";
2-
import { assertEquals, assertRejects } from "@std/assert";
2+
import { assertEquals, assertRejects, assertThrows } from "@std/assert";
33
import { promiseState } from "./promise_state.ts";
44
import { Notify } from "./notify.ts";
55

@@ -109,4 +109,17 @@ Deno.test("Notify", async (t) => {
109109
);
110110
},
111111
);
112+
113+
await t.step(
114+
"'notify' throws RangeError if size is not a positive safe integer",
115+
() => {
116+
const notify = new Notify();
117+
assertThrows(() => notify.notify(NaN), RangeError);
118+
assertThrows(() => notify.notify(Infinity), RangeError);
119+
assertThrows(() => notify.notify(-Infinity), RangeError);
120+
assertThrows(() => notify.notify(-1), RangeError);
121+
assertThrows(() => notify.notify(1.1), RangeError);
122+
assertThrows(() => notify.notify(0), RangeError);
123+
},
124+
);
112125
});

0 commit comments

Comments
 (0)