Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
fix(redesignusers): redesigns user service to use observables instead…
Browse files Browse the repository at this point in the history
… of promises

fixes: issue 5
  • Loading branch information
dgutride authored and joshuawilson committed Mar 6, 2017
1 parent 18e61bd commit 4ce60a0
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/app/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ import { Injectable, Inject } from '@angular/core';
import { Headers, Http } from '@angular/http';

import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/catch';

import { Broadcaster } from '../shared/broadcaster.service';
import { AuthenticationService } from '../auth/authentication.service';
import { Logger } from '../shared/logger.service';
import { AUTH_API_URL } from '../shared/auth-api';
import { User } from './user';
import { Observable } from 'rxjs';

//import { ProfileService } from './../profile/profile.service';
/**
* Provides user and user list methods to retrieve current or user list details
*
* The UserService should be injected at the root of the application to ensure it is a singleton
* getUser and getAllUsers return observables that can be subscribed to for information
*/

@Injectable()
export class UserService {
Expand Down Expand Up @@ -44,19 +51,21 @@ export class UserService {
return this.allUserData;
}

getUser(): Promise<User> {
/**
* Get currently logged in user
*
* @returns Observable<User>
*/
getUser(): Observable<User> {
// Check if we have the user data if not then check if the user is logged in.
// We need the auth key to get the user data. So either we already the data or we don't have the keys
// in either case don't try to get the data.
if (Object.keys(this.userData).length || !this.auth.isLoggedIn()) {
return new Promise((resolve, reject) => {
resolve(this.userData);
});
return Observable.create(this.userData);
} else {
return this.http
.get(this.userUrl, {headers: this.headers})
.toPromise()
.then(response => {
.map(response => {
let userData = response.json().data as User;
// The reference of this.userData is
// being used in Header
Expand All @@ -70,27 +79,26 @@ export class UserService {
};
this.userData.id = userData.id;
// this.profile.initDefaults(this.userData);
this.broadcaster.broadcast('currentUserInit', this.userData);
return this.userData;
})
.catch(() => {});
});
}
}

getAllUsers(): Promise<User[]> {
/**
* Get all users
*
* @returns Observable<User[]>
*/
getAllUsers(): Observable<User[]> {
if (this.allUserData.length) {
return new Promise((resolve, reject) => {
resolve(this.allUserData);
});
return Observable.create(this.allUserData);
} else {
return this.http
.get(this.usersUrl, {headers: this.headers})
.toPromise()
.then(response => {
.map(response => {
this.allUserData = response.json().data as User[];
return this.allUserData;
})
.catch(() => {});
});
}
}

Expand Down

0 comments on commit 4ce60a0

Please sign in to comment.