Skip to content

Commit

Permalink
fix: register error handler in threadedClass
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarpl authored and KvelaGorrrrnio committed Oct 5, 2022
1 parent 5e7905d commit 67f42f3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
3 changes: 3 additions & 0 deletions packages/timeline-state-resolver/src/conductor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ export class Conductor extends EventEmitter<ConductorEvents> {
}

newDevice.device.on('resetResolver', () => this.resetResolver()).catch(console.error)
newDevice.on('error', (context, e) => {
this.emit('error', `deviceContainer for "${newDevice?.deviceId}" emitted an error: ${context}, ${e}`)
})

// Double check that it hasnt been created while we were busy waiting
if (this.devices.has(deviceId)) {
Expand Down
27 changes: 19 additions & 8 deletions packages/timeline-state-resolver/src/devices/deviceContainer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { ThreadedClass, threadedClass, ThreadedClassConfig, ThreadedClassManager } from 'threadedclass'
import { Device } from './device'
import { DeviceType, DeviceOptionsBase } from 'timeline-state-resolver-types'
import { EventEmitter } from 'eventemitter3'

export type DeviceContainerEvents = {
error: [context: string, err: Error]
}

/**
* A device container is a wrapper around a device in ThreadedClass class, it
* keeps a local property of some basic information about the device (like
* names and id's) to prevent a costly round trip over IPC.
*/
export class DeviceContainer<TOptions extends DeviceOptionsBase<any>> {
export class DeviceContainer<TOptions extends DeviceOptionsBase<any>> extends EventEmitter<DeviceContainerEvents> {
private _device: ThreadedClass<Device<TOptions>>
private _deviceId = 'N/A'
private _deviceType: DeviceType
Expand All @@ -17,11 +22,12 @@ export class DeviceContainer<TOptions extends DeviceOptionsBase<any>> {
public onChildClose: () => void | undefined
private _instanceId = -1
private _startTime = -1
private _onEventListener: { stop: () => void } | undefined
private _onEventListeners: { stop: () => void }[] | undefined
private _debugLogging = true
private _initialized = false

private constructor(deviceOptions: TOptions, threadConfig?: ThreadedClassConfig) {
super()
this._deviceOptions = deviceOptions
this._threadConfig = threadConfig
this._debugLogging = deviceOptions.debug || false
Expand Down Expand Up @@ -54,10 +60,15 @@ export class DeviceContainer<TOptions extends DeviceOptionsBase<any>> {
)

if (deviceOptions.isMultiThreaded) {
container._onEventListener = ThreadedClassManager.onEvent(container._device, 'thread_closed', () => {
// This is called if a child crashes
if (container.onChildClose) container.onChildClose()
})
container._onEventListeners = [
ThreadedClassManager.onEvent(container._device, 'thread_closed', () => {
// This is called if a child crashes
if (container.onChildClose) container.onChildClose()
}),
ThreadedClassManager.onEvent(container._device, 'error', (error) => {
container.emit('error', 'threadedClass', error)
}),
]
}

await container.reloadProps()
Expand Down Expand Up @@ -88,8 +99,8 @@ export class DeviceContainer<TOptions extends DeviceOptionsBase<any>> {
}

public async terminate() {
if (this._onEventListener) {
this._onEventListener.stop()
if (this._onEventListeners) {
this._onEventListeners.forEach((listener) => listener.stop())
}
await ThreadedClassManager.destroy(this._device)
}
Expand Down

0 comments on commit 67f42f3

Please sign in to comment.