Adding unsubscribe method for non-socket-enabled routes

with the idea that we only want to unsubscribe directly following a subscribe
(no need to unsubscribe if we're already unsubscribed).
This commit is contained in:
Jared Tabor 2016-09-13 12:43:41 -07:00
parent 1510d826a6
commit f875d65ef7
2 changed files with 30 additions and 4 deletions

View File

@ -100,8 +100,34 @@ export default
}, 2000);
},
subscribe: function(state){
console.log(state.name);
console.log("Next state: " + state.name);
this.emit(JSON.stringify(state.socket));
this.setLast(state);
},
unsubscribe: function(state){
if(this.requiresNewSubscribe(state)){
this.emit(JSON.stringify(state.socket));
}
this.setLast(state);
},
setLast: function(state){
this.last = state;
},
getLast: function(){
return this.last;
},
requiresNewSubscribe(state){
if (this.getLast() !== undefined){
if( _.isEmpty(state.socket.groups) && _.isEmpty(this.getLast().socket.groups)){
return false;
}
else {
return true;
}
}
else {
return true;
}
},
checkStatus: function() {
@ -140,7 +166,7 @@ export default
},
emit: function(data, callback) {
var self = this;
$log.debug('Sent to Server: ' + data);
$log.debug('Sent to Websocket Server: ' + data);
$rootScope.socketPromise.promise.then(function(){
self.socket.send(data, function () {
var args = arguments;

View File

@ -11,6 +11,7 @@ export default function($stateProvider) {
$rootScope.socketPromise.promise.then(function(){
if(!state.socket){
state.socket = {groups: {}};
SocketService.unsubscribe(state);
}
else{
if(state.socket.groups.hasOwnProperty( "job_events")){
@ -19,9 +20,8 @@ export default function($stateProvider) {
if(state.socket.groups.hasOwnProperty( "ad_hoc_command_events")){
state.socket.groups.job_events = [$stateParams.id];
}
SocketService.subscribe(state);
}
SocketService.subscribe(state);
return true;
});
}];