Skip to content

Commit

Permalink
chore: refactor: internal debugLogError function
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Apr 10, 2024
1 parent 910e843 commit c4f8638
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/parent-process/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class ThreadedClassManagerClassInternal {
this._children[newChild.id] = newChild
child = newChild

if (this.debug) this.consoleLog(`New child: "${newChild.id}"`)
this.debugLog(`New child: "${newChild.id}"`)
}

return child
Expand Down Expand Up @@ -260,7 +260,7 @@ export class ThreadedClassManagerClassInternal {
}
child.instances[instance.id] = instance

if (this.debug) this.consoleLog(`Add instance "${instance.id}" to "${child.id}"`)
this.debugLog(`Add instance "${instance.id}" to "${child.id}"`)

return instance
}
Expand Down Expand Up @@ -405,7 +405,7 @@ export class ThreadedClassManagerClassInternal {
return Promise.all(
Object.keys(this._children).map((id) => {
const child = this._children[id]
if (this.debug) this.consoleLog(`Killing child "${this.getChildDescriptor(child)}"`)
this.debugLog(`Killing child "${this.getChildDescriptor(child)}"`)
return this.killChild(id, 'killAllChildren')
})
).then(() => {
Expand Down Expand Up @@ -832,7 +832,7 @@ ${getStack()}`)

} else {
this._emitProxyEvent(child, 'error', err)
if (this.debug) this.consoleError('Error when running restartChild()', err)
this.debugLogError('Error when running restartChild()', err)

// Clean up the child:
this.killChild(child, 'timeout when restarting', true).catch((e) => {
Expand All @@ -846,7 +846,7 @@ ${getStack()}`)
this.killChild(child, `child has crashed (${reason})`, false)
.catch((err) => {
this._emitProxyEvent(child, 'error', err)
if (this.debug) this.consoleError('Error when running killChild()', err)
this.debugLogError('Error when running killChild()', err)
})
}
}
Expand Down Expand Up @@ -885,14 +885,14 @@ ${getStack()}`)
})
child.process.on('error', (err) => {
this._emitProxyEvent(child, 'error', err)
if (this.debug) this.consoleError('Error from child ' + child.id, err)
this.debugLogError('Error from child ' + child.id, err)
})
child.process.on('message', (message: Message.From.Any) => {
if (message.messageType === 'child') {
try {
this._onMessageFromChild(child, message)
} catch (e) {
if (this.debug) this.consoleError(`Error in onMessageCallback in child ${child.id}`, message, e)
this.debugLogError(`Error in onMessageCallback in child ${child.id}`, message, e)
throw e
}
} else if (message.messageType === 'instance') {
Expand All @@ -901,18 +901,18 @@ ${getStack()}`)
try {
instance.onMessageCallback(instance, message)
} catch (e) {
if (this.debug) this.consoleError(`Error in onMessageCallback in instance ${instance.id}`, message, instance, e)
this.debugLogError(`Error in onMessageCallback in instance ${instance.id}`, message, instance, e)
throw e
}
} else {
const err = new Error(`Instance "${message.instanceId}" not found. Received message "${message.messageType}" from child "${child.id}", "${childName(child)}"`)
this._emitProxyEvent(child, 'error', err)
if (this.debug) this.consoleError(err)
this.debugLogError(err)
}
} else {
const err = new Error(`Unknown messageType "${message['messageType']}"!`)
this._emitProxyEvent(child, 'error', err)
if (this.debug) this.consoleError(err)
this.debugLogError(err)
}
})
}
Expand Down Expand Up @@ -1000,7 +1000,7 @@ ${getStack()}`)
} else {
child = idOrChild
}
if (this.debug) this.consoleLog(`Killing child ${child.id} due to: ${reason}`)
this.debugLog(`Killing child ${child.id} due to: ${reason}`)
if (child) {
if (cleanUp) {
this.clearRestartTimeout(child)
Expand Down Expand Up @@ -1059,6 +1059,17 @@ ${getStack()}`)
})
child.methods = {}
}
/** trace to console.error if debugging is enabled*/
public debugLogError (...args: any[]) {
if (this.debug) this.consoleError(...args)

}
/** trace to console.error if debugging is enabled */
public debugLog (...args: any[]) {
if (this.debug) this.consoleLog(...args)

}

/** trace to console.error */
private consoleError (...args: any[]) {
console.error(`ThreadedClass Error (${this.uniqueId})`, ...args)
Expand Down

0 comments on commit c4f8638

Please sign in to comment.