This commit is contained in:
Max Lynch
2015-09-23 14:04:05 -05:00
parent 27373e0448
commit 2ab2024227
2 changed files with 12 additions and 5 deletions

View File

@@ -27,13 +27,17 @@ class MyApp {
events.subscribe('user:created', (user) => {
console.log('User created', user);
return {
what: 'what'
}
})
setInterval(() => {
events.publish('user:created', {
var results = events.publish('user:created', {
name: 'Max Lynch',
id: 1
})
console.log('Got results', results);
}, 2000);
setTimeout(() => {

View File

@@ -1,13 +1,12 @@
import {Injectable} from 'angular2/angular2';
/**
* Event is a pub/sub style event system for sending and responding to application-level
* Events is a pub/sub style event system for sending and responding to application-level
* events across your app.
*/
@Injectable()
export class Events {
constructor() {
console.log('Events constructed');
this.channels = [];
}
@@ -33,13 +32,17 @@ export class Events {
*
* @param topic the topic to unsubscribe from
* @param handler the event handler
*
* @return true if a handler was removed
*/
unsubscribe(topic, handler) {
var t = this.channels[topic];
let t = this.channels[topic];
if(!t) {
return null;
return false;
}
let i = t.indexOf(handler);
t.splice(t.indexOf(handler), 1);
// If the channel is empty now, remove it from the channel map