metal-events
Advanced tools
Comparing version 1.0.0-rc.1 to 1.0.0-rc.2
@@ -57,2 +57,11 @@ 'use strict'; | ||
/** | ||
* A list of events that are pending to be listened by an actual origin | ||
* emitter. Events are stored here when the origin doesn't exist, so they | ||
* can be set on a new origin when one is set. | ||
* @type {!Array} | ||
* @protected | ||
*/ | ||
_this.pendingEvents_ = []; | ||
/** | ||
* Holds a map of events from the origin emitter that are already being proxied. | ||
@@ -146,3 +155,3 @@ * @type {Object<string, !EventHandle>} | ||
if (this.shouldProxyEvent_(event)) { | ||
this.proxiedEvents_[event] = this.addListenerForEvent_(event); | ||
this.tryToAddListener_(event); | ||
} | ||
@@ -164,2 +173,3 @@ } | ||
this.proxiedEvents_ = {}; | ||
this.pendingEvents_ = []; | ||
} | ||
@@ -171,2 +181,3 @@ | ||
* them on the new emitter instead. | ||
* @param {!EventEmitter} originEmitter | ||
*/ | ||
@@ -177,10 +188,10 @@ | ||
value: function setOriginEmitter(originEmitter) { | ||
var handles = this.proxiedEvents_; | ||
var _this2 = this; | ||
var events = this.originEmitter_ ? Object.keys(this.proxiedEvents_) : this.pendingEvents_; | ||
this.removeListeners_(); | ||
this.originEmitter_ = originEmitter; | ||
var events = Object.keys(handles); | ||
for (var i = 0; i < events.length; i++) { | ||
this.proxiedEvents_[events[i]] = this.addListenerForEvent_(events[i]); | ||
} | ||
events.forEach(function (event) { | ||
return _this2.tryToAddListener_(event); | ||
}); | ||
} | ||
@@ -217,2 +228,19 @@ | ||
} | ||
/** | ||
* Adds a listener to the origin emitter, if it exists. Otherwise, stores | ||
* the pending listener so it can be used on a future origin emitter. | ||
* @param {string} event | ||
* @protected | ||
*/ | ||
}, { | ||
key: 'tryToAddListener_', | ||
value: function tryToAddListener_(event) { | ||
if (this.originEmitter_) { | ||
this.proxiedEvents_[event] = this.addListenerForEvent_(event); | ||
} else { | ||
this.pendingEvents_.push(event); | ||
} | ||
} | ||
}]); | ||
@@ -219,0 +247,0 @@ |
{ | ||
"name": "metal-events", | ||
"version": "1.0.0-rc.1", | ||
"description": "Classes responsible for emitting and listening to events", | ||
"license": "BSD", | ||
"repository": "metal/metal-events", | ||
"engines": { | ||
"node": ">=0.12.0", | ||
"npm": ">=3.0.0" | ||
}, | ||
"main": "lib/events.js", | ||
"jsnext:main": "src/events.js", | ||
"files": [ | ||
"lib", | ||
"src", | ||
"test" | ||
], | ||
"scripts": { | ||
"compile": "babel --presets es2015 -d lib/ src/", | ||
"prepublish": "npm run compile", | ||
"test": "gulp test" | ||
}, | ||
"keywords": [ | ||
"metal" | ||
], | ||
"dependencies": { | ||
"name": "metal-events", | ||
"version": "1.0.0-rc.2", | ||
"description": "Classes responsible for emitting and listening to events", | ||
"license": "BSD", | ||
"repository": "metal/metal-events", | ||
"engines": { | ||
"node": ">=0.12.0", | ||
"npm": ">=3.0.0" | ||
}, | ||
"main": "lib/events.js", | ||
"jsnext:main": "src/events.js", | ||
"files": [ | ||
"lib", | ||
"src", | ||
"test" | ||
], | ||
"scripts": { | ||
"compile": "babel --presets es2015 -d lib/ src/", | ||
"prepublish": "npm run compile", | ||
"test": "gulp test" | ||
}, | ||
"keywords": [ | ||
"metal" | ||
], | ||
"dependencies": { | ||
"bootstrap": "^3.3.6", | ||
"metal": "^1.0.0-rc.1" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.4.5", | ||
"babel-preset-es2015": "^6.3.13", | ||
"gulp": "^3.8.11", | ||
"gulp-metal": "^1.0.0" | ||
} | ||
"metal": "^1.0.0-rc.1" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.4.5", | ||
"babel-preset-es2015": "^6.3.13", | ||
"gulp": "^3.8.11", | ||
"gulp-metal": "^1.0.0", | ||
"sinon": "^1.17.3" | ||
} | ||
} |
@@ -39,2 +39,11 @@ 'use strict'; | ||
/** | ||
* A list of events that are pending to be listened by an actual origin | ||
* emitter. Events are stored here when the origin doesn't exist, so they | ||
* can be set on a new origin when one is set. | ||
* @type {!Array} | ||
* @protected | ||
*/ | ||
this.pendingEvents_ = []; | ||
/** | ||
* Holds a map of events from the origin emitter that are already being proxied. | ||
@@ -111,3 +120,3 @@ * @type {Object<string, !EventHandle>} | ||
if (this.shouldProxyEvent_(event)) { | ||
this.proxiedEvents_[event] = this.addListenerForEvent_(event); | ||
this.tryToAddListener_(event); | ||
} | ||
@@ -126,2 +135,3 @@ } | ||
this.proxiedEvents_ = {}; | ||
this.pendingEvents_ = []; | ||
} | ||
@@ -133,12 +143,11 @@ | ||
* them on the new emitter instead. | ||
* @param {!EventEmitter} originEmitter | ||
*/ | ||
setOriginEmitter(originEmitter) { | ||
var handles = this.proxiedEvents_; | ||
var events = this.originEmitter_? | ||
Object.keys(this.proxiedEvents_) : | ||
this.pendingEvents_; | ||
this.removeListeners_(); | ||
this.originEmitter_ = originEmitter; | ||
var events = Object.keys(handles); | ||
for (var i = 0; i < events.length; i++) { | ||
this.proxiedEvents_[events[i]] = this.addListenerForEvent_(events[i]); | ||
} | ||
events.forEach(event => this.tryToAddListener_(event)); | ||
} | ||
@@ -169,4 +178,18 @@ | ||
} | ||
/** | ||
* Adds a listener to the origin emitter, if it exists. Otherwise, stores | ||
* the pending listener so it can be used on a future origin emitter. | ||
* @param {string} event | ||
* @protected | ||
*/ | ||
tryToAddListener_(event) { | ||
if (this.originEmitter_) { | ||
this.proxiedEvents_[event] = this.addListenerForEvent_(event); | ||
} else { | ||
this.pendingEvents_.push(event); | ||
} | ||
} | ||
} | ||
export default EventEmitterProxy; |
@@ -119,2 +119,35 @@ 'use strict'; | ||
it('should not throw error if origin emitter is set to null', function() { | ||
var origin = new EventEmitter(); | ||
var target = new EventEmitter(); | ||
var proxy = new EventEmitterProxy(origin, target); | ||
var listener = sinon.stub(); | ||
target.on('event1', listener); | ||
assert.doesNotThrow(() => proxy.setOriginEmitter(null)); | ||
}); | ||
it('should pass proxied events to new origin emitters, even when no emitter exists for a while', function() { | ||
var origin = new EventEmitter(); | ||
var target = new EventEmitter(); | ||
var proxy = new EventEmitterProxy(origin, target); | ||
var listener = sinon.stub(); | ||
target.on('event1', listener); | ||
proxy.setOriginEmitter(null); | ||
var origin2 = new EventEmitter(); | ||
proxy.setOriginEmitter(origin2); | ||
origin.emit('event1', 1, 2); | ||
assert.strictEqual(0, listener.callCount); | ||
origin2.emit('event1', 1, 2); | ||
assert.strictEqual(1, listener.callCount); | ||
assert.strictEqual(1, listener.args[0][0]); | ||
assert.strictEqual(2, listener.args[0][1]); | ||
}); | ||
it('should allow manually choosing events to be proxied', function() { | ||
@@ -121,0 +154,0 @@ var origin = new EventEmitter(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
72195
1942
5