Skip to content

Commit

Permalink
chore(users.ts): Use one subscribe
Browse files Browse the repository at this point in the history
Refactor to use only one subscribe in the constructor. Makes it easier to unsubscribe if needed.
  • Loading branch information
SekibOmazic committed Jan 6, 2016
1 parent 4e56f25 commit 1e9ece3
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/app/models/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,27 @@ export class Users {
}, []);
});

this.actions$.filter(action => action.type === ADD_USER)
let adds = this.actions$.filter(action => action.type === ADD_USER)
.do(() => this._store.dispatch({type: ADDING_USER}))
.mergeMap(action => this._http.post(this._url, JSON.stringify(action.payload)))
.map((res: Response) => res.json())
.subscribe(data => this._store.dispatch({type: ADDED_USER, payload: data}));
.map(data => ({type: ADDED_USER, payload: data}));

this.actions$.filter(action => action.type === LOAD_USERS)
let loads = this.actions$.filter(action => action.type === LOAD_USERS)
.do(() => this._store.dispatch({type: LOADING_USERS}))
.mergeMap(action => this._http.get(this._url))
.map((res: Response) => res.json())
.subscribe(data => this._store.dispatch({type: LOADED_USERS, payload: data}));
.map(data => ({type: LOADED_USERS, payload: data}));

this.actions$.filter(action => action.type === DELETE_USER && ! action.payload.deleting)
let deletes = this.actions$.filter(action => action.type === DELETE_USER && ! action.payload.deleting)
.do(action => this._store.dispatch({type: DELETING_USER, payload: action.payload}))
.mergeMap(action => this._http.delete(`${this._url}/${action.payload.id}`))
.map((res: Response) => res.json())
.subscribe(data => this._store.dispatch({type: DELETED_USER, payload: data}));
.map(data => ({type: DELETED_USER, payload: data}));

Observable
.merge(adds, loads, deletes)
.subscribe(action => this._store.dispatch(action));

this.loadUsers();
}
Expand Down

0 comments on commit 1e9ece3

Please sign in to comment.