@videosdk.live/react-sdk 0.1.12 → 0.1.13

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,8 +1,8 @@
1
1
  import React, { createContext, useEffect, useContext, useState, useRef, useMemo } from 'react';
2
2
  import EventEmitter from 'events';
3
- import { ZujoSDK } from '@videosdk.live/js-sdk';
3
+ import { VideoSDK } from '@videosdk.live/js-sdk';
4
4
 
5
- var eventPrifix = 'zn-meeting-react-sdk';
5
+ var eventPrifix = 'videosdk-live-react-sdk';
6
6
  var events = {
7
7
  'participant-joined': eventPrifix + "-participant-joined",
8
8
  'participant-left': eventPrifix + "-participant-left",
@@ -22,7 +22,10 @@ var events = {
22
22
  'video-seeked': eventPrifix + "-video-seeked",
23
23
  'webcam-requested': eventPrifix + "-webcam-requested",
24
24
  'mic-requested': eventPrifix + "-mic-requested",
25
- 'pin-state-changed': eventPrifix + "-pin-state-changed"
25
+ 'pin-state-changed': eventPrifix + "-pin-state-changed",
26
+ 'connection-open': eventPrifix + "-connection-open",
27
+ 'connection-close': eventPrifix + "-connection-close",
28
+ 'switch-meeting': eventPrifix + "-switch-meeting"
26
29
  };
27
30
  var eventEmitter = new EventEmitter();
28
31
  eventEmitter.setMaxListeners(9999);
@@ -67,7 +70,13 @@ var useMeeting = function useMeeting(_temp) {
67
70
  _ref$onMicRequested = _ref.onMicRequested,
68
71
  onMicRequested = _ref$onMicRequested === void 0 ? function () {} : _ref$onMicRequested,
69
72
  _ref$onPinStateChange = _ref.onPinStateChanged,
70
- onPinStateChanged = _ref$onPinStateChange === void 0 ? function () {} : _ref$onPinStateChange;
73
+ onPinStateChanged = _ref$onPinStateChange === void 0 ? function () {} : _ref$onPinStateChange,
74
+ _ref$onConnectionOpen = _ref.onConnectionOpen,
75
+ onConnectionOpen = _ref$onConnectionOpen === void 0 ? function () {} : _ref$onConnectionOpen,
76
+ _ref$onConnetionClose = _ref.onConnetionClose,
77
+ onConnetionClose = _ref$onConnetionClose === void 0 ? function () {} : _ref$onConnetionClose,
78
+ _ref$onSwitchMeeting = _ref.onSwitchMeeting,
79
+ onSwitchMeeting = _ref$onSwitchMeeting === void 0 ? function () {} : _ref$onSwitchMeeting;
71
80
 
72
81
  useEffect(function () {
73
82
  eventEmitter.on(events['participant-joined'], onParticipantJoined);
@@ -89,6 +98,9 @@ var useMeeting = function useMeeting(_temp) {
89
98
  eventEmitter.on(events['webcam-requested'], onWebcamRequested);
90
99
  eventEmitter.on(events['mic-requested'], onMicRequested);
91
100
  eventEmitter.on(events['pin-state-changed'], onPinStateChanged);
101
+ eventEmitter.on(events['connection-open'], onConnectionOpen);
102
+ eventEmitter.on(events['connection-close'], onConnetionClose);
103
+ eventEmitter.on(events['switch-meeting'], onSwitchMeeting);
92
104
  return function () {
93
105
  eventEmitter.off(events['participant-joined'], onParticipantJoined);
94
106
  eventEmitter.off(events['participant-left'], onParticipantLeft);
@@ -109,6 +121,9 @@ var useMeeting = function useMeeting(_temp) {
109
121
  eventEmitter.off(events['webcam-requested'], onWebcamRequested);
110
122
  eventEmitter.off(events['mic-requested'], onMicRequested);
111
123
  eventEmitter.off(events['pin-state-changed'], onPinStateChanged);
124
+ eventEmitter.off(events['connection-open'], onConnectionOpen);
125
+ eventEmitter.off(events['connection-close'], onConnetionClose);
126
+ eventEmitter.off(events['switch-meeting'], onSwitchMeeting);
112
127
  };
113
128
  }, []);
114
129
  return useContext(MeetingProviderContex);
@@ -152,7 +167,13 @@ var MeetingConsumer = function MeetingConsumer(_ref2) {
152
167
  _ref2$onMicRequested = _ref2.onMicRequested,
153
168
  onMicRequested = _ref2$onMicRequested === void 0 ? function () {} : _ref2$onMicRequested,
154
169
  _ref2$onPinStateChang = _ref2.onPinStateChanged,
155
- onPinStateChanged = _ref2$onPinStateChang === void 0 ? function () {} : _ref2$onPinStateChang;
170
+ onPinStateChanged = _ref2$onPinStateChang === void 0 ? function () {} : _ref2$onPinStateChang,
171
+ _ref2$onConnectionOpe = _ref2.onConnectionOpen,
172
+ onConnectionOpen = _ref2$onConnectionOpe === void 0 ? function () {} : _ref2$onConnectionOpe,
173
+ _ref2$onConnetionClos = _ref2.onConnetionClose,
174
+ onConnetionClose = _ref2$onConnetionClos === void 0 ? function () {} : _ref2$onConnetionClos,
175
+ _ref2$onSwitchMeeting = _ref2.onSwitchMeeting,
176
+ onSwitchMeeting = _ref2$onSwitchMeeting === void 0 ? function () {} : _ref2$onSwitchMeeting;
156
177
  useEffect(function () {
157
178
  eventEmitter.on(events['participant-joined'], onParticipantJoined);
158
179
  eventEmitter.on(events['participant-left'], onParticipantLeft);
@@ -173,6 +194,9 @@ var MeetingConsumer = function MeetingConsumer(_ref2) {
173
194
  eventEmitter.on(events['webcam-requested'], onWebcamRequested);
174
195
  eventEmitter.on(events['mic-requested'], onMicRequested);
175
196
  eventEmitter.on(events['pin-state-changed'], onPinStateChanged);
197
+ eventEmitter.on(events['connection-open'], onConnectionOpen);
198
+ eventEmitter.on(events['connection-close'], onConnetionClose);
199
+ eventEmitter.on(events['switch-meeting'], onSwitchMeeting);
176
200
  return function () {
177
201
  eventEmitter.off(events['participant-joined'], onParticipantJoined);
178
202
  eventEmitter.off(events['participant-left'], onParticipantLeft);
@@ -193,6 +217,9 @@ var MeetingConsumer = function MeetingConsumer(_ref2) {
193
217
  eventEmitter.off(events['webcam-requested'], onWebcamRequested);
194
218
  eventEmitter.off(events['mic-requested'], onMicRequested);
195
219
  eventEmitter.off(events['pin-state-changed'], onPinStateChanged);
220
+ eventEmitter.off(events['connection-open'], onConnectionOpen);
221
+ eventEmitter.off(events['connection-close'], onConnetionClose);
222
+ eventEmitter.off(events['switch-meeting'], onSwitchMeeting);
196
223
  };
197
224
  }, []);
198
225
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(MeetingProviderContex.Consumer, null, children));
@@ -202,7 +229,8 @@ var MeetingProvider = function MeetingProvider(_ref) {
202
229
  var children = _ref.children,
203
230
  config = _ref.config,
204
231
  token = _ref.token,
205
- joinWithoutUserInteraction = _ref.joinWithoutUserInteraction;
232
+ joinWithoutUserInteraction = _ref.joinWithoutUserInteraction,
233
+ _reinitialiseMeetingOnConfigChange = _ref.reinitialiseMeetingOnConfigChange;
206
234
 
207
235
  var _useState = useState(null),
208
236
  meeting = _useState[0],
@@ -256,11 +284,21 @@ var MeetingProvider = function MeetingProvider(_ref) {
256
284
  isLiveStreaming = _useState13[0],
257
285
  setIsLiveStreaming = _useState13[1];
258
286
 
287
+ var _useState14 = useState(new Map()),
288
+ connections = _useState14[0],
289
+ setConnections = _useState14[1];
290
+
259
291
  var meetingRef = useRef();
260
292
  var localMicOnRef = useRef();
261
293
  var localWebcamOnRef = useRef();
262
294
  var localScreenShareOnRef = useRef();
263
295
  var isRecordingRef = useRef();
296
+ var configRef = useRef(config);
297
+ var tokenRef = useRef(token);
298
+ var joinedOnFirstRender = useRef(false);
299
+ var reinitialiseMeetingOnConfigChange = useMemo(function () {
300
+ return _reinitialiseMeetingOnConfigChange;
301
+ }, []);
264
302
  useEffect(function () {
265
303
  meetingRef.current = meeting;
266
304
  }, [meeting]);
@@ -276,6 +314,12 @@ var MeetingProvider = function MeetingProvider(_ref) {
276
314
  useEffect(function () {
277
315
  isRecordingRef.current = isRecording;
278
316
  }, [isRecording]);
317
+ useEffect(function () {
318
+ configRef.current = config;
319
+ }, [config]);
320
+ useEffect(function () {
321
+ tokenRef.current = token;
322
+ }, [token]);
279
323
 
280
324
  var resetStates = function resetStates() {
281
325
  setMeeting(null);
@@ -291,6 +335,7 @@ var MeetingProvider = function MeetingProvider(_ref) {
291
335
  setIsLiveStreaming(false);
292
336
  setParticipants(new Map());
293
337
  setPinnedParticipants(new Map());
338
+ setConnections(new Map());
294
339
  };
295
340
 
296
341
  var _handle_participant_joined = function _handle_participant_joined(participant) {
@@ -401,6 +446,7 @@ var MeetingProvider = function MeetingProvider(_ref) {
401
446
  };
402
447
 
403
448
  var _handle_meeting_joined = function _handle_meeting_joined(data) {
449
+ joinedOnFirstRender.current = true;
404
450
  setMessages(function (s) {
405
451
  return [].concat(s, data.messages);
406
452
  });
@@ -433,6 +479,10 @@ var MeetingProvider = function MeetingProvider(_ref) {
433
479
  meeting.off('video-seeked', _handle_video_seeked);
434
480
  meeting.off('webcam-requested', _handle_webcam_requested);
435
481
  meeting.off('mic-requested', _handle_mic_requested);
482
+ meeting.off('pin-state-changed', _handle_pin_state_changed);
483
+ meeting.off('connection-open', _handle_connection_open);
484
+ meeting.off('connection-close', _handle_connection_close);
485
+ meeting.off('switch-meeting', _handle_switch_meeting);
436
486
  }
437
487
 
438
488
  if (typeof (meeting === null || meeting === void 0 ? void 0 : (_meeting$localPartici = meeting.localParticipant) === null || _meeting$localPartici === void 0 ? void 0 : _meeting$localPartici.off) === 'function') {
@@ -465,8 +515,30 @@ var MeetingProvider = function MeetingProvider(_ref) {
465
515
  });
466
516
  };
467
517
 
518
+ var _handle_connection_open = function _handle_connection_open(connection) {
519
+ setConnections(function (s) {
520
+ s.set(connection.id, connection);
521
+ var connectionsToSet = new Map(s);
522
+ return connectionsToSet;
523
+ });
524
+ eventEmitter.emit(events['connection-open'], connection);
525
+ };
526
+
527
+ var _handle_connection_close = function _handle_connection_close(connectionId) {
528
+ setConnections(function (s) {
529
+ s["delete"](connectionId);
530
+ var newConnections = new Map(s);
531
+ return newConnections;
532
+ });
533
+ eventEmitter.emit(events['connection-close'], connectionId);
534
+ };
535
+
536
+ var _handle_switch_meeting = function _handle_switch_meeting(d) {
537
+ eventEmitter.emit(events['switch-meeting'], d);
538
+ };
539
+
468
540
  var join = function join() {
469
- var meeting = ZujoSDK && ZujoSDK.initMeeting(config);
541
+ var meeting = VideoSDK && VideoSDK.initMeeting(configRef.current);
470
542
  setMeeting(meeting);
471
543
  var localParticipant = meeting.localParticipant,
472
544
  participants = meeting.participants;
@@ -492,6 +564,9 @@ var MeetingProvider = function MeetingProvider(_ref) {
492
564
  meeting.on('webcam-requested', _handle_webcam_requested);
493
565
  meeting.on('mic-requested', _handle_mic_requested);
494
566
  meeting.on('pin-state-changed', _handle_pin_state_changed);
567
+ meeting.on('connection-open', _handle_connection_open);
568
+ meeting.on('connection-close', _handle_connection_close);
569
+ meeting.on('switch-meeting', _handle_switch_meeting);
495
570
  meeting.localParticipant.on('stream-enabled', _handle_localParticipant_stream_enabled);
496
571
  meeting.localParticipant.on('stream-disabled', _handle_localParticipant_stream_disabled);
497
572
  meeting.join();
@@ -734,8 +809,30 @@ var MeetingProvider = function MeetingProvider(_ref) {
734
809
  }
735
810
  };
736
811
 
812
+ var connectTo = function connectTo(_ref6) {
813
+ var meetingId = _ref6.meetingId,
814
+ payload = _ref6.payload;
815
+
816
+ try {
817
+ var _meeting3 = meetingRef.current;
818
+
819
+ var _temp2 = function () {
820
+ if (_meeting3) {
821
+ return Promise.resolve(_meeting3.connectTo({
822
+ meetingId: meetingId,
823
+ payload: payload
824
+ })).then(function () {});
825
+ }
826
+ }();
827
+
828
+ return Promise.resolve(_temp2 && _temp2.then ? _temp2.then(function () {}) : void 0);
829
+ } catch (e) {
830
+ return Promise.reject(e);
831
+ }
832
+ };
833
+
737
834
  var initSDK = function initSDK() {
738
- ZujoSDK.config(token);
835
+ VideoSDK.config(tokenRef.current);
739
836
 
740
837
  if (joinWithoutUserInteraction) {
741
838
  join();
@@ -745,9 +842,16 @@ var MeetingProvider = function MeetingProvider(_ref) {
745
842
  useEffect(function () {
746
843
  initSDK();
747
844
  }, []);
845
+ useEffect(function () {
846
+ if (reinitialiseMeetingOnConfigChange && joinedOnFirstRender.current) {
847
+ leave();
848
+ initSDK();
849
+ !joinWithoutUserInteraction && join();
850
+ }
851
+ }, [config.meetingId, token, reinitialiseMeetingOnConfigChange]);
748
852
  return /*#__PURE__*/React.createElement(MeetingProviderContex.Provider, {
749
853
  value: {
750
- meetingId: config.meetingId,
854
+ meetingId: meeting === null || meeting === void 0 ? void 0 : meeting.id,
751
855
  meeting: meeting,
752
856
  localParticipant: localParticipant,
753
857
  mainParticipant: mainParticipant,
@@ -761,6 +865,7 @@ var MeetingProvider = function MeetingProvider(_ref) {
761
865
  isRecording: isRecording,
762
866
  isLiveStreaming: isLiveStreaming,
763
867
  pinnedParticipants: pinnedParticipants,
868
+ connections: connections,
764
869
  join: join,
765
870
  leave: leave,
766
871
  end: end,
@@ -787,7 +892,8 @@ var MeetingProvider = function MeetingProvider(_ref) {
787
892
  pauseVideo: pauseVideo,
788
893
  seekVideo: seekVideo,
789
894
  startLivestream: startLivestream,
790
- stopLivestream: stopLivestream
895
+ stopLivestream: stopLivestream,
896
+ connectTo: connectTo
791
897
  }
792
898
  }, children);
793
899
  };
@@ -914,6 +1020,10 @@ var useParticipant = function useParticipant(participantId, _temp) {
914
1020
  participant === null || participant === void 0 ? void 0 : participant.unpin(data);
915
1021
  };
916
1022
 
1023
+ var switchTo = function switchTo(data) {
1024
+ return Promise.resolve(participant === null || participant === void 0 ? void 0 : participant.switchTo(data)).then(function () {});
1025
+ };
1026
+
917
1027
  useEffect(function () {
918
1028
  var streams = participant === null || participant === void 0 ? void 0 : participant.streams;
919
1029
 
@@ -949,9 +1059,92 @@ var useParticipant = function useParticipant(participantId, _temp) {
949
1059
  enableWebcam: enableWebcam,
950
1060
  disableWebcam: disableWebcam,
951
1061
  pin: pin,
952
- unpin: unpin
1062
+ unpin: unpin,
1063
+ switchTo: switchTo
1064
+ };
1065
+ };
1066
+
1067
+ var useConnection = function useConnection(connectionId, _temp) {
1068
+ var _ref = _temp === void 0 ? {
1069
+ onMeeting: {
1070
+ onParticipantJoined: function onParticipantJoined() {},
1071
+ onParticipantLeft: function onParticipantLeft() {},
1072
+ onChatMessage: function onChatMessage() {}
1073
+ }
1074
+ } : _temp,
1075
+ onMeeting = _ref.onMeeting;
1076
+
1077
+ var onParticipantJoined = onMeeting === null || onMeeting === void 0 ? void 0 : onMeeting.onParticipantJoined;
1078
+ var onParticipantLeft = onMeeting === null || onMeeting === void 0 ? void 0 : onMeeting.onParticipantLeft;
1079
+ var onChatMessage = onMeeting === null || onMeeting === void 0 ? void 0 : onMeeting.onChatMessage;
1080
+ var meeting = useMeeting();
1081
+
1082
+ var _useMemo = useMemo(function () {
1083
+ var connection = meeting === null || meeting === void 0 ? void 0 : meeting.connections.get(connectionId);
1084
+
1085
+ if (!connection) {
1086
+ throw new Error('connectionId not found');
1087
+ }
1088
+
1089
+ var connectionParticipants = new Map(connection.meeting.participants);
1090
+ return {
1091
+ connection: connection,
1092
+ connectionParticipants: connectionParticipants
1093
+ };
1094
+ }, [meeting]),
1095
+ connection = _useMemo.connection,
1096
+ connectionParticipants = _useMemo.connectionParticipants;
1097
+
1098
+ var _useState = useState(new Map(connectionParticipants)),
1099
+ _connectionParticipants = _useState[0],
1100
+ setConnectionParticipants = _useState[1];
1101
+
1102
+ var _handleParticipantJoined = function _handleParticipantJoined(participant) {
1103
+ setConnectionParticipants(function (s) {
1104
+ s.set(participant.id, participant);
1105
+ var participantsToSet = new Map(s);
1106
+ return participantsToSet;
1107
+ });
1108
+ onParticipantJoined(participant);
1109
+ };
1110
+
1111
+ var _handleParticipantLeft = function _handleParticipantLeft(participantId) {
1112
+ setConnectionParticipants(function (s) {
1113
+ s["delete"](participantId);
1114
+ var newParticipants = new Map(s);
1115
+ return newParticipants;
1116
+ });
1117
+ onParticipantLeft(participantId);
1118
+ };
1119
+
1120
+ var _handleChatMessage = function _handleChatMessage(data) {
1121
+ onChatMessage(data);
1122
+ };
1123
+
1124
+ useEffect(function () {
1125
+ connection === null || connection === void 0 ? void 0 : connection.meeting.on('participant-joined', _handleParticipantJoined);
1126
+ connection === null || connection === void 0 ? void 0 : connection.meeting.on('participant-left', _handleParticipantLeft);
1127
+ connection === null || connection === void 0 ? void 0 : connection.meeting.on('chat-message', _handleChatMessage);
1128
+ return function () {
1129
+ connection === null || connection === void 0 ? void 0 : connection.meeting.off('participant-joined', _handleParticipantJoined);
1130
+ connection === null || connection === void 0 ? void 0 : connection.meeting.off('participant-left', _handleParticipantLeft);
1131
+ connection === null || connection === void 0 ? void 0 : connection.meeting.off('chat-message', _handleChatMessage);
1132
+ };
1133
+ }, [connection]);
1134
+ return {
1135
+ connection: {
1136
+ id: connection.id,
1137
+ close: connection.close,
1138
+ payload: connection.payload,
1139
+ meeting: {
1140
+ id: connection.meeting.id,
1141
+ sendChatMessage: connection.meeting.sendChatMessage,
1142
+ end: connection.meeting.end,
1143
+ participants: _connectionParticipants
1144
+ }
1145
+ }
953
1146
  };
954
1147
  };
955
1148
 
956
- export { MeetingConsumer, MeetingProvider, useMeeting, useParticipant };
1149
+ export { MeetingConsumer, MeetingProvider, useConnection, useMeeting, useParticipant };
957
1150
  //# sourceMappingURL=index.modern.js.map