Last active
December 23, 2016 13:03
-
-
Save aegyed91/57f560b8c4d1a730233191fc92183b87 to your computer and use it in GitHub Desktop.
ng2 alternate state management -to keep ur sanity :D-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { BehaviorSubject, Subscription } from 'rxjs'; | |
import { get, set, assign, merge, isEmpty } from 'lodash'; | |
export interface IStateOpts { | |
initState?: Object; | |
} | |
@Injectable() | |
export class StateS { | |
state; | |
constructor() { | |
this.state = new BehaviorSubject({}); | |
} | |
subscribe(next, error?, complete?): Subscription { | |
return this.state.distinctUntilChanged().subscribe(next, error, complete); | |
} | |
get(path?: string, defaultValue?: any) { | |
return isEmpty(path) | |
? this.state.getValue() | |
: get(this.state.getValue(), path, defaultValue); | |
} | |
set(path: string, value: any) { | |
this.state.next(set(this.get(), path, value)); | |
return this; | |
} | |
assign(obj: Object) { | |
this.state.next(assign({}, this.get(), obj)); | |
return this; | |
} | |
merge(obj: Object) { | |
this.state.next(merge({}, this.get(), obj)); | |
return this; | |
} | |
// fetch initial state from api | |
populate() { | |
} | |
// stuff u do after initial state set | |
afterPopulate() { | |
} | |
// set state to an empty object | |
clear() { | |
this.state.next({}); | |
} | |
// stuff u do b4 freeing up allocated mem | |
destroy() { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { StateS } from '../../shared/services/state.s'; | |
import { UserS } from './user.s'; | |
import { ResMsgHandlerS } from '../../shared/services/res-msg-handler.s'; | |
import { TranslateService } from 'ng2-translate'; | |
import { get } from 'lodash'; | |
@Injectable() | |
export class UserSt extends StateS { | |
constructor(public mh: ResMsgHandlerS, private userS: UserS, private translate: TranslateService) { | |
super(); | |
} | |
// fetch initial state from api | |
populate() { | |
return new Promise((resolve, reject) => { | |
if (get(document.body, 'dataset.user')) { | |
try { | |
this.assign(JSON.parse((document.body.dataset as any).user)).afterPopulate(); | |
resolve(); | |
} catch (e) { | |
reject(); | |
} | |
} else { | |
reject() | |
} | |
}); | |
} | |
// stuff u do after initial state set | |
afterPopulate() { | |
if (!this.get('isEmailVerified')) { | |
this.translate.get('AUTH.COMMON.CONFIRM_MAIL_ADDR').toPromise().then(v => this.mh.showInToastr(v, 2)); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment