Skip to content

Instantly share code, notes, and snippets.

@e-schultz
Created March 17, 2016 18:05
Show Gist options
  • Select an option

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

Select an option

Save e-schultz/8ac4110b1c7acd663d31 to your computer and use it in GitHub Desktop.
state-service
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