From f0b08389ff63c6c2f979d4b99235c12db7818c37 Mon Sep 17 00:00:00 2001 From: Chris L Date: Tue, 23 Mar 2021 18:38:35 +0100 Subject: [PATCH] Add and remove listeners --- serial.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/serial.ts b/serial.ts index 4e0c7ab..68e8497 100644 --- a/serial.ts +++ b/serial.ts @@ -600,6 +600,29 @@ class Serial { }); return ports; } + + /** + * Attach an event listener. + * + * @param {string} event the event to listen for. + * @param {Function} handleEvent the function to be triggered on the event. + */ + addEventListener(event: 'connect' | 'disconnect', + handleEvent: EventListener | EventListenerObject | null): void { + navigator.usb.addEventListener(event, handleEvent); + } + + /** + * Remove an event listener. + * + * @param {string} event the event for which the listener should be removed. + * @param {Function} handleEvent the handler to be removed. + */ + removeEventListener(event: 'connect' | 'disconnect', + handleEvent: EventListener | EventListenerObject | null): + void { + navigator.usb.removeEventListener(event, handleEvent); + } } /* an object to be used for starting the serial workflow */