Skip to content

Commit

Permalink
perf: remove redundant promise API overrides (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub authored Nov 24, 2024
1 parent c4f27cb commit d717b2a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
45 changes: 19 additions & 26 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,25 @@ export const $: Shell & Options = new Proxy<Shell & Options>(

type Resolve = (out: ProcessOutput) => void

export interface ProcessPromise extends Promise<ProcessOutput> {
then<R = ProcessOutput, E = ProcessOutput>(
onfulfilled?:
| ((value: ProcessOutput) => PromiseLike<R> | R)
| undefined
| null,
onrejected?:
| ((reason: ProcessOutput) => PromiseLike<E> | E)
| undefined
| null
): Promise<R | E>
catch<T = ProcessOutput>(
onrejected?:
| ((reason: ProcessOutput) => PromiseLike<T> | T)
| undefined
| null
): Promise<ProcessOutput | T>
}

export class ProcessPromise extends Promise<ProcessOutput> {
private _command = ''
private _from = ''
Expand Down Expand Up @@ -520,32 +539,6 @@ export class ProcessPromise extends Promise<ProcessOutput> {
return this._nothrow ?? this._snapshot.nothrow
}

// Promise API
then<R = ProcessOutput, E = ProcessOutput>(
onfulfilled?:
| ((value: ProcessOutput) => PromiseLike<R> | R)
| undefined
| null,
onrejected?:
| ((reason: ProcessOutput) => PromiseLike<E> | E)
| undefined
| null
): Promise<R | E> {
if (this.isHalted() && !this.child) {
throw new Error('The process is halted!')
}
return super.then(onfulfilled, onrejected)
}

catch<T = ProcessOutput>(
onrejected?:
| ((reason: ProcessOutput) => PromiseLike<T> | T)
| undefined
| null
): Promise<ProcessOutput | T> {
return super.catch(onrejected)
}

// Stream-like API
private writable = true
private emit(event: string, ...args: any[]) {
Expand Down
12 changes: 0 additions & 12 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,18 +633,6 @@ describe('core', () => {
assert.ok(fs.existsSync(filepath), 'The cmd should have been called')
})

test('await on halted throws', async () => {
const p = $({ halt: true })`sleep 1`
let ok = true
try {
await p
ok = false
} catch (err) {
assert.equal(err.message, 'The process is halted!')
}
assert.ok(ok, 'Expected failure!')
})

test('sync process ignores halt option', () => {
const p = $.sync({ halt: true })`echo foo`
assert.equal(p.stdout, 'foo\n')
Expand Down

0 comments on commit d717b2a

Please sign in to comment.