@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.mjs CHANGED
@@ -691,7 +691,7 @@ var Call = class {
691
691
  // package.json
692
692
  var package_default = {
693
693
  name: "@koi-design/callkit",
694
- version: "2.2.0-beta.3",
694
+ version: "2.2.0-beta.4",
695
695
  description: "callkit",
696
696
  author: "koi",
697
697
  license: "ISC",
@@ -16675,12 +16675,152 @@ var Connect = class {
16675
16675
  }
16676
16676
  };
16677
16677
 
16678
+ // core/heartbeat-worker.ts
16679
+ var workerCode = `
16680
+ let timer = null;
16681
+ let interval = 30000;
16682
+
16683
+ self.onmessage = function(e) {
16684
+ const { type, interval: newInterval } = e.data;
16685
+
16686
+ if (type === 'start') {
16687
+ if (timer) {
16688
+ clearInterval(timer);
16689
+ }
16690
+ interval = newInterval || interval;
16691
+ timer = setInterval(() => {
16692
+ self.postMessage({ type: 'tick' });
16693
+ }, interval);
16694
+ }
16695
+
16696
+ if (type === 'stop') {
16697
+ if (timer) {
16698
+ clearInterval(timer);
16699
+ timer = null;
16700
+ }
16701
+ }
16702
+
16703
+ if (type === 'updateInterval') {
16704
+ interval = newInterval;
16705
+ if (timer) {
16706
+ clearInterval(timer);
16707
+ timer = setInterval(() => {
16708
+ self.postMessage({ type: 'tick' });
16709
+ }, interval);
16710
+ }
16711
+ }
16712
+ };
16713
+ `;
16714
+ function createHeartbeatWorker() {
16715
+ try {
16716
+ const blob = new Blob([workerCode], { type: "application/javascript" });
16717
+ const workerUrl = URL.createObjectURL(blob);
16718
+ const worker = new Worker(workerUrl);
16719
+ URL.revokeObjectURL(workerUrl);
16720
+ return worker;
16721
+ } catch {
16722
+ return null;
16723
+ }
16724
+ }
16725
+ var HeartbeatManager = class {
16726
+ worker = null;
16727
+ fallbackTimer = null;
16728
+ interval = 3e4;
16729
+ onTick = null;
16730
+ isRunning = false;
16731
+ constructor() {
16732
+ this.worker = createHeartbeatWorker();
16733
+ if (this.worker) {
16734
+ this.worker.onmessage = (e) => {
16735
+ if (e.data.type === "tick" && this.onTick) {
16736
+ this.onTick();
16737
+ }
16738
+ };
16739
+ }
16740
+ }
16741
+ /**
16742
+ * Start the heartbeat
16743
+ * @param interval - Interval in milliseconds
16744
+ * @param onTick - Callback function to execute on each tick
16745
+ */
16746
+ start(interval, onTick) {
16747
+ this.stop();
16748
+ this.interval = interval;
16749
+ this.onTick = onTick;
16750
+ this.isRunning = true;
16751
+ if (this.worker) {
16752
+ this.worker.postMessage({
16753
+ type: "start",
16754
+ interval
16755
+ });
16756
+ } else {
16757
+ this.fallbackTimer = setInterval(() => {
16758
+ if (this.onTick) {
16759
+ this.onTick();
16760
+ }
16761
+ }, interval);
16762
+ }
16763
+ }
16764
+ /**
16765
+ * Stop the heartbeat
16766
+ */
16767
+ stop() {
16768
+ this.isRunning = false;
16769
+ this.onTick = null;
16770
+ if (this.worker) {
16771
+ this.worker.postMessage({ type: "stop" });
16772
+ }
16773
+ if (this.fallbackTimer) {
16774
+ clearInterval(this.fallbackTimer);
16775
+ this.fallbackTimer = null;
16776
+ }
16777
+ }
16778
+ /**
16779
+ * Update the heartbeat interval
16780
+ * @param interval - New interval in milliseconds
16781
+ */
16782
+ updateInterval(interval) {
16783
+ this.interval = interval;
16784
+ if (!this.isRunning)
16785
+ return;
16786
+ if (this.worker) {
16787
+ this.worker.postMessage({
16788
+ type: "updateInterval",
16789
+ interval
16790
+ });
16791
+ } else if (this.fallbackTimer && this.onTick) {
16792
+ clearInterval(this.fallbackTimer);
16793
+ this.fallbackTimer = setInterval(() => {
16794
+ if (this.onTick) {
16795
+ this.onTick();
16796
+ }
16797
+ }, interval);
16798
+ }
16799
+ }
16800
+ /**
16801
+ * Destroy the heartbeat manager and release resources
16802
+ */
16803
+ destroy() {
16804
+ this.stop();
16805
+ if (this.worker) {
16806
+ this.worker.terminate();
16807
+ this.worker = null;
16808
+ }
16809
+ }
16810
+ /**
16811
+ * Check if using Web Worker
16812
+ */
16813
+ isUsingWorker() {
16814
+ return this.worker !== null;
16815
+ }
16816
+ };
16817
+
16678
16818
  // core/socket.ts
16679
16819
  var Socket = class {
16680
16820
  callKit;
16681
16821
  ws;
16682
16822
  lastPingTime = void 0;
16683
- pingTimer;
16823
+ heartbeatManager;
16684
16824
  /**
16685
16825
  * @description reconnect timer
16686
16826
  */
@@ -16713,6 +16853,7 @@ var Socket = class {
16713
16853
  }
16714
16854
  constructor(callKit) {
16715
16855
  this.callKit = callKit;
16856
+ this.heartbeatManager = new HeartbeatManager();
16716
16857
  }
16717
16858
  get reconnectConfig() {
16718
16859
  return this.callKit.config.getReconnectConfig("incall");
@@ -17074,22 +17215,27 @@ var Socket = class {
17074
17215
  }
17075
17216
  }
17076
17217
  checkPing() {
17077
- if (this.pingTimer) {
17078
- clearInterval(this.pingTimer);
17079
- }
17080
- this.pingTimer = setInterval(() => {
17218
+ this.heartbeatManager.start(this.pingInterval, () => {
17081
17219
  this.ping();
17082
- }, this.pingInterval);
17220
+ });
17221
+ this.callKit.logger.info(
17222
+ `Heartbeat started with Worker: ${this.heartbeatManager.isUsingWorker()}`,
17223
+ {
17224
+ caller: "Socket.checkPing",
17225
+ type: "INCALL",
17226
+ content: {
17227
+ pingInterval: this.pingInterval,
17228
+ usingWorker: this.heartbeatManager.isUsingWorker()
17229
+ }
17230
+ }
17231
+ );
17083
17232
  }
17084
17233
  /**
17085
17234
  * reset socket connection and all states
17086
17235
  */
17087
17236
  async reset(config) {
17088
17237
  const { force = false } = config || {};
17089
- if (this.pingTimer) {
17090
- clearInterval(this.pingTimer);
17091
- this.pingTimer = void 0;
17092
- }
17238
+ this.heartbeatManager.stop();
17093
17239
  if (force) {
17094
17240
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
17095
17241
  event: "INCALL_RESET"
@@ -17100,6 +17246,12 @@ var Socket = class {
17100
17246
  this.setConnectAuthState("startConfirm", false);
17101
17247
  this.clearWebSocket();
17102
17248
  }
17249
+ /**
17250
+ * Destroy the heartbeat manager
17251
+ */
17252
+ destroyHeartbeat() {
17253
+ this.heartbeatManager.destroy();
17254
+ }
17103
17255
  attemptReconnect() {
17104
17256
  if (this.reconnectTimer) {
17105
17257
  clearTimeout(this.reconnectTimer);