This file contains 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 { 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', | |
}) |
This file contains 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
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); | |
}; | |
} |
This file contains 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
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()); | |
}; |
This file contains 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
function reduceState(stateActionPair = { state: undefined }, [action, reducer]) { | |
const { state } = stateActionPair; | |
return { state: reducer(state, action), action }; | |
} |
This file contains 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
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; |
This file contains 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
... | |
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)); |
This file contains 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 { 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 { |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
... | |
@Component({ | |
selector: 'transaction', | |
templateUrl: './transaction.html', | |
styleUrls: ['./transaction.scss'], | |
changeDetection: ChangeDetectionStrategy.OnPush | |
}) | |
export class TransactionComponent implements OnChanges { |
NewerOlder