Skip to content

Commit

Permalink
feat: add the guard agaist double dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Jan 24, 2020
1 parent 8fa9fc8 commit d52d37f
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions cli/js/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { Signal } from "./process.ts";
import * as dispatch from "./dispatch.ts";
import { sendSync, sendAsync } from "./dispatch_json.ts";
import { DenoError, ErrorKind } from "./errors.ts";
import { build } from "./build.ts";

/**
Expand Down Expand Up @@ -95,9 +94,6 @@ export const signals = {
}
};

const STREAM_DISPOSED_MESSAGE =
"No signal is available because signal stream is disposed";

/** SignalStream represents the stream of signals, implements both
* AsyncIterator and PromiseLike */
export class SignalStream implements AsyncIterator<void>, PromiseLike<boolean> {
Expand All @@ -114,15 +110,17 @@ export class SignalStream implements AsyncIterator<void>, PromiseLike<boolean> {
}

private async pollSignal(): Promise<boolean> {
return (await sendAsync(dispatch.OP_SIGNAL_POLL, {
rid: this.rid
})).done;
return (
await sendAsync(dispatch.OP_SIGNAL_POLL, {
rid: this.rid
})
).done;
}

private async loop(): Promise<void> {
do {
this.pollingPromise = this.pollSignal();
} while (!await this.pollingPromise && !this.disposed);
} while (!(await this.pollingPromise) && !this.disposed);
}

then<T, S>(
Expand All @@ -141,6 +139,9 @@ export class SignalStream implements AsyncIterator<void>, PromiseLike<boolean> {
}

dispose(): void {
if (this.disposed) {
throw new Error("The stream has already been disposed.");
}
this.disposed = true;
sendSync(dispatch.OP_SIGNAL_UNBIND, { rid: this.rid });
}
Expand Down

0 comments on commit d52d37f

Please sign in to comment.