acknowledge the subduxes actions

This commit is contained in:
Yanick Champoux 2022-08-25 20:06:52 -04:00
parent 7494fe553e
commit 6a6bb1636a
6 changed files with 57 additions and 41 deletions

View File

@ -5,7 +5,7 @@ import { Action, ActionGenerator } from './actions.js';
export class Updux { export class Updux {
#localInitial = {}; #localInitial = {};
#subduxes = {}; #subduxes = {};
#actions ; #actions;
#mutations = {}; #mutations = {};
#config = {}; #config = {};
@ -13,7 +13,11 @@ export class Updux {
this.#config = config; this.#config = config;
this.#localInitial = config.initial ?? {}; this.#localInitial = config.initial ?? {};
this.#subduxes = config.subduxes ?? {}; this.#subduxes = config.subduxes ?? {};
this.#actions = config.actions ?? {};
this.#actions = R.mergeAll([
config.actions ?? {},
...Object.values(this.#subduxes).map(R.prop('actions')),
]);
} }
get actions() { get actions() {
@ -31,22 +35,22 @@ export class Updux {
} }
get reducer() { get reducer() {
return (state,action) => this.upreducer(action)(state); return (state, action) => this.upreducer(action)(state);
} }
get upreducer() { get upreducer() {
return (action) => state => { return (action) => (state) => {
const mutation = this.#mutations[action.type]; const mutation = this.#mutations[action.type];
if( mutation ) { if (mutation) {
state = mutation(action.payload,action)(state); state = mutation(action.payload, action)(state);
} }
return state; return state;
}; };
} }
setMutation( action, mutation ) { setMutation(action, mutation) {
this.#mutations[ action.type ] = mutation; this.#mutations[action.type] = mutation;
} }
} }

View File

@ -1,8 +1,4 @@
export function action( export function action(type, payloadFunction, transformer) {
type,
payloadFunction,
transformer,
) {
let generator = function (...payloadArg) { let generator = function (...payloadArg) {
const result = { type }; const result = { type };

View File

@ -15,23 +15,39 @@ test('basic action', () => {
}); });
}); });
test( "Updux config accepts actions", () => { test('Updux config accepts actions', () => {
const foo = new Updux({ const foo = new Updux({
actions: { actions: {
one: action('one', (x) => ({x})), one: action('one', (x) => ({ x })),
two: action('two', x => x), two: action('two', (x) => x),
} },
}); });
expect(Object.keys(foo.actions)).toHaveLength(2); expect(Object.keys(foo.actions)).toHaveLength(2);
expect( foo.actions.one ).toBeTypeOf('function'); expect(foo.actions.one).toBeTypeOf('function');
expect( foo.actions.one("potato") ).toEqual({ expect(foo.actions.one('potato')).toEqual({
type: 'one', type: 'one',
payload: { payload: {
x: 'potato' x: 'potato',
} },
});
});
test('subduxes actions', () => {
const foo = new Updux({
actions: {
foo: null,
},
subduxes: {
beta: {
actions: {
bar: null,
},
},
},
}); });
} ) expect(foo.actions).toHaveProperty('foo');
expect(foo.actions).toHaveProperty('bar');
});

View File

@ -5,7 +5,7 @@ export function buildUpreducer(
initial, initial,
mutations, mutations,
subduxes = {}, subduxes = {},
wrapper = undefined wrapper = undefined,
) { ) {
const subReducers = const subReducers =
Object.keys(subduxes).length > 0 Object.keys(subduxes).length > 0
@ -23,11 +23,11 @@ export function buildUpreducer(
newState = u.updateIn( newState = u.updateIn(
'*', '*',
subduxes['*'].upreducer(action), subduxes['*'].upreducer(action),
newState newState,
); );
} else { } else {
const update = mapValues(subReducers, (upReducer) => const update = mapValues(subReducers, (upReducer) =>
upReducer(action) upReducer(action),
); );
newState = u(update, newState); newState = u(update, newState);

View File

@ -9,22 +9,22 @@ import { Updux } from './Updux.js';
test('set a mutation', () => { test('set a mutation', () => {
const dux = new Updux({ const dux = new Updux({
initial: { initial: {
x: "potato", x: 'potato',
}, },
actions: { actions: {
foo: action('foo', (x) => ({x})), foo: action('foo', (x) => ({ x })),
bar: action('bar'), bar: action('bar'),
} },
}); });
dux.setMutation( dux.actions.foo, (payload,action) => u({ dux.setMutation(dux.actions.foo, (payload, action) =>
x: payload.x + action.type u({
}) ); x: payload.x + action.type,
}),
const result = dux.reducer(undefined,dux.actions.foo("hello ")); );
expect( result ).toEqual({
x: "hello foo"
})
const result = dux.reducer(undefined, dux.actions.foo('hello '));
expect(result).toEqual({
x: 'hello foo',
});
}); });

View File

@ -3,7 +3,7 @@ import { test, expect } from 'vitest';
import { Updux } from './Updux.js'; import { Updux } from './Updux.js';
test('basic reducer', () => { test('basic reducer', () => {
const dux = new Updux({ initial: {a: 3} }); const dux = new Updux({ initial: { a: 3 } });
expect(dux.reducer).toBeTypeOf('function'); expect(dux.reducer).toBeTypeOf('function');
@ -11,9 +11,9 @@ test('basic reducer', () => {
}); });
test('basic upreducer', () => { test('basic upreducer', () => {
const dux = new Updux({ initial: {a: 3} }); const dux = new Updux({ initial: { a: 3 } });
expect(dux.upreducer).toBeTypeOf('function'); expect(dux.upreducer).toBeTypeOf('function');
expect(dux.upreducer({ type: 'foo' })({a:1})).toMatchObject({ a: 1 }); // noop expect(dux.upreducer({ type: 'foo' })({ a: 1 })).toMatchObject({ a: 1 }); // noop
}); });