actions are in
This commit is contained in:
parent
90a2171cc7
commit
ea724ab73b
20
src/Updux.ts
20
src/Updux.ts
@ -5,21 +5,31 @@ import {
|
|||||||
} from 'redux';
|
} from 'redux';
|
||||||
import { configureStore, Reducer } from '@reduxjs/toolkit';
|
import { configureStore, Reducer } from '@reduxjs/toolkit';
|
||||||
|
|
||||||
export default class Updux<T_LocalState = Record<any, any>> {
|
export default class Updux<
|
||||||
#local_initial: T_Local_State;
|
T_LocalState = Record<any, any>,
|
||||||
|
T_LocalActions = {},
|
||||||
|
> {
|
||||||
|
#localInitial: T_LocalState;
|
||||||
|
#localActions: T_LocalActions;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
config: Partial<{
|
config: Partial<{
|
||||||
initial: T_LocalState;
|
initial: T_LocalState;
|
||||||
|
actions: T_LocalActions;
|
||||||
}>,
|
}>,
|
||||||
) {
|
) {
|
||||||
// TODO check that we can't alter the initial after the fact
|
// 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?
|
// TODO memoize?
|
||||||
get initial() {
|
get initial() {
|
||||||
return this.#local_initial;
|
return this.#localInitial;
|
||||||
|
}
|
||||||
|
|
||||||
|
get actions() {
|
||||||
|
return this.#localActions;
|
||||||
}
|
}
|
||||||
|
|
||||||
createStore(
|
createStore(
|
||||||
@ -27,7 +37,7 @@ export default class Updux<T_LocalState = Record<any, any>> {
|
|||||||
initial: T_LocalState;
|
initial: T_LocalState;
|
||||||
}> = {},
|
}> = {},
|
||||||
) {
|
) {
|
||||||
const preloadedState = options.initial ?? this.initial;
|
const preloadedState: any = options.initial ?? this.initial;
|
||||||
const store = configureStore({
|
const store = configureStore({
|
||||||
reducer: ((state) => state) as Reducer<T_LocalState, any>,
|
reducer: ((state) => state) as Reducer<T_LocalState, any>,
|
||||||
preloadedState,
|
preloadedState,
|
||||||
|
5
src/actions.ts
Normal file
5
src/actions.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export { createAction } from '@reduxjs/toolkit';
|
||||||
|
|
||||||
|
export const withPayload =
|
||||||
|
<P>() =>
|
||||||
|
(payload: P) => ({ payload });
|
@ -1,5 +1,5 @@
|
|||||||
import Updux from './Updux';
|
import Updux from './Updux';
|
||||||
|
|
||||||
export { createAction } from '@reduxjs/toolkit';
|
export { withPayload, createAction } from './actions';
|
||||||
|
|
||||||
export default Updux;
|
export default Updux;
|
||||||
|
@ -13,3 +13,19 @@ test('number', () => {
|
|||||||
expect(initial).toBeTypeOf('number');
|
expect(initial).toBeTypeOf('number');
|
||||||
expect(initial).toEqual(3);
|
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,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import Updux from './index.js';
|
import Updux, { createAction, withPayload } from './index.js';
|
||||||
|
|
||||||
const expectType = <T>(value: T) => value;
|
const expectType = <T>(value: T) => value;
|
||||||
|
|
||||||
@ -23,24 +23,8 @@ test('initial state', () => {
|
|||||||
expect(store.getState()).toEqual(initial);
|
expect(store.getState()).toEqual(initial);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('initial to createStore', () => {
|
test.only('actions', () => {
|
||||||
const initial = {
|
const addTodo = createAction('addTodo', withPayload<string>());
|
||||||
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');
|
|
||||||
const todoDone = createAction('todoDone');
|
const todoDone = createAction('todoDone');
|
||||||
|
|
||||||
const todosDux = new Updux({
|
const todosDux = new Updux({
|
||||||
|
Loading…
Reference in New Issue
Block a user