Skip to content
This repository was archived by the owner on Jul 8, 2021. It is now read-only.

Commit 11f8852

Browse files
authored
Merge pull request #1 from sandangel/memoized-selectors
refactor: address all issue in #14
2 parents 1410180 + 842afa7 commit 11f8852

12 files changed

+51
-41
lines changed

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "ng-packagr -p package.json",
8-
"test": "ngc -p tsconfig.json && jasmine dist/*.spec.js",
8+
"test": "ngc -p tsconfig.spec.json && jasmine dist/spec/*.spec.js",
99
"publish": "npm publish dist",
1010
"precommit": "lint-staged",
1111
"lint": "tslint -c tslint.json 'src/**/*.ts'",
@@ -51,7 +51,7 @@
5151
"rxjs": "^5.5.6",
5252
"tsickle": "^0.26.0",
5353
"tslint": "^5.8.0",
54-
"typescript": "~2.5.3"
54+
"typescript": "^2.6.2"
5555
},
5656
"ngPackage": {
5757
"lib": {

src/index.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
import 'reflect-metadata';
2-
export * from './module';
3-
export * from './action';
4-
export * from './of-action';
5-
export * from './store';
6-
export * from './select';
7-
export * from './symbols';
8-
export * from './factory';
2+
export * from './public_api';

src/internals.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Action } from '@ngrx/store';
21
import { ActionType } from './symbols';
32

43
export interface ActionMeta {

src/of-action.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { Action } from '@ngrx/store';
2-
import { Actions } from '@ngrx/effects';
32
import { filter } from 'rxjs/operators';
4-
import { Observable } from 'rxjs/Observable';
53
import { OperatorFunction } from 'rxjs/interfaces';
6-
import { ActionType } from '.';
4+
import { ActionType } from './symbols';
75

86
export function ofAction<T extends Action>(allowedType: ActionType<T>): OperatorFunction<Action, T>;
97
export function ofAction<T extends Action>(...allowedTypes: ActionType[]): OperatorFunction<Action, T>;

src/public_api.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { NgrxActionsModule } from './module';
2+
export { Action } from './action';
3+
export { ofAction } from './of-action';
4+
export { Store } from './store';
5+
export { Select, NgrxSelect } from './select';
6+
export { createReducer } from './factory';

src/select.ts

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { Injectable } from '@angular/core';
22
import { Store, Selector } from '@ngrx/store';
3-
import { Observable } from 'rxjs/Observable';
43

54
@Injectable()
65
export class NgrxSelect {
7-
static store: Store<any> = undefined;
6+
static store: Store<any> | undefined = undefined;
87
connect(store: Store<any>) {
98
NgrxSelect.store = store;
109
}
@@ -18,21 +17,19 @@ export function Select<TState = any, TValue = any>(
1817
...paths: string[]
1918
): (target: any, name: string) => void;
2019
export function Select<TState = any, TValue = any>(
21-
selectorOrFeature: string | Selector<TState, TValue>,
20+
selectorOrFeature?: string | Selector<TState, TValue>,
2221
...paths: string[]
2322
) {
2423
return function(target: any, name: string): void {
2524
let fn: Selector<TState, TValue>;
2625
// Nothing here? Use propery name as selector
27-
if (typeof selectorOrFeature === 'undefined') {
26+
if (!selectorOrFeature) {
2827
selectorOrFeature = name;
2928
}
3029
// Handle string vs Selector<TState, TValue>
3130
if (typeof selectorOrFeature === 'string') {
32-
if (paths.length) {
33-
selectorOrFeature = [selectorOrFeature, ...paths].join('.');
34-
}
35-
fn = fastPropGetter(selectorOrFeature);
31+
const propsArray = paths.length ? [selectorOrFeature, ...paths] : selectorOrFeature.split('.');
32+
fn = fastPropGetter(propsArray);
3633
} else {
3734
fn = selectorOrFeature;
3835
}
@@ -55,21 +52,21 @@ export function Select<TState = any, TValue = any>(
5552
}
5653

5754
/**
58-
* The generated function is faster then:
55+
* The generated function is faster than:
5956
* - pluck (Observable operator)
6057
* - memoize (old ngrx-actions implementation)
6158
* - MemoizedSelector (ngrx)
6259
* @param path
6360
*/
64-
function fastPropGetter(path: string): (x: any) => any {
65-
const segments = path.split('.');
61+
export function fastPropGetter(paths: string[]): (x: any) => any {
62+
const segments = paths;
6663
let seg = 'store.' + segments[0],
67-
i = 0,
68-
l = segments.length;
64+
i = 0;
65+
const l = segments.length;
6966
let expr = seg;
7067
while (++i < l) {
7168
expr = expr + ' && ' + (seg = seg + '.' + segments[i]);
7269
}
73-
const fn = new Function('store', 'return ' + expr + '');
70+
const fn = new Function('store', 'return ' + expr + ';');
7471
return <(x: any) => any>fn;
7572
}

src/index.spec.ts src/spec/index.spec.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Store, createReducer, Action, ofAction, Select, NgrxSelect } from './index';
2-
import { Action as NgRxAction, Store as NgRxStore, createFeatureSelector, createSelector } from '@ngrx/store';
1+
import { Store, createReducer, Action, ofAction, Select, NgrxSelect } from '../index';
2+
import { Action as NgRxAction, createFeatureSelector, createSelector, Store as NgRxStore } from '@ngrx/store';
33
import { Observable } from 'rxjs/Observable';
44
import { of } from 'rxjs/observable/of';
55

@@ -26,9 +26,9 @@ describe('actions', () => {
2626
}
2727
}
2828

29-
const reducer = createReducer<FooState>(Bar);
29+
const reducer = createReducer<FooState | undefined>(Bar);
3030
const res = reducer(undefined, new MyAction());
31-
expect(res.foo).toBe(true);
31+
expect(res && res.foo).toBe(true);
3232
});
3333

3434
it('adds defaults', () => {
@@ -146,7 +146,7 @@ describe('actions', () => {
146146
action2 = new MyAction2(),
147147
action3 = new MyAction3('a', 0);
148148
const actions = of<NgRxAction>(action, action2, action3);
149-
let tappedActions: NgRxAction[] = [];
149+
const tappedActions: NgRxAction[] = [];
150150
actions.pipe(ofAction<MyAction | MyAction2>(MyAction, MyAction2)).subscribe(a => {
151151
tappedActions.push(a);
152152
});
@@ -157,7 +157,7 @@ describe('actions', () => {
157157
});
158158

159159
it('selects sub state', () => {
160-
const state: {
160+
const globalState: {
161161
myFeature: FooState;
162162
} = {
163163
myFeature: {
@@ -183,7 +183,7 @@ describe('actions', () => {
183183
@Select(msBar) bar$: Observable<any>; // using MemoizedSelector
184184
}
185185

186-
const store = new NgRxStore(of(state), undefined, undefined);
186+
const store = new NgRxStore(of(globalState), undefined, undefined);
187187

188188
try {
189189
NgrxSelect.store = store;
@@ -195,11 +195,11 @@ describe('actions', () => {
195195
});
196196

197197
mss.myFeature.subscribe(n => {
198-
expect(n).toBe(state.myFeature);
198+
expect(n).toBe(globalState.myFeature);
199199
});
200200

201201
mss.bar$.subscribe(n => {
202-
expect(n).toBe(state.myFeature.bar);
202+
expect(n).toBe(globalState.myFeature.bar);
203203
});
204204
} finally {
205205
NgrxSelect.store = undefined;

src/spec/tsconfig.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* This file is for IDE only */
2+
{
3+
"extends": "../../tsconfig.spec.json"
4+
}

src/store.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { INITIAL_STATE_KEY } from './keys';
2-
import { Action } from '@ngrx/store';
32

43
export function Store<TState>(initialState?: TState): (target: Function) => void;
54
export function Store(initialState?: any): (target: Function) => void;

tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"rootDir": "src/",
1515
"skipDefaultLibCheck": true,
1616
"skipLibCheck": true,
17+
"noImplicitAny": false,
18+
"noUnusedLocals": true,
19+
"strict": true,
1720
"sourceMap": true,
1821
"target": "es5",
1922
"types": [
@@ -23,5 +26,8 @@
2326
},
2427
"include": [
2528
"src/**/*"
29+
],
30+
"exclude": [
31+
"src/spec/**/*"
2632
]
2733
}

tsconfig.spec.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"strictNullChecks": false
5+
},
6+
"files": ["src/spec/index.spec.ts"]
7+
}

0 commit comments

Comments
 (0)