1
- import { EventEmitter , promise } from "@xmpp/events" ;
1
+ import { EventEmitter , promise , listeners } from "@xmpp/events" ;
2
2
import jid from "@xmpp/jid" ;
3
3
import xml from "@xmpp/xml" ;
4
4
import StreamError from "./lib/StreamError.js" ;
@@ -8,13 +8,14 @@ const NS_STREAM = "urn:ietf:params:xml:ns:xmpp-streams";
8
8
const NS_JABBER_STREAM = "http://etherx.jabber.org/streams" ;
9
9
10
10
class Connection extends EventEmitter {
11
+ #socketListeners = null ;
12
+ #parserListeners = null ;
13
+
11
14
constructor ( options = { } ) {
12
15
super ( ) ;
13
16
this . jid = null ;
14
17
this . timeout = 2000 ;
15
18
this . options = options ;
16
- this . socketListeners = Object . create ( null ) ;
17
- this . parserListeners = Object . create ( null ) ;
18
19
this . status = "offline" ;
19
20
this . socket = null ;
20
21
this . parser = null ;
@@ -40,7 +41,7 @@ class Connection extends EventEmitter {
40
41
this . parser . write ( str ) ;
41
42
}
42
43
43
- _onParserError ( error ) {
44
+ #onParserError ( error ) {
44
45
// https://xmpp.org/rfcs/rfc6120.html#streams-error-conditions-bad-format
45
46
// "This error can be used instead of the more specific XML-related errors,
46
47
// such as <bad-namespace-prefix/>, <invalid-xml/>, <not-well-formed/>, <restricted-xml/>,
@@ -62,32 +63,16 @@ class Connection extends EventEmitter {
62
63
63
64
_attachSocket ( socket ) {
64
65
this . socket = socket ;
65
- const listeners = this . socketListeners ;
66
-
67
- listeners . data = this . _onData . bind ( this ) ;
68
-
69
- listeners . close = this . #onSocketClosed. bind ( this ) ;
70
-
71
- listeners . connect = ( ) => {
72
- this . _status ( "connect" ) ;
73
- } ;
74
-
75
- listeners . error = ( error ) => {
76
- this . emit ( "error" , error ) ;
77
- } ;
78
-
79
- this . socket . on ( "close" , listeners . close ) ;
80
- this . socket . on ( "data" , listeners . data ) ;
81
- this . socket . on ( "error" , listeners . error ) ;
82
- this . socket . on ( "connect" , listeners . connect ) ;
66
+ this . #socketListeners ??= listeners ( socket , {
67
+ data : this . _onData . bind ( this ) ,
68
+ close : this . #onSocketClosed. bind ( this ) ,
69
+ connect : ( ) => this . _status ( "connect" ) ,
70
+ error : ( error ) => this . emit ( "error" , error ) ,
71
+ } ) . subscribe ( ) ;
83
72
}
84
73
85
74
_detachSocket ( ) {
86
- const { socketListeners, socket } = this ;
87
- for ( const k of Object . getOwnPropertyNames ( socketListeners ) ) {
88
- socket . removeListener ( k , socketListeners [ k ] ) ;
89
- delete socketListeners [ k ] ;
90
- }
75
+ this . #socketListeners?. unsubscribe ( ) ;
91
76
this . socket = null ;
92
77
}
93
78
@@ -143,29 +128,16 @@ class Connection extends EventEmitter {
143
128
144
129
_attachParser ( parser ) {
145
130
this . parser = parser ;
146
- const listeners = this . parserListeners ;
147
-
148
- listeners . element = this . _onElement . bind ( this ) ;
149
- listeners . error = this . _onParserError . bind ( this ) ;
150
-
151
- listeners . end = this . #onStreamClosed. bind ( this ) ;
152
-
153
- listeners . start = ( element ) => {
154
- this . _status ( "open" , element ) ;
155
- } ;
156
-
157
- this . parser . on ( "error" , listeners . error ) ;
158
- this . parser . on ( "element" , listeners . element ) ;
159
- this . parser . on ( "end" , listeners . end ) ;
160
- this . parser . on ( "start" , listeners . start ) ;
131
+ this . #parserListeners ??= listeners ( parser , {
132
+ element : this . _onElement . bind ( this ) ,
133
+ error : this . #onParserError. bind ( this ) ,
134
+ end : this . #onStreamClosed. bind ( this ) ,
135
+ start : ( element ) => this . _status ( "open" , element ) ,
136
+ } ) . subscribe ( ) ;
161
137
}
162
138
163
139
_detachParser ( ) {
164
- const listeners = this . parserListeners ;
165
- for ( const k of Object . getOwnPropertyNames ( listeners ) ) {
166
- this . parser . removeListener ( k , listeners [ k ] ) ;
167
- delete listeners [ k ] ;
168
- }
140
+ this . #parserListeners?. unsubscribe ( ) ;
169
141
this . parser = null ;
170
142
this . root = null ;
171
143
}
0 commit comments