Created
December 15, 2016 16:11
-
-
Save aegyed91/ca8d10c88a1b7eb4deeaaf7f9bdbd76b to your computer and use it in GitHub Desktop.
state
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'; | |
import { IS_DEV } from '../constants'; | |
import { ResMsgHandlerS } from './res-msg-handler.s'; | |
export interface IStateOpts { | |
initState?: Object; | |
} | |
@Injectable() | |
export class StateS { | |
state; | |
mh; | |
constructor(mh: ResMsgHandlerS) { | |
this.mh = mh; | |
this.state = new BehaviorSubject({}); | |
} | |
subscribe(next, error?, complete?): Subscription { | |
return this.state.distinctUntilChanged().share().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() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment