@vgroup/dialbox 0.0.48 → 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,49 +1315,56 @@ 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
- console.log('Initializing Twilio Device...');
1322
- console.log('Token exists:', !!this.token);
1323
- if (!this.token) {
1324
- console.error('No token available for Twilio initialization');
1336
+ if (this.isInitialized || !this.token) {
1325
1337
  return;
1326
1338
  }
1339
+ console.log('Initializing Twilio Device...');
1327
1340
  this.extensionService.getIncomingCallToken().subscribe({
1328
1341
  next: (data) => {
1329
- console.log('Received Twilio token');
1330
- if (!data || !data.token) {
1331
- console.error('Invalid token received from server');
1342
+ if (!data?.token) {
1343
+ console.error('No token received from server');
1332
1344
  return;
1333
1345
  }
1334
- this.incomingCallToken = data.token;
1335
- localStorage.setItem('in-token', data.token);
1336
1346
  try {
1337
- console.log('Creating Twilio Device instance with token:', data.token.substring(0, 10) + '...');
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
1338
1355
  this.device = new Device(data.token, {
1339
1356
  allowIncomingWhileBusy: true,
1340
1357
  closeProtection: true
1341
1358
  });
1342
- console.log('Registering device...');
1359
+ // Set up event listeners
1360
+ this.setupDeviceListeners();
1361
+ // Register the device
1343
1362
  this.device.register();
1344
- this.device.on('registered', () => {
1345
- console.log('Twilio Device registered successfully');
1346
- });
1347
- this.device.on('incoming', (call) => {
1348
- console.log('Incoming call received');
1349
- this.currentCall.next(call);
1350
- this.callType.next('INCOMING');
1351
- this.currentCallState.next('incoming');
1352
- });
1353
- this.device.on('error', (error) => {
1354
- console.error('Twilio Device Error:', error);
1355
- // Attempt to reinitialize after delay
1356
- setTimeout(() => this.initializeTwilioDevice(), 5000);
1357
- });
1363
+ this.isInitialized = true;
1358
1364
  }
1359
1365
  catch (error) {
1360
1366
  console.error('Error initializing Twilio Device:', error);
1367
+ this.isInitialized = false;
1361
1368
  }
1362
1369
  },
1363
1370
  error: (error) => {
@@ -1367,6 +1374,31 @@ class TwilioService {
1367
1374
  }
1368
1375
  });
1369
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
+ });
1401
+ }
1370
1402
  // onIncomingCall(){
1371
1403
  // this.device.on('incoming', (call:any) => {
1372
1404
  // console.log(call);
@@ -1573,6 +1605,7 @@ class IncomingCallComponent {
1573
1605
  if (call) {
1574
1606
  this.twilioCallData = call;
1575
1607
  this.twilioAuthId = call.customParameters.get('twilioAuthId');
1608
+ this.newIncomingCallsList.push(call);
1576
1609
  if (!call.parameters) {
1577
1610
  call.parameters = {};
1578
1611
  }