From 5ff07b2e2f35139e6f38fed427d9aea71d6af5ad Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 23 Mar 2023 18:15:32 -0400 Subject: [PATCH] rename initial as initialState --- src/Updux.original | 18 +++++------ src/Updux.test.ts | 4 +-- src/Updux.todo | 12 ++++---- src/Updux.ts | 24 +++++++-------- src/actions.ts | 1 + src/buildInitial.test.ts | 4 +-- src/dux-selectors.test.todo | 8 ++--- src/effects.test.ts | 6 ++-- src/initial.test.ts | 58 ++++++++++++++++++------------------ src/initial.ts | 6 ++-- src/mutations.test.ts | 8 ++--- src/reactions.test.ts | 4 +-- src/reducer.test.ts | 4 +-- src/reducer.ts | 4 +-- src/selectors.test.ts | 4 +-- src/splatReactions.test.todo | 8 ++--- src/tutorial.test.ts | 14 ++++----- src/types.ts | 2 +- 18 files changed, 95 insertions(+), 94 deletions(-) diff --git a/src/Updux.original b/src/Updux.original index 586b803..51ca3b9 100644 --- a/src/Updux.original +++ b/src/Updux.original @@ -36,10 +36,10 @@ export interface UpduxConfig< TSubduxes = {} > { /** - * Local initial state. + * Local initialState state. * @default {} */ - initial?: TState; + initialState?: TState; /** * Subduxes to be merged to this dux. @@ -116,7 +116,7 @@ export class Updux< TSubduxes extends object = {} > { /** @type { unknown } */ - #initial = {}; + #initialState = {}; #subduxes = {}; /** @type Record */ @@ -134,7 +134,7 @@ export class Updux< constructor( config: UpduxConfig ) { - this.#initial = config.initial ?? {}; + this.#initialState = config.initialState ?? {}; this.#subduxes = config.subduxes ?? {}; if (config.subduxes) { @@ -210,8 +210,8 @@ export class Updux< } /** @member { unknown } */ - get initial(): AggregateDuxState { - return this.#memoInitial(this.#initial, this.#subduxes); + get initialState(): AggregateDuxState { + return this.#memoInitial(this.#initialState, this.#subduxes); } get actions(): AggregateDuxActions { @@ -233,7 +233,7 @@ export class Updux< ItemsOf> > { return this.#memoUpreducer( - this.initial, + this.initialState, this.#mutations, this.#subduxes, this.#upreducerWrapper @@ -407,7 +407,7 @@ export class Updux< }; } - createStore(initial?: unknown, enhancerGenerator?: Function) { + createStore(initialState?: unknown, enhancerGenerator?: Function) { const enhancer = (enhancerGenerator ?? applyMiddleware)( this.middleware ); @@ -419,7 +419,7 @@ export class Updux< actions: AggregateDuxActions; } = reduxCreateStore( this.reducer as any, - initial ?? this.initial, + initialState ?? this.initialState, enhancer ) as any; diff --git a/src/Updux.test.ts b/src/Updux.test.ts index 822aaa2..1e5f0ea 100644 --- a/src/Updux.test.ts +++ b/src/Updux.test.ts @@ -3,7 +3,7 @@ import Updux from './Updux'; test('subdux idempotency', () => { // const c = new Updux({ - // initial: 2, + // initialState: 2, // }); // const b = new Updux({ // subduxes: { @@ -12,7 +12,7 @@ test('subdux idempotency', () => { // }); const foo = new Updux({ subduxes: { - a: new Updux({ initial: 2 }), + a: new Updux({ initialState: 2 }), }, }); diff --git a/src/Updux.todo b/src/Updux.todo index 31e9b04..dd22be2 100644 --- a/src/Updux.todo +++ b/src/Updux.todo @@ -35,7 +35,7 @@ export class Updux { this.#middlewareWrapper = config.middlewareWrapper; - this.#localInitial = config.initial; + this.#localInitial = config.initialState; this.#subduxes = config.subduxes ?? {}; this.#actions = R.mapValues(config.actions ?? {}, (arg, name) => @@ -89,7 +89,7 @@ export class Updux { return this.#actions; } - get initial() { + get initialState() { if (Object.keys(this.#subduxes).length === 0) return this.#localInitial ?? {}; @@ -101,7 +101,7 @@ export class Updux { return Object.assign( {}, this.#localInitial ?? {}, - R.mapValues(this.#subduxes, ({ initial }) => initial), + R.mapValues(this.#subduxes, ({ initialState }) => initialState), ); } @@ -167,14 +167,14 @@ export class Updux { ); } - createStore(initial = undefined, enhancerGenerator = undefined) { + createStore(initialState = undefined, enhancerGenerator = undefined) { const enhancer = (enhancerGenerator ?? applyMiddleware)( this.middleware, ); const store = reduxCreateStore( this.reducer, - initial ?? this.initial, + initialState ?? this.initialState, enhancer, ); @@ -256,7 +256,7 @@ export class Updux { delete gone[key]; } else { const dux = new Updux({ - initial: null, + initialState: null, actions: { update: null }, mutations: { update: (payload) => () => payload, diff --git a/src/Updux.ts b/src/Updux.ts index 835a840..b3c8952 100644 --- a/src/Updux.ts +++ b/src/Updux.ts @@ -100,7 +100,7 @@ export default class Updux< #actions: AggregateActions, T_Subduxes>; - #initial: AggregateState; + #initialState: AggregateState; #localSelectors: Record< string, @@ -112,20 +112,20 @@ export default class Updux< constructor( config: Partial<{ - initial: T_LocalState; + initialState: T_LocalState; actions: T_LocalActions; subduxes: T_Subduxes; selectors: T_LocalSelectors; }>, ) { - // TODO check that we can't alter the initial after the fact - this.#localInitial = config.initial ?? ({} as T_LocalState); + // TODO check that we can't alter the initialState after the fact + this.#localInitial = config.initialState ?? ({} as T_LocalState); this.#localActions = config.actions ?? ({} as T_LocalActions); this.#subduxes = config.subduxes ?? ({} as T_Subduxes); this.#actions = buildActions(this.#localActions, this.#subduxes); - this.#initial = buildInitial(this.#localInitial, this.#subduxes); + this.#initialState = buildInitial(this.#localInitial, this.#subduxes); this.#localSelectors = config.selectors; const basedSelectors = R.mergeAll( @@ -144,8 +144,8 @@ export default class Updux< } // TODO memoize? - get initial() { - return this.#initial; + get initialState() { + return this.#initialState; } get effects() { @@ -178,10 +178,10 @@ export default class Updux< createStore( options: Partial<{ - initial: T_LocalState; + initialState: T_LocalState; }> = {}, ) { - const preloadedState: any = options.initial ?? this.initial; + const preloadedState: any = options.initialState ?? this.initialState; const effects = buildEffectsMiddleware( this.effects, @@ -246,14 +246,14 @@ export default class Updux< // TODO memoize this sucker get reducer() { return buildReducer( - this.initial, + this.initialState, this.#localMutations, this.#defaultMutation, this.#subduxes, ) as any as ( - state: undefined | typeof this.initial, + state: undefined | typeof this.initialState, action: Action, - ) => typeof this.initial; + ) => typeof this.initialState; } // TODO be smarter with the guard? diff --git a/src/actions.ts b/src/actions.ts index cf559b6..5712b0a 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -14,6 +14,7 @@ export const withPayload: WithPayload = ((prepare) => payload: prepare ? prepare(...input) : input[0], })) as any; +const id = (x) => x; export const createPayloadAction = < P extends any = any, T extends string = string, diff --git a/src/buildInitial.test.ts b/src/buildInitial.test.ts index 63743e8..8cc1d15 100644 --- a/src/buildInitial.test.ts +++ b/src/buildInitial.test.ts @@ -5,7 +5,7 @@ test('basic', () => { expect( buildInitial( { a: 1 }, - { b: { initial: { c: 2 } }, d: { initial: 'e' } }, + { b: { initialState: { c: 2 } }, d: { initialState: 'e' } }, ), ).toEqual({ a: 1, @@ -14,7 +14,7 @@ test('basic', () => { }); }); -test('throw if subduxes and initial is not an object', () => { +test('throw if subduxes and initialState is not an object', () => { expect(() => { buildInitial(3, { bar: 'foo' }); }).toThrow(); diff --git a/src/dux-selectors.test.todo b/src/dux-selectors.test.todo index 011b796..c6f659d 100644 --- a/src/dux-selectors.test.todo +++ b/src/dux-selectors.test.todo @@ -6,7 +6,7 @@ import { matches } from './utils.js'; test('basic selectors', () => { const foo = dux({ - initial: { + initialState: { x: 1, }, selectors: { @@ -14,7 +14,7 @@ test('basic selectors', () => { }, subduxes: { bar: { - initial: { y: 2 }, + initialState: { y: 2 }, selectors: { getY: ({ y }) => y, }, @@ -27,7 +27,7 @@ test('basic selectors', () => { test('splat selector', async () => { const bar = new Updux({ - initial: { id: 0, label: '' }, + initialState: { id: 0, label: '' }, selectors: { getLabel: R.prop('label'), getLabelAppended: (state) => (suffix) => state.label + ' ' + suffix, @@ -35,7 +35,7 @@ test('splat selector', async () => { }); const foo = new Updux({ - initial: [], + initialState: [], findSelectors: { getBar: (state) => (id) => { return state.find(matches({ id })); diff --git a/src/effects.test.ts b/src/effects.test.ts index f500cae..a6cde57 100644 --- a/src/effects.test.ts +++ b/src/effects.test.ts @@ -48,7 +48,7 @@ test('buildEffectsMiddleware', () => { test('basic', () => { const dux = new Updux({ - initial: { + initialState: { loaded: true, }, actions: { @@ -77,7 +77,7 @@ test('basic', () => { test('subdux', () => { const bar = new Updux({ - initial: 'bar state', + initialState: 'bar state', actions: { foo: 0 }, }); @@ -89,7 +89,7 @@ test('subdux', () => { }); const dux = new Updux({ - initial: { + initialState: { loaded: true, }, subduxes: { diff --git a/src/initial.test.ts b/src/initial.test.ts index 245632a..4446ee7 100644 --- a/src/initial.test.ts +++ b/src/initial.test.ts @@ -1,40 +1,40 @@ import { expectType } from './tutorial.test.js'; import Updux from './Updux.js'; -const bar = new Updux({ initial: 123 }); +const bar = new Updux({ initialState: 123 }); const foo = new Updux({ - initial: { root: 'abc' }, + initialState: { root: 'abc' }, subduxes: { bar, }, }); test('default', () => { - const { initial } = new Updux({}); + const { initialState } = new Updux({}); - expect(initial).toBeTypeOf('object'); - expect(initial).toEqual({}); + expect(initialState).toBeTypeOf('object'); + expect(initialState).toEqual({}); }); test('number', () => { - const { initial } = new Updux({ initial: 3 }); + const { initialState } = new Updux({ initialState: 3 }); - expect(initial).toBeTypeOf('number'); - expect(initial).toEqual(3); + expect(initialState).toBeTypeOf('number'); + expect(initialState).toEqual(3); }); -test('initial to createStore', () => { - const initial = { +test('initialState to createStore', () => { + const initialState = { a: 1, b: 2, }; const dux = new Updux({ - initial, + initialState, }); - expect(dux.createStore({ initial: { a: 3, b: 4 } }).getState()).toEqual({ + expect(dux.createStore({ initialState: { a: 3, b: 4 } }).getState()).toEqual({ a: 3, b: 4, }); @@ -42,15 +42,15 @@ test('initial to createStore', () => { test('single dux', () => { const foo = new Updux({ - initial: { a: 1 }, + initialState: { a: 1 }, }); - expect(foo.initial).toEqual({ a: 1 }); + expect(foo.initialState).toEqual({ a: 1 }); }); // TODO add 'check for no todo eslint rule' -test('initial value', () => { - expect(foo.initial).toEqual({ +test('initialState value', () => { + expect(foo.initialState).toEqual({ root: 'abc', bar: 123, }); @@ -58,41 +58,41 @@ test('initial value', () => { expectType<{ root: string; bar: number; - }>(foo.initial); + }>(foo.initialState); }); -test('no initial', () => { +test('no initialState', () => { const dux = new Updux({}); - expectType<{}>(dux.initial); - expect(dux.initial).toEqual({}); + expectType<{}>(dux.initialState); + expect(dux.initialState).toEqual({}); }); -test('no initial for subdux', () => { +test('no initialState for subdux', () => { const dux = new Updux({ subduxes: { bar: new Updux({}), - baz: new Updux({ initial: 'potato' }), + baz: new Updux({ initialState: 'potato' }), }, }); - expectType<{ bar: {}; baz: string }>(dux.initial); - expect(dux.initial).toEqual({ bar: {}, baz: 'potato' }); + expectType<{ bar: {}; baz: string }>(dux.initialState); + expect(dux.initialState).toEqual({ bar: {}, baz: 'potato' }); }); -test.todo('splat initial', async () => { +test.todo('splat initialState', async () => { const bar = new Updux({ - initial: { id: 0 }, + initialState: { id: 0 }, }); const foo = new Updux({ subduxes: { '*': bar }, }); - expect(foo.initial).toEqual([]); + expect(foo.initialState).toEqual([]); expect( new Updux({ - initial: 'overriden', + initialState: 'overriden', subduxes: { '*': bar }, - }).initial, + }).initialState, ).toEqual('overriden'); }); diff --git a/src/initial.ts b/src/initial.ts index 36aaf92..41eb872 100644 --- a/src/initial.ts +++ b/src/initial.ts @@ -1,7 +1,7 @@ import u from '@yanick/updeep-remeda'; import * as R from 'remeda'; -type SubduxState = 'initial' extends keyof S ? S['initial'] : {}; +type SubduxState = 'initialState' extends keyof S ? S['initialState'] : {}; export type AggregateState> = LOCAL & (keyof SUBDUXES extends never @@ -15,9 +15,9 @@ export type AggregateState> = LOCAL & export function buildInitial(localInitial, subduxes) { if (Object.keys(subduxes).length > 0 && typeof localInitial !== 'object') { throw new Error( - "can't have subduxes when the initial value is not an object", + "can't have subduxes when the initialState value is not an object", ); } - return u(localInitial, R.mapValues(subduxes, R.pathOr(['initial'], {}))); + return u(localInitial, R.mapValues(subduxes, R.pathOr(['initialState'], {}))); } diff --git a/src/mutations.test.ts b/src/mutations.test.ts index 5858b63..73ae207 100644 --- a/src/mutations.test.ts +++ b/src/mutations.test.ts @@ -4,7 +4,7 @@ import Updux, { createAction } from './index.js'; test('set a mutation', () => { const dux = new Updux({ - initial: 'potato', + initialState: 'potato', actions: { foo: (x) => ({ x }), bar: 0, @@ -27,7 +27,7 @@ test('set a mutation', () => { test('catch-all mutation', () => { const dux = new Updux({ - initial: '', + initialState: '', }); dux.addMutation( @@ -40,7 +40,7 @@ test('catch-all mutation', () => { test('default mutation', () => { const dux = new Updux({ - initial: '', + initialState: '', actions: { foo: 0, }, @@ -60,7 +60,7 @@ test('mutation of a subdux', () => { const stopit = createAction('stopit'); const bar = new Updux({ - initial: 0, + initialState: 0, actions: { baz, stopit, diff --git a/src/reactions.test.ts b/src/reactions.test.ts index 8dc5d18..b807f8a 100644 --- a/src/reactions.test.ts +++ b/src/reactions.test.ts @@ -3,7 +3,7 @@ import Updux from './index.js'; test('basic reactions', () => { const foo = new Updux({ - initial: 0, + initialState: 0, actions: { inc: 0, reset: 0 }, }); @@ -39,7 +39,7 @@ test('basic reactions', () => { test('subdux reactions', () => { const bar = new Updux({ - initial: 0, + initialState: 0, actions: { inc: 0, reset: 0 }, selectors: { getIt: (x) => x, diff --git a/src/reducer.test.ts b/src/reducer.test.ts index 9ce0ba8..7e7b36c 100644 --- a/src/reducer.test.ts +++ b/src/reducer.test.ts @@ -3,7 +3,7 @@ import { test, expect } from 'vitest'; import { buildReducer } from './reducer.js'; import Updux from './Updux.js'; -test('buildReducer, initial state', () => { +test('buildReducer, initialState state', () => { const reducer = buildReducer({ a: 1 }); expect(reducer(undefined, { type: 'foo' })).toEqual({ a: 1 }); @@ -23,7 +23,7 @@ test('buildReducer, mutation', () => { }); test.todo('basic reducer', () => { - const dux = new Updux({ initial: { a: 3 } }); + const dux = new Updux({ initialState: { a: 3 } }); expect(dux.reducer).toBeTypeOf('function'); diff --git a/src/reducer.ts b/src/reducer.ts index 2f92f5a..c11c60b 100644 --- a/src/reducer.ts +++ b/src/reducer.ts @@ -12,7 +12,7 @@ export type MutationCase = { }; export function buildReducer( - initialState: any, + initialStateState: any, mutations: MutationCase[] = [], defaultMutation?: Omit, subduxes: Record = {}, @@ -22,7 +22,7 @@ export function buildReducer( // TODO matcherMutation // TODO defaultMutation // - const reducer = (state = initialState, action: Action) => { + const reducer = (state = initialStateState, action: Action) => { const orig = state; if (!action?.type) throw new Error('upreducer called with a bad action'); diff --git a/src/selectors.test.ts b/src/selectors.test.ts index d0695b9..b7efb51 100644 --- a/src/selectors.test.ts +++ b/src/selectors.test.ts @@ -6,7 +6,7 @@ test('basic selectors', () => { type State = { x: number }; const foo = new Updux({ - initial: { + initialState: { x: 1, }, selectors: { @@ -14,7 +14,7 @@ test('basic selectors', () => { }, subduxes: { bar: new Updux({ - initial: { y: 2 }, + initialState: { y: 2 }, selectors: { getY: ({ y }: { y: number }) => y, getYPlus: diff --git a/src/splatReactions.test.todo b/src/splatReactions.test.todo index 7fbe918..11da9a1 100644 --- a/src/splatReactions.test.todo +++ b/src/splatReactions.test.todo @@ -9,7 +9,7 @@ const thingReactionSnitch = vi.fn(); const subThing = new Updux({ name: 'subThing', - initial: 0, + initialState: 0, }); subThing.addReaction((api) => (state, previousState, unsubscribe) => { @@ -18,7 +18,7 @@ subThing.addReaction((api) => (state, previousState, unsubscribe) => { const thing = new Updux({ name: 'thing', - initial: {}, + initialState: {}, subduxes: { '*': subThing, }, @@ -41,11 +41,11 @@ const things = new Updux({ subduxes: { '*': thing, }, - initial: {}, + initialState: {}, actions: { newThing: (id) => id }, splatReactionMapper: ({ id }) => id, mutations: { - newThing: (id) => (state) => ({ ...state, [id]: thing.initial }), + newThing: (id) => (state) => ({ ...state, [id]: thing.initialState }), }, }); diff --git a/src/tutorial.test.ts b/src/tutorial.test.ts index 74b4f09..67c5c48 100644 --- a/src/tutorial.test.ts +++ b/src/tutorial.test.ts @@ -3,25 +3,25 @@ import u from '@yanick/updeep-remeda'; export const expectType = (value: T) => value; -test('initial state', () => { - const initial = { +test('initialState state', () => { + const initialState = { next_id: 1, todos: [], }; const dux = new Updux({ - initial, + initialState, }); expectType<{ next_id: number; todos: unknown[]; - }>(dux.initial); + }>(dux.initialState); - expect(dux.initial).toEqual(initial); + expect(dux.initialState).toEqual(initialState); const store = dux.createStore(); - expect(store.getState()).toEqual(initial); + expect(store.getState()).toEqual(initialState); }); test('actions', () => { @@ -51,7 +51,7 @@ test('mutation', () => { }; const dux = new Updux({ - initial: { nextId: 0, todos: [] as Todo[] }, + initialState: { nextId: 0, todos: [] as Todo[] }, }); dux.addMutation(addTodo, (description) => (state) => { diff --git a/src/types.ts b/src/types.ts index 54db08d..87f93a2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,7 +4,7 @@ export type Dux< STATE = any, ACTIONS extends Record> = {}, > = Partial<{ - initial: STATE; + initialState: STATE; actions: ACTIONS; selectors: Record any>; reducer: (