Explorar o código

Let converse.initialize return a deferred

and use that in the tests, instead of a callback.
JC Brand %!s(int64=9) %!d(string=hai) anos
pai
achega
454f8ef034
Modificáronse 3 ficheiros con 13 adicións e 22 borrados
  1. 1 1
      src/converse-api.js
  2. 11 18
      src/converse-core.js
  3. 1 3
      tests/main.js

+ 1 - 1
src/converse-api.js

@@ -23,7 +23,7 @@
     var Strophe = strophe.Strophe;
     return {
         'initialize': function (settings, callback) {
-            converse.initialize(settings, callback);
+            return converse.initialize(settings, callback);
         },
         'log': converse.log,
         'connection': {

+ 11 - 18
src/converse-core.js

@@ -132,6 +132,7 @@
 
     converse.initialize = function (settings, callback) {
         "use strict";
+        var init_deferred = new $.Deferred();
         var converse = this;
         var unloadevent;
         if ('onpagehide' in window) {
@@ -621,7 +622,7 @@
         };
 
 
-        this.onStatusInitialized = function (deferred) {
+        this.onStatusInitialized = function () {
             this.registerIntervalHandler();
             this.roster = new this.RosterContacts();
             this.roster.browserStorage = new Backbone.BrowserStorage[this.storage](
@@ -629,20 +630,9 @@
             this.chatboxes.onConnected();
             this.giveFeedback(__('Contacts'));
             if (typeof this.callback === 'function') {
-                // A callback method may be passed in via the
-                // converse.initialize method.
-                // XXX: Can we use $.Deferred instead of this callback?
-                if (this.connection.service === 'jasmine tests') {
-                    // XXX: Call back with the internal converse object. This
-                    // object should never be exposed to production systems.
-                    // 'jasmine tests' is an invalid http bind service value,
-                    // so we're sure that this is just for tests.
-                    this.callback(this);
-                } else  {
-                    this.callback();
-                }
+                // XXX: Deprecate in favor of init_deferred
+                this.callback();
             }
-            deferred.resolve();
             converse.emit('initialized');
         };
 
@@ -650,7 +640,6 @@
             // When reconnecting, there might be some open chat boxes. We don't
             // know whether these boxes are of the same account or not, so we
             // close them now.
-            var deferred = new $.Deferred();
             // XXX: ran into an issue where a returned PubSub BOSH response was
             // not received by the browser. The solution was to flush the
             // connection early on. I don't know what the underlying cause of
@@ -670,13 +659,11 @@
             this.domain = Strophe.getDomainFromJid(this.connection.jid);
             this.features = new this.Features();
             this.enableCarbons();
-            this.initStatus().done(_.bind(this.onStatusInitialized, this, deferred));
+            this.initStatus().done(_.bind(this.onStatusInitialized, this));
             converse.emit('connected');
             converse.emit('ready'); // BBB: Will be removed.
-            return deferred.promise();
         };
 
-
         this.RosterContact = Backbone.Model.extend({
 
             initialize: function (attributes, options) {
@@ -1796,7 +1783,13 @@
         }).then(function () {
             converse._initialize();
             converse.registerGlobalEventHandlers();
+            if (converse.connection.service === 'jasmine tests') {
+                init_deferred.resolve(converse);
+            } else {
+                init_deferred.resolve();
+            }
         });
+        return init_deferred.promise();
     };
     return converse;
 }));

+ 1 - 3
tests/main.js

@@ -56,7 +56,7 @@ require([
             jid: 'dummy@localhost',
             password: 'secret',
             debug: true 
-        }, function (converse) {
+        }).then(function (converse) {
             window.converse = converse;
             window.crypto = {
                 getRandomValues: function (buf) {
@@ -86,8 +86,6 @@ require([
                 "spec/register",
                 "spec/xmppstatus",
             ], function () {
-                // Make sure this callback is only called once.
-                delete converse.callback;
                 // Stub the trimChat method. It causes havoc when running with
                 // phantomJS.
                 converse.ChatBoxViews.prototype.trimChat = function () {};