-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store): add createReducer function (#1746)
Closes #1724
- Loading branch information
1 parent
dbfdbaf
commit f954e14
Showing
25 changed files
with
341 additions
and
284 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { on, createReducer, createAction, props, union } from '@ngrx/store'; | ||
import { expecter } from 'ts-snippet'; | ||
|
||
describe('classes/reducer', function(): void { | ||
const expectSnippet = expecter( | ||
code => ` | ||
// path goes from root | ||
import {createAction, props} from './modules/store/src/action_creator'; | ||
import {on} from './modules/store/src/reducer_creator'; | ||
${code}`, | ||
{ | ||
moduleResolution: 'node', | ||
target: 'es2015', | ||
} | ||
); | ||
|
||
describe('base', () => { | ||
const bar = createAction('[foobar] BAR', props<{ bar: number }>()); | ||
const foo = createAction('[foobar] FOO', props<{ foo: number }>()); | ||
|
||
describe('on', () => { | ||
it('should enforce action property types', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', props<{ foo: number }>()); | ||
on(foo, (state, action) => { const foo: string = action.foo; return state; }); | ||
`).toFail(/'number' is not assignable to type 'string'/); | ||
}); | ||
|
||
it('should enforce action property names', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', props<{ foo: number }>()); | ||
on(foo, (state, action) => { const bar: string = action.bar; return state; }); | ||
`).toFail(/'bar' does not exist on type/); | ||
}); | ||
|
||
it('should support reducers with multiple actions', () => { | ||
const both = union({ bar, foo }); | ||
const func = (state: {}, action: typeof both) => ({}); | ||
const result = on(foo, bar, func); | ||
expect(result.types).toContain(bar.type); | ||
expect(result.types).toContain(foo.type); | ||
}); | ||
}); | ||
|
||
describe('createReducer', () => { | ||
it('should create a reducer', () => { | ||
interface State { | ||
foo?: number; | ||
bar?: number; | ||
} | ||
|
||
const fooBarReducer = createReducer<State>( | ||
[ | ||
on(foo, (state, { foo }) => ({ ...state, foo })), | ||
on(bar, (state, { bar }) => ({ ...state, bar })), | ||
], | ||
{} | ||
); | ||
|
||
expect(typeof fooBarReducer).toEqual('function'); | ||
|
||
let state = fooBarReducer(undefined, { type: 'UNKNOWN' }); | ||
expect(state).toEqual({}); | ||
|
||
state = fooBarReducer(state, foo({ foo: 42 })); | ||
expect(state).toEqual({ foo: 42 }); | ||
|
||
state = fooBarReducer(state, bar({ bar: 54 })); | ||
expect(state).toEqual({ foo: 42, bar: 54 }); | ||
}); | ||
|
||
it('should support reducers with multiple actions', () => { | ||
type State = string[]; | ||
|
||
const fooBarReducer = createReducer<State>( | ||
[on(foo, bar, (state, { type }) => [...state, type])], | ||
[] | ||
); | ||
|
||
expect(typeof fooBarReducer).toEqual('function'); | ||
|
||
let state = fooBarReducer(undefined, { type: 'UNKNOWN' }); | ||
expect(state).toEqual([]); | ||
|
||
state = fooBarReducer(state, foo({ foo: 42 })); | ||
expect(state).toEqual(['[foobar] FOO']); | ||
|
||
state = fooBarReducer(state, bar({ bar: 54 })); | ||
expect(state).toEqual(['[foobar] FOO', '[foobar] BAR']); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { ActionCreator, ActionReducer, ActionType, Action } from './models'; | ||
|
||
// Return type of the `on` fn. | ||
export interface On<S> { | ||
reducer: ActionReducer<S>; | ||
types: string[]; | ||
} | ||
|
||
// Specialized Reducer that is aware of the Action type it needs to handle | ||
export interface OnReducer<S, C extends ActionCreator[]> { | ||
(state: S, action: ActionType<C[number]>): S; | ||
} | ||
|
||
export function on<C1 extends ActionCreator, S>( | ||
creator1: C1, | ||
reducer: OnReducer<S, [C1]> | ||
): On<S>; | ||
export function on<C1 extends ActionCreator, C2 extends ActionCreator, S>( | ||
creator1: C1, | ||
creator2: C2, | ||
reducer: OnReducer<S, [C1, C2]> | ||
): On<S>; | ||
export function on< | ||
C1 extends ActionCreator, | ||
C2 extends ActionCreator, | ||
C3 extends ActionCreator, | ||
S | ||
>( | ||
creator1: C1, | ||
creator2: C2, | ||
creator3: C3, | ||
reducer: OnReducer<S, [C1, C2, C3]> | ||
): On<S>; | ||
export function on<S>( | ||
creator: ActionCreator, | ||
...rest: (ActionCreator | OnReducer<S, [ActionCreator]>)[] | ||
): On<S>; | ||
export function on( | ||
...args: (ActionCreator | Function)[] | ||
): { reducer: Function; types: string[] } { | ||
const reducer = args.pop() as Function; | ||
const types = args.reduce( | ||
(result, creator) => [...result, (creator as ActionCreator).type], | ||
[] as string[] | ||
); | ||
return { reducer, types }; | ||
} | ||
|
||
export function createReducer<S>( | ||
ons: On<S>[], | ||
initialState: S | ||
): ActionReducer<S> { | ||
const map = new Map<string, ActionReducer<S>>(); | ||
for (let on of ons) { | ||
for (let type of on.types) { | ||
map.set(type, on.reducer); | ||
} | ||
} | ||
return function(state: S = initialState, action: Action): S { | ||
const reducer = map.get(action.type); | ||
return reducer ? reducer(state, action) : state; | ||
}; | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
import { createAction, union } from '@ngrx/store'; | ||
import { createAction } from '@ngrx/store'; | ||
|
||
export const logout = createAction('[Auth] Logout'); | ||
export const logoutConfirmation = createAction('[Auth] Logout Confirmation'); | ||
export const logoutConfirmationDismiss = createAction( | ||
'[Auth] Logout Confirmation Dismiss' | ||
); | ||
|
||
const all = union({ logout, logoutConfirmation, logoutConfirmationDismiss }); | ||
export type AuthActionsUnion = typeof all; |
4 changes: 1 addition & 3 deletions
4
projects/example-app/src/app/auth/actions/login-page.actions.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import { createAction, props, union } from '@ngrx/store'; | ||
import { createAction, props } from '@ngrx/store'; | ||
import { Credentials } from '@example-app/auth/models/user'; | ||
|
||
export const login = createAction( | ||
'[Login Page] Login', | ||
props<{ credentials: Credentials }>() | ||
); | ||
|
||
export type LoginPageActionsUnion = ReturnType<typeof login>; |
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
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
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
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
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
Oops, something went wrong.