prettier
This commit is contained in:
parent
3bb9221c27
commit
43702fe096
18
src/Updux.js
18
src/Updux.js
@ -3,7 +3,7 @@ import R from 'remeda';
|
|||||||
import { action } from './actions.js';
|
import { action } from './actions.js';
|
||||||
|
|
||||||
function isActionGen(action) {
|
function isActionGen(action) {
|
||||||
return typeof action === "function" && action.type;
|
return typeof action === 'function' && action.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Updux {
|
export class Updux {
|
||||||
@ -18,18 +18,20 @@ export class Updux {
|
|||||||
this.#localInitial = config.initial ?? {};
|
this.#localInitial = config.initial ?? {};
|
||||||
this.#subduxes = config.subduxes ?? {};
|
this.#subduxes = config.subduxes ?? {};
|
||||||
|
|
||||||
this.#actions = R.mapValues( config.actions ?? {}, (arg,name) =>
|
this.#actions = R.mapValues(config.actions ?? {}, (arg, name) =>
|
||||||
isActionGen(arg) ? arg : action(name,arg)
|
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) )
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#addSubduxActions(_slice, subdux) {
|
#addSubduxActions(_slice, subdux) {
|
||||||
if( ! subdux.actions ) return;
|
if (!subdux.actions) return;
|
||||||
// TODO action 'blah' defined multiple times: <where>
|
// TODO action 'blah' defined multiple times: <where>
|
||||||
Object.entries( subdux.actions ).forEach( ([action,gen]) => {
|
Object.entries(subdux.actions).forEach(([action, gen]) => {
|
||||||
if( this.#actions[action] ) {
|
if (this.#actions[action]) {
|
||||||
if( this.#actions[action] === gen ) return;
|
if (this.#actions[action] === gen) return;
|
||||||
throw new Error(`action '${action}' already defined`);
|
throw new Error(`action '${action}' already defined`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,29 +53,34 @@ test('subduxes actions', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('throw if double action', () => {
|
test('throw if double action', () => {
|
||||||
expect( () => new Updux({
|
expect(
|
||||||
actions: {
|
() =>
|
||||||
foo: action('foo'),
|
new Updux({
|
||||||
},
|
|
||||||
subduxes: {
|
|
||||||
beta: {
|
|
||||||
actions: {
|
actions: {
|
||||||
foo: action('foo'),
|
foo: action('foo'),
|
||||||
},
|
},
|
||||||
},
|
subduxes: {
|
||||||
},
|
beta: {
|
||||||
}) ).toThrow(/action 'foo' already defined/);
|
actions: {
|
||||||
|
foo: action('foo'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
).toThrow(/action 'foo' already defined/);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('action definition shortcut', () => {
|
test('action definition shortcut', () => {
|
||||||
const foo = new Updux({
|
const foo = new Updux({
|
||||||
actions: {
|
actions: {
|
||||||
foo: null,
|
foo: null,
|
||||||
bar: x => ({x}),
|
bar: (x) => ({ x }),
|
||||||
},}
|
},
|
||||||
);
|
});
|
||||||
|
|
||||||
expect( foo.actions.foo("hello") ).toEqual({ type: 'foo', payload: 'hello' });
|
expect(foo.actions.foo('hello')).toEqual({ type: 'foo', payload: 'hello' });
|
||||||
expect( foo.actions.bar("hello") ).toEqual({ type: 'bar', payload: {x:'hello'} });
|
expect(foo.actions.bar('hello')).toEqual({
|
||||||
|
type: 'bar',
|
||||||
|
payload: { x: 'hello' },
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user