Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //home.ts
- export class Home implements OnInit, OnDestroy{
- conversations: Array<any>;
- async ngOnInit() {
- this._chatInit();
- }
- private async _chatInit() {
- this.dataService.loadConversations(); //getter for local storage
- const data = this.messageStorage.hasChats();
- if (data.length) {
- //there is data already loaded
- this.conversations = data;
- } else {
- //there is an empty array, subscribe to it.
- this.messageStorage
- .getChatList$()
- .subscribe(conversation => {
- console.log('Conversation', conversation)
- this.conversations = conversation;
- });
- }
- }
- //dataService
- export class DataService {
- //the object where it is all stored
- private conversations: any = {
- peerToPeer: {},
- groups: {},
- };
- private listOfChats: Array<any> = new Array<any>();
- private bsListOfChats$: BehaviorSubject<any> = new BehaviorSubject<any>(this.listOfChats);
- public loadConversations() {
- this.storage.get('conversations').then(chat=> {
- this.conversations = chat;
- this.formatChats(this.conversations);//converts the Object to an Array so *ngFor directive can be used
- });
- }
- public hasChats() {
- return this.listOfChats;
- }
- public getChatList$() {
- return this.bsListOfChats$;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement