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

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 () {