Selaa lähdekoodia

fix(XmppConnection): intercept callback passed to attach

Either connect or attach can be used to initiate the connection.
dev1
paweldomas 5 vuotta sitten
vanhempi
commit
aadfce2283
1 muutettua tiedostoa jossa 36 lisäystä ja 23 poistoa
  1. 36
    23
      modules/xmpp/XmppConnection.js

+ 36
- 23
modules/xmpp/XmppConnection.js Näytä tiedosto

@@ -190,13 +190,15 @@ export default class XmppConnection extends Listenable {
190 190
         this._stropheConn.addHandler(...args);
191 191
     }
192 192
 
193
+    /* eslint-disable max-params */
193 194
     /**
194
-     * See {@link Strophe.Connection.attach}.
195
+     * Wraps {@link Strophe.Connection.attach} method in order to intercept the connection status updates.
196
+     * See {@link Strophe.Connection.attach} for the params description.
195 197
      *
196 198
      * @returns {void}
197 199
      */
198
-    attach(...args) {
199
-        this._stropheConn.attach(...args);
200
+    attach(jid, sid, rid, callback, ...args) {
201
+        this._stropheConn.attach(jid, sid, rid, this._stropheConnectionCb.bind(this, callback), ...args);
200 202
     }
201 203
 
202 204
     /**
@@ -206,30 +208,41 @@ export default class XmppConnection extends Listenable {
206 208
      * @returns {void}
207 209
      */
208 210
     connect(jid, pass, callback, ...args) {
209
-        const connectCb = (status, ...cbArgs) => {
210
-            this._status = status;
211
-
212
-            let blockCallback = false;
213
-
214
-            if (status === Strophe.Status.CONNECTED) {
215
-                this._maybeEnableStreamResume();
216
-                this._maybeStartWSKeepAlive();
217
-                this._resumeRetryN = 0;
218
-            } else if (status === Strophe.Status.DISCONNECTED) {
219
-                // FIXME add RECONNECTING state instead of blocking the DISCONNECTED update
220
-                blockCallback = this._tryResumingConnection();
221
-                if (!blockCallback) {
222
-                    clearTimeout(this._wsKeepAlive);
223
-                }
224
-            }
211
+        this._stropheConn.connect(jid, pass, this._stropheConnectionCb.bind(this, callback), ...args);
212
+    }
213
+
214
+    /* eslint-enable max-params */
225 215
 
216
+    /**
217
+     * Handles {@link Strophe.Status} updates for the current connection.
218
+     *
219
+     * @param {function} targetCallback - The callback passed by the {@link XmppConnection} consumer to one of
220
+     * the connect methods.
221
+     * @param {Strophe.Status} status - The new connection status.
222
+     * @param {*} args - The rest of the arguments passed by Strophe.
223
+     * @private
224
+     */
225
+    _stropheConnectionCb(targetCallback, status, ...args) {
226
+        this._status = status;
227
+
228
+        let blockCallback = false;
229
+
230
+        if (status === Strophe.Status.CONNECTED) {
231
+            this._maybeEnableStreamResume();
232
+            this._maybeStartWSKeepAlive();
233
+            this._resumeRetryN = 0;
234
+        } else if (status === Strophe.Status.DISCONNECTED) {
235
+            // FIXME add RECONNECTING state instead of blocking the DISCONNECTED update
236
+            blockCallback = this._tryResumingConnection();
226 237
             if (!blockCallback) {
227
-                callback(status, ...cbArgs);
228
-                this.eventEmitter.emit(XmppConnection.Events.CONN_STATUS_CHANGED, status);
238
+                clearTimeout(this._wsKeepAlive);
229 239
             }
230
-        };
240
+        }
231 241
 
232
-        this._stropheConn.connect(jid, pass, connectCb, ...args);
242
+        if (!blockCallback) {
243
+            targetCallback(status, ...args);
244
+            this.eventEmitter.emit(XmppConnection.Events.CONN_STATUS_CHANGED, status);
245
+        }
233 246
     }
234 247
 
235 248
     /**

Loading…
Peruuta
Tallenna