Skip to content

Instantly share code, notes, and snippets.

View evgenyfedorenko's full-sized avatar

Evgeny Fedorenko evgenyfedorenko

  • Fannie Mae
  • Springfield, VA
View GitHub Profile
function reduceState(stateActionPair = { state: undefined }, [action, reducer]) {
const { state } = stateActionPair;
return { state: reducer(state, action), action };
}
export function select<T, Props, K>(selector: (state: T, props?: Props) => any) {
return function selectOperator(source$: Observable<T>): Observable<K> {
let mapped$: Observable<any>;
mapped$ = source$.pipe(
map(source => selector(source))
);
return mapped$.pipe(distinctUntilChanged());
};
export function createSelector(...args: any[]) {
const selectors = args.slice(0, args.length - 1);
const projector = args[args.length - 1];
return function (state: any) {
const args = selectors.map(fn => fn(state));
return projector.apply(null, args);
};
}
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import * as fromRoot from './reducers/';
import { Increment, Decrement, Reset } from './actions/counter.actions';
import { StoreService, select } from './store.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
})