From b96f5e72acb04813e048012a7d186b6e376ae43f Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 25 Aug 2022 17:13:26 -0400 Subject: [PATCH] change upreducer to purry version of reducer --- src/Updux.ts | 40 +++++++--------------------------------- src/reducer.test.ts | 4 ++-- src/types.ts | 5 ++++- tsconfig.json | 2 +- 4 files changed, 14 insertions(+), 37 deletions(-) diff --git a/src/Updux.ts b/src/Updux.ts index 6cff7bc..0ef2c32 100644 --- a/src/Updux.ts +++ b/src/Updux.ts @@ -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 = Record, TSubduxes = {}> { - /** - * Local initial state. - * @default {} - */ - initial?: TState; - - actions?: TActions; - - /** - * Subduxes to be merged to this dux. - */ - subduxes?: TSubduxes; -} - -type StateOf = D extends { initial: infer I } ? I : unknown; - -export type DuxStateSubduxes = keyof C extends never - ? unknown - : { [K in keyof C]: StateOf }; - -type DuxAggregateState = TState & DuxStateSubduxes ; - -export class Updux { +export class Updux< + TState extends any = {}, + TActions extends { [key: string]: ActionGenerator } = {}, + TSubduxes = {} + > { #localInitial: any = {}; #subduxes; #actions : TActions; @@ -55,11 +33,7 @@ export class Updux state => this.reducer(state,action); - } - get reducer() { - return (state : DuxAggregateState, _action : any) => state; + return (...args) => R.purry((state : DuxAggregateState, _action : any) => state, args); } } diff --git a/src/reducer.test.ts b/src/reducer.test.ts index c668a1e..3babb7d 100644 --- a/src/reducer.test.ts +++ b/src/reducer.test.ts @@ -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 }); diff --git a/src/types.ts b/src/types.ts index 3486a05..01d18d5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,14 +1,17 @@ +import { ActionGenerator } from "./actions.js"; /** * Configuration object typically passed to the constructor of the class Updux. */ -export interface UpduxConfig { +export interface UpduxConfig = Record, TSubduxes = {}> { /** * Local initial state. * @default {} */ initial?: TState; + actions?: TActions; + /** * Subduxes to be merged to this dux. */ diff --git a/tsconfig.json b/tsconfig.json index 109a621..6ccb27a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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. */