diff --git a/api/browser/selection.js b/api/browser/selection.js index 6f68125f..44610bbf 100644 --- a/api/browser/selection.js +++ b/api/browser/selection.js @@ -43,12 +43,39 @@ class Selection { return } this.#client = newValue + this.#setup() } constructor (props) { this.#props = props } + async #setup () { + /* + Only listen to changes to + the _serverSelection key + and set the selection in this + thread when it changes + + Typically the API shouldn't + attach event listeners itself + as it may force data to be pushed + even though it's not used by the client + + - but make an exception here as + this code is only run in the renderer + thread and listeners on state.change + are required anyway + */ + this.#props.Events.on('state.change', (state, set) => { + if (!set?._connections?.[this.#client.getIdentity()]?._serverSelection) { + return + } + const newSelection = state?._connections?.[this.#client.getIdentity()]?._serverSelection + this.setSelection(newSelection) + }) + } + /** * Select an item, * will replace the diff --git a/api/state.js b/api/state.js index 8b87225c..bdc153ad 100644 --- a/api/state.js +++ b/api/state.js @@ -64,7 +64,7 @@ class State { this.applyLocally(set) } - return this.#state + return [this.#state, set] }) } diff --git a/plugins/rundown/lib/handlers.js b/plugins/rundown/lib/handlers.js index 158c45af..4d5b211e 100644 --- a/plugins/rundown/lib/handlers.js +++ b/plugins/rundown/lib/handlers.js @@ -99,7 +99,12 @@ async function selectItem (id) { bridge.state.apply({ _connections: { [mainClientId]: { - selection: { $replace: [id] } + /* + Use a key that the + client can listen to + and act on + */ + _serverSelection: { $replace: [id] } } } })