actions are in

This commit is contained in:
Yanick Champoux 2023-03-02 13:19:31 -05:00
parent 90a2171cc7
commit ea724ab73b
5 changed files with 40 additions and 25 deletions

View File

@ -5,21 +5,31 @@ import {
} from 'redux';
import { configureStore, Reducer } from '@reduxjs/toolkit';
export default class Updux<T_LocalState = Record<any, any>> {
#local_initial: T_Local_State;
export default class Updux<
T_LocalState = Record<any, any>,
T_LocalActions = {},
> {
#localInitial: T_LocalState;
#localActions: T_LocalActions;
constructor(
config: Partial<{
initial: T_LocalState;
actions: T_LocalActions;
}>,
) {
// TODO check that we can't alter the initial after the fact
this.#local_initial = config.initial ?? ({} as T_LocalState);
this.#localInitial = config.initial ?? ({} as T_LocalState);
this.#localActions = config.actions ?? ({} as T_LocalActions);
}
// TODO memoize?
get initial() {
return this.#local_initial;
return this.#localInitial;
}
get actions() {
return this.#localActions;
}
createStore(
@ -27,7 +37,7 @@ export default class Updux<T_LocalState = Record<any, any>> {
initial: T_LocalState;
}> = {},
) {
const preloadedState = options.initial ?? this.initial;
const preloadedState: any = options.initial ?? this.initial;
const store = configureStore({
reducer: ((state) => state) as Reducer<T_LocalState, any>,
preloadedState,

5
src/actions.ts Normal file
View File

@ -0,0 +1,5 @@
export { createAction } from '@reduxjs/toolkit';
export const withPayload =
<P>() =>
(payload: P) => ({ payload });

View File

@ -1,5 +1,5 @@
import Updux from './Updux';
export { createAction } from '@reduxjs/toolkit';
export { withPayload, createAction } from './actions';
export default Updux;

View File

@ -13,3 +13,19 @@ test('number', () => {
expect(initial).toBeTypeOf('number');
expect(initial).toEqual(3);
});
test('initial to createStore', () => {
const initial = {
a: 1,
b: 2,
};
const dux = new Updux({
initial,
});
expect(dux.createStore({ initial: { a: 3, b: 4 } }).getState()).toEqual({
a: 3,
b: 4,
});
});

View File

@ -1,4 +1,4 @@
import Updux from './index.js';
import Updux, { createAction, withPayload } from './index.js';
const expectType = <T>(value: T) => value;
@ -23,24 +23,8 @@ test('initial state', () => {
expect(store.getState()).toEqual(initial);
});
test('initial to createStore', () => {
const initial = {
a: 1,
b: 2,
};
const dux = new Updux({
initial,
});
expect(dux.createStore({ initial: { a: 3, b: 4 } }).getState()).toEqual({
a: 3,
b: 4,
});
});
test.todo('actions', () => {
const addTodo = createAction('addTodo');
test.only('actions', () => {
const addTodo = createAction('addTodo', withPayload<string>());
const todoDone = createAction('todoDone');
const todosDux = new Updux({