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),
$refreshView : $(".refresh-view", this.$scope),
eventAggregator: _.extend({}, Backbone.Events),
wsuri: "ws://dev.phrasea.net:9090/websockets",
wstopic: "http://phraseanet.com/topics/admin/task-manager"
};
@@ -57,18 +56,14 @@ define([
TaskManagerApp.tasksView.render();
TaskManagerApp.schedulerView.render();
// Sets connection to the web socket
var ws = WSConnection.getInstance(TaskManagerApp.wsuri);
ws.connect();
ws.subscribe(TaskManagerApp.wstopic, function(topic, msg) {
WSConnection.subscribe(TaskManagerApp.wstopic, function(topic, msg) {
// double encoded string
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
TaskManagerApp.eventAggregator.on("ws:manager-tick", function(response) {
WSConnection.on("ws:manager-tick", function(response) {
TaskManagerApp.pingView.render();
TaskManagerApp.Scheduler.set({"actual": "started", "process-id": response.message.manager["process-id"]});
_.each(response.message.jobs, function(data, id) {

View File

@@ -2,25 +2,22 @@ define([
"underscore",
"backbone"
], function (_, Backbone) {
var instance;
function init(url) {
var activeSession = null;
return _.extend({
connect: function() {
connect: function(url) {
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
// it as a UMD module
ab.connect(url, function (session) {
$this.setSession(session);
$this.trigger("ws:connect", activeSession);
that.setSession(session);
that.trigger("ws:connect", activeSession);
},
function (code, reason) {
$this.trigger("ws:session-gone", code, reason);
that.trigger("ws:session-gone", code, reason);
});
},
close: function() {
@@ -34,16 +31,11 @@ define([
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);
this.trigger("ws:session-subscribe", topic);
});
return;
}
@@ -58,14 +50,4 @@ define([
this.trigger("ws:session-unsubscribe", topic);
}
}, 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.unsubscribe = sinon.spy();
this.wsConnection = connection.getInstance();
this.wsConnection = connection;
var $this = this;
var cbSuccess = function (session) {
$this.wsConnection.setSession($this.session);
@@ -29,6 +29,10 @@ define([
}
});
afterEach(function () {
this.wsConnection.close();
});
it("should have a session", function () {
this.wsConnection.connect();
assert.ok(this.wsConnection.hasSession());
@@ -48,12 +52,15 @@ define([
});
it("should not connect anymore after first connect", function () {
var throws = false;
this.wsConnection.connect();
ab.connect = sinon.spy();
try {
this.wsConnection.connect();
this.wsConnection.connect();
this.wsConnection.connect();
expect(ab.connect.should.have.callCount(0)).to.be.ok;
} catch (e) {
throws = true;
}
assert.ok(throws);
});
it("should call session subscribe once", function () {