Skip to content

Commit 0881dae

Browse files
authored
Merge pull request #2 from rivet-dev/04-02-fix_have_isclosed_check_if_sender_is_closed
fix: have isClosed check if sender is closed
2 parents e0e3406 + ac6375e commit 0881dae

6 files changed

Lines changed: 32 additions & 4 deletions

File tree

src/sync/mpsc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export class Sender<T> {
185185
}
186186

187187
isClosed(): boolean {
188-
return this.#state.closed;
188+
return this.#dropped || this.#state.closed;
189189
}
190190

191191
capacity(): number {
@@ -373,7 +373,7 @@ export class UnboundedSender<T> {
373373
}
374374

375375
isClosed(): boolean {
376-
return this.#state.closed;
376+
return this.#dropped || this.#state.closed;
377377
}
378378

379379
clone(): UnboundedSender<T> {

src/sync/oneshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class OneshotSender<T> {
7070
}
7171

7272
isClosed(): boolean {
73-
return this.#state.receiverClosed;
73+
return this.#dropped || this.#state.receiverClosed;
7474
}
7575

7676
closed(signal?: AbortSignal): Promise<void> {

src/sync/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class WatchSender<T> {
8989
}
9090

9191
isClosed(): boolean {
92-
return this.#state.receiverCount === 0;
92+
return this.#closed || this.#state.receiverCount === 0;
9393
}
9494

9595
close(): void {

tests/sync/mpsc.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ describe("bounded channel", () => {
145145
expect(tx.isClosed()).toBe(true);
146146
});
147147

148+
it("isClosed is true after sender close", () => {
149+
const [tx, rx] = channel<number>(8);
150+
tx.close();
151+
expect(tx.isClosed()).toBe(true);
152+
void rx;
153+
});
154+
148155
it("closed() resolves when receiver closes", async () => {
149156
const [tx, rx] = channel<number>(8);
150157
let resolved = false;
@@ -512,6 +519,13 @@ describe("unbounded channel", () => {
512519
expect(tx.isClosed()).toBe(true);
513520
});
514521

522+
it("isClosed is true after sender close", () => {
523+
const [tx, rx] = unboundedChannel<number>();
524+
tx.close();
525+
expect(tx.isClosed()).toBe(true);
526+
void rx;
527+
});
528+
515529
it("closed() resolves when receiver closes", async () => {
516530
const [tx, rx] = unboundedChannel<number>();
517531
let resolved = false;

tests/sync/oneshot.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ describe("oneshot", () => {
205205
expect(tx.isClosed()).toBe(true);
206206
});
207207

208+
it("isClosed is true after sender close", () => {
209+
const [tx, rx] = oneshot<number>();
210+
tx[Symbol.dispose]();
211+
expect(tx.isClosed()).toBe(true);
212+
void rx;
213+
});
214+
208215
it("isClosed is false after send (sender closed but receiver is not)", () => {
209216
const [tx, rx] = oneshot<number>();
210217
tx.send(1);

tests/sync/watch.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ describe("watch", () => {
105105
expect(tx.isClosed()).toBe(true);
106106
});
107107

108+
it("isClosed is true after sender close", () => {
109+
const [tx, rx] = watch(0);
110+
tx.close();
111+
expect(tx.isClosed()).toBe(true);
112+
void rx;
113+
});
114+
108115
it("sendIfModified returns true and wakes receivers when predicate returns true", async () => {
109116
const [tx, rx] = watch({ count: 0 });
110117
rx.borrowAndUpdate();

0 commit comments

Comments
 (0)