Hi Oleg,
I deleted my previous post. the sample was riddled with typos. Should not
be typing stuff like that without a proper editor. the next version is
tested in a small project, to communicate between different layers of the
application. It's the same code, but then cleaned up.
I still don't like the idea of using DOM events, because they are not
available anywhere outside the browser. So you can't use Angular universal
if you do that.
ok, here is a slightly better sample:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
export type Payload = any;
export interface EventBusMessage {
channel: string;
payload: Payload;
}
@Injectable()
export class EventBusService {
public listen: Observable<Payload>;
private next;
constructor() {
this.listen = new Observable(observer => {
this.next = (payload: Payload) => observer.next(payload);
return _ => _ /* nothing to clean */;
});
this.channelEmmiter = this.channelEmmiter.bind(this);
this.emit = this.emit.bind(this);
}
allChannels(): Observable<EventBusMessage> {
return this.listen;
}
channel(channelName: string): Observable<Payload> {
return this.listen
.filter(e => e.channel === channelName)
.map(e => e.payload);
}
channelEmmiter(channelName) {
console.log('made listener for', channelName);
return (payload: Payload): void =>
this.emit({ channel: channelName, payload });
}
emit({ channel, payload }: { channel: string; payload: any }): void {
console.log('emit', channel);
this.next({ channel, payload });
}
}
Regards
Sander
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.