Compare commits
3 Commits
760263b555
...
40b58a41a6
Author | SHA1 | Date | |
---|---|---|---|
40b58a41a6 | |||
93582bcb70 | |||
eec442ff68 |
@ -19,6 +19,7 @@ tasks:
|
||||
- checks
|
||||
cmds:
|
||||
- npm version prerelease
|
||||
- git push --tags
|
||||
- npm pack --pack-destination releases
|
||||
- { task: 'release:gitea' }
|
||||
|
||||
|
18
src/Updux.ts
18
src/Updux.ts
@ -81,6 +81,9 @@ export default class Updux<D extends DuxConfig> {
|
||||
/** @internal */
|
||||
#inheritedReducer: (state: DuxState<D> | undefined, action: AnyAction) => DuxState<D>;
|
||||
|
||||
/** @internal */
|
||||
#selectors = {};
|
||||
|
||||
constructor(private readonly duxConfig: D) {
|
||||
if (duxConfig.subduxes)
|
||||
this.#subduxes = D.map(duxConfig.subduxes, (s) =>
|
||||
@ -94,6 +97,8 @@ export default class Updux<D extends DuxConfig> {
|
||||
this.#reactions = (duxConfig.reactions as any) ?? [];
|
||||
|
||||
this.#actions = buildActions(duxConfig.actions, this.#subduxes) as any;
|
||||
|
||||
this.#selectors = duxConfig.selectors ?? {};
|
||||
}
|
||||
|
||||
|
||||
@ -166,6 +171,17 @@ export default class Updux<D extends DuxConfig> {
|
||||
throw new Error(`redefining action ${type}`);
|
||||
}
|
||||
|
||||
addSelector<R, N extends string>(name: N, selector: (state: DuxState<D>) => R) {
|
||||
this.#selectors = {
|
||||
...this.#selectors,
|
||||
[name]: selector,
|
||||
}
|
||||
|
||||
return this as any as Updux<D & {
|
||||
selectors: Record<N, (state: DuxState<D>) => R>
|
||||
}>;
|
||||
}
|
||||
|
||||
addMutation<A extends keyof DuxActions<D>>(
|
||||
actionType: A,
|
||||
mutation: Mutation<
|
||||
@ -240,7 +256,7 @@ export default class Updux<D extends DuxConfig> {
|
||||
|
||||
get selectors(): DuxSelectors<D> {
|
||||
return this.#memoBuildSelectors(
|
||||
this.duxConfig.selectors,
|
||||
this.#selectors,
|
||||
this.#subduxes,
|
||||
) as any;
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
import { ActionCreatorWithPreparedPayload } from '@reduxjs/toolkit';
|
||||
import {
|
||||
ActionCreator,
|
||||
ActionCreatorWithPreparedPayload,
|
||||
} from '@reduxjs/toolkit';
|
||||
import { buildActions, createAction, withPayload } from './actions.js';
|
||||
import Updux from './index.js';
|
||||
|
||||
@ -42,7 +45,8 @@ test('withPayload, just the type', () => {
|
||||
test('buildActions', () => {
|
||||
const actions = buildActions(
|
||||
{
|
||||
one: createAction('one'), two: (x: string) => x,
|
||||
one: createAction('one'),
|
||||
two: (x: string) => x,
|
||||
withoutValue: null,
|
||||
},
|
||||
{ a: { actions: buildActions({ three: () => 3 }) } },
|
||||
@ -87,15 +91,15 @@ describe('Updux interactions', () => {
|
||||
subduxes: {
|
||||
subdux1: new Updux({
|
||||
actions: {
|
||||
fromSubdux: (x: string) => x
|
||||
}
|
||||
fromSubdux: (x: string) => x,
|
||||
},
|
||||
}),
|
||||
subdux2: {
|
||||
actions: {
|
||||
ohmy: () => { }
|
||||
}
|
||||
}
|
||||
}
|
||||
ohmy: () => { },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
test('actions getter', () => {
|
||||
@ -103,11 +107,18 @@ describe('Updux interactions', () => {
|
||||
});
|
||||
|
||||
expectTypeOf(dux.actions.withNull).toMatchTypeOf<
|
||||
ActionCreatorWithPreparedPayload<any, null, 'withNull'>>();
|
||||
ActionCreator<'withNull'>
|
||||
>();
|
||||
|
||||
expect(dux.actions.withNull()).toMatchObject({
|
||||
type: 'withNull',
|
||||
});
|
||||
|
||||
expectTypeOf(dux.actions).toMatchTypeOf<Record<string, any>>();
|
||||
expectTypeOf(dux.actions?.add).not.toEqualTypeOf<any>();
|
||||
|
||||
expect(dux.actions.fromSubdux('payload')).toEqual({ type: 'fromSubdux', payload: 'payload' });
|
||||
|
||||
expect(dux.actions.fromSubdux('payload')).toEqual({
|
||||
type: 'fromSubdux',
|
||||
payload: 'payload',
|
||||
});
|
||||
});
|
||||
|
@ -51,4 +51,17 @@ describe('basic selectors', () => {
|
||||
expect(store.getState.getY()).toBe(2);
|
||||
expect(store.getState.getYPlus(3)).toBe(5);
|
||||
});
|
||||
|
||||
test('addSelector', () => {
|
||||
const dux = new Updux({ initialState: 13 }).addSelector(
|
||||
'plusHundred',
|
||||
(state) => state + 100,
|
||||
);
|
||||
|
||||
expectTypeOf(dux.selectors.plusHundred).toMatchTypeOf<
|
||||
(state: number) => number
|
||||
>();
|
||||
|
||||
expect(dux.selectors.plusHundred(7)).toBe(107);
|
||||
});
|
||||
});
|
||||
|
@ -45,11 +45,16 @@ type SubduxesActions<D> = D extends {
|
||||
? UnionToIntersection<DuxActions<UpduxConfig<S[keyof S]>>>
|
||||
: unknown;
|
||||
|
||||
type ResolveAction<K extends string, A> = A extends Function & { type: string }
|
||||
type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
|
||||
type IsAny<T> = IfAny<T, true, never>;
|
||||
|
||||
type ResolveAction<K extends string, A> = true extends IsAny<A>
|
||||
? ActionCreator<A>
|
||||
: A extends Function & { type: string }
|
||||
? A
|
||||
: A extends (...args: infer PARAMS) => infer R
|
||||
? ActionCreatorWithPreparedPayload<PARAMS, R, K, never, never>
|
||||
: A;
|
||||
: ActionCreator<A>;
|
||||
|
||||
type ResolveActions<A> = A extends { [key: string]: any }
|
||||
? {
|
||||
|
Loading…
Reference in New Issue
Block a user