UNPKG

@bbc/timeline-state-resolver-types

Version:
47 lines 1.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fillStateFromDatastore = void 0; /** * Set a value on an object from a .-delimited path * @param obj The base object * @param path Path of the value to set * @param val The value to set */ const set = (obj, path, val) => { const p = path.split('.'); p.slice(0, -1).reduce((a, b) => (a[b] ? a[b] : (a[b] = {})), obj)[p.slice(-1)[0]] = val; }; const updateRefs = (layer, path, modified) => { if (!layer.datastoreRefs) layer.datastoreRefs = {}; layer.datastoreRefs[path] = modified; layer.lastModified = Math.max(modified, layer.lastModified ?? 0); }; function fillStateFromDatastore(state, datastore) { // clone the state so we can freely manipulate it const filledState = JSON.parse(JSON.stringify(state)); Object.values(filledState.layers).forEach((layer) => { const { content, instance } = layer; if (content.$references) { Object.entries(content.$references || {}).forEach(([path, ref]) => { const datastoreVal = datastore[ref.datastoreKey]; if (datastoreVal !== undefined) { if (ref.overwrite) { // only use the datastore value if it was changed after the tl obj started if ((instance.originalStart || instance.start || 0) <= datastoreVal.modified) { set(content, path, datastoreVal.value); updateRefs(layer, path, datastoreVal.modified); } } else { set(content, path, datastoreVal.value); updateRefs(layer, path, datastoreVal.modified); } } }); } }); return filledState; } exports.fillStateFromDatastore = fillStateFromDatastore; //# sourceMappingURL=datastore.js.map