typed-event-emitter
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "typed-event-emitter", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Alternative event emitter for JavaScript and TypeScript.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,4 +10,11 @@ # typed-event-emitter | ||
## Install | ||
Via npm: | ||
$ npm install typed-event-emitter | ||
## Usage | ||
Take a look at the following snippet (TypeScript): | ||
@@ -48,7 +55,7 @@ | ||
First the *EventEmitter* is loaded from the module. Next, any class, that shall | ||
emit events, must extend that *EventEmitter*. If your class has its own | ||
constructor, make sure to call `super()`. | ||
First, the *EventEmitter* is loaded from the module. Any class, that shall emit | ||
events, must extend that *EventEmitter*. If your class has its own constructor, | ||
make sure to call `super()`. | ||
Any events, your class could emit, must be registered in the form: | ||
Any events, your class shall be able to emit, must be registered in the form: | ||
@@ -69,4 +76,46 @@ ~~~TypeScript | ||
### JavaScript | ||
Your JavaScript host (i.e., your browser, node.js, etc.) should support classes | ||
and inheritance in order to work correctly. The code shown above can also be | ||
written in JavaScript (node.js): | ||
~~~JavaScript | ||
const EventEmitter = require('typed-event-emitter').EventEmitter; | ||
class MyClass extends EventEmitter { | ||
constructor(value) { | ||
// initialize EventEmitter | ||
super(); | ||
/* newValue: number */ | ||
this.onValueChanged = this.registerEvent(); | ||
this._value = value; | ||
} | ||
get value() { | ||
return this._value; | ||
} | ||
set value(value) { | ||
this._value = value; | ||
this.emit(this.onValueChanged, this._value); | ||
} | ||
} | ||
let instance = new MyClass(); | ||
instance.onValueChanged(newValue => { | ||
console.log(`Value changed: ${newValue}`); | ||
}); | ||
instance.value = 27; | ||
~~~ | ||
Node that the events are registered explicitly within the constructor. Make sure | ||
to initialize them *after* calling `super()`. | ||
## License | ||
typed-event-emitter is licensed under the ISC License. |
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
6954
118