Skip to content

Commit

Permalink
Merge pull request #15 from SoopSASM/feature/runtime-node-state-as-array
Browse files Browse the repository at this point in the history
Modify global node object
  • Loading branch information
jekwan authored Sep 27, 2022
2 parents 8fb6231 + 314a1cf commit 08c3602
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
65 changes: 44 additions & 21 deletions dashboard/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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,
};
});
}
4 changes: 2 additions & 2 deletions dashboard/nodes/soop_dashboard_config.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -27,7 +27,7 @@

function createDashboardConfigNode() {
const configNodes = [];
RED.nodes.eachConfig((node) => {
RED.nodes.eachConfig(node => {
if (node.type === "soopDashboardConfig") {
configNodes.push(node);
}
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 08c3602

Please sign in to comment.