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
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',
})
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);
};
}
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());
};
function reduceState(stateActionPair = { state: undefined }, [action, reducer]) {
const { state } = stateActionPair;
return { state: reducer(state, action), action };
}
function reducerFactory(reducers) {
return function combination(state, action) {
const nextState: any = {};
const reducerKeys = Object.keys(reducers);
for (let i = 0; i < reducerKeys.length; i++) {
const key = reducerKeys[i];
const reducer: any = reducers[key];
nextState[key] = reducer(state[key], action);
}
return nextState;
...
actions$: BehaviorSubject<Action>;
state$: BehaviorSubject<any>;
constructor() {
this.actions$ = new BehaviorSubject({ type: 'INIT' });
const actionsOnQueue$: Observable<Action> = this.actions$.pipe(observeOn(queueScheduler));
const reducer$: Observable<
ActionReducer<any, any>
> = new BehaviorSubject(reducerFactory(reducers));
import { Injectable } from '@angular/core';
import {
BehaviorSubject,
Observable,
queueScheduler,
} from 'rxjs';
import { observeOn, scan, withLatestFrom, map, distinctUntilChanged } from 'rxjs/operators';
import { reducers } from './reducers';
export interface Action {
CPU type/Number of rows 1 5 10
Optimal CPU 11ms 40ms 37ms
6x CPU slowdown 81ms 160ms 201ms
Optimal CPU (change detection optimizations) 17ms 18ms 22ms
6x CPU Slowdown (change detection optimizations) 83ms 99ms 103ms
Optimal CPU (cd optimizations and list modification) 13ms 14ms 18ms
6x CPU Slowdown (cd optimizations and list modification) 83ms 83ms 85ms
CPU type/Number of rows 1 5 10
Optimal CPU 11ms 40ms 37ms
6x CPU slowdown 81ms 160ms 201ms
Optimal CPU (change detection optimizations) 17ms 18ms 22ms
6x CPU Slowdown (change detection optimizations) 83ms 99ms 103ms
...
@Component({
selector: 'transaction',
templateUrl: './transaction.html',
styleUrls: ['./transaction.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class TransactionComponent implements OnChanges {