mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
fix(events): a few tweaks
This commit is contained in:
@ -25,12 +25,21 @@ class MyApp {
|
|||||||
|
|
||||||
console.log('Events', events);
|
console.log('Events', events);
|
||||||
|
|
||||||
events.subscribe('user:created', (user) => {
|
let handler = (user) => {
|
||||||
console.log('User created', user);
|
console.log('User created', user);
|
||||||
return {
|
return {
|
||||||
what: 'what'
|
what: 'what'
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
let handler2 = (user) => {
|
||||||
|
console.log('2User created', user);
|
||||||
|
return {
|
||||||
|
things: 'yes'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
events.subscribe('user:created', handler);
|
||||||
|
events.subscribe('user:created', handler2);
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
var results = events.publish('user:created', {
|
var results = events.publish('user:created', {
|
||||||
|
@ -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.
|
* no longer receive events published to this topic.
|
||||||
*
|
*
|
||||||
* @param topic the topic to unsubscribe from
|
* @param topic the topic to unsubscribe from
|
||||||
@ -38,12 +38,25 @@ export class Events {
|
|||||||
unsubscribe(topic, handler) {
|
unsubscribe(topic, handler) {
|
||||||
let t = this.channels[topic];
|
let t = this.channels[topic];
|
||||||
if(!t) {
|
if(!t) {
|
||||||
|
// Wasn't found, wasn't removed
|
||||||
return false;
|
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);
|
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 the channel is empty now, remove it from the channel map
|
||||||
if(!t.length) {
|
if(!t.length) {
|
||||||
|
Reference in New Issue
Block a user