From 6cb7f14407ac967a5f93264e3cb9aa7bf0dc0965 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 26 Aug 2022 14:41:22 -0400 Subject: [PATCH 1/3] 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/); + +}); From 3bb9221c27263d7f7b99e181be7f7211a2166505 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 26 Aug 2022 15:29:24 -0400 Subject: [PATCH 2/3] action shorcuts --- src/Updux.js | 10 ++++++++-- src/actions.test.js | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Updux.js b/src/Updux.js index d0ad6df..c239fe6 100644 --- a/src/Updux.js +++ b/src/Updux.js @@ -1,6 +1,10 @@ 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 { #localInitial = {}; @@ -14,7 +18,9 @@ export class Updux { this.#localInitial = config.initial ?? {}; 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) ) } diff --git a/src/actions.test.js b/src/actions.test.js index 1501fd2..c722040 100644 --- a/src/actions.test.js +++ b/src/actions.test.js @@ -67,3 +67,15 @@ test('throw if double action', () => { }) ).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'} }); +}); From 43702fe0963110889a207170b24978b57625dd5d Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 26 Aug 2022 15:30:03 -0400 Subject: [PATCH 3/3] prettier --- src/Updux.js | 18 ++++++++++-------- src/actions.test.js | 35 ++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/Updux.js b/src/Updux.js index c239fe6..0ae1d4b 100644 --- a/src/Updux.js +++ b/src/Updux.js @@ -3,7 +3,7 @@ import R from 'remeda'; import { action } from './actions.js'; function isActionGen(action) { - return typeof action === "function" && action.type; + return typeof action === 'function' && action.type; } export class Updux { @@ -18,18 +18,20 @@ export class Updux { this.#localInitial = config.initial ?? {}; this.#subduxes = config.subduxes ?? {}; - this.#actions = R.mapValues( config.actions ?? {}, (arg,name) => - isActionGen(arg) ? arg : action(name,arg) + 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) ) } #addSubduxActions(_slice, subdux) { - if( ! subdux.actions ) return; + 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; + Object.entries(subdux.actions).forEach(([action, gen]) => { + if (this.#actions[action]) { + if (this.#actions[action] === gen) return; throw new Error(`action '${action}' already defined`); } diff --git a/src/actions.test.js b/src/actions.test.js index c722040..eb6e22f 100644 --- a/src/actions.test.js +++ b/src/actions.test.js @@ -53,29 +53,34 @@ test('subduxes actions', () => { }); test('throw if double action', () => { - expect( () => new Updux({ - actions: { - foo: action('foo'), - }, - subduxes: { - beta: { + expect( + () => + new Updux({ actions: { foo: action('foo'), }, - }, - }, - }) ).toThrow(/action 'foo' already defined/); - + subduxes: { + beta: { + actions: { + foo: action('foo'), + }, + }, + }, + }), + ).toThrow(/action 'foo' already defined/); }); test('action definition shortcut', () => { const foo = new Updux({ actions: { foo: null, - bar: x => ({x}), - },} - ); + bar: (x) => ({ x }), + }, + }); - expect( foo.actions.foo("hello") ).toEqual({ type: 'foo', payload: 'hello' }); - expect( foo.actions.bar("hello") ).toEqual({ type: 'bar', payload: {x:'hello'} }); + expect(foo.actions.foo('hello')).toEqual({ type: 'foo', payload: 'hello' }); + expect(foo.actions.bar('hello')).toEqual({ + type: 'bar', + payload: { x: 'hello' }, + }); });