Created
April 28, 2018 16:50
-
-
Save nerdic-coder/f7837e48f9dfc22621d92b80d0dae04e to your computer and use it in GitHub Desktop.
message.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Observable, Subject } from 'rxjs'; | |
export class MessageService { | |
private static _instance: MessageService; | |
private messages: string[] = []; | |
private subject: Subject<string[]> = new Subject(); | |
add(message: string) { | |
this.messages.push(message); | |
this.subject.next(this.messages); | |
} | |
clear() { | |
this.messages = []; | |
this.subject.next(this.messages); | |
} | |
getMessages(): Observable<string[]> { | |
return this.subject.asObservable(); | |
} | |
public static get Instance(): MessageService { | |
// Do you need arguments? Make it a regular method instead. | |
return this._instance || (this._instance = new this()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment