import { Action , ActionReducer , createAction , props } from '@ngrx/store' ; import { ActionMap , CaseReducer , HandlerMap , PlainCaseReducer } from './models' ; param initialState param source param handlers returns example see mergeReducers export function combineHandlers < State , Handlers extends HandlerMap < State > > ( initialState , source , handlers ) { return [ createActions ( source , handlers ) , createReducer ( initialState , source , handlers ) ] ; } returns example see combineHandlers export function plain < State > ( ) { return state => state ; } returns example see combineHandlers export function withPayload < Payload > ( ) { return plain ( ) ; } function createActions < State , Handlers extends HandlerMap < State > > ( source , handlers ) { return Object . keys ( handlers ) . reduce ( ( actionMap , event ) => ( { ... actionMap , [ event ] : createAction ( toActionType ( source , event ) , props < any > ( ) ) , } ) , { } as ActionMap < State , Handlers > ) ; } function createReducer < State , Handlers extends HandlerMap < State > > ( initialState , source , handlers ) { return ( state = initialState , action ) => { const { type , ... payload } = action ; const reducer = source === toSource ( type ) ? handlers [ toEvent ( type ) ] : null ; return reducer ? reducer ( state , payload ) : state ; } ; } function toActionType ( source , event ) { return `[${ source }] ${ event }` ; } function toSource ( actionType ) { return actionType . slice ( 1 ) . split ( ']' ) . shift ( ) ! ; } function toEvent ( actionType ) { return actionType . split ( '] ' ) . pop ( ) ! ; }