diff --git a/red/nodes/Node.js b/red/nodes/Node.js index 9cec99133..3f2115530 100644 --- a/red/nodes/Node.js +++ b/red/nodes/Node.js @@ -28,6 +28,8 @@ function Node(n) { this.id = n.id; this.type = n.type; this.z = n.z; + this._closeCallbacks = []; + if (n.name) { this.name = n.name; } @@ -64,23 +66,35 @@ Node.prototype._on = Node.prototype.on; Node.prototype.on = function(event, callback) { var node = this; if (event == "close") { - if (callback.length == 1) { - this.close = function() { - return when.promise(function(resolve) { - callback.call(node, function() { - resolve(); - }); - }); - }; - } else { - this.close = callback; - } + this._closeCallbacks.push(callback); } else { this._on(event, callback); } }; -Node.prototype.close = function() {}; +Node.prototype.close = function() { + var promises = []; + var node = this; + for (var i=0;i 0) { + return when.settle(promises); + } else { + return; + } +}; function constructUniqueIdentifier() { return (1+Math.random()*4294967295).toString(16); diff --git a/test/red/nodes/Node_spec.js b/test/red/nodes/Node_spec.js index 4d2fcd8d8..e582f3599 100644 --- a/test/red/nodes/Node_spec.js +++ b/test/red/nodes/Node_spec.js @@ -1,5 +1,5 @@ /** - * Copyright 2014 IBM Corp. + * Copyright 2014, 2015 IBM Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,6 +73,34 @@ describe('Node', function() { testdone(); }); }); + + it('allows multiple close handlers to be registered',function(testdone) { + var n = new RedNode({id:'123',type:'abc'}); + var callbacksClosed = 0; + n.on('close',function(done) { + setTimeout(function() { + callbacksClosed++; + done(); + },200); + }); + n.on('close',function(done) { + setTimeout(function() { + callbacksClosed++; + done(); + },200); + }); + n.on('close',function() { + callbacksClosed++; + }); + var p = n.close(); + should.exist(p); + p.then(function() { + callbacksClosed.should.eql(3); + testdone(); + }).otherwise(function(e) { + testdone(e); + }); + }); });