warn of action clashes

typescript
Yanick Champoux 2022-08-26 14:41:22 -04:00
parent 6a6bb1636a
commit 6cb7f14407
2 changed files with 31 additions and 4 deletions

View File

@ -14,10 +14,21 @@ export class Updux {
this.#localInitial = config.initial ?? {};
this.#subduxes = config.subduxes ?? {};
this.#actions = R.mergeAll([
config.actions ?? {},
...Object.values(this.#subduxes).map(R.prop('actions')),
]);
this.#actions = { ...config.actions };
Object.entries(this.#subduxes).forEach( ([slice,sub]) => this.#addSubduxActions(slice,sub) )
}
#addSubduxActions(_slice, subdux) {
if( ! subdux.actions ) return;
// TODO action 'blah' defined multiple times: <where>
Object.entries( subdux.actions ).forEach( ([action,gen]) => {
if( this.#actions[action] ) {
if( this.#actions[action] === gen ) return;
throw new Error(`action '${action}' already defined`);
}
this.#actions[action] = gen;
});
}
get actions() {

View File

@ -51,3 +51,19 @@ test('subduxes actions', () => {
expect(foo.actions).toHaveProperty('foo');
expect(foo.actions).toHaveProperty('bar');
});
test('throw if double action', () => {
expect( () => new Updux({
actions: {
foo: action('foo'),
},
subduxes: {
beta: {
actions: {
foo: action('foo'),
},
},
},
}) ).toThrow(/action 'foo' already defined/);
});