Files
LanBackup/src/LanBackup.WebApp/ClientApp/app/services/message.service.ts
T
2017-02-02 01:31:36 +02:00

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';
}