Skip to content

Commit 08402f7

Browse files
committed
fix(web): change js device,call event listener to add/remove listener
1 parent 1985bae commit 08402f7

File tree

4 files changed

+34
-45
lines changed

4 files changed

+34
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Feat: [Web] Add Twilio Device [DeviceState] accessor protecting un/registration.
44
* Feat: [Web] Add Twilio Device `updateToken(String)` function to allow updating of active device tokens.
5+
* Fix: [Web] Twilio Device does not unregister on `unregister()` method call due to 'device.off' not visible in js object causing device event listeners to remain attached on unregistered device.
56
* Feat: update example.
67
* Docs: update CHANGELOG
78

lib/_internal/js/call/call.dart

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,6 @@ class Call extends Twilio {
9090
@JS("isMuted")
9191
external bool isMuted();
9292

93-
/// Attach event listener for Twilio Call object. See [TwilioCallEvents]
94-
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliocall#events
95-
/// possibly use js interop here
96-
@JS("on")
97-
external void on(String event, Function callback);
98-
99-
/// Deattach event listener for Twilio Call object. See [TwilioCallEvents]
100-
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliocall#events
101-
/// possibly use js interop here
102-
@JS("off")
103-
external void off(String event, Function callback);
104-
10593
/// Send digits to active Twilio Call
10694
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliocall#callsenddigitsdigits
10795
@JS("sendDigits")

lib/_internal/js/device/device.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ class Device extends Twilio {
6969
/// Attach event listener for Twilio Device object. See [TwilioDeviceEvents]
7070
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#events
7171
/// possibly use js interop here
72-
@JS("on")
73-
external void on(String event, Function callback);
72+
@JS("addListener")
73+
external void addListener(String event, Function callback);
7474

7575
/// Detach event listener for Twilio Device object. See [TwilioDeviceEvents]
7676
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#events
7777
/// possibly use js interop here
78-
@JS("off")
79-
external void off(String event, Function callback);
78+
@JS("removeListener")
79+
external void removeListener(String event, Function callback);
8080

8181
/// Update device options
8282
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#deviceupdateoptionsoptions

lib/_internal/twilio_voice_web.dart

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -392,37 +392,37 @@ class TwilioVoiceWeb extends MethodChannelTwilioVoice {
392392
}
393393

394394
/// Attach event listeners to [twilio_js.Device]
395-
/// See [twilio_js.Device.on]
395+
/// See [twilio_js.Device.addListener](https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#deviceaddlistenereventname-listener)
396396
void _attachDeviceListeners(twilio_js.Device device) {
397397
// ignore: unnecessary_null_comparison
398398
assert(device != null, "Device cannot be null");
399-
device.on("registered", js.allowInterop(_onDeviceRegistered));
400-
device.on("unregistered", js.allowInterop(_onDeviceUnregistered));
401-
device.on("error", js.allowInterop(_onDeviceError));
402-
device.on("incoming", js.allowInterop(_onDeviceIncoming));
403-
device.on("tokenWillExpire", js.allowInterop(_onTokenWillExpire));
399+
device.addListener("registered", js.allowInterop(_onDeviceRegistered));
400+
device.addListener("unregistered", js.allowInterop(_onDeviceUnregistered));
401+
device.addListener("error", js.allowInterop(_onDeviceError));
402+
device.addListener("incoming", js.allowInterop(_onDeviceIncoming));
403+
device.addListener("tokenWillExpire", js.allowInterop(_onTokenWillExpire));
404404
}
405405

406406
/// Detach event listeners to [twilio_js.Device]
407-
/// See [twilio_js.Device.off]
407+
/// See [twilio_js.Device.removeListener](https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#deviceremovelistenereventname-listener)
408408
void _detachDeviceListeners(twilio_js.Device device) {
409409
// ignore: unnecessary_null_comparison
410410
assert(device != null, "Device cannot be null");
411-
device.off("registered", js.allowInterop(_onDeviceRegistered));
412-
device.off("unregistered", js.allowInterop(_onDeviceUnregistered));
413-
device.off("error", js.allowInterop(_onDeviceError));
414-
device.off("incoming", js.allowInterop(_onDeviceIncoming));
415-
device.off("tokenWillExpire", js.allowInterop(_onTokenWillExpire));
411+
device.removeListener("registered", js.allowInterop(_onDeviceRegistered));
412+
device.removeListener("unregistered", js.allowInterop(_onDeviceUnregistered));
413+
device.removeListener("error", js.allowInterop(_onDeviceError));
414+
device.removeListener("incoming", js.allowInterop(_onDeviceIncoming));
415+
device.removeListener("tokenWillExpire", js.allowInterop(_onTokenWillExpire));
416416
}
417417

418-
/// On device registered and ready to make/receive calls via [twilio_js.Device.on] and [twilio_js.TwilioDeviceEvents.registered]
418+
/// On device registered and ready to make/receive calls via [twilio_js.Device.addListener] and [twilio_js.TwilioDeviceEvents.registered]
419419
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#registered-event
420420
void _onDeviceRegistered() {
421421
printDebug("_onDeviceRegistered");
422422
printDebug("Device registered for callInvites");
423423
}
424424

425-
// /// On device registered and ready to make/receive calls via [twilioJs.Device.on] and [twilioJs.TwilioDeviceEvents.registered]
425+
// /// On device registered and ready to make/receive calls via [twilioJs.Device.addListener] and [twilioJs.TwilioDeviceEvents.registered]
426426
// /// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#registered-event
427427
// Function _onDeviceRegistered() {
428428
// // final _f = (twilioJs.Device device) {
@@ -434,21 +434,21 @@ class TwilioVoiceWeb extends MethodChannelTwilioVoice {
434434
// });
435435
// }
436436

437-
/// On device unregistered, access token disabled and won't receive any more call invites [twilio_js.Device.off] and [twilio_js.TwilioDeviceEvents.unregistered]
437+
/// On device unregistered, access token disabled and won't receive any more call invites [twilio_js.Device.removeListener] and [twilio_js.TwilioDeviceEvents.unregistered]
438438
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#unregistered-event
439439
void _onDeviceUnregistered() {
440440
printDebug("_onDeviceUnregistered");
441441
printDebug("Device unregistered, won't receive no more callInvites");
442442
}
443443

444444
/// On device error
445-
/// See [twilio_js.Device.on] and [twilio_js.TwilioDeviceEvents.error]
445+
/// See [twilio_js.Device.addListener] and [twilio_js.TwilioDeviceEvents.error]
446446
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#error-event
447447
void _onDeviceError(twilio_js.TwilioError twilioError, twilio_js.Call? call) {
448448
logLocalEvent(twilioError.message);
449449
}
450450

451-
/// On incoming call received via [twilio_js.Device.on] and [twilio_js.TwilioDeviceEvents.incoming]
451+
/// On incoming call received via [twilio_js.Device.addListener] and [twilio_js.TwilioDeviceEvents.incoming]
452452
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#incoming-event
453453
void _onDeviceIncoming(twilio_js.Call call) {
454454
requestMicAccess();
@@ -507,7 +507,7 @@ class TwilioVoiceWeb extends MethodChannelTwilioVoice {
507507
);
508508
}
509509

510-
/// On device token about to expire (default is 10s prior to expiry), via [twilio_js.Device.on] and [twilio_js.TwilioDeviceEvents.tokenWillExpire]
510+
/// On device token about to expire (default is 10s prior to expiry), via [twilio_js.Device.addListener] and [twilio_js.TwilioDeviceEvents.tokenWillExpire]
511511
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#tokenwillexpire-event
512512
void _onTokenWillExpire(twilio_js.Device device) {
513513
logLocalEventEntries(["DEVICETOKEN", device.token], prefix: "");
@@ -831,23 +831,23 @@ class Call extends MethodChannelTwilioCall {
831831
}
832832

833833
/// Attach event listeners to the active call
834-
/// See [twilio_js.Call.on]
834+
/// See [twilio_js.Call.addListener]
835835
void _attachCallEventListeners(twilio_js.Call call) {
836836
// ignore: unnecessary_null_comparison
837837
assert(call != null, "Call cannot be null");
838-
call.on("ringing", js.allowInterop(_onCallRinging));
839-
call.on("accept", js.allowInterop(_onCallAccept));
840-
call.on("disconnect", js.allowInterop(_onCallDisconnect));
841-
call.on("cancel", js.allowInterop(_onCallCancel));
842-
call.on("reject", js.allowInterop(_onCallReject));
843-
call.on("error", js.allowInterop(_onCallError));
844-
call.on("reconnecting", js.allowInterop(_onCallReconnecting));
845-
call.on("reconnected", js.allowInterop(_onCallReconnected));
846-
call.on("log", js.allowInterop(_onLogEvent));
838+
call.addListener("ringing", js.allowInterop(_onCallRinging));
839+
call.addListener("accept", js.allowInterop(_onCallAccept));
840+
call.addListener("disconnect", js.allowInterop(_onCallDisconnect));
841+
call.addListener("cancel", js.allowInterop(_onCallCancel));
842+
call.addListener("reject", js.allowInterop(_onCallReject));
843+
call.addListener("error", js.allowInterop(_onCallError));
844+
call.addListener("reconnecting", js.allowInterop(_onCallReconnecting));
845+
call.addListener("reconnected", js.allowInterop(_onCallReconnected));
846+
call.addListener("log", js.allowInterop(_onLogEvent));
847847
}
848848

849849
/// Detach event listeners to the active call
850-
/// See [twilio_js.Call.off]
850+
/// See [twilio_js.Call.removeListener]
851851
/// 'off' event listener isn't implemented in twilio-voice.js
852852
void _detachCallEventListeners(twilio_js.Call call) {
853853
// ignore: unnecessary_null_comparison

0 commit comments

Comments
 (0)