WSConnection is not a singleton anymore

This commit is contained in:
Nicolas Le Goff
2014-02-27 14:25:10 +01:00
parent 5216e8fc9c
commit ef5b3fadb7
3 changed files with 59 additions and 75 deletions

View File

@@ -27,7 +27,6 @@ define([
$pingView : $(".ping-view", this.$scope), $pingView : $(".ping-view", this.$scope),
$refreshView : $(".refresh-view", this.$scope), $refreshView : $(".refresh-view", this.$scope),
eventAggregator: _.extend({}, Backbone.Events), eventAggregator: _.extend({}, Backbone.Events),
wsuri: "ws://dev.phrasea.net:9090/websockets",
wstopic: "http://phraseanet.com/topics/admin/task-manager" wstopic: "http://phraseanet.com/topics/admin/task-manager"
}; };
@@ -57,18 +56,14 @@ define([
TaskManagerApp.tasksView.render(); TaskManagerApp.tasksView.render();
TaskManagerApp.schedulerView.render(); TaskManagerApp.schedulerView.render();
// Sets connection to the web socket WSConnection.subscribe(TaskManagerApp.wstopic, function(topic, msg) {
var ws = WSConnection.getInstance(TaskManagerApp.wsuri);
ws.connect();
ws.subscribe(TaskManagerApp.wstopic, function(topic, msg) {
// double encoded string // double encoded string
var msg = JSON.parse(JSON.parse(msg)); var msg = JSON.parse(JSON.parse(msg));
TaskManagerApp.eventAggregator.trigger("ws:"+msg.event, msg); WSConnection.trigger("ws:"+msg.event, msg);
}); });
// On ticks re-render ping view, update tasks & scheduler model // On ticks re-render ping view, update tasks & scheduler model
TaskManagerApp.eventAggregator.on("ws:manager-tick", function(response) { WSConnection.on("ws:manager-tick", function(response) {
TaskManagerApp.pingView.render(); TaskManagerApp.pingView.render();
TaskManagerApp.Scheduler.set({"actual": "started", "process-id": response.message.manager["process-id"]}); TaskManagerApp.Scheduler.set({"actual": "started", "process-id": response.message.manager["process-id"]});
_.each(response.message.jobs, function(data, id) { _.each(response.message.jobs, function(data, id) {

View File

@@ -2,25 +2,22 @@ define([
"underscore", "underscore",
"backbone" "backbone"
], function (_, Backbone) { ], function (_, Backbone) {
var instance;
function init(url) {
var activeSession = null; var activeSession = null;
return _.extend({ return _.extend({
connect: function() { connect: function(url) {
if (this.hasSession()) { if (this.hasSession()) {
return; throw "Connection is already active";
} }
var $this = this; var that = this;
// autobahn js is defined as a global object there is no way to load // autobahn js is defined as a global object there is no way to load
// it as a UMD module // it as a UMD module
ab.connect(url, function (session) { ab.connect(url, function (session) {
$this.setSession(session); that.setSession(session);
$this.trigger("ws:connect", activeSession); that.trigger("ws:connect", activeSession);
}, },
function (code, reason) { function (code, reason) {
$this.trigger("ws:session-gone", code, reason); that.trigger("ws:session-gone", code, reason);
}); });
}, },
close: function() { close: function() {
@@ -34,16 +31,11 @@ define([
hasSession: function() { hasSession: function() {
return this.getSession() !== null; return this.getSession() !== null;
}, },
getSession: function() {
return activeSession;
},
setSession: function(session) {
activeSession = session;
},
subscribe: function(topic, callback) { subscribe: function(topic, callback) {
if (false === this.hasSession()) { if (false === this.hasSession()) {
this.on("ws:connect", function(session) { this.on("ws:connect", function(session) {
session.subscribe(topic, callback); session.subscribe(topic, callback);
this.trigger("ws:session-subscribe", topic);
}); });
return; return;
} }
@@ -58,14 +50,4 @@ define([
this.trigger("ws:session-unsubscribe", topic); this.trigger("ws:session-unsubscribe", topic);
} }
}, Backbone.Events); }, Backbone.Events);
}
return {
getInstance: function(url) {
if (!instance) {
instance = init(url);
}
return instance;
}
};
}); });

View File

@@ -17,7 +17,7 @@ define([
this.session.subscribe = sinon.spy(); this.session.subscribe = sinon.spy();
this.session.unsubscribe = sinon.spy(); this.session.unsubscribe = sinon.spy();
this.wsConnection = connection.getInstance(); this.wsConnection = connection;
var $this = this; var $this = this;
var cbSuccess = function (session) { var cbSuccess = function (session) {
$this.wsConnection.setSession($this.session); $this.wsConnection.setSession($this.session);
@@ -29,6 +29,10 @@ define([
} }
}); });
afterEach(function () {
this.wsConnection.close();
});
it("should have a session", function () { it("should have a session", function () {
this.wsConnection.connect(); this.wsConnection.connect();
assert.ok(this.wsConnection.hasSession()); assert.ok(this.wsConnection.hasSession());
@@ -48,12 +52,15 @@ define([
}); });
it("should not connect anymore after first connect", function () { it("should not connect anymore after first connect", function () {
var throws = false;
this.wsConnection.connect(); this.wsConnection.connect();
ab.connect = sinon.spy(); try {
this.wsConnection.connect(); this.wsConnection.connect();
this.wsConnection.connect(); } catch (e) {
this.wsConnection.connect(); throws = true;
expect(ab.connect.should.have.callCount(0)).to.be.ok; }
assert.ok(throws);
}); });
it("should call session subscribe once", function () { it("should call session subscribe once", function () {