@vgroup/dialbox 0.0.81 → 0.0.83

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.
@@ -1387,30 +1387,107 @@ class TwilioService {
1387
1387
  this.isAvailableNumber = new BehaviorSubject(false);
1388
1388
  this.callerIdList = new BehaviorSubject([]);
1389
1389
  this.triggerSMSReload = new BehaviorSubject(false);
1390
- this.initializeTwilioDevice();
1390
+ this.deviceInitialized = false;
1391
+ this.initializationInProgress = false;
1392
+ // Initialize immediately if token exists
1393
+ this.token = localStorage.getItem('ext_token');
1394
+ if (this.token) {
1395
+ this.initializeTwilioDevice();
1396
+ }
1397
+ // Set up a polling mechanism to check for token availability
1398
+ this.startTokenCheck();
1399
+ }
1400
+ startTokenCheck() {
1401
+ // Check every 2 seconds if we have a token
1402
+ this.tokenCheckInterval = setInterval(() => {
1403
+ const currentToken = localStorage.getItem('ext_token');
1404
+ if (currentToken && currentToken !== this.token) {
1405
+ this.token = currentToken;
1406
+ this.initializeTwilioDevice();
1407
+ }
1408
+ }, 2000);
1409
+ }
1410
+ ngOnDestroy() {
1411
+ if (this.tokenCheckInterval) {
1412
+ clearInterval(this.tokenCheckInterval);
1413
+ }
1414
+ if (this.device) {
1415
+ this.device.destroy();
1416
+ }
1391
1417
  }
1392
1418
  initializeTwilioDevice() {
1393
- if (this.token) {
1394
- this.extensionService.getIncomingCallToken().subscribe((data) => {
1395
- this.incomingCallToken = data.token;
1396
- localStorage.setItem('in-token', data.token);
1397
- this.device = new Device(this.incomingCallToken, {
1398
- allowIncomingWhileBusy: true,
1399
- // Add any other necessary options
1400
- });
1401
- this.device.register();
1402
- this.device.on('incoming', (call) => {
1403
- this.currentCall.next(call);
1404
- this.callType.next('INCOMING');
1405
- this.currentCallState.next('incoming');
1406
- this.notificationSerivce.showNotification(call);
1407
- });
1408
- this.device.on('error', (error) => {
1409
- console.error('Twilio Device Error:', error);
1410
- // Add error handling and reconnection logic
1411
- });
1412
- });
1419
+ // Prevent multiple initializations
1420
+ if (this.deviceInitialized || this.initializationInProgress || !this.token) {
1421
+ return;
1413
1422
  }
1423
+ this.initializationInProgress = true;
1424
+ this.extensionService.getIncomingCallToken().subscribe({
1425
+ next: (data) => {
1426
+ try {
1427
+ this.incomingCallToken = data.token;
1428
+ localStorage.setItem('in-token', data.token);
1429
+ // Destroy existing device if it exists
1430
+ if (this.device) {
1431
+ this.device.destroy();
1432
+ }
1433
+ // Initialize Twilio Device with required options
1434
+ const deviceOptions = {
1435
+ allowIncomingWhileBusy: true,
1436
+ closeProtection: true
1437
+ };
1438
+ // Only add debug in development
1439
+ if (!environment.production) {
1440
+ deviceOptions.debug = false;
1441
+ }
1442
+ this.device = new Device(this.incomingCallToken, deviceOptions);
1443
+ this.device.register();
1444
+ this.deviceInitialized = true;
1445
+ this.initializationInProgress = false;
1446
+ console.log('Twilio Device initialized successfully');
1447
+ this.setupDeviceEventHandlers();
1448
+ }
1449
+ catch (error) {
1450
+ console.error('Error initializing Twilio Device:', error);
1451
+ this.initializationInProgress = false;
1452
+ // Retry after a delay
1453
+ setTimeout(() => this.initializeTwilioDevice(), 5000);
1454
+ }
1455
+ },
1456
+ error: (error) => {
1457
+ console.error('Error getting Twilio token:', error);
1458
+ this.initializationInProgress = false;
1459
+ // Retry after a delay
1460
+ setTimeout(() => this.initializeTwilioDevice(), 5000);
1461
+ }
1462
+ });
1463
+ }
1464
+ setupDeviceEventHandlers() {
1465
+ if (!this.device)
1466
+ return;
1467
+ this.device.on('incoming', (call) => {
1468
+ console.log('Incoming call received:', call.parameters);
1469
+ this.currentCall.next(call);
1470
+ this.callType.next('INCOMING');
1471
+ this.currentCallState.next('incoming');
1472
+ this.notificationSerivce.showNotification(call);
1473
+ });
1474
+ this.device.on('error', (error) => {
1475
+ console.error('Twilio Device Error:', error);
1476
+ this.deviceInitialized = false;
1477
+ // Attempt to reinitialize on error
1478
+ if (error.code === 31201 || error.code === 20101) {
1479
+ console.log('Authentication error, reinitializing device...');
1480
+ this.initializeTwilioDevice();
1481
+ }
1482
+ });
1483
+ this.device.on('registered', () => {
1484
+ console.log('Twilio Device registered');
1485
+ this.deviceInitialized = true;
1486
+ });
1487
+ this.device.on('unregistered', () => {
1488
+ console.log('Twilio Device unregistered');
1489
+ this.deviceInitialized = false;
1490
+ });
1414
1491
  }
1415
1492
  // onIncomingCall(){
1416
1493
  // this.device.on('incoming', (call:any) => {
@@ -2206,7 +2283,28 @@ class DialboxComponent {
2206
2283
  return;
2207
2284
  this.token = localStorage.getItem('ext_token') || '';
2208
2285
  if (!this.token) {
2209
- console.error('No authentication token found');
2286
+ console.warn('No authentication token found. Will retry initializing Twilio when token is available.');
2287
+ // Clear any existing interval to prevent multiple intervals
2288
+ if (this.twilioInitializationInterval) {
2289
+ clearInterval(this.twilioInitializationInterval);
2290
+ }
2291
+ // Set up a check for when token becomes available
2292
+ this.twilioInitializationInterval = setInterval(() => {
2293
+ const newToken = localStorage.getItem('ext_token');
2294
+ if (newToken) {
2295
+ clearInterval(this.twilioInitializationInterval);
2296
+ this.token = newToken;
2297
+ this.initializeTwilio();
2298
+ }
2299
+ }, 2000);
2300
+ // Add cleanup for this interval
2301
+ this.subscriptions.add({
2302
+ unsubscribe: () => {
2303
+ if (this.twilioInitializationInterval) {
2304
+ clearInterval(this.twilioInitializationInterval);
2305
+ }
2306
+ }
2307
+ });
2210
2308
  return;
2211
2309
  }
2212
2310
  this.isInitialized = true;
@@ -2325,8 +2423,10 @@ class DialboxComponent {
2325
2423
  // }
2326
2424
  ngOnInit() {
2327
2425
  try {
2426
+ // Initialize Twilio when component loads
2427
+ this.initializeTwilio();
2428
+ // Get token and load initial data
2328
2429
  this.token = localStorage.getItem('ext_token') || '';
2329
- //this.isCallInProgress = true;
2330
2430
  this.getContactList();
2331
2431
  this.getUserCallSetting();
2332
2432
  const sub1 = this.twilioService.dialNumberFromOtherModule.subscribe((contact) => {
@@ -3185,6 +3285,13 @@ class DialboxComponent {
3185
3285
  if (this.subscriptions) {
3186
3286
  this.subscriptions.unsubscribe();
3187
3287
  }
3288
+ // Clear any active intervals
3289
+ if (this.tokenCheckInterval) {
3290
+ clearInterval(this.tokenCheckInterval);
3291
+ }
3292
+ if (this.twilioInitializationInterval) {
3293
+ clearInterval(this.twilioInitializationInterval);
3294
+ }
3188
3295
  // Clean up Twilio device when component is destroyed
3189
3296
  if (this.twilioService['device']) {
3190
3297
  this.twilioService['device'].destroy();