diff --git a/demos/native/index.ts b/demos/native/index.ts index 4512d43387..b129a21690 100644 --- a/demos/native/index.ts +++ b/demos/native/index.ts @@ -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', { diff --git a/ionic/util/events.ts b/ionic/util/events.ts index 215f283f66..afc2921710 100644 --- a/ionic/util/events.ts +++ b/ionic/util/events.ts @@ -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) {