@@ -57,45 +57,21 @@ export class USB extends EventDispatcher {
57
57
options = options || { } ;
58
58
this . devicesFound = options . devicesFound ;
59
59
60
- const deviceConnectCallback = device => {
60
+ adapter . addListener ( USBAdapter . EVENT_DEVICE_CONNECT , device => {
61
61
if ( this . replaceAllowedDevice ( device ) ) {
62
62
this . emit ( USB . EVENT_DEVICE_CONNECT , device ) ;
63
63
}
64
- } ;
64
+ } ) ;
65
65
66
- const deviceDisconnectCallback = handle => {
66
+ adapter . addListener ( USBAdapter . EVENT_DEVICE_DISCONNECT , handle => {
67
67
const allowedDevice = this . allowedDevices . find ( allowedDevices => allowedDevices . _handle === handle ) ;
68
68
69
69
if ( allowedDevice ) {
70
70
this . emit ( USB . EVENT_DEVICE_DISCONNECT , allowedDevice ) ;
71
- }
72
- } ;
73
-
74
- this . on ( "newListener" , event => {
75
- const listenerCount = this . listenerCount ( event ) ;
76
-
77
- if ( listenerCount !== 0 ) {
78
- return ;
79
- }
80
-
81
- if ( event === USB . EVENT_DEVICE_CONNECT ) {
82
- adapter . addListener ( USBAdapter . EVENT_DEVICE_CONNECT , deviceConnectCallback ) ;
83
- } else if ( event === USB . EVENT_DEVICE_DISCONNECT ) {
84
- adapter . addListener ( USBAdapter . EVENT_DEVICE_DISCONNECT , deviceDisconnectCallback ) ;
85
- }
86
- } ) ;
87
-
88
- this . on ( "removeListener" , event => {
89
- const listenerCount = this . listenerCount ( event ) ;
90
-
91
- if ( listenerCount !== 0 ) {
92
- return ;
93
- }
94
71
95
- if ( event === USB . EVENT_DEVICE_CONNECT ) {
96
- adapter . removeListener ( USBAdapter . EVENT_DEVICE_CONNECT , deviceConnectCallback ) ;
97
- } else if ( event === USB . EVENT_DEVICE_DISCONNECT ) {
98
- adapter . removeListener ( USBAdapter . EVENT_DEVICE_DISCONNECT , deviceDisconnectCallback ) ;
72
+ // Delete device from cache
73
+ const index = this . allowedDevices . indexOf ( allowedDevice ) ;
74
+ this . allowedDevices . splice ( index , 1 ) ;
99
75
}
100
76
} ) ;
101
77
}
0 commit comments