add generic initial
This commit is contained in:
parent
85e478c02f
commit
27958a6d14
@ -6,6 +6,13 @@ vars:
|
|||||||
GREETING: Hello, World!
|
GREETING: Hello, World!
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
|
tsc: tsc
|
||||||
|
test: jest
|
||||||
|
|
||||||
|
check:
|
||||||
|
deps: [tsc, test]
|
||||||
|
|
||||||
|
|
||||||
'test:types': tsd
|
'test:types': tsd
|
||||||
docs:
|
docs:
|
||||||
cmds:
|
cmds:
|
||||||
|
22
src/Updux.ts
22
src/Updux.ts
@ -14,12 +14,17 @@ import {
|
|||||||
augmentMiddlewareApi,
|
augmentMiddlewareApi,
|
||||||
} from './buildMiddleware';
|
} from './buildMiddleware';
|
||||||
|
|
||||||
import { Dict } from './types';
|
import { AggregateDuxState, Dict } from './types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration object typically passed to the constructor of the class Updux.
|
* Configuration object typically passed to the constructor of the class Updux.
|
||||||
*/
|
*/
|
||||||
export interface UpduxConfig<TState = unknown> {
|
export interface UpduxConfig<
|
||||||
|
TState = any,
|
||||||
|
TActions = {},
|
||||||
|
TSelectors = {},
|
||||||
|
TSubduxes = {},
|
||||||
|
> {
|
||||||
/**
|
/**
|
||||||
* Local initial state.
|
* Local initial state.
|
||||||
* @default {}
|
* @default {}
|
||||||
@ -29,7 +34,7 @@ export interface UpduxConfig<TState = unknown> {
|
|||||||
/**
|
/**
|
||||||
* Subduxes to be merged to this dux.
|
* Subduxes to be merged to this dux.
|
||||||
*/
|
*/
|
||||||
subduxes?: Dict<Updux | UpduxConfig>;
|
subduxes?: TSubduxes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local actions.
|
* Local actions.
|
||||||
@ -70,7 +75,12 @@ export interface UpduxConfig<TState = unknown> {
|
|||||||
mappedReaction?: Function | boolean;
|
mappedReaction?: Function | boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Updux {
|
export class Updux<
|
||||||
|
TState extends any = {},
|
||||||
|
TActions = {},
|
||||||
|
TSelectors = {},
|
||||||
|
TSubduxes extends object = {},
|
||||||
|
> {
|
||||||
/** @type { unknown } */
|
/** @type { unknown } */
|
||||||
#initial = {};
|
#initial = {};
|
||||||
#subduxes = {};
|
#subduxes = {};
|
||||||
@ -84,7 +94,7 @@ export class Updux {
|
|||||||
#mappedSelectors = undefined;
|
#mappedSelectors = undefined;
|
||||||
#mappedReaction = undefined;
|
#mappedReaction = undefined;
|
||||||
|
|
||||||
constructor(config: UpduxConfig) {
|
constructor(config: UpduxConfig<TState,TActions,TSelectors,TSubduxes>) {
|
||||||
this.#initial = config.initial ?? {};
|
this.#initial = config.initial ?? {};
|
||||||
this.#subduxes = config.subduxes ?? {};
|
this.#subduxes = config.subduxes ?? {};
|
||||||
|
|
||||||
@ -148,7 +158,7 @@ export class Updux {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @member { unknown } */
|
/** @member { unknown } */
|
||||||
get initial() {
|
get initial() : AggregateDuxState<TState,TSubduxes> {
|
||||||
return this.#memoInitial(this.#initial, this.#subduxes);
|
return this.#memoInitial(this.#initial, this.#subduxes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { action } from './actions.js';
|
import { action } from './actions';
|
||||||
|
|
||||||
test('action generators', () => {
|
test('action generators', () => {
|
||||||
const foo = action('foo');
|
const foo = action('foo');
|
||||||
|
18
src/types.ts
18
src/types.ts
@ -1,2 +1,20 @@
|
|||||||
|
|
||||||
export type Dict<T> = Record<string, T>;
|
export type Dict<T> = Record<string, T>;
|
||||||
|
|
||||||
|
type Subdux<TState = any> = {
|
||||||
|
initial: TState
|
||||||
|
};
|
||||||
|
|
||||||
|
type StateOf<D> = D extends { initial: infer I } ? I : unknown;
|
||||||
|
|
||||||
|
type Subduxes = Record<string,Subdux>;
|
||||||
|
|
||||||
|
export type DuxStateSubduxes<C> = C extends { '*': infer I }
|
||||||
|
? {
|
||||||
|
[key: string]: StateOf<I>;
|
||||||
|
[index: number]: StateOf<I>;
|
||||||
|
}
|
||||||
|
: { [K in keyof C]: StateOf<C[K]> };
|
||||||
|
|
||||||
|
export type AggregateDuxState<TState, TSubduxes> = TState & DuxStateSubduxes<TSubduxes>;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user