Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { BehaviorSubject } from 'rxjs';
  3. import { UserRestService } from '../../services/user.rest.service';
  4.  
  5. @Injectable()
  6. export class DirectoryService {
  7.   users: any[] = [];
  8.   users$: BehaviorSubject<Object[]>;
  9.  
  10.   constructor(
  11.    private userRestService: UserRestService
  12.   ) {
  13.     this.users$ = new BehaviorSubject(this.users);
  14.     this.__uploadUsers();
  15.   }
  16.  
  17.   protected __uploadUsers():void {
  18.     let uploader = () => {
  19.       let recentDate = this.users.length ? new Date(this.users[this.users.length - 1].ModifiedTime) : new Date(0);
  20.  
  21.       this.userRestService.getUsersFrom(recentDate, 100)
  22.         .subscribe(
  23.           (value: Object[]) => {
  24.             if (value && value.length) {
  25.               this.__uploadAvatars(value);
  26.               this.users = this.users.concat(value);
  27.               this.users$.next(this.users);
  28.               uploader();
  29.             } else {
  30.               this.users$.complete();
  31.             }
  32.           }
  33.         );
  34.     };
  35.  
  36.     uploader();
  37.   }
  38.  
  39.   protected __uploadAvatars(users: Object[]):void {
  40.     users.forEach((user: any) => {
  41.       user.userImage = this.userRestService.getUserPhotoById(user.Id);
  42.     });
  43.   }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement