@vgroup/dialbox 0.0.53 → 0.0.54

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.
@@ -1317,25 +1317,54 @@ class TwilioService {
1317
1317
  this.triggerSMSReload = new BehaviorSubject(false);
1318
1318
  this.initializeTwilioDevice();
1319
1319
  }
1320
+ setupDeviceListeners() {
1321
+ if (!this.device)
1322
+ return;
1323
+ // Remove any existing listeners first
1324
+ this.device.off('incoming');
1325
+ this.device.off('error');
1326
+ this.device.off('registered');
1327
+ this.device.off('unregistered');
1328
+ // Set up new listeners
1329
+ this.device.on('registered', () => {
1330
+ console.log('Twilio Device registered');
1331
+ });
1332
+ this.device.on('incoming', (call) => {
1333
+ console.log('Incoming call received');
1334
+ this.currentCall.next(call);
1335
+ this.callType.next('INCOMING');
1336
+ this.currentCallState.next('incoming');
1337
+ });
1338
+ this.device.on('error', (error) => {
1339
+ console.error('Twilio Device Error:', error);
1340
+ });
1341
+ }
1320
1342
  initializeTwilioDevice() {
1343
+ if (this.device) {
1344
+ // If device already exists, just re-register
1345
+ this.device.register();
1346
+ return;
1347
+ }
1321
1348
  if (this.token) {
1322
- this.extensionService.getIncomingCallToken().subscribe((data) => {
1323
- this.incomingCallToken = data.token;
1324
- localStorage.setItem('in-token', data.token);
1325
- this.device = new Device(this.incomingCallToken, {
1326
- allowIncomingWhileBusy: true,
1327
- // Add any other necessary options
1328
- });
1329
- this.device.register();
1330
- this.device.on('incoming', (call) => {
1331
- this.currentCall.next(call);
1332
- this.callType.next('INCOMING');
1333
- this.currentCallState.next('incoming');
1334
- });
1335
- this.device.on('error', (error) => {
1336
- console.error('Twilio Device Error:', error);
1337
- // Add error handling and reconnection logic
1338
- });
1349
+ this.extensionService.getIncomingCallToken().subscribe({
1350
+ next: (data) => {
1351
+ this.incomingCallToken = data.token;
1352
+ localStorage.setItem('in-token', data.token);
1353
+ // Destroy existing device if any
1354
+ if (this.device) {
1355
+ this.device.destroy();
1356
+ }
1357
+ this.device = new Device(this.incomingCallToken, {
1358
+ allowIncomingWhileBusy: true,
1359
+ closeProtection: true
1360
+ });
1361
+ this.setupDeviceListeners();
1362
+ this.device.register();
1363
+ },
1364
+ error: (error) => {
1365
+ console.error('Error getting Twilio token:', error);
1366
+ // Add retry logic here if needed
1367
+ }
1339
1368
  });
1340
1369
  }
1341
1370
  }
@@ -2336,7 +2365,16 @@ class DialboxComponent {
2336
2365
  if (this.isTwilioInitialized || !this.token)
2337
2366
  return;
2338
2367
  this.isTwilioInitialized = true;
2368
+ // Clear any existing device first
2369
+ if (this.twilioService.device) {
2370
+ this.twilioService.device.destroy();
2371
+ this.twilioService.device = null;
2372
+ }
2373
+ // Initialize new device
2339
2374
  this.twilioService.initializeTwilioDevice();
2375
+ // Reset the current call state
2376
+ this.twilioService.currentCall.next(null);
2377
+ this.twilioService.currentCallState.next('none');
2340
2378
  }
2341
2379
  handleIncomingCall(call) {
2342
2380
  try {
@@ -3084,19 +3122,22 @@ class DialboxComponent {
3084
3122
  }
3085
3123
  ngOnDestroy() {
3086
3124
  try {
3087
- console.log('Cleaning up C2cDialpadComponent');
3088
- // Unsubscribe from all subscriptions
3089
- if (this.subscriptions) {
3090
- this.subscriptions.unsubscribe();
3091
- }
3092
- // End any active call
3093
- if (this.isCallInProgress) {
3094
- this.endCall();
3095
- }
3096
3125
  // Clear any timeouts or intervals if they exist
3097
3126
  if (this.toastTimeout) {
3098
3127
  clearTimeout(this.toastTimeout);
3099
3128
  }
3129
+ // Unsubscribe from all subscriptions
3130
+ this.subscriptions.unsubscribe();
3131
+ // Clean up Twilio device if it exists
3132
+ if (this.twilioService.device) {
3133
+ try {
3134
+ this.twilioService.device.destroy();
3135
+ this.twilioService.device = null;
3136
+ }
3137
+ catch (error) {
3138
+ console.error('Error cleaning up Twilio device:', error);
3139
+ }
3140
+ }
3100
3141
  console.log('C2cDialpadComponent cleanup complete');
3101
3142
  }
3102
3143
  catch (error) {