Skip to content

Commit

Permalink
fix: perform deep diff for MultiViewerSourceUpdateCommand before up…
Browse files Browse the repository at this point in the history
…dating state #132
  • Loading branch information
Julusian committed Jul 31, 2023
1 parent 1caeb34 commit 6350a5b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/commands/Settings/MultiViewerSourceCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BasicWritableCommand, DeserializedCommand } from '../CommandBase'
import { AtemState, AtemStateUtil, InvalidIdError } from '../../state'
import { MultiViewerSourceState } from '../../state/settings'
import { isRunningInTests } from '../../lib/atemUtil'

export class MultiViewerSourceCommand extends BasicWritableCommand<
Pick<MultiViewerSourceState, 'windowIndex' | 'source'>
Expand Down Expand Up @@ -47,14 +48,30 @@ export class MultiViewerSourceUpdateCommand extends DeserializedCommand<MultiVie
return new MultiViewerSourceUpdateCommand(multiViewerId, properties)
}

public applyToState(state: AtemState): string {
public applyToState(state: AtemState): string | string[] {
if (!state.info.multiviewer || this.multiViewerId >= state.info.multiviewer.count) {
throw new InvalidIdError('MultiViewer', this.multiViewerId)
}

const multiviewer = AtemStateUtil.getMultiViewer(state, this.multiViewerId)
const currentWindow = multiviewer.windows[this.properties.windowIndex]

// The Constellation HD range has a bug where it sends this command for every window on every frame
// This hides that from library consumers by doing a deep diff, when we usually do not.
if (currentWindow && !isRunningInTests()) {
let isChanged = false
for (const stringKey of Object.keys(this.properties)) {
const typedKey = stringKey as keyof MultiViewerSourceState
if (this.properties[typedKey] !== currentWindow[typedKey]) {
isChanged = true
break
}
}
if (!isChanged) return []
}

multiviewer.windows[this.properties.windowIndex] = {
...multiviewer.windows[this.properties.windowIndex],
...currentWindow,
...this.properties,
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib/atemUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,7 @@ export function omit<T, K extends keyof T>(o: T, ...keys: K[]): Omit<T, K> {
export function assertNever(_val: never): void {
// Nothing to do
}

export function isRunningInTests(): boolean {
return process.env.JEST_WORKER_ID !== undefined
}

0 comments on commit 6350a5b

Please sign in to comment.