@vgroup/dialbox 0.1.24 → 0.1.25

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,70 @@ 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
+ const callEndHandler = () => {
1461
+ this.processedCallSids.delete(callSid);
1462
+ call.off('disconnect', callEndHandler);
1463
+ call.off('cancel', callEndHandler);
1464
+ };
1465
+ call.on('disconnect', callEndHandler);
1466
+ call.on('cancel', callEndHandler);
1467
+ }
1468
+ else {
1469
+ console.log('Duplicate incoming call event ignored for CallSid:', callSid);
1470
+ }
1471
+ };
1472
+ device.on('registered', onRegistered);
1473
+ device.on('error', onError);
1474
+ device.on('incoming', onIncoming);
1475
+ device.register();
1450
1476
  });
1477
+ return this.devicePromise;
1451
1478
  }
1452
1479
  // onIncomingCall(){
1453
1480
  // this.device.on('incoming', (call:any) => {
@@ -1575,29 +1602,7 @@ class IncomingCallComponent {
1575
1602
  try {
1576
1603
  this.twilioService.currentCall.subscribe(call => {
1577
1604
  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
- }
1605
+ this.addCallToList(call);
1601
1606
  }
1602
1607
  });
1603
1608
  }
@@ -1605,6 +1610,22 @@ class IncomingCallComponent {
1605
1610
  console.log(e);
1606
1611
  }
1607
1612
  }
1613
+ addCallToList(call) {
1614
+ if (!this.newIncomingCallsList) {
1615
+ this.newIncomingCallsList = [];
1616
+ }
1617
+ const callSid = call.parameters['CallSid'];
1618
+ // The service now handles de-duplication, so we just add the call.
1619
+ this.newIncomingCallsList.push(call);
1620
+ this.incomingCallsNewList.emit([...this.newIncomingCallsList]);
1621
+ if (!this.twilioCallData) {
1622
+ this.twilioCallData = call;
1623
+ this.twilioAuthId = call.customParameters.get('twilioAuthId');
1624
+ this.sendIPforIncomingCall(this.twilioAuthId, '');
1625
+ }
1626
+ call.on('cancel', () => this.removeCallFromList(callSid));
1627
+ call.on('disconnect', () => this.removeCallFromList(callSid));
1628
+ }
1608
1629
  acceptCallFromList(data) {
1609
1630
  data.accept();
1610
1631
  // data.parameters['isCallConnected'] = true;