Merge branch 'actions' into rewrite

typescript
Yanick Champoux 2022-08-25 16:46:56 -04:00
commit 3c78f34d5b
3 changed files with 101 additions and 3 deletions

View File

@ -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,20 @@ export type DuxStateSubduxes<C extends {}> = keyof C extends never
? unknown
: { [K in keyof C]: StateOf<C[K]> };
export class Updux<TState extends any = {}, TSubduxes = {}> {
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 initial(): TState & DuxStateSubduxes<TSubduxes> {

37
src/actions.test.ts Normal file
View File

@ -0,0 +1,37 @@
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 }));
expect(foo('bar')).toEqual({
type: 'foo',
payload: {
thing: 'bar',
},
});
});
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'
}
});
} )

51
src/actions.ts Normal file
View File

@ -0,0 +1,51 @@
export type Action<T extends string = string, TPayload = any> = {
type: T;
meta?: Record<string, unknown>;
payload?: TPayload;
};
export type ActionGenerator<
TType extends string = string,
TPayloadGen = (...args: any[]) => any,
> = {
type: TType;
} & (TPayloadGen extends (...args: any) => any
? ReturnType<TPayloadGen> extends void
? (...args: Parameters<TPayloadGen>) => {
type: TType;
}
: (...args: Parameters<TPayloadGen>) => {
type: TType;
payload: ReturnType<TPayloadGen>;
}
: (...args: any[]) => { type: TType; payload?: unknown });
export function action<
TType extends string,
TPayload extends (...args: any) => any,
>(
type: TType,
payloadFunction?: TPayload,
transformer?: Function,
): ActionGenerator<TType, TPayload> {
let generator: any = function (...payloadArg:any[]) {
const result: Action = { type };
if (payloadFunction) {
result.payload = payloadFunction(...payloadArg);
} else {
if (payloadArg[0] !== undefined) result.payload = payloadArg[0];
}
return result;
};
if (transformer) {
const orig = generator;
generator = (...args: any) => transformer(orig(...args), args);
}
generator.type = type;
return generator;
}