network-collection
Version:
Baseline collection of graphs and network data structures using **socket.io**.
155 lines (151 loc) • 4.29 kB
JavaScript
'use strict';
Array.prototype.flatten = function() {
var ret = [];
for (var i = 0; i < this.length; i++) {
if (Array.isArray(this[i])) {
ret = ret.concat(this[i].flatten());
} else {
ret.push(this[i]);
}
}
return ret;
};
class Network {
constructor(options) {
options = options || {};
this.graphs = options.graphs || {};
}
setupListeners(socket) {
if (!socket) {
throw new Error('Socket must be defined.');
}
let events = this.events();
Object.keys(events).map((event) => {
socket.on(event, events[event]);
});
}
events() {
return {
'add_graph': this.handleAddGraph.bind(this),
'remove_graph': this.handleRemoveGraph.bind(this),
'get_graph': this.handleGetGraph.bind(this),
'add_edge': this.handleAddEdge.bind(this),
'remove_edge': this.handleRemoveEdge.bind(this),
'get_edge': this.handleGetEdge.bind(this),
'add_node': this.handleAddNode.bind(this),
'remove_node': this.handleRemoveNode.bind(this),
'get_node': this.handleGetNode.bind(this),
'find': this.handleFind.bind(this),
};
}
connect(socket, callback) {
this.socket = socket;
this.id = this.socket.id;
this.setupListeners(this.socket);
callback();
}
addGraph(graph) {
let response = {};
this.graphs[graph.id] = graph;
this.socket.emit(`add_graph_response_${this.id}`, response);
}
handleAddGraph(graph) {
this.addGraph(graph);
}
removeGraph(graphId) {
let response = {};
delete this.graphs[graphId];
this.socket.emit(`remove_graph_response_${this.id}`, response);
}
handleRemoveGraph(graphId) {
this.removeGraph(graphId);
}
getGraph(graphId) {
let response = this.graphs[graphId];
this.socket.emit(`get_graph_response_${this.id}`, response);
}
handleGetGraph(graphId) {
this.getGraph(graphId);
}
addEdge(graphId, fromId, toId) {
let response = {};
this.graphs[graphId].addEdge(fromId, toId);
this.socket.emit(`add_edge_response_${this.id}`, response);
}
handleAddEdge(graphId, fromId, toId) {
this.addEdge(graphId, fromId, toId);
}
removeEdge(graphId, fromId, toId) {
let response = {};
this.graphs[graphId].removeEdge(fromId, toId);
this.socket.emit(`remove_edge_response_${this.id}`, response);
}
handleRemoveEdge(graphId, fromId, toId) {
this.removeEdge(graphId, fromId, toId);
}
getEdge(graphId, fromId, toId) {
let response = this.graphs[graphId].getEdge(fromId, toId);
this.socket.emit(`get_edge_response_${this.id}`, response);
}
handleGetEdge(graphId, fromId, toId) {
this.getEdge(graphId, fromId, toId);
}
addNode(graphId, node) {
let response = {};
this.graphs[graphId].addNode(node);
this.socket.emit(`add_node_response_${this.id}`, response);
}
handleAddNode(graphId, node) {
this.addNode(graphId, node);
}
removeNode(graphId, nodeId) {
let response = {};
this.graphs[graphId].removeNode(nodeId);
this.socket.emit(`remove_node_response_${this.id}`, response);
}
handleRemoveNode(graphId, nodeId) {
this.removeNode(graphId, nodeId);
}
getNode(graphId, nodeId) {
let response = this.graphs[graphId].getNode(nodeId);
this.socket.emit(`get_node_response_${this.id}`, response);
}
handleGetNode(graphId, nodeId) {
this.getNode(graphId, nodeId);
}
handleFind(label, query) {
let matches = Object.keys(this.graphs).map((key) => {
let graph = this.graphs[key];
return graph.label === label ? graph : null;
})
.filter((graph) => {
return graph;
})
.map((graph) => {
return Object.keys(graph.nodes).map((key) => {
let node = graph.nodes[key];
let data = node.data;
return Object.keys(data).map((dataKey) => {
if (!!query[dataKey]) {
return data[dataKey] === query[dataKey] ? node : null;
}
return null;
})
.filter((value) => {
return value;
});
})
.filter((node) => {
return node;
});
})
.filter((match) => {
return match;
})
.flatten();
this.socket.emit(`find_response_${this.socket.id}`, {
data: matches
});
}
}
module.exports = Network;