action shorcuts
This commit is contained in:
parent
6cb7f14407
commit
3bb9221c27
10
src/Updux.js
10
src/Updux.js
@ -1,6 +1,10 @@
|
|||||||
import R from 'remeda';
|
import R from 'remeda';
|
||||||
|
|
||||||
import { Action, ActionGenerator } from './actions.js';
|
import { action } from './actions.js';
|
||||||
|
|
||||||
|
function isActionGen(action) {
|
||||||
|
return typeof action === "function" && action.type;
|
||||||
|
}
|
||||||
|
|
||||||
export class Updux {
|
export class Updux {
|
||||||
#localInitial = {};
|
#localInitial = {};
|
||||||
@ -14,7 +18,9 @@ export class Updux {
|
|||||||
this.#localInitial = config.initial ?? {};
|
this.#localInitial = config.initial ?? {};
|
||||||
this.#subduxes = config.subduxes ?? {};
|
this.#subduxes = config.subduxes ?? {};
|
||||||
|
|
||||||
this.#actions = { ...config.actions };
|
this.#actions = R.mapValues( config.actions ?? {}, (arg,name) =>
|
||||||
|
isActionGen(arg) ? arg : action(name,arg)
|
||||||
|
);
|
||||||
Object.entries(this.#subduxes).forEach( ([slice,sub]) => this.#addSubduxActions(slice,sub) )
|
Object.entries(this.#subduxes).forEach( ([slice,sub]) => this.#addSubduxActions(slice,sub) )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,3 +67,15 @@ test('throw if double action', () => {
|
|||||||
}) ).toThrow(/action 'foo' already defined/);
|
}) ).toThrow(/action 'foo' already defined/);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('action definition shortcut', () => {
|
||||||
|
const foo = new Updux({
|
||||||
|
actions: {
|
||||||
|
foo: null,
|
||||||
|
bar: x => ({x}),
|
||||||
|
},}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect( foo.actions.foo("hello") ).toEqual({ type: 'foo', payload: 'hello' });
|
||||||
|
expect( foo.actions.bar("hello") ).toEqual({ type: 'bar', payload: {x:'hello'} });
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user