Last active
March 8, 2016 23:27
-
-
Save e-schultz/bbf8afbbc90b7981ad24 to your computer and use it in GitHub Desktop.
Stuff
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 { is } from 'immutable' | |
| let x = 0; | |
| let tasksTest$ = this.stateService.select(state=> state.tasks, is).subscribe(n=> { | |
| console.log('this is called... yeah',++x) | |
| }) |
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 { BehaviorSubject } from 'rxjs'; | |
| import {Injectable, Inject} from 'angular2/core'; | |
| import {bindActionCreators} from 'redux'; | |
| @Injectable() | |
| export default class StateService { | |
| store: any; | |
| private _ngRedux: any; | |
| constructor( @Inject('ngRedux') ngRedux) { | |
| this.store = this.observableFromStore(ngRedux); | |
| this._ngRedux = ngRedux; | |
| this._ngRedux.subscribe(() => this.store.next(this._ngRedux.getState())); | |
| } | |
| 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()); | |
| dispatch = (action) => { | |
| return this._ngRedux.dispatch(action) | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment