Skip to content

Commit

Permalink
wip: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Apr 24, 2024
1 parent 0a9bb35 commit ea1eba8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 41 deletions.
49 changes: 25 additions & 24 deletions src/lib/atemSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class AtemSocket extends EventEmitter<AtemSocketEvents> {
private _socketProcess: Comlink.Remote<Api> | undefined
private _creatingSocket: Promise<void> | undefined
private _exitUnsubscribe?: () => void
private _targetState: 'connected' | 'disconnected' = 'disconnected'

constructor(options: AtemSocketOptions) {
super()
Expand All @@ -62,6 +63,7 @@ export class AtemSocket extends EventEmitter<AtemSocketEvents> {
}
}

this._targetState = 'connected'
await this._socketProcess.connect(this._address, this._port || DEFAULT_PORT)
}

Expand All @@ -84,6 +86,7 @@ export class AtemSocket extends EventEmitter<AtemSocketEvents> {
}

public async disconnect(): Promise<void> {
this._targetState = 'disconnected'
this._isDisconnecting = true

if (this._socketProcess) {
Expand Down Expand Up @@ -130,39 +133,37 @@ export class AtemSocket extends EventEmitter<AtemSocketEvents> {
this._socketWorker = undefined
worker.terminate().catch(() => null)
this.emit('disconnect')

if (this._targetState === 'connected') {
// Trigger a reconnect
this.connect().catch((error) =>
this.emit('error', `Failed to reconnect after socket process exit: ${error}`)
)
}
})

this._socketProcess = Comlink.wrap<Api>(nodeEndpoint(worker))
this._socketWorker = worker

await this._socketProcess.init(
Comlink.proxy({
debugBuffers: this._debugBuffers,
onDisconnect: async (): Promise<void> => {
this.emit('disconnect')
},
onLog: async (message: string): Promise<void> => {
this.emit('info', message)
},
onCommandsReceived: async (payload: Buffer): Promise<void> => {
this._parseCommands(Buffer.from(payload))
},
onPacketsAcknowledged: async (ids: Array<{ packetId: number; trackingId: number }>): Promise<void> => {
this.emit(
'ackPackets',
ids.map((id) => id.trackingId)
)
},
this._debugBuffers,
Comlink.proxy(async (): Promise<void> => {
this.emit('disconnect')
}),
Comlink.proxy(async (message: string): Promise<void> => {
this.emit('info', message)
}),
Comlink.proxy(async (payload: Buffer): Promise<void> => {
this._parseCommands(Buffer.from(payload))
}),
Comlink.proxy(async (ids: Array<{ packetId: number; trackingId: number }>): Promise<void> => {
this.emit(
'ackPackets',
ids.map((id) => id.trackingId)
)
})
)

// nocommit: reimplement a restart mechanism
// ThreadedClassManager.onEvent(this._socketProcess, 'restarted', () => {
// this.connect().catch((error) => {
// const errorMsg = `Failed to reconnect after respawning socket process: ${error}`
// this.emit('error', errorMsg)
// })
// })
this._exitUnsubscribe = exitHook(() => {
this.destroy().catch(() => null)
})
Expand Down
25 changes: 8 additions & 17 deletions src/lib/atemSocketChild2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import { AtemSocketChild, OutboundPacketInfo } from './atemSocketChild'
if (!parentPort) {
throw new Error('InvalidWorker')
}

export interface InitData {
debugBuffers: boolean
onDisconnect: () => Promise<void>
onLog: (message: string) => Promise<void>
onCommandsReceived: (payload: Buffer) => Promise<void>
onPacketsAcknowledged: (ids: Array<{ packetId: number; trackingId: number }>) => Promise<void>
}
parentPort.setMaxListeners(100)

export class Api {
private tempChild: AtemSocketChild | undefined

public init({ debugBuffers, onDisconnect, onLog, onCommandsReceived, onPacketsAcknowledged }: InitData): void {
public init(
debugBuffers: boolean,
onDisconnect: () => Promise<void>,
onLog: (message: string) => Promise<void>,
onCommandsReceived: (payload: Buffer) => Promise<void>,
onPacketsAcknowledged: (ids: Array<{ packetId: number; trackingId: number }>) => Promise<void>
): void {
if (this.tempChild) throw new Error('Already initialised!')

this.tempChild = new AtemSocketChild(
Expand Down Expand Up @@ -51,12 +50,4 @@ export class Api {
}
}

setInterval(() => {
console.log('im alive')
}, 1000)

setTimeout(() => {
process.exit(1)
}, 5000)

comlink.expose(new Api(), nodeEndpoint(parentPort))

0 comments on commit ea1eba8

Please sign in to comment.