Skip to content

Commit

Permalink
perf: release ee handlers on process end
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Sep 20, 2024
1 parent 6d317a6 commit 49d89a9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/main/ts/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ export const buildSpawnOpts = ({spawnOpts, stdio, cwd, shell, input, env, detach
signal
})

export const attachListeners = (ee: EventEmitter, on: Partial<TSpawnListeners> = {}) => {
export const toggleListeners = (pos: 'on' | 'off', ee: EventEmitter, on: Partial<TSpawnListeners> = {}) => {
for (const [name, listener] of Object.entries(on)) {
ee.on(name, listener as any)
ee[pos](name, listener as any)
}
}

Expand All @@ -159,7 +159,7 @@ export const invoke = (c: TSpawnCtxNormalized): TSpawnCtxNormalized => {

try {
if (c.sync) {
attachListeners(c.ee, c.on)
toggleListeners('on', c.ee, c.on)
const opts = buildSpawnOpts(c)
const result = c.spawnSync(c.cmd, c.args, opts)
c.ee.emit('start', result, c)
Expand All @@ -186,7 +186,7 @@ export const invoke = (c: TSpawnCtxNormalized): TSpawnCtxNormalized => {

} else {
c.run(() => {
attachListeners(c.ee, c.on)
toggleListeners('on', c.ee, c.on)

let error: any = null
const opts = buildSpawnOpts(c)
Expand Down Expand Up @@ -259,6 +259,8 @@ export const invoke = (c: TSpawnCtxNormalized): TSpawnCtxNormalized => {
)
c.ee.emit('err', error, c)
c.ee.emit('end', c.fulfilled, c)
} finally {
toggleListeners('off', c.ee, c.on)
}

return c
Expand Down

0 comments on commit 49d89a9

Please sign in to comment.