mirror of
https://github.com/urbit/shrub.git
synced 2024-11-28 22:33:06 +03:00
interface: added remove operations to graph reducer
This commit is contained in:
parent
90a6282b4e
commit
b5df4e1d71
@ -1,20 +1,14 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
|
|
||||||
const OrderedMap = (arr = []) => {
|
|
||||||
let map = new Map(arr);
|
|
||||||
// map[Symbol.iterator] = function* () {
|
|
||||||
// yield* [...this.entries()].sort((a, b) => a[1] - b[1]);
|
|
||||||
// };
|
|
||||||
return map;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const GraphReducer = (json, state) => {
|
export const GraphReducer = (json, state) => {
|
||||||
const data = _.get(json, 'graph-update', false);
|
const data = _.get(json, 'graph-update', false);
|
||||||
if (data) {
|
if (data) {
|
||||||
keys(data, state);
|
keys(data, state);
|
||||||
addGraph(data, state);
|
addGraph(data, state);
|
||||||
|
removeGraph(data, state);
|
||||||
addNodes(data, state);
|
addNodes(data, state);
|
||||||
|
removeNodes(data, state);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -25,7 +19,7 @@ const keys = (json, state) => {
|
|||||||
let resource = res.ship + '/' + res.name;
|
let resource = res.ship + '/' + res.name;
|
||||||
|
|
||||||
if (!(resource in state.graphs)) {
|
if (!(resource in state.graphs)) {
|
||||||
state.graphs[resource] = new OrderedMap();
|
state.graphs[resource] = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
return resource;
|
return resource;
|
||||||
@ -34,39 +28,16 @@ const keys = (json, state) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const addGraph = (json, state) => {
|
const addGraph = (json, state) => {
|
||||||
const data = _.get(json, 'add-graph', false);
|
|
||||||
if (data) {
|
|
||||||
if (!('graphs' in state)) {
|
|
||||||
state.graphs = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
let resource = data.resource.ship + '/' + data.resource.name;
|
const _processNode = (node) => {
|
||||||
state.graphs[resource] = new OrderedMap();
|
|
||||||
|
|
||||||
for (let i in data.graph) {
|
|
||||||
let item = data.graph[i];
|
|
||||||
let index = item[0].split('/').slice(1).map((ind) => {
|
|
||||||
return parseInt(ind, 10);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (index.length === 0) { break; }
|
|
||||||
|
|
||||||
let node = _processNode(item[1]);
|
|
||||||
state.graphs[resource].set(index[index.length - 1], node);
|
|
||||||
}
|
|
||||||
state.graphKeys.add(resource);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const _processNode = (node) => {
|
|
||||||
// is empty
|
// is empty
|
||||||
if (!node.children) {
|
if (!node.children) {
|
||||||
node.children = new OrderedMap();
|
node.children = new Map();
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
// is graph
|
// is graph
|
||||||
let converted = new OrderedMap();
|
let converted = new Map();
|
||||||
for (let i in node.children) {
|
for (let i in node.children) {
|
||||||
let item = node.children[i];
|
let item = node.children[i];
|
||||||
let index = item[0].split('/').slice(1).map((ind) => {
|
let index = item[0].split('/').slice(1).map((ind) => {
|
||||||
@ -82,8 +53,33 @@ const _processNode = (node) => {
|
|||||||
}
|
}
|
||||||
node.children = converted;
|
node.children = converted;
|
||||||
return node;
|
return node;
|
||||||
|
};
|
||||||
|
|
||||||
|
const data = _.get(json, 'add-graph', false);
|
||||||
|
if (data) {
|
||||||
|
if (!('graphs' in state)) {
|
||||||
|
state.graphs = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
let resource = data.resource.ship + '/' + data.resource.name;
|
||||||
|
state.graphs[resource] = new Map();
|
||||||
|
|
||||||
|
for (let i in data.graph) {
|
||||||
|
let item = data.graph[i];
|
||||||
|
let index = item[0].split('/').slice(1).map((ind) => {
|
||||||
|
return parseInt(ind, 10);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (index.length === 0) { break; }
|
||||||
|
|
||||||
|
let node = _processNode(item[1]);
|
||||||
|
state.graphs[resource].set(index[index.length - 1], node);
|
||||||
|
}
|
||||||
|
state.graphKeys.add(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
const removeGraph = (json, state) => {
|
const removeGraph = (json, state) => {
|
||||||
const data = _.get(json, 'remove-graph', false);
|
const data = _.get(json, 'remove-graph', false);
|
||||||
if (data) {
|
if (data) {
|
||||||
@ -96,6 +92,24 @@ const removeGraph = (json, state) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const addNodes = (json, state) => {
|
const addNodes = (json, state) => {
|
||||||
|
const _addNode = (graph, index, node) => {
|
||||||
|
// set child of graph
|
||||||
|
if (index.length === 1) {
|
||||||
|
graph.set(index[0], node);
|
||||||
|
return graph;
|
||||||
|
}
|
||||||
|
|
||||||
|
// set parent of graph
|
||||||
|
let parNode = graph.get(index[0]);
|
||||||
|
if (!parNode) {
|
||||||
|
console.error('parent node does not exist, cannot add child');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
parNode.children = _addNode(parNode.children, index.slice(1), node);
|
||||||
|
graph.set(index[0], parNode);
|
||||||
|
return graph;
|
||||||
|
};
|
||||||
|
|
||||||
const data = _.get(json, 'add-nodes', false);
|
const data = _.get(json, 'add-nodes', false);
|
||||||
if (data) {
|
if (data) {
|
||||||
if (!('graphs' in state)) { return; }
|
if (!('graphs' in state)) { return; }
|
||||||
@ -125,21 +139,26 @@ const addNodes = (json, state) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const _addNode = (graph, index, node) => {
|
const removeNodes = (json, state) => {
|
||||||
// set child of graph
|
const data = _.get(json, 'remove-nodes', false);
|
||||||
if (index.length === 1) {
|
if (data) {
|
||||||
graph.set(index[0], node);
|
console.log(data);
|
||||||
return graph;
|
if (!(data.resource in state.graphs)) { return; }
|
||||||
}
|
|
||||||
|
|
||||||
// set parent of graph
|
data.indices.forEach((index) => {
|
||||||
let parNode = graph.get(index[0]);
|
console.log(index);
|
||||||
if (!parNode) {
|
if (index.split('/').length === 0) { return; }
|
||||||
console.error('parent node does not exist, cannot add child');
|
let indexArr = index.split('/').slice(1).map((ind) => {
|
||||||
return;
|
return parseInt(ind, 10);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (indexArr.length === 1) {
|
||||||
|
state.graphs[data.resource].delete(indexArr[0]);
|
||||||
|
} else {
|
||||||
|
// TODO: recursive
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
parNode.children = _addNode(parNode.children, index.slice(1), node);
|
|
||||||
graph.set(index[0], parNode);
|
|
||||||
return graph;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user