change upreducer to purry version of reducer

typescript
Yanick Champoux 2022-08-25 17:13:26 -04:00
parent fa837a345e
commit b96f5e72ac
4 changed files with 14 additions and 37 deletions

View File

@ -1,35 +1,13 @@
import R from 'remeda';
import { DuxAggregateState, UpduxConfig } from './types';
import { Action, ActionGenerator } from './actions';
import { Action, ActionGenerator } from './actions.js';
/**
* Configuration object typically passed to the constructor of the class Updux.
*/
export interface UpduxConfig<TState = any, TActions extends Record<string,ActionGenerator> = Record<string,ActionGenerator>, TSubduxes = {}> {
/**
* Local initial state.
* @default {}
*/
initial?: TState;
actions?: TActions;
/**
* Subduxes to be merged to this dux.
*/
subduxes?: TSubduxes;
}
type StateOf<D> = D extends { initial: infer I } ? I : unknown;
export type DuxStateSubduxes<C extends {}> = keyof C extends never
? unknown
: { [K in keyof C]: StateOf<C[K]> };
type DuxAggregateState<TState,TSubduxes> = TState & DuxStateSubduxes<TSubduxes> ;
export class Updux<TState extends any = {}, TActions extends { [key: string]: ActionGenerator } = {}, TSubduxes = {}> {
export class Updux<
TState extends any = {},
TActions extends { [key: string]: ActionGenerator } = {},
TSubduxes = {}
> {
#localInitial: any = {};
#subduxes;
#actions : TActions;
@ -55,11 +33,7 @@ export class Updux<TState extends any = {}, TActions extends { [key: string]: Ac
);
}
get upreducer() {
return action => state => this.reducer(state,action);
}
get reducer() {
return (state : DuxAggregateState<TState,TSubduxes>, _action : any) => state;
return (...args) => R.purry((state : DuxAggregateState<TState,TSubduxes>, _action : any) => state, args);
}
}

View File

@ -13,7 +13,7 @@ test('basic reducer', () => {
test('basic upreducer', () => {
const dux = new Updux({ initial: {a: 3} });
expect(dux.upreducer).toBeTypeOf('function');
expect(dux.reducer).toBeTypeOf('function');
expect(dux.upreducer({ type: 'foo' })({a:1})).toMatchObject({ a: 1 }); // noop
expect(dux.reducer({ type: 'foo' })({a:1})).toMatchObject({ a: 1 }); // noop
});

View File

@ -1,14 +1,17 @@
import { ActionGenerator } from "./actions.js";
/**
* Configuration object typically passed to the constructor of the class Updux.
*/
export interface UpduxConfig<TState = any, TSubduxes = {}> {
export interface UpduxConfig<TState = any,TActions extends Record<string,ActionGenerator> = Record<string,ActionGenerator>, TSubduxes = {}> {
/**
* Local initial state.
* @default {}
*/
initial?: TState;
actions?: TActions;
/**
* Subduxes to be merged to this dux.
*/

View File

@ -79,7 +79,7 @@
/* Type Checking */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
"noImplicitAny": false, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */