react-jssip-kit 0.2.0 → 0.3.1

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.
package/dist/index.cjs CHANGED
@@ -820,11 +820,13 @@ var SipClient = class extends EventTargetEmitter {
820
820
  const debug = this.debugPattern ?? cfgDebug;
821
821
  this.userAgent.start(uri, password, uaCfg, { debug });
822
822
  this.attachUAHandlers();
823
+ this.attachBeforeUnload();
823
824
  }
824
825
  registerUA() {
825
826
  this.userAgent.register();
826
827
  }
827
828
  disconnect() {
829
+ this.detachBeforeUnload();
828
830
  this.detachUAHandlers();
829
831
  this.userAgent.stop();
830
832
  this.cleanupAllSessions();
@@ -846,16 +848,21 @@ var SipClient = class extends EventTargetEmitter {
846
848
  }
847
849
  }
848
850
  answer(options = {}) {
849
- const sessionId = this.resolveSessionId();
851
+ const sessionId = this.resolveExistingSessionId();
850
852
  if (!sessionId)
851
853
  return false;
852
854
  return this.answerSession(sessionId, options);
853
855
  }
854
- hangup(options) {
855
- const sessionId = this.resolveSessionId();
856
- if (!sessionId)
856
+ hangup(sessionId, options) {
857
+ const resolved = this.resolveExistingSessionId(sessionId);
858
+ if (!resolved)
857
859
  return false;
858
- return this.hangupSession(sessionId, options);
860
+ return this.hangupSession(resolved, options);
861
+ }
862
+ hangupAll(options) {
863
+ const ids = this.getSessionIds();
864
+ ids.forEach((id) => this.hangupSession(id, options));
865
+ return ids.length > 0;
859
866
  }
860
867
  toggleMute() {
861
868
  return this.toggleMuteSession();
@@ -864,7 +871,7 @@ var SipClient = class extends EventTargetEmitter {
864
871
  return this.toggleHoldSession();
865
872
  }
866
873
  sendDTMF(tones, options) {
867
- const sessionId = this.resolveSessionId();
874
+ const sessionId = this.resolveExistingSessionId();
868
875
  return this.sendDTMFSession(tones, options, sessionId ?? void 0);
869
876
  }
870
877
  transfer(target, options) {
@@ -979,24 +986,33 @@ var SipClient = class extends EventTargetEmitter {
979
986
  const active = sessions.find((s) => s.status === CallStatus.Active);
980
987
  return active?.id ?? sessions[0]?.id ?? null;
981
988
  }
989
+ sessionExists(sessionId) {
990
+ return !!this.sessionManager.getSession(sessionId) || !!this.sessionManager.getRtc(sessionId);
991
+ }
992
+ resolveExistingSessionId(sessionId) {
993
+ const id = this.resolveSessionId(sessionId);
994
+ if (!id)
995
+ return null;
996
+ return this.sessionExists(id) ? id : null;
997
+ }
982
998
  ensureMediaConstraints(opts) {
983
999
  if (opts.mediaStream || opts.mediaConstraints)
984
1000
  return opts;
985
1001
  return { ...opts, mediaConstraints: { audio: true, video: false } };
986
1002
  }
987
1003
  answerSession(sessionId, options = {}) {
988
- if (!sessionId)
1004
+ if (!sessionId || !this.sessionExists(sessionId))
989
1005
  return false;
990
1006
  const opts = this.ensureMediaConstraints(options);
991
1007
  return this.sessionManager.answer(sessionId, opts);
992
1008
  }
993
1009
  hangupSession(sessionId, options) {
994
- if (!sessionId)
1010
+ if (!sessionId || !this.sessionExists(sessionId))
995
1011
  return false;
996
1012
  return this.sessionManager.hangup(sessionId, options);
997
1013
  }
998
1014
  toggleMuteSession(sessionId) {
999
- const resolved = this.resolveSessionId(sessionId);
1015
+ const resolved = this.resolveExistingSessionId(sessionId);
1000
1016
  if (!resolved)
1001
1017
  return false;
1002
1018
  const sessionState = this.stateStore.getState().sessions.find((s) => s.id === resolved);
@@ -1009,7 +1025,7 @@ var SipClient = class extends EventTargetEmitter {
1009
1025
  return true;
1010
1026
  }
1011
1027
  toggleHoldSession(sessionId) {
1012
- const resolved = this.resolveSessionId(sessionId);
1028
+ const resolved = this.resolveExistingSessionId(sessionId);
1013
1029
  if (!resolved)
1014
1030
  return false;
1015
1031
  const sessionState = this.stateStore.getState().sessions.find((s) => s.id === resolved);
@@ -1025,7 +1041,7 @@ var SipClient = class extends EventTargetEmitter {
1025
1041
  return true;
1026
1042
  }
1027
1043
  sendDTMFSession(tones, options, sessionId) {
1028
- const resolved = this.resolveSessionId(sessionId);
1044
+ const resolved = this.resolveExistingSessionId(sessionId);
1029
1045
  if (!resolved)
1030
1046
  return false;
1031
1047
  const sessionState = this.stateStore.getState().sessions.find((s) => s.id === resolved);
@@ -1034,7 +1050,7 @@ var SipClient = class extends EventTargetEmitter {
1034
1050
  return true;
1035
1051
  }
1036
1052
  transferSession(target, options, sessionId) {
1037
- const resolved = this.resolveSessionId(sessionId);
1053
+ const resolved = this.resolveExistingSessionId(sessionId);
1038
1054
  if (!resolved)
1039
1055
  return false;
1040
1056
  const sessionState = this.stateStore.getState().sessions.find((s) => s.id === resolved);
@@ -1043,7 +1059,7 @@ var SipClient = class extends EventTargetEmitter {
1043
1059
  return true;
1044
1060
  }
1045
1061
  attendedTransferSession(otherSession, sessionId) {
1046
- const resolved = this.resolveSessionId(sessionId);
1062
+ const resolved = this.resolveExistingSessionId(sessionId);
1047
1063
  if (!resolved)
1048
1064
  return false;
1049
1065
  const sessionState = this.stateStore.getState().sessions.find((s) => s.id === resolved);
@@ -1052,21 +1068,31 @@ var SipClient = class extends EventTargetEmitter {
1052
1068
  return true;
1053
1069
  }
1054
1070
  setSessionMedia(sessionId, stream) {
1071
+ if (!this.sessionExists(sessionId))
1072
+ return;
1055
1073
  this.sessionManager.setSessionMedia(sessionId, stream);
1056
1074
  }
1057
1075
  switchCameraSession(sessionId, track) {
1076
+ if (!this.sessionExists(sessionId))
1077
+ return false;
1058
1078
  const rtc = this.sessionManager.getRtc(sessionId);
1059
1079
  return rtc ? rtc.switchCamera(track) : false;
1060
1080
  }
1061
1081
  startScreenShareSession(sessionId, getDisplayMedia) {
1082
+ if (!this.sessionExists(sessionId))
1083
+ return false;
1062
1084
  return this.sessionManager.startScreenShare(sessionId, getDisplayMedia);
1063
1085
  }
1064
1086
  enableVideoSession(sessionId) {
1087
+ if (!this.sessionExists(sessionId))
1088
+ return false;
1065
1089
  const rtc = this.sessionManager.getRtc(sessionId);
1066
1090
  rtc?.enableVideo();
1067
1091
  return !!rtc;
1068
1092
  }
1069
1093
  disableVideoSession(sessionId) {
1094
+ if (!this.sessionExists(sessionId))
1095
+ return false;
1070
1096
  const rtc = this.sessionManager.getRtc(sessionId);
1071
1097
  rtc?.disableVideo();
1072
1098
  return !!rtc;
@@ -1080,6 +1106,22 @@ var SipClient = class extends EventTargetEmitter {
1080
1106
  getSessions() {
1081
1107
  return this.sessionManager.getSessions();
1082
1108
  }
1109
+ attachBeforeUnload() {
1110
+ if (typeof window === "undefined" || this.unloadHandler)
1111
+ return;
1112
+ const handler = () => {
1113
+ this.hangupAll();
1114
+ this.disconnect();
1115
+ };
1116
+ window.addEventListener("beforeunload", handler);
1117
+ this.unloadHandler = handler;
1118
+ }
1119
+ detachBeforeUnload() {
1120
+ if (typeof window === "undefined" || !this.unloadHandler)
1121
+ return;
1122
+ window.removeEventListener("beforeunload", this.unloadHandler);
1123
+ this.unloadHandler = void 0;
1124
+ }
1083
1125
  };
1084
1126
  function createSipClientInstance(options) {
1085
1127
  return new SipClient(options);