From 6cb7f14407ac967a5f93264e3cb9aa7bf0dc0965 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 26 Aug 2022 14:41:22 -0400 Subject: [PATCH] warn of action clashes --- src/Updux.js | 19 +++++++++++++++---- src/actions.test.js | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Updux.js b/src/Updux.js index 5524aaf..d0ad6df 100644 --- a/src/Updux.js +++ b/src/Updux.js @@ -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: + 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() { diff --git a/src/actions.test.js b/src/actions.test.js index b81d8e1..1501fd2 100644 --- a/src/actions.test.js +++ b/src/actions.test.js @@ -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/); + +});