@koi-design/callkit 2.2.0-beta.3 → 2.2.0-beta.4

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.js CHANGED
@@ -718,7 +718,7 @@ var Call = class {
718
718
  // package.json
719
719
  var package_default = {
720
720
  name: "@koi-design/callkit",
721
- version: "2.2.0-beta.3",
721
+ version: "2.2.0-beta.4",
722
722
  description: "callkit",
723
723
  author: "koi",
724
724
  license: "ISC",
@@ -16702,12 +16702,152 @@ var Connect = class {
16702
16702
  }
16703
16703
  };
16704
16704
 
16705
+ // core/heartbeat-worker.ts
16706
+ var workerCode = `
16707
+ let timer = null;
16708
+ let interval = 30000;
16709
+
16710
+ self.onmessage = function(e) {
16711
+ const { type, interval: newInterval } = e.data;
16712
+
16713
+ if (type === 'start') {
16714
+ if (timer) {
16715
+ clearInterval(timer);
16716
+ }
16717
+ interval = newInterval || interval;
16718
+ timer = setInterval(() => {
16719
+ self.postMessage({ type: 'tick' });
16720
+ }, interval);
16721
+ }
16722
+
16723
+ if (type === 'stop') {
16724
+ if (timer) {
16725
+ clearInterval(timer);
16726
+ timer = null;
16727
+ }
16728
+ }
16729
+
16730
+ if (type === 'updateInterval') {
16731
+ interval = newInterval;
16732
+ if (timer) {
16733
+ clearInterval(timer);
16734
+ timer = setInterval(() => {
16735
+ self.postMessage({ type: 'tick' });
16736
+ }, interval);
16737
+ }
16738
+ }
16739
+ };
16740
+ `;
16741
+ function createHeartbeatWorker() {
16742
+ try {
16743
+ const blob = new Blob([workerCode], { type: "application/javascript" });
16744
+ const workerUrl = URL.createObjectURL(blob);
16745
+ const worker = new Worker(workerUrl);
16746
+ URL.revokeObjectURL(workerUrl);
16747
+ return worker;
16748
+ } catch {
16749
+ return null;
16750
+ }
16751
+ }
16752
+ var HeartbeatManager = class {
16753
+ worker = null;
16754
+ fallbackTimer = null;
16755
+ interval = 3e4;
16756
+ onTick = null;
16757
+ isRunning = false;
16758
+ constructor() {
16759
+ this.worker = createHeartbeatWorker();
16760
+ if (this.worker) {
16761
+ this.worker.onmessage = (e) => {
16762
+ if (e.data.type === "tick" && this.onTick) {
16763
+ this.onTick();
16764
+ }
16765
+ };
16766
+ }
16767
+ }
16768
+ /**
16769
+ * Start the heartbeat
16770
+ * @param interval - Interval in milliseconds
16771
+ * @param onTick - Callback function to execute on each tick
16772
+ */
16773
+ start(interval, onTick) {
16774
+ this.stop();
16775
+ this.interval = interval;
16776
+ this.onTick = onTick;
16777
+ this.isRunning = true;
16778
+ if (this.worker) {
16779
+ this.worker.postMessage({
16780
+ type: "start",
16781
+ interval
16782
+ });
16783
+ } else {
16784
+ this.fallbackTimer = setInterval(() => {
16785
+ if (this.onTick) {
16786
+ this.onTick();
16787
+ }
16788
+ }, interval);
16789
+ }
16790
+ }
16791
+ /**
16792
+ * Stop the heartbeat
16793
+ */
16794
+ stop() {
16795
+ this.isRunning = false;
16796
+ this.onTick = null;
16797
+ if (this.worker) {
16798
+ this.worker.postMessage({ type: "stop" });
16799
+ }
16800
+ if (this.fallbackTimer) {
16801
+ clearInterval(this.fallbackTimer);
16802
+ this.fallbackTimer = null;
16803
+ }
16804
+ }
16805
+ /**
16806
+ * Update the heartbeat interval
16807
+ * @param interval - New interval in milliseconds
16808
+ */
16809
+ updateInterval(interval) {
16810
+ this.interval = interval;
16811
+ if (!this.isRunning)
16812
+ return;
16813
+ if (this.worker) {
16814
+ this.worker.postMessage({
16815
+ type: "updateInterval",
16816
+ interval
16817
+ });
16818
+ } else if (this.fallbackTimer && this.onTick) {
16819
+ clearInterval(this.fallbackTimer);
16820
+ this.fallbackTimer = setInterval(() => {
16821
+ if (this.onTick) {
16822
+ this.onTick();
16823
+ }
16824
+ }, interval);
16825
+ }
16826
+ }
16827
+ /**
16828
+ * Destroy the heartbeat manager and release resources
16829
+ */
16830
+ destroy() {
16831
+ this.stop();
16832
+ if (this.worker) {
16833
+ this.worker.terminate();
16834
+ this.worker = null;
16835
+ }
16836
+ }
16837
+ /**
16838
+ * Check if using Web Worker
16839
+ */
16840
+ isUsingWorker() {
16841
+ return this.worker !== null;
16842
+ }
16843
+ };
16844
+
16705
16845
  // core/socket.ts
16706
16846
  var Socket = class {
16707
16847
  callKit;
16708
16848
  ws;
16709
16849
  lastPingTime = void 0;
16710
- pingTimer;
16850
+ heartbeatManager;
16711
16851
  /**
16712
16852
  * @description reconnect timer
16713
16853
  */
@@ -16740,6 +16880,7 @@ var Socket = class {
16740
16880
  }
16741
16881
  constructor(callKit) {
16742
16882
  this.callKit = callKit;
16883
+ this.heartbeatManager = new HeartbeatManager();
16743
16884
  }
16744
16885
  get reconnectConfig() {
16745
16886
  return this.callKit.config.getReconnectConfig("incall");
@@ -17101,22 +17242,27 @@ var Socket = class {
17101
17242
  }
17102
17243
  }
17103
17244
  checkPing() {
17104
- if (this.pingTimer) {
17105
- clearInterval(this.pingTimer);
17106
- }
17107
- this.pingTimer = setInterval(() => {
17245
+ this.heartbeatManager.start(this.pingInterval, () => {
17108
17246
  this.ping();
17109
- }, this.pingInterval);
17247
+ });
17248
+ this.callKit.logger.info(
17249
+ `Heartbeat started with Worker: ${this.heartbeatManager.isUsingWorker()}`,
17250
+ {
17251
+ caller: "Socket.checkPing",
17252
+ type: "INCALL",
17253
+ content: {
17254
+ pingInterval: this.pingInterval,
17255
+ usingWorker: this.heartbeatManager.isUsingWorker()
17256
+ }
17257
+ }
17258
+ );
17110
17259
  }
17111
17260
  /**
17112
17261
  * reset socket connection and all states
17113
17262
  */
17114
17263
  async reset(config) {
17115
17264
  const { force = false } = config || {};
17116
- if (this.pingTimer) {
17117
- clearInterval(this.pingTimer);
17118
- this.pingTimer = void 0;
17119
- }
17265
+ this.heartbeatManager.stop();
17120
17266
  if (force) {
17121
17267
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
17122
17268
  event: "INCALL_RESET"
@@ -17127,6 +17273,12 @@ var Socket = class {
17127
17273
  this.setConnectAuthState("startConfirm", false);
17128
17274
  this.clearWebSocket();
17129
17275
  }
17276
+ /**
17277
+ * Destroy the heartbeat manager
17278
+ */
17279
+ destroyHeartbeat() {
17280
+ this.heartbeatManager.destroy();
17281
+ }
17130
17282
  attemptReconnect() {
17131
17283
  if (this.reconnectTimer) {
17132
17284
  clearTimeout(this.reconnectTimer);