Created
March 17, 2016 18:05
-
-
Save e-schultz/8ac4110b1c7acd663d31 to your computer and use it in GitHub Desktop.
state-service
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, Inject} from 'angular2/core'; | |
| import {BehaviorSubject} from 'rxjs/Rx'; | |
| @Injectable() | |
| export default class StateService { | |
| store: any; | |
| _ngRedux: any; | |
| constructor(@Inject('ngRedux') ngRedux) { | |
| this.store = this.observableFromStore(ngRedux); | |
| this._ngRedux = ngRedux; | |
| this._ngRedux.subscribe(() => this.store.next(this._ngRedux.getState())); | |
| Object.assign(this,ngRedux) | |
| } | |
| init(store) { | |
| } | |
| select(selector: any, comparer?:(x: any, y: any)=> boolean) { | |
| if ( | |
| typeof selector === 'string' || | |
| typeof selector === 'number' || | |
| typeof selector === 'symbol' | |
| ) { | |
| return this.store.map(state => state[selector]).distinctUntilChanged(comparer); | |
| } | |
| else if (typeof selector === 'function') { | |
| return this.store.map(selector).distinctUntilChanged(comparer) | |
| } | |
| } | |
| observableFromStore = (store) => new BehaviorSubject(store.getState()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment