generic broadcaster service

This commit is contained in:
Kyle Spearrin 2019-01-22 16:12:34 -05:00
parent 39b2ba1548
commit 6dc44c0885
3 changed files with 40 additions and 29 deletions

View File

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

View File

@ -1,33 +1,7 @@
import { Injectable } from '@angular/core';
import { BroadcasterService as BaseBroadcasterService } from '../../services/broadcaster.service';
@Injectable()
export class BroadcasterService {
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);
}
}
export class BroadcasterService extends BaseBroadcasterService {
}

View File

@ -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);
}
}
}