Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/timeline-state-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"p-timeout": "^3.2.0",
"simple-oauth2": "^5.0.0",
"sprintf-js": "^1.1.3",
"superfly-timeline": "9.0.2",
"superfly-timeline": "9.1.2",
"threadedclass": "^1.2.1",
"timeline-state-resolver-types": "9.2.0",
"tslib": "^2.6.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,78 @@ describe('stateHandler', () => {
},
})
})

test('ignore transitions to states older than current state', async () => {
const stateHandler = getNewStateHandler()

stateHandler
.setCurrentState({
entry1: { value: true },
})
.catch((e) => {
console.error('Error while setting current state', e)
})

stateHandler.handleState(createTimelineState(10000, {}), {}).catch((e) => {
console.error('Error while handling state', e)
})

await mockTime.tick()

expect(MOCK_COMMAND_RECEIVER).toHaveBeenCalledTimes(1)
expect(MOCK_COMMAND_RECEIVER).toHaveBeenCalledWith({
command: {
type: 'removed',
property: 'entry1',
},
})

stateHandler
.handleState(
createTimelineState(10100, {
entry1: { value: true },
}),
{}
)
.catch((e) => {
console.error('Error while handling state', e)
})

await mockTime.tick()

// do not expect to be called because this is in the future
expect(MOCK_COMMAND_RECEIVER).toHaveBeenCalledTimes(1)

// advance time
MOCK_COMMAND_RECEIVER.mockReset()
await mockTime.advanceTimeTicks(100)

// now expect to be called with new commands
expect(MOCK_COMMAND_RECEIVER).toHaveBeenCalledTimes(1)
expect(MOCK_COMMAND_RECEIVER).toHaveBeenCalledWith({
command: {
type: 'added',
property: 'entry1',
},
})

//
MOCK_COMMAND_RECEIVER.mockReset()

stateHandler.handleState(createTimelineState(10000, {}), {}).catch((e) => {
console.error('Error while handling state', e)
})

await mockTime.tick()

// do not expect to be called because new state is in the past
expect(MOCK_COMMAND_RECEIVER).toHaveBeenCalledTimes(0)

await mockTime.advanceTimeTicks(100)

// still no new commands to be received
expect(MOCK_COMMAND_RECEIVER).toHaveBeenCalledTimes(0)
})
})

function createTimelineState(
Expand Down
12 changes: 9 additions & 3 deletions packages/timeline-state-resolver/src/service/stateHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export class StateHandler<DeviceState, Command extends CommandWithContext> {
}

async handleState(state: Timeline.TimelineState<TSRTimelineContent>, mappings: Mappings) {
if (this.currentState?.state && this.currentState.state.time > state.time) return // the incoming state is stale, we ignore it

const nextState = this.stateQueue[0]

const trace = startTrace('device:convertTimelineStateToDeviceState', { deviceId: this.context.deviceId })
Expand Down Expand Up @@ -109,8 +111,8 @@ export class StateHandler<DeviceState, Command extends CommandWithContext> {
this.currentState = {
commands: [],
deviceState: state,
state: this.currentState?.state || { time: this.context.getCurrentTime(), layers: {}, nextEvents: [] },
mappings: this.currentState?.mappings || {},
state: this.currentState?.state ?? { time: this.context.getCurrentTime(), layers: {}, nextEvents: [] },
mappings: this.currentState?.mappings ?? {},
}
await this.calculateNextStateChange()
}
Expand Down Expand Up @@ -142,7 +144,11 @@ export class StateHandler<DeviceState, Command extends CommandWithContext> {
nextState.commands = []
}

if (nextState.state.time - (nextState.preliminary ?? 0) <= this.context.getCurrentTime() && this.currentState) {
if (
!this._executingStateChange &&
nextState === this.stateQueue[0] &&
nextState.state.time - (nextState.preliminary ?? 0) <= this.context.getCurrentTime()
) {
await this.executeNextStateChange()
}
}
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11110,12 +11110,12 @@ asn1@evs-broadcast/node-asn1:
languageName: node
linkType: hard

"superfly-timeline@npm:9.0.2":
version: 9.0.2
resolution: "superfly-timeline@npm:9.0.2"
"superfly-timeline@npm:9.1.2":
version: 9.1.2
resolution: "superfly-timeline@npm:9.1.2"
dependencies:
tslib: ^2.6.0
checksum: d628d467d5384f5667bc10b877478c5b8b0a91774b5d5c5e9d9d3134b8f1b760225f2fbbb0f9ccd3e55f930c9f3719f81b9347b94ea853fbc0a18bc121d97665
checksum: c195d3e65fd3d63223dd2a008facb32850b71da0355b258c0a08e1417bd447b144e01088f28e8e71fe8c240885c2bc5f73e8df7c84f131e963521510edf8bde2
languageName: node
linkType: hard

Expand Down Expand Up @@ -11415,7 +11415,7 @@ asn1@evs-broadcast/node-asn1:
p-timeout: ^3.2.0
simple-oauth2: ^5.0.0
sprintf-js: ^1.1.3
superfly-timeline: 9.0.2
superfly-timeline: 9.1.2
threadedclass: ^1.2.1
timeline-state-resolver-types: 9.2.0
tslib: ^2.6.2
Expand Down