@vgroup/dialbox 0.0.83 → 0.0.84

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.
@@ -2,11 +2,11 @@ import * as i0 from '@angular/core';
2
2
  import { Injectable, EventEmitter, Component, Input, Output, ViewChild, Inject, NgModule } from '@angular/core';
3
3
  import swal from 'sweetalert2';
4
4
  import { AsYouType } from 'libphonenumber-js';
5
- import { throwError, BehaviorSubject, interval, Subscription } from 'rxjs';
5
+ import { throwError, BehaviorSubject, of, interval, Subscription } from 'rxjs';
6
6
  import * as i1 from '@angular/common/http';
7
7
  import { HttpHeaders, HttpParams, HttpClientModule } from '@angular/common/http';
8
+ import { catchError, switchMap, map, tap, shareReplay } from 'rxjs/operators';
8
9
  import { Device } from '@twilio/voice-sdk';
9
- import { catchError, switchMap, map } from 'rxjs/operators';
10
10
  import * as i3$1 from '@angular/material/dialog';
11
11
  import { MAT_DIALOG_DATA } from '@angular/material/dialog';
12
12
  import * as i5 from '@angular/router';
@@ -1387,85 +1387,48 @@ class TwilioService {
1387
1387
  this.isAvailableNumber = new BehaviorSubject(false);
1388
1388
  this.callerIdList = new BehaviorSubject([]);
1389
1389
  this.triggerSMSReload = new BehaviorSubject(false);
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);
1390
+ this.tokenInitialized = false;
1391
+ this.tokenInitialization$ = null;
1392
+ this.initializeToken();
1393
+ }
1394
+ initializeToken() {
1395
+ if (this.tokenInitialized) {
1396
+ return this.tokenInitialization$ || of(null);
1397
+ }
1398
+ this.tokenInitialization$ = this.extensionService.getIncomingCallToken().pipe(tap((data) => {
1399
+ this.incomingCallToken = data.token;
1400
+ localStorage.setItem('in-token', data.token);
1401
+ this.tokenInitialized = true;
1402
+ this.initializeDevice();
1403
+ }), catchError(error => {
1404
+ console.error('Error initializing Twilio token:', error);
1405
+ return throwError(() => error);
1406
+ }), shareReplay(1) // Cache the result for subsequent subscribers
1407
+ );
1408
+ return this.tokenInitialization$;
1409
1409
  }
1410
- ngOnDestroy() {
1411
- if (this.tokenCheckInterval) {
1412
- clearInterval(this.tokenCheckInterval);
1410
+ initializeTwilioDevice() {
1411
+ if (this.tokenInitialized && this.incomingCallToken) {
1412
+ this.initializeDevice();
1413
+ }
1414
+ else {
1415
+ this.initializeToken().subscribe();
1413
1416
  }
1417
+ }
1418
+ initializeDevice() {
1414
1419
  if (this.device) {
1415
1420
  this.device.destroy();
1416
1421
  }
1417
- }
1418
- initializeTwilioDevice() {
1419
- // Prevent multiple initializations
1420
- if (this.deviceInitialized || this.initializationInProgress || !this.token) {
1422
+ if (!this.incomingCallToken) {
1423
+ console.error('No Twilio token available');
1421
1424
  return;
1422
1425
  }
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
- }
1426
+ this.device = new Device(this.incomingCallToken, {
1427
+ allowIncomingWhileBusy: true,
1428
+ closeProtection: true
1462
1429
  });
1463
- }
1464
- setupDeviceEventHandlers() {
1465
- if (!this.device)
1466
- return;
1430
+ this.device.register();
1467
1431
  this.device.on('incoming', (call) => {
1468
- console.log('Incoming call received:', call.parameters);
1469
1432
  this.currentCall.next(call);
1470
1433
  this.callType.next('INCOMING');
1471
1434
  this.currentCallState.next('incoming');
@@ -1473,20 +1436,15 @@ class TwilioService {
1473
1436
  });
1474
1437
  this.device.on('error', (error) => {
1475
1438
  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
- }
1439
+ // Reset initialization state on error to allow retry
1440
+ this.tokenInitialized = false;
1482
1441
  });
1483
1442
  this.device.on('registered', () => {
1484
- console.log('Twilio Device registered');
1485
- this.deviceInitialized = true;
1443
+ console.log('Twilio Device registered successfully');
1486
1444
  });
1487
1445
  this.device.on('unregistered', () => {
1488
1446
  console.log('Twilio Device unregistered');
1489
- this.deviceInitialized = false;
1447
+ this.tokenInitialized = false;
1490
1448
  });
1491
1449
  }
1492
1450
  // onIncomingCall(){
@@ -2283,28 +2241,7 @@ class DialboxComponent {
2283
2241
  return;
2284
2242
  this.token = localStorage.getItem('ext_token') || '';
2285
2243
  if (!this.token) {
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
- });
2244
+ console.error('No authentication token found');
2308
2245
  return;
2309
2246
  }
2310
2247
  this.isInitialized = true;
@@ -2423,10 +2360,8 @@ class DialboxComponent {
2423
2360
  // }
2424
2361
  ngOnInit() {
2425
2362
  try {
2426
- // Initialize Twilio when component loads
2427
- this.initializeTwilio();
2428
- // Get token and load initial data
2429
2363
  this.token = localStorage.getItem('ext_token') || '';
2364
+ //this.isCallInProgress = true;
2430
2365
  this.getContactList();
2431
2366
  this.getUserCallSetting();
2432
2367
  const sub1 = this.twilioService.dialNumberFromOtherModule.subscribe((contact) => {
@@ -3285,13 +3220,6 @@ class DialboxComponent {
3285
3220
  if (this.subscriptions) {
3286
3221
  this.subscriptions.unsubscribe();
3287
3222
  }
3288
- // Clear any active intervals
3289
- if (this.tokenCheckInterval) {
3290
- clearInterval(this.tokenCheckInterval);
3291
- }
3292
- if (this.twilioInitializationInterval) {
3293
- clearInterval(this.twilioInitializationInterval);
3294
- }
3295
3223
  // Clean up Twilio device when component is destroyed
3296
3224
  if (this.twilioService['device']) {
3297
3225
  this.twilioService['device'].destroy();