rename SUBDUXES to T_Subduxes

This commit is contained in:
Yanick Champoux 2023-03-07 11:57:41 -05:00
parent 042f5b8f13
commit 736377effd
3 changed files with 21 additions and 23 deletions

View File

@ -18,23 +18,10 @@ import {
import { withPayload } from './actions.js';
import { AggregateActions, Dux, UnionToIntersection } from './types.js';
import { buildActions } from './buildActions.js';
import { buildInitial } from './buildInitial.js';
type SubduxState<S> = 'initial' extends keyof S ? S['initial'] : {};
type AggregateState<LOCAL, SUBDUXES extends Record<any, any>> = LOCAL &
(keyof SUBDUXES extends never
? {}
: {
[Slice in keyof SUBDUXES]: Slice extends string
? SubduxState<SUBDUXES[Slice]>
: never;
});
import { buildInitial, AggregateState } from './initial.js';
type MyActionCreator = { type: string } & ((...args: any) => any);
type F = null extends any ? 'u' : 'n';
type ResolveAction<
ActionType extends string,
ActionArg extends any,
@ -69,33 +56,33 @@ export default class Updux<
T_LocalActions extends {
[actionType: string]: any;
} = {},
SUBDUXES extends Record<string, Dux> = {},
T_Subduxes extends Record<string, Dux> = {},
> {
#localInitial: T_LocalState;
#localActions: T_LocalActions;
#localMutations: Record<
string,
Mutation<ActionCreator<any>, AggregateState<T_LocalState, SUBDUXES>>
Mutation<ActionCreator<any>, AggregateState<T_LocalState, T_Subduxes>>
> = {};
#subduxes: SUBDUXES;
#subduxes: T_Subduxes;
#name: string;
#actions: AggregateActions<ResolveActions<T_LocalActions>, SUBDUXES>;
#actions: AggregateActions<ResolveActions<T_LocalActions>, T_Subduxes>;
#initial: AggregateState<T_LocalState, SUBDUXES>;
#initial: AggregateState<T_LocalState, T_Subduxes>;
constructor(
config: Partial<{
initial: T_LocalState;
actions: T_LocalActions;
subduxes: SUBDUXES;
subduxes: T_Subduxes;
}>,
) {
// TODO check that we can't alter the initial after the fact
this.#localInitial = config.initial ?? ({} as T_LocalState);
this.#localActions = config.actions ?? ({} as T_LocalActions);
this.#subduxes = config.subduxes ?? ({} as SUBDUXES);
this.#subduxes = config.subduxes ?? ({} as T_Subduxes);
this.#actions = buildActions(this.#localActions, this.#subduxes);
@ -128,7 +115,7 @@ export default class Updux<
// TODO force the actionCreator to be one of the actions?
mutation<A extends ActionCreator<any>>(
actionCreator: A,
mutation: Mutation<A, AggregateState<T_LocalState, SUBDUXES>>,
mutation: Mutation<A, AggregateState<T_LocalState, T_Subduxes>>,
) {
this.#localMutations[(actionCreator as any).type] = mutation;
}

View File

@ -1,5 +1,5 @@
import { test, expect } from 'vitest';
import { buildInitial } from './buildInitial.js';
import { buildInitial } from './initial.js';
test('basic', () => {
expect(

View File

@ -1,6 +1,17 @@
import u from '@yanick/updeep-remeda';
import * as R from 'remeda';
type SubduxState<S> = 'initial' extends keyof S ? S['initial'] : {};
export type AggregateState<LOCAL, SUBDUXES extends Record<any, any>> = LOCAL &
(keyof SUBDUXES extends never
? {}
: {
[Slice in keyof SUBDUXES]: Slice extends string
? SubduxState<SUBDUXES[Slice]>
: never;
});
export function buildInitial(localInitial, subduxes) {
if (Object.keys(subduxes).length > 0 && typeof localInitial !== 'object') {
throw new Error(