Skip to content

Instantly share code, notes, and snippets.

@nerdic-coder
Created April 28, 2018 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nerdic-coder/f7837e48f9dfc22621d92b80d0dae04e to your computer and use it in GitHub Desktop.
Save nerdic-coder/f7837e48f9dfc22621d92b80d0dae04e to your computer and use it in GitHub Desktop.
message.service.ts
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