summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/serpentity.js2
-rw-r--r--lib/serpentity/node_collection.js19
2 files changed, 19 insertions, 2 deletions
diff --git a/lib/serpentity.js b/lib/serpentity.js
index 0d932ac..60fb23c 100644
--- a/lib/serpentity.js
+++ b/lib/serpentity.js
@@ -223,7 +223,7 @@ const Serpentity = class Serpentity {
nodeCollection.add(entity);
}
- return nodeCollection.nodes;
+ return nodeCollection;
}
/*
diff --git a/lib/serpentity/node_collection.js b/lib/serpentity/node_collection.js
index 8da41da..c603d6f 100644
--- a/lib/serpentity/node_collection.js
+++ b/lib/serpentity/node_collection.js
@@ -1,5 +1,7 @@
'use strict';
+const Events = require('events');
+
/*
* Node Collections contain nodes, in order to keep the lists of nodes
* that belong to each type.
@@ -8,10 +10,12 @@
* instances of that class.
*/
-const NodeCollection = class NodeCollection {
+const NodeCollection = class NodeCollection extends Events {
constructor(config) {
+ super();
+
this.nodes = [];
this.type = null;
@@ -39,6 +43,7 @@ const NodeCollection = class NodeCollection {
}
this.nodes.push(node);
+ this.emit('nodeAdded', { node });
return true;
}
@@ -54,17 +59,20 @@ const NodeCollection = class NodeCollection {
remove(entity) {
let foundIndex = -1;
+ let foundNode = null;
const found = this.nodes.some((node, i) => {
if (node.entity === entity) {
foundIndex = i;
+ foundNode = node;
return true;
}
});
if (found) {
this.nodes.splice(foundIndex, 1);
+ this.emit('nodeRemoved', { node: foundNode });
}
return found;
@@ -87,4 +95,13 @@ const NodeCollection = class NodeCollection {
}
};
+
+/*
+ * Make the node collection iterable without returning the array directly
+ */
+NodeCollection.prototype[Symbol.iterator] = function * () {
+
+ yield* this.nodes;
+};
+
module.exports = NodeCollection;