Emitter
Object of event emitters that use observable to transmit data based off of the example at https://goo.gl/gw1Z3I with a few more bells and whistles.
Example:
let emitter = new Emitter();
let subcription = emitter.listen('data', function (data) {
console.log('data: ' + data);
});
emitter.emit('data', 'foo');
// => data: foo
// Destroy the subscription
subscription.dispose();
TODO:
- write lost data method for events for testing.
Constructor Summary
Public Constructor | ||
public |
constructor(lostCount: Number) |
Method Summary
Public Methods | ||
public |
Emitter.emit: Emits event to a subscribed listener |
|
public |
hasObserver(name: String): Boolean Emitter.hasObserver: Access the hasObserver property to check if subject has an observer. |
|
public |
Emitter.listSubjects: Returns a list of current Subjects. |
|
public |
Emitter.listen: listens for an event name and handles it via the handle callback function. |
|
public |
Emitter.unlisten: unlisten from a single Subject |
|
public |
Emitter.unlistenAll: unlisten from all Subjects |
Public Constructors
Public Methods
public emit(name: String, data: *) source
Emitter.emit: Emits event to a subscribed listener
Params:
Name | Type | Attribute | Description |
name | String | Name of variable |
|
data | * | Any value to transmit |
Example:
let emitter = new Emitter();
let subcription = emitter.listen('data', function (data) {
console.log('data: ' + data);
});
emitter.emit('data', 'foo');
// => data: foo
public hasObserver(name: String): Boolean source
Emitter.hasObserver: Access the hasObserver property to check if subject has an observer. This comes in handy in testing.
Params:
Name | Type | Attribute | Description |
name | String | Name of variable |
Example:
let emitter = new Emitter();
console.log(emitter.hasObservers(data));
// => false
var subscription = emitter.listen(
(x) => {
console.log(x);
});
console.log(emitter.hasObservers(data));
// => true
public listSubjects(): Array source
Emitter.listSubjects: Returns a list of current Subjects. Each will have a preceding $ tag.
Example:
let emitter = new Emitter();
let subcription = emitter.listen('data', function (data) {
console.log('data: ' + data);
});
console.log(emitter.listSubjects());
// => ['$data']
public listen(name: String, handler: Function): Function source
Emitter.listen: listens for an event name and handles it via the handle callback function. It is the subscribe method of an observable. If you wish to handle error and have a complete function use method subject
Example:
let emitter = new Emitter();
let subcription = emitter.listen('data', function (data) {
console.log('data: ' + data);
});
subcription.dispose()
public unlisten(name: String) source
Emitter.unlisten: unlisten from a single Subject
Params:
Name | Type | Attribute | Description |
name | String | Name of variable |
Example:
let emitter = new Emitter();
emitter.listen('data1', (data)=> console.log(data));
emitter.listen('data2', (data)=> console.log(data));
console.log(emitter.listSubjects());
// => ['$data1', '$data2']
emitter.unlisten('data1');
console.log(emitter.listSubjects());
// => ['$data2']
public unlistenAll() source
Emitter.unlistenAll: unlisten from all Subjects
Example:
let emitter = new Emitter();
emitter.listen('data1', (data) => console.log(data));
emitter.listen('data2', (data) => console.log(data));
emitter.listen('data3', (data) => console.log(data));
console.log(emitter.listSubjects());
// => ['$data1', '$data2', '$data3']
emitter.unlistenAll();
console.log(emitter.listSubjects());
// => []