fix(events): a few tweaks

This commit is contained in:
Max Lynch
2015-09-23 14:19:24 -05:00
parent 2ab2024227
commit fb9fb47c37
2 changed files with 26 additions and 4 deletions

View File

@ -25,12 +25,21 @@ class MyApp {
console.log('Events', events);
events.subscribe('user:created', (user) => {
let handler = (user) => {
console.log('User created', user);
return {
what: 'what'
}
})
}
let handler2 = (user) => {
console.log('2User created', user);
return {
things: 'yes'
}
}
events.subscribe('user:created', handler);
events.subscribe('user:created', handler2);
setInterval(() => {
var results = events.publish('user:created', {

View File

@ -27,7 +27,7 @@ export class Events {
}
/**
* Unsubscribe the given handler from the given topic. Your handler will
* Unsubscribe from the given topic. Your handler will
* no longer receive events published to this topic.
*
* @param topic the topic to unsubscribe from
@ -38,12 +38,25 @@ export class Events {
unsubscribe(topic, handler) {
let t = this.channels[topic];
if(!t) {
// Wasn't found, wasn't removed
return false;
}
if(!handler) {
// Remove all handlers for this topic
delete this.channels[topic];
return true;
}
// We need to find and remove a specific handler
let i = t.indexOf(handler);
t.splice(t.indexOf(handler), 1);
if(i < 0) {
// Wasn't found, wasn't removed
return false;
}
t.splice(i, 1);
// If the channel is empty now, remove it from the channel map
if(!t.length) {