From 3aefba32004415e685c32a5a2ebc6c2c148f788a Mon Sep 17 00:00:00 2001 From: JC Brand Date: Mon, 21 Mar 2016 09:53:09 +0000 Subject: [PATCH] Change ping plugin to not have to use overrides. Had to add a `reconnect` event to core. --- docs/source/development.rst | 10 ++++++---- spec/ping.js | 34 ++++++++++++++++++++-------------- src/converse-core.js | 1 + src/converse-ping.js | 28 +++++++--------------------- 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/docs/source/development.rst b/docs/source/development.rst index 1737e25c4..6d2bd5e0c 100644 --- a/docs/source/development.rst +++ b/docs/source/development.rst @@ -862,12 +862,14 @@ When keepalive=true but there aren't any stored prebind tokens. ``converse.listen.on('noResumeableSession', function (event) { ... });`` -reconnect -~~~~~~~~~ +reconnected +~~~~~~~~~~~ -After the connection has dropped. Converse.js will attempt to reconnect when not in prebind mode. +After the connection has dropped and converse.js has reconnected. +Any Strophe stanza handlers (as registered via `converse.listen.stanza`) will +have to be registered anew. -``converse.listen.on('reconnect', function (event) { ... });`` +``converse.listen.on('reconnected', function (event) { ... });`` roomInviteSent ~~~~~~~~~~~~~~ diff --git a/spec/ping.js b/spec/ping.js index 1d51e7f3c..0c7299df0 100644 --- a/spec/ping.js +++ b/spec/ping.js @@ -2,28 +2,34 @@ (function (root, factory) { define([ "jquery", - "mock", - "test_utils", "converse-ping" - ], function ($, mock, test_utils) { - return factory($, mock, test_utils); + ], function ($) { + return factory($); } ); -} (this, function ($, mock, test_utils) { +} (this, function ($) { "use strict"; - describe("XMPP Ping", $.proxy(function (mock, test_utils) { - describe("Ping and pong handlers", $.proxy(function (mock, test_utils) { - it("are registered when converse.js is initialized", $.proxy(function () { + describe("XMPP Ping", function () { + describe("Ping and pong handlers", function () { + it("are registered when converse.js is connected", function () { spyOn(converse, 'registerPingHandler').andCallThrough(); spyOn(converse, 'registerPongHandler').andCallThrough(); - converse._initialize(); + converse.emit('connected'); expect(converse.registerPingHandler).toHaveBeenCalled(); expect(converse.registerPongHandler).toHaveBeenCalled(); - }, converse, mock, test_utils)); - })); + }); - describe("An IQ stanza", $.proxy(function (mock, test_utils) { + it("are registered when converse.js reconnected", function () { + spyOn(converse, 'registerPingHandler').andCallThrough(); + spyOn(converse, 'registerPongHandler').andCallThrough(); + converse.emit('reconnected'); + expect(converse.registerPingHandler).toHaveBeenCalled(); + expect(converse.registerPongHandler).toHaveBeenCalled(); + }); + }); + + describe("An IQ stanza", function () { it("is sent out when converse.js pings a server", function () { var sent_stanza, IQ_id; var sendIQ = converse.connection.sendIQ; @@ -37,6 +43,6 @@ ""+ ""); }); - })); - }, converse, mock, test_utils)); + }); + }); })); diff --git a/src/converse-core.js b/src/converse-core.js index ef4139276..114d020b4 100755 --- a/src/converse-core.js +++ b/src/converse-core.js @@ -626,6 +626,7 @@ this.afterReconnected(); deferred.resolve(); }.bind(this)); + converse.emit('reconnected'); return deferred.promise(); }; diff --git a/src/converse-ping.js b/src/converse-ping.js index e69a63053..1c56a8777 100644 --- a/src/converse-ping.js +++ b/src/converse-ping.js @@ -24,27 +24,6 @@ converse_api.plugins.add('ping', { - overrides: { - // Overrides mentioned here will be picked up by converse.js's - // plugin architecture they will replace existing methods on the - // relevant objects or classes. - // - // New functions which don't exist yet can also be added. - - onConnected: function () { - var promise = this._super.onConnected(); - promise.done(converse.registerPingHandler); - return promise; - }, - onReconnected: function () { - // We need to re-register the ping event handler on the newly - // created connection. - var promise = this._super.onReconnected(); - promise.done(converse.registerPingHandler); - return promise; - } - }, - initialize: function () { /* The initialize function gets called as soon as the plugin is * loaded by converse.js's plugin machinery. @@ -118,6 +97,13 @@ converse.ping(jid); } }); + + var onConnected = function () { + // Wrapper so that we can spy on registerPingHandler in tests + converse.registerPingHandler(); + }; + converse.on('connected', onConnected); + converse.on('reconnected', onConnected); } }); }));