@vgroup/dialbox 0.0.49 → 0.0.50

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.
@@ -1315,29 +1315,89 @@ class TwilioService {
1315
1315
  this.isAvailableNumber = new BehaviorSubject(false);
1316
1316
  this.callerIdList = new BehaviorSubject([]);
1317
1317
  this.triggerSMSReload = new BehaviorSubject(false);
1318
- this.initializeTwilioDevice();
1318
+ this.isInitialized = false;
1319
+ // Initialize when token is available
1320
+ if (this.token) {
1321
+ this.initializeTwilioDevice();
1322
+ }
1323
+ else {
1324
+ // If token is not available, listen for it
1325
+ const tokenCheck = setInterval(() => {
1326
+ const token = localStorage.getItem('ext_token');
1327
+ if (token) {
1328
+ this.token = token;
1329
+ this.initializeTwilioDevice();
1330
+ clearInterval(tokenCheck);
1331
+ }
1332
+ }, 1000);
1333
+ }
1319
1334
  }
1320
1335
  initializeTwilioDevice() {
1321
- 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
- });
1339
- });
1336
+ if (this.isInitialized || !this.token) {
1337
+ return;
1340
1338
  }
1339
+ console.log('Initializing Twilio Device...');
1340
+ this.extensionService.getIncomingCallToken().subscribe({
1341
+ next: (data) => {
1342
+ if (!data?.token) {
1343
+ console.error('No token received from server');
1344
+ return;
1345
+ }
1346
+ try {
1347
+ console.log('Setting up Twilio Device with token');
1348
+ this.incomingCallToken = data.token;
1349
+ localStorage.setItem('in-token', data.token);
1350
+ // Destroy existing device if any
1351
+ if (this.device) {
1352
+ this.device.destroy();
1353
+ }
1354
+ // Create new device instance
1355
+ this.device = new Device(data.token, {
1356
+ allowIncomingWhileBusy: true,
1357
+ closeProtection: true
1358
+ });
1359
+ // Set up event listeners
1360
+ this.setupDeviceListeners();
1361
+ // Register the device
1362
+ this.device.register();
1363
+ this.isInitialized = true;
1364
+ }
1365
+ catch (error) {
1366
+ console.error('Error initializing Twilio Device:', error);
1367
+ this.isInitialized = false;
1368
+ }
1369
+ },
1370
+ error: (error) => {
1371
+ console.error('Error getting Twilio token:', error);
1372
+ // Retry after delay
1373
+ setTimeout(() => this.initializeTwilioDevice(), 5000);
1374
+ }
1375
+ });
1376
+ }
1377
+ setupDeviceListeners() {
1378
+ if (!this.device)
1379
+ return;
1380
+ this.device.on('registered', () => {
1381
+ console.log('Twilio Device registered successfully');
1382
+ });
1383
+ this.device.on('incoming', (call) => {
1384
+ console.log('Incoming call received');
1385
+ this.currentCall.next(call);
1386
+ this.callType.next('INCOMING');
1387
+ this.currentCallState.next('incoming');
1388
+ });
1389
+ this.device.on('error', (error) => {
1390
+ console.error('Twilio Device Error:', error);
1391
+ this.isInitialized = false;
1392
+ // Attempt to reinitialize after delay
1393
+ setTimeout(() => this.initializeTwilioDevice(), 5000);
1394
+ });
1395
+ this.device.on('unregistered', () => {
1396
+ console.log('Twilio Device unregistered');
1397
+ this.isInitialized = false;
1398
+ // Attempt to re-register
1399
+ setTimeout(() => this.initializeTwilioDevice(), 5000);
1400
+ });
1341
1401
  }
1342
1402
  // onIncomingCall(){
1343
1403
  // this.device.on('incoming', (call:any) => {