diff --git a/client/app/scripts/actions/app-actions.js b/client/app/scripts/actions/app-actions.js index 3db3b7c269..943f6453fb 100644 --- a/client/app/scripts/actions/app-actions.js +++ b/client/app/scripts/actions/app-actions.js @@ -30,6 +30,12 @@ module.exports = { WebapiUtils.getNodesDelta(AppStore.getCurrentTopologyUrl()); }, + openWebsocket: function() { + AppDispatcher.dispatch({ + type: ActionTypes.OPEN_WEBSOCKET + }); + }, + closeWebsocket: function() { AppDispatcher.dispatch({ type: ActionTypes.CLOSE_WEBSOCKET diff --git a/client/app/scripts/constants/action-types.js b/client/app/scripts/constants/action-types.js index bf9234a950..2450cbccdb 100644 --- a/client/app/scripts/constants/action-types.js +++ b/client/app/scripts/constants/action-types.js @@ -10,6 +10,7 @@ module.exports = keymirror({ HIT_ESC_KEY: null, LEAVE_EDGE: null, LEAVE_NODE: null, + OPEN_WEBSOCKET: null, RECEIVE_NODE_DETAILS: null, RECEIVE_NODES: null, RECEIVE_NODES_DELTA: null, diff --git a/client/app/scripts/stores/__tests__/app-store-test.js b/client/app/scripts/stores/__tests__/app-store-test.js index aa71805f0d..de0adf6f02 100644 --- a/client/app/scripts/stores/__tests__/app-store-test.js +++ b/client/app/scripts/stores/__tests__/app-store-test.js @@ -39,6 +39,10 @@ describe('AppStore', function() { type: ActionTypes.HIT_ESC_KEY }; + const OpenWebsocketAction = { + type: ActionTypes.OPEN_WEBSOCKET + }; + const ReceiveEmptyNodesDeltaAction = { type: ActionTypes.RECEIVE_NODES_DELTA, delta: {} @@ -170,6 +174,9 @@ describe('AppStore', function() { expect(AppStore.isWebsocketClosed()).toBeTruthy(); expect(AppStore.getNodes()).toEqual(NODE_SET); + registeredCallback(OpenWebsocketAction); + expect(AppStore.isWebsocketClosed()).toBeFalsy(); + registeredCallback(ReceiveEmptyNodesDeltaAction); expect(AppStore.getNodes()).toEqual({}); }); diff --git a/client/app/scripts/stores/app-store.js b/client/app/scripts/stores/app-store.js index 1f52041663..08731fed97 100644 --- a/client/app/scripts/stores/app-store.js +++ b/client/app/scripts/stores/app-store.js @@ -188,6 +188,14 @@ AppStore.registeredCallback = function(payload) { AppStore.emit(AppStore.CHANGE_EVENT); break; + case ActionTypes.OPEN_WEBSOCKET: + // flush nodes cache after re-connect + nodes = {}; + websocketClosed = false; + + AppStore.emit(AppStore.CHANGE_EVENT); + break; + case ActionTypes.RECEIVE_ERROR: errorUrl = payload.errorUrl; AppStore.emit(AppStore.CHANGE_EVENT); @@ -207,12 +215,6 @@ AppStore.registeredCallback = function(payload) { errorUrl = null; - // flush nodes cache after re-connect - if (websocketClosed) { - nodes = {}; - } - websocketClosed = false; - // nodes that no longer exist _.each(payload.delta.remove, function(nodeId) { // in case node disappears before mouseleave event diff --git a/client/app/scripts/utils/web-api-utils.js b/client/app/scripts/utils/web-api-utils.js index 4add414e76..4400e90338 100644 --- a/client/app/scripts/utils/web-api-utils.js +++ b/client/app/scripts/utils/web-api-utils.js @@ -26,6 +26,10 @@ function createWebsocket(topologyUrl) { socket = new WebSocket(WS_URL + topologyUrl + '/ws?t=' + updateFrequency); + socket.onopen = function() { + AppActions.openWebsocket(); + }; + socket.onclose = function() { clearTimeout(reconnectTimer); socket = null;