generic broadcaster service
This commit is contained in:
parent
39b2ba1548
commit
6dc44c0885
|
@ -0,0 +1,5 @@
|
||||||
|
export abstract class BroadcasterService {
|
||||||
|
send: (message: any, id?: string) => void;
|
||||||
|
subscribe: (id: string, messageCallback: (message: any) => any) => void;
|
||||||
|
unsubscribe: (id: string) => void;
|
||||||
|
}
|
|
@ -1,33 +1,7 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
|
import { BroadcasterService as BaseBroadcasterService } from '../../services/broadcaster.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class BroadcasterService {
|
export class BroadcasterService extends BaseBroadcasterService {
|
||||||
subscribers: Map<string, (message: any) => any> = new Map<string, (message: any) => any>();
|
|
||||||
|
|
||||||
send(message: any, id?: string) {
|
|
||||||
if (id != null) {
|
|
||||||
if (this.subscribers.has(id)) {
|
|
||||||
this.subscribers.get(id)(message);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.subscribers.forEach((value) => {
|
|
||||||
value(message);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
subscribe(id: string, messageCallback: (message: any) => any) {
|
|
||||||
if (this.subscribers.has(id)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.subscribers.set(id, messageCallback);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsubscribe(id: string) {
|
|
||||||
if (this.subscribers.has(id)) {
|
|
||||||
this.subscribers.delete(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
import { BroadcasterService as BroadcasterServiceAbstraction } from '../abstractions/broadcaster.service';
|
||||||
|
|
||||||
|
export class BroadcasterService implements BroadcasterServiceAbstraction {
|
||||||
|
subscribers: Map<string, (message: any) => any> = new Map<string, (message: any) => any>();
|
||||||
|
|
||||||
|
send(message: any, id?: string) {
|
||||||
|
if (id != null) {
|
||||||
|
if (this.subscribers.has(id)) {
|
||||||
|
this.subscribers.get(id)(message);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.subscribers.forEach((value) => {
|
||||||
|
value(message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
subscribe(id: string, messageCallback: (message: any) => any) {
|
||||||
|
if (this.subscribers.has(id)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.subscribers.set(id, messageCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsubscribe(id: string) {
|
||||||
|
if (this.subscribers.has(id)) {
|
||||||
|
this.subscribers.delete(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue