diff options
| author | Ben Beltran <ben@nsovocal.com> | 2018-04-21 03:26:31 -0500 |
|---|---|---|
| committer | Ben Beltran <ben@nsovocal.com> | 2018-04-21 03:26:31 -0500 |
| commit | 724fec20a20bc50dec32cb0f3c461140daf51c05 (patch) | |
| tree | 802c11608da9140e3567baba2a8aa06127552a96 /lib | |
| parent | b58bb0a6b290c8afe6d219897a547fa087b279d2 (diff) | |
| parent | 564951b9b728e9e2a4c46a571867faa28fd396a8 (diff) | |
Merge branch 'hotfix/2.1.0' into develop
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/serpentity.js | 2 | ||||
| -rw-r--r-- | lib/serpentity/node_collection.js | 19 |
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; |