From 4908bfb50681ed410dc557005033959457e05d2f Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Sat, 21 Apr 2018 02:49:42 -0500 Subject: Emit events --- lib/serpentity/node_collection.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/serpentity/node_collection.js b/lib/serpentity/node_collection.js index 8da41da..bb66a8d 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,7 +10,7 @@ * instances of that class. */ -const NodeCollection = class NodeCollection { +const NodeCollection = class NodeCollection extends Events { constructor(config) { @@ -39,6 +41,7 @@ const NodeCollection = class NodeCollection { } this.nodes.push(node); + this.emit('nodeAdded', { node }); return true; } @@ -65,6 +68,7 @@ const NodeCollection = class NodeCollection { if (found) { this.nodes.splice(foundIndex, 1); + this.emit('nodeRemoved', { node }); } return found; -- cgit From 2a2e1583f260830fba4ac7c319b2b8f7bb5a34b8 Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Sat, 21 Apr 2018 02:51:20 -0500 Subject: Call super when constructing node collection --- lib/serpentity/node_collection.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib') diff --git a/lib/serpentity/node_collection.js b/lib/serpentity/node_collection.js index bb66a8d..73998b1 100644 --- a/lib/serpentity/node_collection.js +++ b/lib/serpentity/node_collection.js @@ -14,6 +14,8 @@ const NodeCollection = class NodeCollection extends Events { constructor(config) { + super(); + this.nodes = []; this.type = null; -- cgit From 17e4efc7eeb88d7764582bea124c71989fe1f1cb Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Sat, 21 Apr 2018 03:11:47 -0500 Subject: Yields nodeCollection instead of nodes array --- lib/serpentity.js | 2 +- lib/serpentity/node_collection.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'lib') 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 73998b1..7c2c627 100644 --- a/lib/serpentity/node_collection.js +++ b/lib/serpentity/node_collection.js @@ -93,4 +93,13 @@ const NodeCollection = class NodeCollection extends Events { } }; + +/* + * Make the node collection iterable without returning the array directly + */ +NodeCollection.prototype[Symbol.iterator] = function * () { + + yield* this.nodes; +}; + module.exports = NodeCollection; -- cgit From b35f7b4b2eb720a3227c512ed14f76747f206ff2 Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Sat, 21 Apr 2018 03:15:04 -0500 Subject: Correctly emit the node on remove --- lib/serpentity/node_collection.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/serpentity/node_collection.js b/lib/serpentity/node_collection.js index 7c2c627..deeb26d 100644 --- a/lib/serpentity/node_collection.js +++ b/lib/serpentity/node_collection.js @@ -59,18 +59,20 @@ const NodeCollection = class NodeCollection extends Events { 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 }); + this.emit('nodeRemoved', { foundNode }); } return found; -- cgit From a2e3955172fea7ca30fe6d7e710157a8a00d9bbc Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Sat, 21 Apr 2018 03:23:31 -0500 Subject: Use the key node on nodeRemoved event --- lib/serpentity/node_collection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/serpentity/node_collection.js b/lib/serpentity/node_collection.js index deeb26d..c603d6f 100644 --- a/lib/serpentity/node_collection.js +++ b/lib/serpentity/node_collection.js @@ -72,7 +72,7 @@ const NodeCollection = class NodeCollection extends Events { if (found) { this.nodes.splice(foundIndex, 1); - this.emit('nodeRemoved', { foundNode }); + this.emit('nodeRemoved', { node: foundNode }); } return found; -- cgit