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