diff --git a/dashboard/dashboard.js b/dashboard/dashboard.js index e186f699..20f0be64 100644 --- a/dashboard/dashboard.js +++ b/dashboard/dashboard.js @@ -18,8 +18,7 @@ const serveStatic = require("serve-static"); const { FRONT_SOCKET_TYPE, EDITOR_SOCKET_TYPE, DASHBOARD_PATH } = require("./common/common"); let io = null; -let dashboardState = {}; -let dashboardNodes = {}; +let globalNodes = {}; function init(RED) { const app = RED.httpNode || RED.httpAdmin; @@ -32,39 +31,38 @@ function init(RED) { } function initSocket(io) { - io.on("connection", (socket) => { - socket.emit(FRONT_SOCKET_TYPE.INIT_STATES, dashboardState); + io.on("connection", socket => { + socket.emit(FRONT_SOCKET_TYPE.INIT_STATES, getInitNodeStates); - socket.on(FRONT_SOCKET_TYPE.RECEIVE_MESSAGE, (message) => { - const node = dashboardNodes[message.nodeId]; + socket.on(FRONT_SOCKET_TYPE.RECEIVE_MESSAGE, message => { + const node = globalNodes[message.nodeId].runtime; if (!node) return; if (typeof node.onMessage === "function") { node.onMessage(message); } }); - socket.on(EDITOR_SOCKET_TYPE.FLOW_DEPLOYED, (nodes) => { - initializeDashboardState(nodes); - io.emit(FRONT_SOCKET_TYPE.INIT_STATES, dashboardState); + socket.on(EDITOR_SOCKET_TYPE.FLOW_DEPLOYED, nodes => { + setInitNodes(nodes); + io.emit(FRONT_SOCKET_TYPE.INIT_STATES, globalNodes); }); }); } -function initializeDashboardState(nodes) { - dashboardState = {}; - for (let i = 0; i < nodes.length; ++i) { - dashboardState[nodes[i].id] = nodes[i]; - } -} - function emitState(state) { - const node_id = state.node_id; + const nodeId = state.node_id; state.time = Date.now(); - if (dashboardState.hasOwnProperty(node_id)) { - dashboardState[node_id].push(state); + if (globalNodes && globalNodes.hasOwnProperty(nodeId)) { + if (Array.isArray(globalNodes[nodeId].states)) { + globalNodes[nodeId].states.push(state); + } else { + globalNodes[nodeId].states = [state]; + } } else { - dashboardState[node_id] = [state]; + globalNodes[nodeId] = { + states: [state], + }; } emit(state); @@ -75,6 +73,31 @@ function emit(state) { else io.emit(FRONT_SOCKET_TYPE.UPDATE_STATE, [state]); } +function setInitNodes(nodes) { + for (let i = 0; i < nodes.length; ++i) { + globalNodes[nodes[i].id] = { + editor: nodes[i], + states: [], + }; + } +} + function addNode(node) { - dashboardNodes[node.id] = node; + if (globalNodes && globalNodes.hasOwnProperty(node.id)) { + globalNodes[node.id].runtime = node; + } else { + globalNodes[node.id] = { + runtime: node, + states: [], + }; + } +} + +function getInitNodeStates() { + return globalNodes.map(node => { + return { + editor: node.editor, + states: node.states, + }; + }); } diff --git a/dashboard/nodes/soop_dashboard_config.html b/dashboard/nodes/soop_dashboard_config.html index 9dc59a10..0c20dcc2 100644 --- a/dashboard/nodes/soop_dashboard_config.html +++ b/dashboard/nodes/soop_dashboard_config.html @@ -14,7 +14,7 @@ function initializeDashboardConfig() { RED.events.on("deploy", function () { const dashboardNodes = []; - RED.nodes.eachNode((node) => { + RED.nodes.eachNode(node => { if (node.type.indexOf("soop") !== -1) { dashboardNodes.push(node); } @@ -27,7 +27,7 @@ function createDashboardConfigNode() { const configNodes = []; - RED.nodes.eachConfig((node) => { + RED.nodes.eachConfig(node => { if (node.type === "soopDashboardConfig") { configNodes.push(node); } diff --git a/package.json b/package.json index 412dc963..534a6901 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,8 @@ "AWS": "301-AWS.js", "DB": "302-DB.js", "SlackNotification": "303-SlackNotification.js", - "soopLowerCase": "dashboard/nodes/soopLowerCase.js", - "soopDashboardConfig": "dashboard/nodes/soopDashboardConfig.js", - "test-node": "dashboard/nodes/test-node.js" + "soopLowerCase": "dashboard/nodes/soop_lower_case.js", + "soopDashboardConfig": "dashboard/nodes/soop_dashboard_config.js" } }, "dependencies": {