Skip to content

Instantly share code, notes, and snippets.

@e-schultz
Last active March 8, 2016 23:27
Show Gist options
  • Select an option

  • Save e-schultz/bbf8afbbc90b7981ad24 to your computer and use it in GitHub Desktop.

Select an option

Save e-schultz/bbf8afbbc90b7981ad24 to your computer and use it in GitHub Desktop.
Stuff
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)
})
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