add createPayloadAction
This commit is contained in:
parent
bde9cac053
commit
a9cb408521
38
src/Updux.test.ts
Normal file
38
src/Updux.test.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { test, expect } from 'vitest';
|
||||
import Updux from './Updux';
|
||||
|
||||
test('subdux idempotency', () => {
|
||||
// const c = new Updux({
|
||||
// initial: 2,
|
||||
// });
|
||||
// const b = new Updux({
|
||||
// subduxes: {
|
||||
// c,
|
||||
// },
|
||||
// });
|
||||
const foo = new Updux({
|
||||
subduxes: {
|
||||
a: new Updux({ initial: 2 }),
|
||||
},
|
||||
});
|
||||
|
||||
let fooState = foo.reducer(undefined, { type: 'noop' });
|
||||
expect(foo.reducer(fooState, { type: 'noop' })).toBe(fooState);
|
||||
|
||||
return;
|
||||
const store = foo.createStore();
|
||||
|
||||
const s1 = store.getState();
|
||||
console.log(s1);
|
||||
|
||||
store.dispatch({ type: 'noop' });
|
||||
const s2 = store.getState();
|
||||
|
||||
expect(s2.a).toBe(s1.a);
|
||||
|
||||
let bState = b.reducer(undefined, { type: 'noop' });
|
||||
expect(b.reducer(bState, { type: 'noop' })).toBe(bState);
|
||||
|
||||
expect(s2.b).toBe(s1.b);
|
||||
expect(s2).toBe(s1);
|
||||
});
|
@ -13,3 +13,16 @@ export const withPayload: WithPayload = ((prepare) =>
|
||||
(...input) => ({
|
||||
payload: prepare ? prepare(...input) : input[0],
|
||||
})) as any;
|
||||
|
||||
export const createPayloadAction = <
|
||||
P extends any = any,
|
||||
T extends string = string,
|
||||
F extends (...args: any[]) => P = (input: P) => P,
|
||||
>(
|
||||
type: T,
|
||||
prepare?: F,
|
||||
) =>
|
||||
createAction(
|
||||
type,
|
||||
withPayload<ReturnType<F>, Parameters<F>>(prepare ?? (id as any)),
|
||||
);
|
||||
|
@ -1,5 +1,5 @@
|
||||
import Updux from './Updux.js';
|
||||
|
||||
export { withPayload, createAction } from './actions.js';
|
||||
export { withPayload, createAction, createPayloadAction } from './actions.js';
|
||||
|
||||
export default Updux;
|
||||
|
Loading…
Reference in New Issue
Block a user