Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. //home.ts
  2.  
  3. export class Home implements OnInit, OnDestroy{
  4. conversations: Array<any>;
  5.  
  6. async ngOnInit() {
  7. this._chatInit();
  8. }
  9.  
  10. private async _chatInit() {
  11. this.dataService.loadConversations(); //getter for local storage
  12.  
  13. const data = this.messageStorage.hasChats();
  14.  
  15. if (data.length) {
  16. //there is data already loaded
  17. this.conversations = data;
  18. } else {
  19. //there is an empty array, subscribe to it.
  20. this.messageStorage
  21. .getChatList$()
  22. .subscribe(conversation => {
  23. console.log('Conversation', conversation)
  24. this.conversations = conversation;
  25. });
  26. }
  27. }
  28.  
  29. //dataService
  30. export class DataService {
  31.  
  32. //the object where it is all stored
  33. private conversations: any = {
  34. peerToPeer: {},
  35. groups: {},
  36. };
  37.  
  38. private listOfChats: Array<any> = new Array<any>();
  39. private bsListOfChats$: BehaviorSubject<any> = new BehaviorSubject<any>(this.listOfChats);
  40.  
  41. public loadConversations() {
  42. this.storage.get('conversations').then(chat=> {
  43. console.log('chat-->', chat);
  44. this.conversations = chat;
  45. this.formatChats(this.conversations);//converts the Object to an Array so *ngFor directive can be used
  46. });
  47. }
  48.  
  49. public hasChats() {
  50. return this.listOfChats;
  51. }
  52.  
  53. public getChatList$() {
  54. return this.bsListOfChats$;
  55. }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement