diff --git a/src/buildActions/index.js b/src/buildActions/index.js index de2d987..120a10e 100644 --- a/src/buildActions/index.js +++ b/src/buildActions/index.js @@ -1,16 +1,15 @@ import fp from 'lodash/fp'; -import { ActionEffects, ActionCreator, ActionCreators, ActionMutations } from '../types'; -function actionFor(type: string) { +function actionFor(type) { return ( (payload = undefined, meta = undefined) => fp.pickBy(v => v !== null)({type, payload, meta}) - ) as ActionCreator; + ); } export default function buildActions( - mutations : ActionMutations = {}, - effects : ActionEffects = {}, - subActions : ActionCreators = {}, + mutations = {}, + effects = {}, + subActions = {}, ) { return { ...subActions, diff --git a/src/buildCreateStore.js b/src/buildCreateStore.js index f2d6034..76654ba 100644 --- a/src/buildCreateStore.js +++ b/src/buildCreateStore.js @@ -12,6 +12,6 @@ export default function buildCreateStore( reducer, initial, middleware, }; } - return store as any; + return store; } }; diff --git a/src/buildInitial/index.js b/src/buildInitial/index.js index b3bf588..7df345e 100644 --- a/src/buildInitial/index.js +++ b/src/buildInitial/index.js @@ -1,8 +1,8 @@ import fp from 'lodash/fp'; -export default function buildInitial( - initial: any = {}, +export default function buildInitial( + initial= {}, subduxes = {}, -): S { +) { return fp.isPlainObject(initial) ? fp.mergeAll([subduxes, initial]) : initial; } diff --git a/src/buildInitial/index.test-d.js b/src/buildInitial/index.test-d.js index 39abd06..e69de29 100644 --- a/src/buildInitial/index.test-d.js +++ b/src/buildInitial/index.test-d.js @@ -1,6 +0,0 @@ -// TypeScript Version: 3.4 - -import buildInitial from '.'; - -// $ExpectType any -const x = buildInitial(); diff --git a/src/buildInitial/test-d.js b/src/buildInitial/test-d.js index 68893f3..e69de29 100644 --- a/src/buildInitial/test-d.js +++ b/src/buildInitial/test-d.js @@ -1,14 +0,0 @@ -import buildInitial from '.'; -import {expectType} from 'tsd'; - -type MyState = { - foo: number, - bar: string, -}; - -const x = buildInitial({ - initial: { - glug: 3, - } -}); -expectType(x); diff --git a/src/buildMiddleware/index.test-d.js b/src/buildMiddleware/index.test-d.js deleted file mode 100644 index 36cc0de..0000000 --- a/src/buildMiddleware/index.test-d.js +++ /dev/null @@ -1,6 +0,0 @@ -import buildMiddleware from '../buildMiddleware'; -import {expectType} from 'tsd'; - - -expectType( buildMiddleware ); - diff --git a/src/buildMutations/index.js b/src/buildMutations/index.js index 3e26f1e..f7dab81 100644 --- a/src/buildMutations/index.js +++ b/src/buildMutations/index.js @@ -1,25 +1,23 @@ import fp from 'lodash/fp'; import u from 'updeep'; -import { Mutation, Mutations } from '../types'; - -const composeMutations = (mutations:Mutation[]) => +const composeMutations = (mutations) => mutations.reduce( (m1,m2) => (payload=null,action={}) => state => m2(payload,action)( m1(payload,action)(state) )); -export default function buildMutations(mutations = {}, subduxes= {}) :Mutations{ +export default function buildMutations(mutations = {}, subduxes= {}) { // we have to differentiate the subduxes with '*' than those // without, as the root '*' is not the same as any sub-'*' const actions = fp.uniq( Object.keys(mutations).concat( - ...Object.values( subduxes ).map( ({mutations = {}}:any) => Object.keys(mutations) ) + ...Object.values( subduxes ).map( ({mutations = {}}) => Object.keys(mutations) ) ) ); let mergedMutations = {}; let [ globby, nonGlobby ] = fp.partition( - ([_,{mutations={}}]:any) => mutations['*'], + ([_,{mutations={}}]) => mutations['*'], Object.entries(subduxes) ); @@ -32,16 +30,16 @@ export default function buildMutations(mutations = {}, subduxes= {}) :Mutations{ ])(globby); const globbyMutation = (payload,action) => u( - fp.mapValues( (mut:any) => mut(payload,action) )(globby) + fp.mapValues( (mut) => mut(payload,action) )(globby) ); actions.forEach( action => { mergedMutations[action] = [ globbyMutation ] }); - nonGlobby.forEach( ([slice, {mutations={},reducer={}}]:any) => { + nonGlobby.forEach( ([slice, {mutations={},reducer={}}]) => { Object.entries(mutations).forEach(([type,mutation]) => { - const localized = (payload=null,action={}) => u.updateIn( slice )( (mutation as any)(payload,action) ); + const localized = (payload=null,action={}) => u.updateIn( slice )( (mutation)(payload,action) ); mergedMutations[type].push(localized); }) @@ -51,5 +49,5 @@ export default function buildMutations(mutations = {}, subduxes= {}) :Mutations{ mergedMutations[type].push(mutation); }); - return fp.mapValues( composeMutations )(mergedMutations) as Mutations; + return fp.mapValues( composeMutations )(mergedMutations); } diff --git a/src/buildUpreducer/index.js b/src/buildUpreducer/index.js index 1c121ab..52724f6 100644 --- a/src/buildUpreducer/index.js +++ b/src/buildUpreducer/index.js @@ -1,18 +1,15 @@ import fp from 'lodash/fp'; -import {Mutations} from '../types'; -type Upreducer = (action:any) => (state:S) => S; - -export default function buildUpreducer(initial: S, mutations: Mutations): Upreducer { - return (action = {}) => (state:any) => { +export default function buildUpreducer(initial, mutations) { + return (action = {}) => (state) => { if (state === null) state = initial; const a = - mutations[(action as any).type] || + mutations[(action).type] || mutations['*']; if(!a) return state; - return a((action as any).payload, action)(state) as S; + return a((action).payload, action)(state); }; } diff --git a/src/index.js b/src/index.js index b04ab06..ffdb8d5 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,6 @@ import fp from 'lodash/fp'; import u from 'updeep'; -export { Updux } from './updux'; import Updux from './updux'; export default function updux(config) { diff --git a/src/index.test-d.js b/src/index.test-d.js deleted file mode 100644 index a46be7c..0000000 --- a/src/index.test-d.js +++ /dev/null @@ -1,5 +0,0 @@ -import updux, {Updux } from '.'; -import {expectType} from 'tsd'; - -const x = updux({}); -expectType(x); diff --git a/src/types.js b/src/types.js deleted file mode 100644 index dbf1eb2..0000000 --- a/src/types.js +++ /dev/null @@ -1,23 +0,0 @@ - -export type Dictionary = { [key: string]: T }; - -export type Mutation = (payload: any, action: any) => (state:any) => any; - -export type Mutations = Dictionary; - -export type ActionMutations = Dictionary; - -export type Effect = (api:any) => (next: Function) => (action: any) => any; - -export type ActionEffects = Dictionary; - -export type Action = { - type: T, - payload: P, - meta: M, -} - -export type ActionCreator = (payload?: P, meta?: M ) => Action; - -export type ActionCreators = Dictionary; - diff --git a/src/updux.js b/src/updux.js index 0b780bf..08a0b31 100644 --- a/src/updux.js +++ b/src/updux.js @@ -3,42 +3,17 @@ import buildActions from './buildActions'; import buildInitial from './buildInitial'; import buildMutations from './buildMutations'; -import { Dictionary, Mutation, ActionCreators } from './types'; import buildCreateStore from './buildCreateStore'; import buildMiddleware from './buildMiddleware'; import buildUpreducer from './buildUpreducer'; -type UpduxConfig = { - initial?: any, - mutations?: any, - effects?: any, - subduxes?: { - [ slice: string ]: UpduxConfig | Updux - } -}; - export class Updux { - actions: ActionCreators; - subduxes: Dictionary; - - initial: any; - - mutations: Dictionary; - - createStore: Function; - - upreducer: (action:any)=>(state:any)=>any; - - reducer: (state:S,action:any) => S; - - middleware: (api:any) => (next: Function) => (action: any) => any; - - constructor(config: UpduxConfig) { + constructor(config) { this.subduxes = fp.mapValues( - value => fp.isPlainObject(value) ? new Updux(value as UpduxConfig) : value )(fp.getOr({},'subduxes',config) - ) as Dictionary; + value => fp.isPlainObject(value) ? new Updux(value ) : value )(fp.getOr({},'subduxes',config) + ); this.actions = buildActions(