Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/add reader #92

Merged
merged 2 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 21 additions & 31 deletions src/runtime/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Deferred, deferred, writeAll } from "./deps.ts";
import { readLines } from "./lib/readline.ts";
import { ProcessError } from "./process_error.ts";
import { ProcessOutput } from "./process_output.ts";
import { Reader } from "./reader.ts";
import { $ } from "./shell.ts";

export type RetryCallback = (error: ProcessError) => boolean | Promise<boolean>;
Expand All @@ -11,7 +12,7 @@ export interface ProcessOptions {
errorContext?: Function;
}

export class Process implements Promise<ProcessOutput> {
export class Process extends Reader<ProcessOutput> {
readonly [Symbol.toStringTag] = "Process";
readonly #cmd: string;
#proc: Deno.Process | null = null;
Expand All @@ -27,8 +28,22 @@ export class Process implements Promise<ProcessOutput> {
#throwErrors = true;
#isKilled = false;
#shouldRetry?: RetryCallback;
#stdoutReader: Reader<string>;
#stderrReader: Reader<string>;

constructor(cmd: string, { errorContext }: ProcessOptions = {}) {
super({
resolve: () => this.#resolve(),
});

this.#stdoutReader = new Reader({
resolve: () => this.#resolve().then(({ stdout }) => stdout),
});

this.#stderrReader = new Reader({
resolve: () => this.#resolve().then(({ stderr }) => stderr),
});

this.#cmd = cmd;
this.#baseError = new ProcessError({
combined: "",
Expand Down Expand Up @@ -60,6 +75,7 @@ export class Process implements Promise<ProcessOutput> {
);
}
}

return this.#proc;
}

Expand Down Expand Up @@ -99,8 +115,8 @@ export class Process implements Promise<ProcessOutput> {
* console.log(output); // -> "Hello\n"
* ```
*/
get stdout(): Promise<string> {
return this.#resolve().then(({ stdout }) => stdout);
get stdout(): Reader<string> {
return this.#stdoutReader;
}

/**
Expand All @@ -111,8 +127,8 @@ export class Process implements Promise<ProcessOutput> {
* console.log(errorOutput); // -> "World\n"
* ```
*/
get stderr(): Promise<string> {
return this.#resolve().then(({ stderr }) => stderr);
get stderr(): Reader<string> {
return this.#stderrReader;
}

retry(retries: number | RetryCallback): this {
Expand All @@ -138,32 +154,6 @@ export class Process implements Promise<ProcessOutput> {
this.#process.kill(signo);
}

then<TResult1 = ProcessOutput, TResult2 = never>(
onfulfilled?:
| ((value: ProcessOutput) => TResult1 | PromiseLike<TResult1>)
| undefined
| null,
onrejected?:
| ((reason: unknown) => TResult2 | PromiseLike<TResult2>)
| undefined
| null,
): Promise<TResult1 | TResult2> {
return this.#resolve().then(onfulfilled).catch(onrejected);
}

catch<TResult = never>(
onrejected?:
| ((reason: unknown) => TResult | PromiseLike<TResult>)
| undefined
| null,
): Promise<ProcessOutput | TResult> {
return this.#resolve().catch(onrejected);
}

finally(onfinally?: (() => void) | undefined | null): Promise<ProcessOutput> {
return this.#resolve().finally(onfinally);
}

#resolve(): Promise<ProcessOutput> {
if (this.#deferred) {
return this.#deferred;
Expand Down
37 changes: 37 additions & 0 deletions src/runtime/reader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export interface ReaderOptions<TResolve> {
resolve(): Promise<TResolve>;
}

export class Reader<TResolve> {
#opts: ReaderOptions<TResolve>;

constructor(opts: ReaderOptions<TResolve>) {
this.#opts = opts;
}

then<TResult1 = TResolve, TResult2 = never>(
onfulfilled?:
| ((value: TResolve) => TResult1 | PromiseLike<TResult1>)
| undefined
| null,
onrejected?:
| ((reason: unknown) => TResult2 | PromiseLike<TResult2>)
| undefined
| null,
): Promise<TResult1 | TResult2> {
return this.#opts.resolve().then(onfulfilled).catch(onrejected);
}

catch<TResult = never>(
onrejected?:
| ((reason: unknown) => TResult | PromiseLike<TResult>)
| undefined
| null,
): Promise<TResolve | TResult> {
return this.#opts.resolve().catch(onrejected);
}

finally(onfinally?: (() => void) | undefined | null): Promise<TResolve> {
return this.#opts.resolve().finally(onfinally);
}
}