File size: 878 Bytes
5170d86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(() => {
  const stage = document.querySelector('deck-stage');
  if (!stage || !('BroadcastChannel' in window)) return;

  const channel = new BroadcastChannel('omniagent-deck-sync');
  let applyingRemote = false;

  const publish = (index) => {
    if (!applyingRemote) channel.postMessage({ type: 'slide', index });
  };

  stage.addEventListener('slidechange', (event) => {
    publish(event.detail && typeof event.detail.index === 'number'
      ? event.detail.index
      : stage.index || 0);
  });

  channel.onmessage = (event) => {
    const data = event.data || {};
    if (data.type === 'hello') {
      publish(stage.index || 0);
      return;
    }
    if (data.type !== 'goto' || typeof data.index !== 'number') return;
    if (typeof stage.goTo !== 'function') return;

    applyingRemote = true;
    stage.goTo(data.index);
    applyingRemote = false;
  };
})();