Skip to content

Commit

Permalink
fix: replace IPCMessageType with stricter basic strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Dec 7, 2019
1 parent 69c7d32 commit 3b07d0a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
3 changes: 1 addition & 2 deletions src/__tests__/atem.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Atem, DEFAULT_PORT } from '../atem'
import { cleanupAtem } from './lib'
import { CutCommand } from '../commands'
import { IPCMessageType } from '../enums'
import { promisify } from 'util'
import { EventEmitter } from 'events'

Expand Down Expand Up @@ -123,7 +122,7 @@ describe('Atem', () => {
expect(socket.sendCommand).toHaveBeenCalledWith(cmd, 124)

// Trigger the ack, and it should switfy resolve
socket.emit(IPCMessageType.CommandAcknowledged, 124)
socket.emit('commandAck', 124)
expect(Object.keys(sentQueue)).toHaveLength(0)

// Finally, it should now resolve without a timeout
Expand Down
10 changes: 8 additions & 2 deletions src/atem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export class Atem extends EventEmitter {
((event: 'stateChanged', listener: (state: AtemState, path: string) => void) => this) &
((event: 'receivedCommand', listener: (cmd: IDeserializedCommand) => void) => this)

public emit!: ((event: 'error', message: any) => boolean) &
((event: 'connected') => boolean) &
((event: 'disconnected') => boolean) &
((event: 'stateChanged', state: AtemState, path: string) => boolean) &
((event: 'receivedCommand', cmd: IDeserializedCommand) => boolean)

constructor (options?: AtemOptions) {
super()
this._log = (options && options.externalLog) || function (...args: any[]): void {
Expand All @@ -74,11 +80,11 @@ export class Atem extends EventEmitter {
this.socket.on('connect', () => this.dataTransferManager.startCommandSending((command: ISerializableCommand) => this.sendCommand(command)))
this.socket.on('disconnect', () => this.dataTransferManager.stopCommandSending())

this.socket.on('receivedStateChange', (command: IDeserializedCommand) => {
this.socket.on('commandReceived', (command: IDeserializedCommand) => {
this.emit('receivedCommand', command)
this._mutateState(command)
})
this.socket.on(Enums.IPCMessageType.CommandAcknowledged, (trackingId: number) => this._resolveCommand(trackingId))
this.socket.on('commandAck', (trackingId: number) => this._resolveCommand(trackingId))
this.socket.on('error', (e) => this.emit('error', e))
this.socket.on('connect', () => this.emit('connected'))
this.socket.on('disconnect', () => {
Expand Down
9 changes: 0 additions & 9 deletions src/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,6 @@ export enum SuperSourceArtOption {
Foreground
}

export enum IPCMessageType {
Log = 'log',
Connect = 'connect',
Disconnect = 'disconnect',
InboundCommand = 'inboundCommand',
OutboundCommand = 'outboundCommand',
CommandAcknowledged = 'commandAcknowledged'
}

export enum TransferMode {
NoOp,
Write,
Expand Down
41 changes: 20 additions & 21 deletions src/lib/__tests__/atemSocket.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CutCommand, ProductIdentifierCommand, InitCompleteCommand, VersionCommand, ProgramInputUpdateCommand, PreviewInputUpdateCommand, ISerializableCommand, BasicWritableCommand, DeserializedCommand } from '../../commands'
import { IPCMessageType, ProtocolVersion, Model } from '../../enums'
import { ProtocolVersion, Model } from '../../enums'
import { AtemSocket } from '../atemSocket'
import { ThreadedClass, ThreadedClassManager } from 'threadedclass'
import { Buffer } from 'buffer'
Expand All @@ -20,26 +20,25 @@ export class AtemSocketChildMock {
// // this._address = options.address
// // this._port = options.port

// this.onDisconnect = null
// this.onDisconnect = onDisconnect
// this.onLog = onLog
// this.onCommandReceived = onCommandReceived
// this.onCommandAcknowledged = onCommandAcknowledged
// }

public hackSetFuncs (onDisconnect: () => Promise<void>, onLog: (message: string) => Promise<void>, onCommandReceived: (payload: Buffer, packetId: number) => Promise<void>, onCommandAcknowledged: (packetId: number, trackingId: number) => Promise<void>) {
this.onDisconnect = onDisconnect
this.onLog = onLog
this.onCommandReceived = onCommandReceived
this.onCommandAcknowledged = onCommandAcknowledged
}

public connect = jest.fn()
public disconnect = jest.fn(() => Promise.resolve(87))
public sendCommand = jest.fn()
}

const AtemSocketChildSingleton = new AtemSocketChildMock()
;(AtemSocketChild as any).mockImplementation(() => AtemSocketChildSingleton)
;(AtemSocketChild as any).mockImplementation((_opts: any, onDisconnect: () => Promise<void>, onLog: (message: string) => Promise<void>, onCommandReceived: (payload: Buffer, packetId: number) => Promise<void>, onCommandAcknowledged: (packetId: number, trackingId: number) => Promise<void>) => {
AtemSocketChildSingleton.onDisconnect = onDisconnect
AtemSocketChildSingleton.onLog = onLog
AtemSocketChildSingleton.onCommandReceived = onCommandReceived
AtemSocketChildSingleton.onCommandAcknowledged = onCommandAcknowledged
return AtemSocketChildSingleton
})

class ThreadedClassManagerMock {
public handlers: Function[] = []
Expand Down Expand Up @@ -108,7 +107,7 @@ describe('AtemSocket', () => {

// New child was constructed
expect(AtemSocketChild).toHaveBeenCalledTimes(1)
expect(AtemSocketChild).toHaveBeenCalledWith({ address: '', port: 890, debug: false }, null, null, null, null)
expect(AtemSocketChild).toHaveBeenCalledWith({ address: '', port: 890, debug: false }, expect.toBeFunction(), expect.toBeFunction(), expect.toBeFunction(), expect.toBeFunction())
})
test('connect initial with params', async () => {
const socket = createSocket()
Expand All @@ -127,7 +126,7 @@ describe('AtemSocket', () => {

// New child was constructed
expect(AtemSocketChild).toHaveBeenCalledTimes(1)
expect(AtemSocketChild).toHaveBeenCalledWith({ address: 'abc', port: 765, debug: false }, null, null, null, null)
expect(AtemSocketChild).toHaveBeenCalledWith({ address: 'abc', port: 765, debug: false }, expect.toBeFunction(), expect.toBeFunction(), expect.toBeFunction(), expect.toBeFunction())
})
test('connect change details', async () => {
const socket = createSocket()
Expand Down Expand Up @@ -290,8 +289,8 @@ describe('AtemSocket', () => {
// const log = jest.fn()
const ack = jest.fn()

socket.on(IPCMessageType.Disconnect, disconnect)
socket.on(IPCMessageType.CommandAcknowledged, ack)
socket.on('disconnect', disconnect)
socket.on('commandAck', ack)

expect(AtemSocketChildSingleton.onDisconnect).toBeDefined()
await AtemSocketChildSingleton.onDisconnect!()
Expand All @@ -302,7 +301,7 @@ describe('AtemSocket', () => {
await AtemSocketChildSingleton.onCommandAcknowledged!(675, 98)
await clock.tickAsync(0)
expect(ack).toHaveBeenCalledTimes(1)
expect(ack).toHaveBeenCalledWith({ packetId: 675, trackingId: 98 })
expect(ack).toHaveBeenCalledWith(98)

})

Expand All @@ -320,7 +319,7 @@ describe('AtemSocket', () => {

socket.on('connect', connect)
socket.on('error', error)
socket.on('receivedStateChange', change)
socket.on('commandReceived', change)

const parser = (socket as any)._commandParser as CommandParser
expect(parser).toBeTruthy()
Expand Down Expand Up @@ -357,7 +356,7 @@ describe('AtemSocket', () => {

socket.on('connect', connect)
socket.on('error', error)
socket.on('receivedStateChange', change)
socket.on('commandReceived', change)

const parser = (socket as any)._commandParser as CommandParser
expect(parser).toBeTruthy()
Expand Down Expand Up @@ -397,7 +396,7 @@ describe('AtemSocket', () => {

socket.on('connect', connect)
socket.on('error', error)
socket.on('receivedStateChange', change)
socket.on('commandReceived', change)

const parser = (socket as any)._commandParser as CommandParser
expect(parser).toBeTruthy()
Expand Down Expand Up @@ -440,7 +439,7 @@ describe('AtemSocket', () => {

socket.on('connect', connect)
socket.on('error', error)
socket.on('receivedStateChange', change)
socket.on('commandReceived', change)

const testBuffer = Buffer.alloc(0)
const pktId = 822
Expand All @@ -466,7 +465,7 @@ describe('AtemSocket', () => {

socket.on('connect', connect)
socket.on('error', error)
socket.on('receivedStateChange', change)
socket.on('commandReceived', change)

const testBuffer = Buffer.alloc(10, 0)
const pktId = 822
Expand All @@ -492,7 +491,7 @@ describe('AtemSocket', () => {

socket.on('connect', connect)
socket.on('error', error)
socket.on('receivedStateChange', change)
socket.on('commandReceived', change)

class BrokenCommand extends DeserializedCommand<{}> {
public static readonly rawName = 'TEST'
Expand Down
21 changes: 17 additions & 4 deletions src/lib/atemSocket.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { EventEmitter } from 'events'
import { CommandParser } from './atemCommandParser'
import { IPCMessageType } from '../enums'
// import exitHook = require('exit-hook')
import { VersionCommand, ISerializableCommand, IDeserializedCommand } from '../commands'
import { DEFAULT_PORT } from '../atem'
Expand Down Expand Up @@ -28,6 +27,20 @@ export class AtemSocket extends EventEmitter {

private readonly log: (args1: any, args2?: any, args3?: any) => void

public on!: ((event: 'disconnect', listener: () => void) => this) &
((event: 'connect', listener: () => void) => this) &
((event: 'restarted', listener: () => void) => this) &
((event: 'error', listener: (message: string) => void) => this) &
((event: 'commandReceived', listener: (cmd: IDeserializedCommand) => void) => this) &
((event: 'commandAck', listener: (trackingId: number) => void) => this)

public emit!: ((event: 'disconnect') => boolean) &
((event: 'connect') => boolean) &
((event: 'restarted') => boolean) &
((event: 'error', message: string) => boolean) &
((event: 'commandReceived', cmd: IDeserializedCommand) => boolean) &
((event: 'commandAck', trackingId: number) => boolean)

constructor (options: AtemSocketOptions) {
super()
this._address = options.address
Expand Down Expand Up @@ -102,10 +115,10 @@ export class AtemSocket extends EventEmitter {
port: this._port,
debug: this._debug
},
async () => { this.emit(IPCMessageType.Disconnect) }, // onDisconnect
async () => { this.emit('disconnect') }, // onDisconnect
async (message: string) => this.log(message), // onLog
async (payload: Buffer) => this._parseCommand(Buffer.from(payload)), // onCommandReceived
async (packetId: number, trackingId: number) => { this.emit(IPCMessageType.CommandAcknowledged, { packetId, trackingId }) } // onCommandAcknowledged
async (_packetId: number, trackingId: number) => { this.emit('commandAck', trackingId) } // onCommandAcknowledged
], {
instanceName: 'atem-connection',
freezeLimit: 200,
Expand Down Expand Up @@ -156,7 +169,7 @@ export class AtemSocket extends EventEmitter {
this._commandParser.version = verCmd.properties.version
}

this.emit('receivedStateChange', cmd)
this.emit('commandReceived', cmd)
} catch (e) {
this.emit('error', e)
}
Expand Down

0 comments on commit 3b07d0a

Please sign in to comment.