@vgroup/dialbox 0.1.24 → 0.1.26

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.
@@ -1389,6 +1389,8 @@ class TwilioService {
1389
1389
  this.triggerSMSReload = new BehaviorSubject(false);
1390
1390
  this.tokenInitialized = false;
1391
1391
  this.tokenInitialization$ = null;
1392
+ this.devicePromise = null;
1393
+ this.processedCallSids = new Set();
1392
1394
  this.initializeToken();
1393
1395
  }
1394
1396
  initializeToken() {
@@ -1409,45 +1411,73 @@ class TwilioService {
1409
1411
  }
1410
1412
  initializeTwilioDevice() {
1411
1413
  if (this.tokenInitialized && this.incomingCallToken) {
1412
- this.initializeDevice();
1414
+ this.initializeDevice().catch(err => console.error('Device initialization failed', err));
1413
1415
  }
1414
1416
  else {
1415
1417
  this.initializeToken().subscribe();
1416
1418
  }
1417
1419
  }
1418
1420
  initializeDevice() {
1421
+ if (this.devicePromise) {
1422
+ return this.devicePromise;
1423
+ }
1424
+ const token = this.incomingCallToken;
1425
+ if (!token) {
1426
+ return Promise.reject('No Twilio token available');
1427
+ }
1428
+ // Destroy any existing device before creating a new one
1419
1429
  if (this.device) {
1420
1430
  this.device.destroy();
1421
1431
  }
1422
- if (!this.incomingCallToken) {
1423
- console.error('No Twilio token available');
1424
- return;
1425
- }
1426
- this.device = new Device(this.incomingCallToken, {
1427
- allowIncomingWhileBusy: true,
1428
- closeProtection: true
1429
- });
1430
- console.log("device created");
1431
- this.device.register();
1432
- this.device.on('incoming', (call) => {
1433
- console.log("incoming call", call);
1434
- this.currentCall.next(call);
1435
- this.callType.next('INCOMING');
1436
- this.currentCallState.next('incoming');
1437
- this.notificationSerivce.showNotification(call);
1438
- });
1439
- this.device.on('error', (error) => {
1440
- console.error('Twilio Device Error:', error);
1441
- // Reset initialization state on error to allow retry
1442
- this.tokenInitialized = false;
1443
- });
1444
- this.device.on('registered', () => {
1445
- console.log('Twilio Device registered successfully');
1446
- });
1447
- this.device.on('unregistered', () => {
1448
- console.log('Twilio Device unregistered');
1449
- this.tokenInitialized = false;
1432
+ this.devicePromise = new Promise((resolve, reject) => {
1433
+ const device = new Device(token, {
1434
+ allowIncomingWhileBusy: true,
1435
+ closeProtection: true,
1436
+ });
1437
+ const cleanup = () => {
1438
+ device.off('registered', onRegistered);
1439
+ device.off('error', onError);
1440
+ device.off('incoming', onIncoming);
1441
+ };
1442
+ const onRegistered = () => {
1443
+ console.log('Twilio Device registered');
1444
+ this.device = device;
1445
+ cleanup();
1446
+ resolve(device);
1447
+ };
1448
+ const onError = (error) => {
1449
+ console.error('Twilio Device Error:', error);
1450
+ this.devicePromise = null; // Allow retry
1451
+ cleanup();
1452
+ reject(error);
1453
+ };
1454
+ const onIncoming = (call) => {
1455
+ const callSid = call.parameters['CallSid'];
1456
+ if (callSid && !this.processedCallSids.has(callSid)) {
1457
+ this.processedCallSids.add(callSid);
1458
+ console.log('New incoming call:', callSid);
1459
+ this.currentCall.next(call);
1460
+ this.callType.next('INCOMING');
1461
+ this.currentCallState.next('incoming');
1462
+ this.notificationSerivce.showNotification(call);
1463
+ const callEndHandler = () => {
1464
+ this.processedCallSids.delete(callSid);
1465
+ call.off('disconnect', callEndHandler);
1466
+ call.off('cancel', callEndHandler);
1467
+ };
1468
+ call.on('disconnect', callEndHandler);
1469
+ call.on('cancel', callEndHandler);
1470
+ }
1471
+ else {
1472
+ console.log('Duplicate incoming call event ignored for CallSid:', callSid);
1473
+ }
1474
+ };
1475
+ device.on('registered', onRegistered);
1476
+ device.on('error', onError);
1477
+ device.on('incoming', onIncoming);
1478
+ device.register();
1450
1479
  });
1480
+ return this.devicePromise;
1451
1481
  }
1452
1482
  // onIncomingCall(){
1453
1483
  // this.device.on('incoming', (call:any) => {
@@ -1575,29 +1605,7 @@ class IncomingCallComponent {
1575
1605
  try {
1576
1606
  this.twilioService.currentCall.subscribe(call => {
1577
1607
  if (call && call.parameters['CallSid']) {
1578
- if (!this.newIncomingCallsList) {
1579
- this.newIncomingCallsList = [];
1580
- }
1581
- const callSid = call.parameters['CallSid'];
1582
- const existingCall = this.newIncomingCallsList.find((c) => c.parameters['CallSid'] === callSid);
1583
- if (!existingCall) {
1584
- this.newIncomingCallsList.push(call);
1585
- this.incomingCallsNewList.emit(this.newIncomingCallsList);
1586
- if (!this.twilioCallData) {
1587
- this.twilioCallData = call;
1588
- this.twilioAuthId = call.customParameters.get('twilioAuthId');
1589
- if (!call.parameters) {
1590
- call.parameters = {};
1591
- }
1592
- this.sendIPforIncomingCall(this.twilioAuthId, '');
1593
- }
1594
- call.on('cancel', () => {
1595
- this.removeCallFromList(callSid);
1596
- });
1597
- call.on('disconnect', () => {
1598
- this.removeCallFromList(callSid);
1599
- });
1600
- }
1608
+ this.addCallToList(call);
1601
1609
  }
1602
1610
  });
1603
1611
  }
@@ -1605,6 +1613,27 @@ class IncomingCallComponent {
1605
1613
  console.log(e);
1606
1614
  }
1607
1615
  }
1616
+ addCallToList(call) {
1617
+ if (!this.newIncomingCallsList) {
1618
+ this.newIncomingCallsList = [];
1619
+ }
1620
+ const callSid = call.parameters['CallSid'];
1621
+ const existingCall = this.newIncomingCallsList.find((c) => c.parameters['CallSid'] === callSid);
1622
+ if (!existingCall) {
1623
+ this.newIncomingCallsList.push(call);
1624
+ this.incomingCallsNewList.emit([...this.newIncomingCallsList]);
1625
+ if (!this.twilioCallData) {
1626
+ this.twilioCallData = call;
1627
+ this.twilioAuthId = call.customParameters.get('twilioAuthId');
1628
+ this.sendIPforIncomingCall(this.twilioAuthId, '');
1629
+ }
1630
+ call.on('cancel', () => this.removeCallFromList(callSid));
1631
+ call.on('disconnect', () => this.removeCallFromList(callSid));
1632
+ }
1633
+ else {
1634
+ console.log('Call already in list, not adding again.');
1635
+ }
1636
+ }
1608
1637
  acceptCallFromList(data) {
1609
1638
  data.accept();
1610
1639
  // data.parameters['isCallConnected'] = true;