timeline-state-resolver
Version:
Have timeline, control stuff
169 lines • 7.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.diffStates = void 0;
const timeline_state_resolver_types_1 = require("timeline-state-resolver-types");
const isPharosObject = (obj) => {
return !!obj && obj.content.deviceType === timeline_state_resolver_types_1.DeviceType.PHAROS;
};
function diffStates(oldPharosState, newPharosState, _mappings) {
const commands = [];
const stoppedLayers = new Set();
const stopLayer = (oldLayer, reason) => {
if (stoppedLayers.has(oldLayer.id))
return; // don't send several remove commands for the same object
if (oldLayer.content.noRelease)
return; // override: don't stop / release
stoppedLayers.add(oldLayer.id);
if (oldLayer.content.type === timeline_state_resolver_types_1.TimelineContentTypePharos.SCENE) {
if (!reason)
reason = 'removed scene';
const scene = oldLayer.content.scene;
const fade = oldLayer.content.fade;
commands.push({
command: {
fcn: async (pharos) => pharos.releaseScene(scene, fade),
},
context: `${reason}: ${oldLayer.id} ${scene}`,
timelineObjId: oldLayer.id,
});
}
else if (oldLayer.content.type === timeline_state_resolver_types_1.TimelineContentTypePharos.TIMELINE) {
if (!reason)
reason = 'removed timeline';
const timeline = oldLayer.content.timeline;
const fade = oldLayer.content.fade;
commands.push({
command: {
fcn: async (pharos) => pharos.releaseTimeline(timeline, fade),
},
context: `${reason}: ${oldLayer.id} ${timeline}`,
timelineObjId: oldLayer.id,
});
}
};
const modifyTimelinePlay = (newLayer, oldLayer) => {
if (newLayer.content.type === timeline_state_resolver_types_1.TimelineContentTypePharos.TIMELINE) {
if ((newLayer.content.pause || false) !==
(oldLayer?.content?.pause || false)) {
const timeline = newLayer.content.timeline;
if (newLayer.content.pause) {
commands.push({
command: {
fcn: async (pharos) => pharos.pauseTimeline(timeline),
},
context: `pause timeline: ${newLayer.id} ${timeline}`,
timelineObjId: newLayer.id,
});
}
else {
commands.push({
command: {
fcn: async (pharos) => pharos.resumeTimeline(timeline),
},
context: `resume timeline: ${newLayer.id} ${timeline}`,
timelineObjId: newLayer.id,
});
}
}
if ((newLayer.content.rate || null) !==
(oldLayer?.content?.rate || null)) {
const timeline = newLayer.content.timeline;
const rate = newLayer.content.rate;
commands.push({
command: {
fcn: async (pharos) => pharos.setTimelineRate(timeline, rate ?? 0),
},
context: `pause timeline: ${newLayer.id} ${timeline}: ${rate}`,
timelineObjId: newLayer.id,
});
}
// @todo: support pause / setTimelinePosition
}
};
const startLayer = (newLayer, reason) => {
if (newLayer.content.stopped) {
// Item is set to "stopped"
stopLayer(newLayer);
}
else if (newLayer.content.type === timeline_state_resolver_types_1.TimelineContentTypePharos.SCENE) {
if (!reason)
reason = 'added scene';
const scene = newLayer.content.scene;
commands.push({
command: {
fcn: async (pharos) => pharos.startScene(scene),
},
context: `${reason}: ${newLayer.id} ${scene}`,
timelineObjId: newLayer.id,
});
}
else if (newLayer.content.type === timeline_state_resolver_types_1.TimelineContentTypePharos.TIMELINE) {
if (!reason)
reason = 'added timeline';
const timeline = newLayer.content.timeline;
commands.push({
command: {
fcn: async (pharos) => pharos.startTimeline(timeline),
},
context: `${reason}: ${newLayer.id} ${timeline}`,
timelineObjId: newLayer.id,
});
modifyTimelinePlay(newLayer);
}
};
// Added / Changed things:
for (const [layerKey, newLayer] of Object.entries(newPharosState)) {
const oldPharosObj0 = oldPharosState?.[layerKey];
const oldPharosObj = isPharosObject(oldPharosObj0) ? oldPharosObj0 : undefined;
const pharosObj = isPharosObject(newLayer) ? newLayer : undefined;
if (!pharosObj) {
if (oldPharosObj) {
stopLayer(oldPharosObj);
}
}
else if (!oldPharosObj || !isPharosObject(oldPharosObj)) {
// item is new
startLayer(pharosObj);
}
// item is not new, but maybe it has changed:
else if (pharosObj.content.type !== oldPharosObj.content.type || // item has changed type!
(pharosObj.content.stopped || false) !== (oldPharosObj.content.stopped || false) // item has stopped / unstopped
) {
if (!oldPharosObj.content.stopped) {
// If it was stopped before, we don't have to stop it now:
stopLayer(oldPharosObj);
}
startLayer(pharosObj);
}
else if (pharosObj.content.type === timeline_state_resolver_types_1.TimelineContentTypePharos.SCENE) {
if (pharosObj.content.scene !== oldPharosObj.content.scene) {
// scene has changed
stopLayer(oldPharosObj, 'scene changed from');
startLayer(pharosObj, 'scene changed to');
}
}
else if (pharosObj.content.type === timeline_state_resolver_types_1.TimelineContentTypePharos.TIMELINE) {
if (pharosObj.content.timeline !== oldPharosObj.content.timeline) {
// timeline has changed
stopLayer(oldPharosObj, 'timeline changed from');
startLayer(pharosObj, 'timeline changed to');
}
else {
modifyTimelinePlay(pharosObj, oldPharosObj);
}
}
}
// Removed things
if (oldPharosState) {
for (const [layerKey, oldLayer] of Object.entries(oldPharosState)) {
const newLayer = newPharosState[layerKey];
if (!newLayer && isPharosObject(oldLayer)) {
// removed item
stopLayer(oldLayer);
}
}
}
return commands;
}
exports.diffStates = diffStates;
//# sourceMappingURL=diffStates.js.map