@reactoo/watchtogether-sdk-js 2.7.80 → 2.7.82

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,11 @@
1
- import {setExactTimeout} from "../wt-utils";
1
+ import {setExactTimeout} from "../../models/utils";
2
2
  import localEmitter from "../wt-emitter";
3
3
 
4
4
  const syncModule = function ({room, emitter} = {}) {
5
-
5
+
6
6
  //SYNC VARS
7
7
  let _emitter = localEmitter();
8
-
8
+
9
9
  let _playerInterface = null;
10
10
  let _propagateMaster = false;
11
11
  let _playerInterfaceOptions = {
@@ -32,19 +32,19 @@ const syncModule = function ({room, emitter} = {}) {
32
32
 
33
33
 
34
34
  const startSyncLoop = () => {
35
-
35
+
36
36
  if(!isConnected() && _playerInterfaceOptions.type !== 'push') {
37
37
  room._log('--- Sync loop will not start due to user not connected yet ---');
38
38
  return
39
39
  }
40
-
40
+
41
41
  if(syncWaitId) {
42
42
  room._log('--- Sync loop already running ---');
43
43
  return
44
44
  }
45
-
45
+
46
46
  room._log('--- Sync enabled ---');
47
-
47
+
48
48
  stopFlag = false;
49
49
 
50
50
  if(_playerInterfaceOptions.type !== 'push') {
@@ -86,7 +86,7 @@ const syncModule = function ({room, emitter} = {}) {
86
86
  }
87
87
 
88
88
  };
89
-
89
+
90
90
  const stopSyncLoop = (doNotResetRetryCount) => {
91
91
  room._log('--- Sync disabled ---');
92
92
  clearTimeout(syncWaitId);
@@ -96,17 +96,17 @@ const syncModule = function ({room, emitter} = {}) {
96
96
  }
97
97
  stopFlag = true;
98
98
  };
99
-
99
+
100
100
  const restartSyncLoop = () => {
101
-
101
+
102
102
  room._log('--- Sync restarting ---');
103
103
  stopSyncLoop();
104
104
  startSyncLoop();
105
-
105
+
106
106
  };
107
-
107
+
108
108
  const setNextWaitTime = (didSyncFail = false) => {
109
-
109
+
110
110
  if(!didSyncFail) {
111
111
  syncNextWaitTime = syncDefaultWaitTime;
112
112
  currentSyncRetry = 0
@@ -119,39 +119,39 @@ const syncModule = function ({room, emitter} = {}) {
119
119
  syncNextWaitTime = syncShortWaitTime;
120
120
  }
121
121
  }
122
-
122
+
123
123
  room._log('--- Next sync will occur in ' + syncNextWaitTime / 1000 + ' seconds ---');
124
-
124
+
125
125
  }
126
-
126
+
127
127
  const sync = () => {
128
-
128
+
129
129
  return getSyncData()
130
130
  .then(syncData => {
131
-
131
+
132
132
  if(syncData.isMaster) {
133
133
  if(_playerInterface.isPaused) _playerInterface.play();
134
134
  setNextWaitTime(false);
135
135
  return Promise.resolve();
136
136
  }
137
-
137
+
138
138
  else if(_propagateMaster) {
139
139
  setNextWaitTime(false);
140
140
  return propagateMasterFunc();
141
141
  }
142
-
142
+
143
143
  else {
144
144
 
145
145
  const syncStartTime = Date.now();
146
- const {position, realPosition, isBufferSufficient} = _playerInterface.getTimeDifference
147
- ? _playerInterface.getTimeDifference(syncData.masterFragmentSn, syncData.masterFragmentPos, syncData.ping)
146
+ const {position, realPosition, isBufferSufficient} = _playerInterface.getTimeDifference
147
+ ? _playerInterface.getTimeDifference(syncData.masterFragmentSn, syncData.masterFragmentPos, syncData.ping)
148
148
  : getTimeDifference(syncData.masterFragmentSn, syncData.masterFragmentPos, syncData.ping);
149
149
  if(position && Math.abs(position) <= maxSyncThreshold) {
150
150
  room._log(`We're within max sync threshold, no need to resync now`);
151
151
  setNextWaitTime(false);
152
152
  return Promise.resolve();
153
153
  }
154
-
154
+
155
155
  if(position !== null && !isNaN(position)) {
156
156
  return seekBy(position)
157
157
  .then(() => {
@@ -164,7 +164,7 @@ const syncModule = function ({room, emitter} = {}) {
164
164
  room._log(`Insufficient buffer: `, !isBufferSufficient)
165
165
  room._log(`Seek duration is ${seekDuration / 1000} seconds`);
166
166
  room._log(`Sync precision should be ${syncPrecision}`);
167
-
167
+
168
168
  const didSyncFail = syncPrecision > maxSyncThreshold;
169
169
  setNextWaitTime(didSyncFail);
170
170
  return Promise.resolve();
@@ -186,7 +186,7 @@ const syncModule = function ({room, emitter} = {}) {
186
186
  return Promise.reject();
187
187
  })
188
188
  }
189
-
189
+
190
190
  const handleAddLocalParticipant = () => {
191
191
  if(_propagateMaster) {
192
192
  propagateMasterFunc();
@@ -195,34 +195,34 @@ const syncModule = function ({room, emitter} = {}) {
195
195
  _emitter.once('timeupdate', restartSyncLoop);
196
196
  }
197
197
  };
198
-
198
+
199
199
  const handleAddRemoteParticipant = () => {
200
200
  if(_propagateMaster) {
201
201
  propagateMasterFunc();
202
202
  }
203
203
  };
204
-
204
+
205
205
  const isConnected = () => {
206
206
  return room._isDataChannelOpen && room.isConnected;
207
207
  };
208
-
208
+
209
209
  const parseDataEvents = (msg = {}) => {
210
210
  if(msg.videoroom === 'sync_request') {
211
211
  roomSyncSend(msg.sync_slave_id).catch(()=>{});
212
212
  }
213
213
  };
214
-
214
+
215
215
  const getCurrentPlayerPosition = () => {
216
216
  let position = _playerInterface.currentTime;
217
217
  return isNaN(position) ? 0 : position * 1000
218
218
  };
219
-
219
+
220
220
  const getTimeDifference = (fragmentSn, fragmentPos, ping) => {
221
-
221
+
222
222
  let seekRanges = _playerInterface.buffered;
223
223
  let position = (fragmentPos + (ping / 2)) / 1000;
224
224
  let currentPlayerPosition = getCurrentPlayerPosition() / 1000;
225
-
225
+
226
226
  if(seekRanges && !_playerInterfaceOptions.ignoreBufferedTimeRanges) {
227
227
  let seekRange = {};
228
228
  for (let i = 0; i < seekRanges.length; i++) {
@@ -403,7 +403,7 @@ const syncModule = function ({room, emitter} = {}) {
403
403
  } else resolve()
404
404
  });
405
405
  };
406
-
406
+
407
407
  const handlePlaying = () => {
408
408
  if(!isProgrammaticallySeeked) {
409
409
  room._log('Handle playing');
@@ -413,13 +413,13 @@ const syncModule = function ({room, emitter} = {}) {
413
413
  isPreloading = _playerInterface.isPaused;
414
414
  isPlaying = !_playerInterface.isPaused;
415
415
  };
416
-
416
+
417
417
  const handlePause = () => {
418
418
  stopSyncLoop()
419
419
  clientPaused().catch(() => {});
420
420
  isPlaying = !_playerInterface.isPaused;
421
421
  };
422
-
422
+
423
423
  const handleBuffering = () => {
424
424
  room._log('handleBuffering');
425
425
  if(!isProgrammaticallySeeked) {
@@ -428,9 +428,9 @@ const syncModule = function ({room, emitter} = {}) {
428
428
  isPreloading = _playerInterface.isPaused;
429
429
  }
430
430
  };
431
-
431
+
432
432
  const roomSyncSend = (slaveId) => {
433
-
433
+
434
434
  if(!_playerInterface) {
435
435
  room._log(
436
436
  `I've been asked for position even if we don't have player attached.
@@ -443,10 +443,10 @@ const syncModule = function ({room, emitter} = {}) {
443
443
  fragment: String("0"),
444
444
  fragment_pos: Number(parseInt(getCurrentPlayerPosition()))
445
445
  }
446
-
446
+
447
447
  room._log(`Sending my position to ${slaveId}`);
448
448
  room._log(`Current time: ${fragmentData.fragment} # ${fragmentData.fragment_pos}`);
449
-
449
+
450
450
  return room.sendMessage(room.handleId, {
451
451
  body : {
452
452
  request: "sync_response",
@@ -456,58 +456,58 @@ const syncModule = function ({room, emitter} = {}) {
456
456
  ...fragmentData,
457
457
  }});
458
458
  };
459
-
459
+
460
460
  const getSyncData = () => {
461
-
461
+
462
462
  room._log('Sending roomSync request');
463
463
  let roomId = room.roomId;
464
464
  var fragmentData = _playerInterface?.currentFragment || {
465
465
  fragment: String("0"),
466
466
  fragment_pos: Number(parseInt(getCurrentPlayerPosition()))
467
467
  }
468
-
468
+
469
469
  return new Promise((resolve, reject) => {
470
-
470
+
471
471
  let now = new Date().getTime();
472
472
  let ping = null;
473
-
473
+
474
474
  let sid = setTimeout(() => {
475
475
  room.off('data', fn, this);
476
476
  reject('Timeout');
477
477
  }, 3000);
478
-
478
+
479
479
  let body = {
480
480
  request: "sync",
481
481
  room: roomId,
482
482
  timestamp: new Date().getTime(),
483
483
  ...fragmentData
484
484
  };
485
-
485
+
486
486
  let fn = (msg) => {
487
-
487
+
488
488
  if(msg.videoroom && ['sync', 'sync_response'].includes(msg.videoroom)) {
489
-
489
+
490
490
  if(msg.sync_master_await) {
491
491
  room._log('Waiting for master position');
492
492
  if(!ping) {
493
493
  ping = (new Date().getTime() - now);
494
494
  }
495
495
  }
496
-
496
+
497
497
  else if(msg.sync_master_fragment || msg.sync_master_fragment_pos) {
498
498
  room._log('Got master position data');
499
499
  if(!ping) {
500
500
  ping = (new Date().getTime() - now);
501
501
  }
502
-
502
+
503
503
  room._log(`I'm master: ${!!msg.sync_master_self}`);
504
504
  room._log(`Ping: ${ping}`);
505
505
  room._log(`Master fragment: ${msg.sync_master_fragment}`);
506
506
  room._log(`Master fragment position: ${msg.sync_master_fragment_pos}`);
507
-
507
+
508
508
  room.off('data', fn, this);
509
509
  clearTimeout(sid);
510
-
510
+
511
511
  resolve({
512
512
  isMaster: !!msg.sync_master_self,
513
513
  ping: ping,
@@ -515,7 +515,7 @@ const syncModule = function ({room, emitter} = {}) {
515
515
  masterFragmentSn: parseInt(msg.sync_master_fragment),
516
516
  });
517
517
  }
518
-
518
+
519
519
  else {
520
520
  clearTimeout(sid);
521
521
  reject('Master lost connection')
@@ -530,14 +530,14 @@ const syncModule = function ({room, emitter} = {}) {
530
530
  });
531
531
  });
532
532
  };
533
-
533
+
534
534
  const clientPaused = () => {
535
535
  room._log('Sending client paused');
536
-
536
+
537
537
  if(!isConnected()) {
538
538
  return Promise.resolve();
539
539
  }
540
-
540
+
541
541
  return room.sendMessage(room.handleId, {
542
542
  body:{
543
543
  request: "sync_paused",
@@ -547,7 +547,7 @@ const syncModule = function ({room, emitter} = {}) {
547
547
  fragment_pos:0
548
548
  }});
549
549
  };
550
-
550
+
551
551
  const propagateMasterFunc = () => {
552
552
  room._log('Propagating master');
553
553
  if(!isConnected()) {
@@ -563,11 +563,11 @@ const syncModule = function ({room, emitter} = {}) {
563
563
  fragment_pos:0
564
564
  }});
565
565
  };
566
-
566
+
567
567
  return {
568
-
568
+
569
569
  __events: ['playerSyncing'],
570
-
570
+
571
571
  initialize: ({playerInterface, propagateMaster = false} = {}) => {
572
572
 
573
573
  _playerInterface = playerInterface;
@@ -611,29 +611,29 @@ const syncModule = function ({room, emitter} = {}) {
611
611
  _emitter.emit('timeupdate', event);
612
612
  }
613
613
  });
614
-
614
+
615
615
  isPlaying = _playerInterface.isPaused === false;
616
-
616
+
617
617
  room.on('disconnect', stopSyncLoop);
618
618
  room.on('removeLocalParticipant', stopSyncLoop);
619
619
  room.on('addLocalParticipant', handleAddLocalParticipant);
620
620
  room.on('addRemoteParticipant', handleAddRemoteParticipant);
621
621
  room.on('data', parseDataEvents);
622
-
622
+
623
623
  if(_propagateMaster) {
624
624
  propagateMasterFunc().catch(() => {});
625
625
  }
626
-
626
+
627
627
  if(_playerInterface.isPaused === false) {
628
628
  _emitter.once('timeupdate', restartSyncLoop);
629
629
  }
630
-
630
+
631
631
  _emitter.on('buffering', handleBuffering);
632
632
  _emitter.on('playing', handlePlaying);
633
633
  _emitter.on('pause', handlePause);
634
634
 
635
635
  },
636
-
636
+
637
637
  destroy: () => {
638
638
  stopSyncLoop();
639
639
  room.off('disconnect', stopSyncLoop);
@@ -642,11 +642,11 @@ const syncModule = function ({room, emitter} = {}) {
642
642
  room.off('addRemoteParticipant', handleAddRemoteParticipant);
643
643
  room.off('data', parseDataEvents);
644
644
  _playerInterface = null;
645
-
645
+
646
646
  _emitter.clear();
647
647
  }
648
648
  };
649
-
649
+
650
650
  };
651
651
 
652
652
  export default syncModule;