acknowledge the subduxes actions
This commit is contained in:
parent
7494fe553e
commit
6a6bb1636a
20
src/Updux.js
20
src/Updux.js
@ -5,7 +5,7 @@ import { Action, ActionGenerator } from './actions.js';
|
||||
export class Updux {
|
||||
#localInitial = {};
|
||||
#subduxes = {};
|
||||
#actions ;
|
||||
#actions;
|
||||
#mutations = {};
|
||||
#config = {};
|
||||
|
||||
@ -13,7 +13,11 @@ export class Updux {
|
||||
this.#config = config;
|
||||
this.#localInitial = config.initial ?? {};
|
||||
this.#subduxes = config.subduxes ?? {};
|
||||
this.#actions = config.actions ?? {};
|
||||
|
||||
this.#actions = R.mergeAll([
|
||||
config.actions ?? {},
|
||||
...Object.values(this.#subduxes).map(R.prop('actions')),
|
||||
]);
|
||||
}
|
||||
|
||||
get actions() {
|
||||
@ -31,22 +35,22 @@ export class Updux {
|
||||
}
|
||||
|
||||
get reducer() {
|
||||
return (state,action) => this.upreducer(action)(state);
|
||||
return (state, action) => this.upreducer(action)(state);
|
||||
}
|
||||
|
||||
get upreducer() {
|
||||
return (action) => state => {
|
||||
return (action) => (state) => {
|
||||
const mutation = this.#mutations[action.type];
|
||||
|
||||
if( mutation ) {
|
||||
state = mutation(action.payload,action)(state);
|
||||
if (mutation) {
|
||||
state = mutation(action.payload, action)(state);
|
||||
}
|
||||
|
||||
return state;
|
||||
};
|
||||
}
|
||||
|
||||
setMutation( action, mutation ) {
|
||||
this.#mutations[ action.type ] = mutation;
|
||||
setMutation(action, mutation) {
|
||||
this.#mutations[action.type] = mutation;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,4 @@
|
||||
export function action(
|
||||
type,
|
||||
payloadFunction,
|
||||
transformer,
|
||||
) {
|
||||
export function action(type, payloadFunction, transformer) {
|
||||
let generator = function (...payloadArg) {
|
||||
const result = { type };
|
||||
|
||||
|
@ -15,23 +15,39 @@ test('basic action', () => {
|
||||
});
|
||||
});
|
||||
|
||||
test( "Updux config accepts actions", () => {
|
||||
test('Updux config accepts actions', () => {
|
||||
const foo = new Updux({
|
||||
actions: {
|
||||
one: action('one', (x) => ({x})),
|
||||
two: action('two', x => x),
|
||||
}
|
||||
one: action('one', (x) => ({ x })),
|
||||
two: action('two', (x) => x),
|
||||
},
|
||||
});
|
||||
|
||||
expect(Object.keys(foo.actions)).toHaveLength(2);
|
||||
|
||||
expect( foo.actions.one ).toBeTypeOf('function');
|
||||
expect( foo.actions.one("potato") ).toEqual({
|
||||
expect(foo.actions.one).toBeTypeOf('function');
|
||||
expect(foo.actions.one('potato')).toEqual({
|
||||
type: 'one',
|
||||
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');
|
||||
});
|
||||
|
@ -5,7 +5,7 @@ export function buildUpreducer(
|
||||
initial,
|
||||
mutations,
|
||||
subduxes = {},
|
||||
wrapper = undefined
|
||||
wrapper = undefined,
|
||||
) {
|
||||
const subReducers =
|
||||
Object.keys(subduxes).length > 0
|
||||
@ -23,11 +23,11 @@ export function buildUpreducer(
|
||||
newState = u.updateIn(
|
||||
'*',
|
||||
subduxes['*'].upreducer(action),
|
||||
newState
|
||||
newState,
|
||||
);
|
||||
} else {
|
||||
const update = mapValues(subReducers, (upReducer) =>
|
||||
upReducer(action)
|
||||
upReducer(action),
|
||||
);
|
||||
|
||||
newState = u(update, newState);
|
||||
|
@ -9,22 +9,22 @@ import { Updux } from './Updux.js';
|
||||
test('set a mutation', () => {
|
||||
const dux = new Updux({
|
||||
initial: {
|
||||
x: "potato",
|
||||
x: 'potato',
|
||||
},
|
||||
actions: {
|
||||
foo: action('foo', (x) => ({x})),
|
||||
foo: action('foo', (x) => ({ x })),
|
||||
bar: action('bar'),
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
dux.setMutation( dux.actions.foo, (payload,action) => u({
|
||||
x: payload.x + action.type
|
||||
}) );
|
||||
|
||||
const result = dux.reducer(undefined,dux.actions.foo("hello "));
|
||||
expect( result ).toEqual({
|
||||
x: "hello foo"
|
||||
})
|
||||
|
||||
dux.setMutation(dux.actions.foo, (payload, action) =>
|
||||
u({
|
||||
x: payload.x + action.type,
|
||||
}),
|
||||
);
|
||||
|
||||
const result = dux.reducer(undefined, dux.actions.foo('hello '));
|
||||
expect(result).toEqual({
|
||||
x: 'hello foo',
|
||||
});
|
||||
});
|
||||
|
@ -3,7 +3,7 @@ import { test, expect } from 'vitest';
|
||||
import { Updux } from './Updux.js';
|
||||
|
||||
test('basic reducer', () => {
|
||||
const dux = new Updux({ initial: {a: 3} });
|
||||
const dux = new Updux({ initial: { a: 3 } });
|
||||
|
||||
expect(dux.reducer).toBeTypeOf('function');
|
||||
|
||||
@ -11,9 +11,9 @@ test('basic reducer', () => {
|
||||
});
|
||||
|
||||
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({ type: 'foo' })({a:1})).toMatchObject({ a: 1 }); // noop
|
||||
expect(dux.upreducer({ type: 'foo' })({ a: 1 })).toMatchObject({ a: 1 }); // noop
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user