add actions
This commit is contained in:
parent
64782096df
commit
4d4a5dde50
31
src/Updux.ts
31
src/Updux.ts
@ -1,15 +1,18 @@
|
||||
import R from 'remeda';
|
||||
import { Action, ActionGenerator } from './actions';
|
||||
|
||||
/**
|
||||
* Configuration object typically passed to the constructor of the class Updux.
|
||||
*/
|
||||
export interface UpduxConfig<TState = any, TSubduxes = {}> {
|
||||
export interface UpduxConfig<TState = any, TActions extends Record<string,ActionGenerator> = Record<string,ActionGenerator>, TSubduxes = {}> {
|
||||
/**
|
||||
* Local initial state.
|
||||
* @default {}
|
||||
*/
|
||||
initial?: TState;
|
||||
|
||||
actions?: TActions;
|
||||
|
||||
/**
|
||||
* Subduxes to be merged to this dux.
|
||||
*/
|
||||
@ -22,13 +25,35 @@ export type DuxStateSubduxes<C extends {}> = keyof C extends never
|
||||
? unknown
|
||||
: { [K in keyof C]: StateOf<C[K]> };
|
||||
|
||||
export class Updux<TState extends any = {}, TSubduxes = {}> {
|
||||
// type UnionArrayTypes<A extends any[]> = A[number];
|
||||
|
||||
// type ActionType<A extends ActionGenerator> = A extends ActionGenerator<infer B> ? B : never;
|
||||
|
||||
// type ActionAsObject<A extends ActionGenerator> = Record< ActionType<A>, A > }>;
|
||||
|
||||
// type UnionToIntersection<U> =
|
||||
// (U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never
|
||||
|
||||
// type ActionsAsObject<A extends ActionGenerator[]> = ActionAsObject< A[number]>
|
||||
|
||||
export class Updux<TState extends any = {}, TActions extends { [key: string]: ActionGenerator } = {}, TSubduxes = {}> {
|
||||
#localInitial: any = {};
|
||||
#subduxes;
|
||||
#actions : TActions;
|
||||
|
||||
constructor(config: UpduxConfig<TState, TSubduxes>) {
|
||||
constructor(config: UpduxConfig<TState, TActions, TSubduxes>) {
|
||||
this.#localInitial = config.initial ?? {};
|
||||
this.#subduxes = config.subduxes ?? {};
|
||||
|
||||
this.#actions = config.actions ?? ([] as any);
|
||||
}
|
||||
|
||||
get actions() {
|
||||
return this.#actions;
|
||||
}
|
||||
|
||||
get action(): ActionsAsObject<TActions> {
|
||||
return Object.fromEntries(this.#actions.map( a => [ a.type, a ] ));
|
||||
}
|
||||
|
||||
get initial(): TState & DuxStateSubduxes<TSubduxes> {
|
||||
|
@ -2,6 +2,8 @@ import { test, expect } from 'vitest';
|
||||
|
||||
import { action } from './actions.js';
|
||||
|
||||
import { Updux } from './Updux.js';
|
||||
|
||||
test('basic action', () => {
|
||||
const foo = action('foo', (thing: string) => ({ thing }));
|
||||
|
||||
@ -12,3 +14,24 @@ test('basic action', () => {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test( "Updux config accepts actions", () => {
|
||||
const foo = new Updux({
|
||||
actions: {
|
||||
one: action('one', (x: string) => ({x})),
|
||||
two: action('two', x => x),
|
||||
}
|
||||
});
|
||||
|
||||
expect(Object.keys(foo.actions)).toHaveLength(2);
|
||||
|
||||
expect( foo.actions.one ).toBeTypeOf('function');
|
||||
expect( foo.actions.one("potato") ).toEqual({
|
||||
type: 'one',
|
||||
payload: {
|
||||
x: 'potato'
|
||||
}
|
||||
});
|
||||
|
||||
} )
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user