mirror of
https://github.com/farcasclaudiu/LanBackup.git
synced 2026-06-28 11:00:57 +03:00
36 lines
849 B
TypeScript
36 lines
849 B
TypeScript
import { Injectable } from '@angular/core';
|
|
import { Subject } from 'rxjs/Subject';
|
|
import { Subscription } from 'rxjs/Subscription';
|
|
import 'rxjs/add/operator/filter'
|
|
import 'rxjs/add/operator/map'
|
|
|
|
|
|
type MessageCallback = (payload: any) => void;
|
|
|
|
@Injectable()
|
|
export class MessageService {
|
|
private handler = new Subject<Message>();
|
|
|
|
broadcast(type: string, payload: any) {
|
|
this.handler.next({ type, payload });
|
|
}
|
|
|
|
subscribe(type: string, callback: MessageCallback): Subscription {
|
|
return this.handler
|
|
.filter(message => message.type === type)
|
|
.map(message => message.payload)
|
|
.subscribe(callback);
|
|
}
|
|
}
|
|
|
|
|
|
interface Message {
|
|
type: string;
|
|
payload: any;
|
|
}
|
|
|
|
export class Messages {
|
|
static readonly MESSAGE_NOTIFY: string = 'notify';
|
|
static readonly MESSAGE_REFRESHAGENTS: string = 'refreshagents';
|
|
|
|
} |