Browse Source

Implemented deferred based initialization to allow plugins to block load process.

pull/79/head
Simon Eisenmann 11 years ago
parent
commit
1d9c8c5b7e
  1. 7
      static/js/app.js
  2. 3
      static/js/controllers/mediastreamcontroller.js
  3. 9
      static/js/directives/usability.js
  4. 120
      static/js/services/continueconnector.js
  5. 21
      static/js/services/mediastream.js
  6. 9
      static/js/services/services.js

7
static/js/app.js

@ -86,10 +86,15 @@ define([
$locationProvider.html5Mode(true); $locationProvider.html5Mode(true);
}]); }]);
app.run(["$rootScope", "mediaStream", "translation", function($rootScope, mediaStream, translation) { app.run(["$rootScope", "$timeout", "mediaStream", "translation", "continueConnector", function($rootScope, $timeout, mediaStream, translation, continueConnector) {
translation.inject($rootScope); translation.inject($rootScope);
console.log("Initializing ..."); console.log("Initializing ...");
var initialize = continueConnector.defer();
mediaStream.initialize($rootScope, translation); mediaStream.initialize($rootScope, translation);
$timeout(function() {
console.log("Initializing complete.")
initialize.resolve();
}, 0);
}]); }]);
app.constant("availableLanguages", languages); app.constant("availableLanguages", languages);

3
static/js/controllers/mediastreamcontroller.js

@ -535,7 +535,8 @@ define(['underscore', 'bigscreen', 'moment', 'sjcl', 'modernizr', 'webrtc.adapte
_.delay(function() { _.delay(function() {
if (autoreconnect && !reconnecting) { if (autoreconnect && !reconnecting) {
reconnecting = true; reconnecting = true;
mediaStream.connector.reconnect() console.log("Requesting to reconnect ...");
mediaStream.reconnect();
} }
}, 500); }, 500);
$scope.setStatus("reconnecting"); $scope.setStatus("reconnecting");

9
static/js/directives/usability.js

@ -24,7 +24,7 @@ define(['jquery', 'underscore', 'text!partials/usability.html'], function($, _,
return ["mediaStream", function(mediaStream) { return ["mediaStream", function(mediaStream) {
var controller = ['$scope', "mediaStream", "safeApply", "$timeout", "localStorage", function($scope, mediaStream, safeApply, $timeout, localStorage) { var controller = ['$scope', "mediaStream", "safeApply", "$timeout", "localStorage", "continueConnector", function($scope, mediaStream, safeApply, $timeout, localStorage, continueConnector) {
var pending = true; var pending = true;
var complete = false; var complete = false;
@ -37,12 +37,15 @@ define(['jquery', 'underscore', 'text!partials/usability.html'], function($, _,
}; };
ctrl.setInfo("waiting"); ctrl.setInfo("waiting");
var continueDeferred = continueConnector.defer();
$scope.continueConnect = function(status) { $scope.continueConnect = function(status) {
safeApply($scope, function() { safeApply($scope, function() {
pending = false; pending = false;
if (status) { if (status) {
localStorage.setItem("mediastream-mediacheck", MEDIA_CHECK) localStorage.setItem("mediastream-mediacheck", MEDIA_CHECK)
$scope.connect() console.log("Continue with connect after media check ...");
continueDeferred.resolve();
if (mediaStream.config.DefaultRoomEnabled !== true) { if (mediaStream.config.DefaultRoomEnabled !== true) {
ctrl.setInfo("initializing"); ctrl.setInfo("initializing");
initializer = $timeout(function() { initializer = $timeout(function() {
@ -81,7 +84,7 @@ define(['jquery', 'underscore', 'text!partials/usability.html'], function($, _,
// Toplevel watcher for connect function to become available. // Toplevel watcher for connect function to become available.
$scope.$watch("connect", function() { $scope.$watch("connect", function() {
if ($scope.connect) { if ($scope.connect) {
console.log("Connecting ..."); console.log("Checking for media access ...");
ctrl.setInfo("checking"); ctrl.setInfo("checking");
$timeout(function() { $timeout(function() {
if (pending) { if (pending) {

120
static/js/services/continueconnector.js

@ -0,0 +1,120 @@
/*
* Spreed WebRTC.
* Copyright (C) 2013-2014 struktur AG
*
* This file is part of Spreed WebRTC.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
define(['underscore'], function(_) {
// Helper class to kill of old defers.
var Caller = function(ref) {
this.ref = ref;
};
Caller.prototype.disable = function() {
this.ref = null;
}
Caller.prototype.resolve = function(value) {
if (this.ref) {
_.bind(this.ref.resolve, this.ref)(value);
}
};
Caller.prototype.reject = function(reason) {
if (this.ref) {
_.bind(this.ref.reject, this.ref)(reason);
}
};
Caller.prototype.notify = function(value) {
if (this.ref) {
_.bind(this.ref.notify, this.ref)(value);
}
};
// continueConnector
return ["$q", function($q) {
var ContinueConnector = function() {
this.deferred = $q.defer();
this.promises = [];
this.thens = [];
this.current = null;
};
ContinueConnector.prototype.add = function(promise) {
this.promises.push(promise);
this.refresh();
};
ContinueConnector.prototype.remove = function(promise) {
this.promises = _.without(this.promises, promise);
this.refresh();
};
ContinueConnector.prototype.refresh = function() {
// Disable old ones.
if (this.current) {
this.current.disable();
}
// Prepare caller.
var current = this.current = new Caller(this);
// Create new promise for all registered promises.
var all = $q.all(this.promises);
all.then(_.bind(current.resolve, current), _.bind(current.reject, current), _.bind(current.notify, current));
};
ContinueConnector.prototype.defer = function() {
var deferred = $q.defer();
this.add(deferred.promise);
return deferred;
};
ContinueConnector.prototype.resolve = function(value) {
//console.log("Continue connector resolved", arguments);
this.deferred.resolve(value);
};
ContinueConnector.prototype.reject = function(reason) {
this.deferred.reject(reason);
};
ContinueConnector.prototype.notify = function(value) {
this.deferred.notify(value);
};
ContinueConnector.prototype.then = function(successCallback, errorCallback, notifyCallback) {
this.thens.push(arguments);
return this.deferred.promise.then(successCallback, errorCallback, notifyCallback);
};
ContinueConnector.prototype.reset = function() {
this.deferred = $q.defer();
var p = this.deferred.promise;
_.each(this.thens, function(args) {
p.then.apply(p, args);
});
this.refresh();
};
return new ContinueConnector();
}];
});

21
static/js/services/mediastream.js

@ -30,7 +30,7 @@ define([
], function($, _, uaparser, Modernizr, Connector, Api, WebRTC, tokens) { ], function($, _, uaparser, Modernizr, Connector, Api, WebRTC, tokens) {
return ["globalContext", "$route", "$location", "$window", "visibility", "alertify", "$http", "safeApply", "$timeout", "$sce", "localStorage", function(context, $route, $location, $window, visibility, alertify, $http, safeApply, $timeout, $sce, localStorage) { return ["globalContext", "$route", "$location", "$window", "visibility", "alertify", "$http", "safeApply", "$timeout", "$sce", "localStorage", "continueConnector", function(context, $route, $location, $window, visibility, alertify, $http, safeApply, $timeout, $sce, localStorage, continueConnector) {
var url = (context.Ssl ? "wss" : "ws") + "://" + context.Host + (context.Cfg.B || "/") + "ws"; var url = (context.Ssl ? "wss" : "ws") + "://" + context.Host + (context.Cfg.B || "/") + "ws";
var version = context.Cfg.Version || "unknown"; var version = context.Cfg.Version || "unknown";
@ -194,6 +194,18 @@ define([
localStorage.removeItem("mediastream-login-" + context.Cfg.UsersMode); localStorage.removeItem("mediastream-login-" + context.Cfg.UsersMode);
} }
}, },
connect: function() {
continueConnector.then(function() {
console.log("Connecting ...");
connector.connect(url);
});
},
reconnect: function() {
continueConnector.then(function() {
console.log("Reconnecting ...");
connector.reconnect();
});
},
initialize: function($rootScope, translation) { initialize: function($rootScope, translation) {
var cont = false; var cont = false;
@ -203,6 +215,7 @@ define([
$rootScope.roomid = null; $rootScope.roomid = null;
$rootScope.roomlink = null; $rootScope.roomlink = null;
$rootScope.roomstatus = false; $rootScope.roomstatus = false;
$rootScope.connect = false;
var connect = function() { var connect = function() {
// We need websocket support to connect. // We need websocket support to connect.
@ -212,10 +225,10 @@ define([
} }
if (ready && cont) { if (ready && cont) {
// Inject connector function into scope, so that controllers can pick it up. // Inject connector function into scope, so that controllers can pick it up.
console.log("Ready to connect ...");
mediaStream.connect();
safeApply($rootScope, function(scope) { safeApply($rootScope, function(scope) {
scope.connect = function() { scope.connect = true;
connector.connect(url);
};
}); });
} }
}; };

9
static/js/services/services.js

@ -52,7 +52,8 @@ define([
'services/animationframe', 'services/animationframe',
'services/dialogs', 'services/dialogs',
'services/geolocation', 'services/geolocation',
'services/screensharing'], function(_, 'services/screensharing',
'services/continueconnector'], function(_,
desktopNotify, desktopNotify,
playSound, playSound,
safeApply, safeApply,
@ -84,7 +85,8 @@ localStorage,
animationFrame, animationFrame,
dialogs, dialogs,
geolocation, geolocation,
screensharing) { screensharing,
continueConnector) {
var services = { var services = {
desktopNotify: desktopNotify, desktopNotify: desktopNotify,
@ -118,7 +120,8 @@ screensharing) {
animationFrame: animationFrame, animationFrame: animationFrame,
dialogs: dialogs, dialogs: dialogs,
geolocation: geolocation, geolocation: geolocation,
screensharing: screensharing screensharing: screensharing,
continueConnector: continueConnector
}; };
var initialize = function(angModule) { var initialize = function(angModule) {

Loading…
Cancel
Save