createStore and initialState
This commit is contained in:
parent
7bb988aa54
commit
455002453b
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,3 +9,4 @@ pnpm-debug.log
|
||||
yarn-error.log
|
||||
GPUCache/
|
||||
updux-2.0.0.tgz
|
||||
pnpm-lock.yaml
|
||||
|
@ -3,6 +3,9 @@
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
test: vitest run src
|
||||
test:dev: vitest src
|
||||
|
||||
lint:fix:delta:
|
||||
vars:
|
||||
FILES:
|
||||
|
@ -29,6 +29,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/yanick/updux#readme",
|
||||
"devDependencies": {
|
||||
"@reduxjs/toolkit": "^1.9.3",
|
||||
"@vitest/browser": "^0.23.1",
|
||||
"@vitest/ui": "^0.23.1",
|
||||
"eslint": "^8.22.0",
|
||||
@ -36,6 +37,7 @@
|
||||
"eslint-plugin-todo-plz": "^1.2.1",
|
||||
"jsdoc-to-markdown": "^7.1.1",
|
||||
"prettier": "^2.7.1",
|
||||
"redux-toolkit": "^1.1.2",
|
||||
"typescript": "^4.9.5",
|
||||
"vitest": "0.23.1"
|
||||
}
|
||||
|
29
src/Updux.ts
29
src/Updux.ts
@ -1,17 +1,38 @@
|
||||
export default class Updux<T_Local_State = Record<any, any>> {
|
||||
import {
|
||||
createStore as reduxCreateStore,
|
||||
applyMiddleware,
|
||||
DeepPartial,
|
||||
} from 'redux';
|
||||
import { configureStore, Reducer } from '@reduxjs/toolkit';
|
||||
|
||||
export default class Updux<T_LocalState = Record<any, any>> {
|
||||
#local_initial: T_Local_State;
|
||||
|
||||
constructor(
|
||||
config: Partial<{
|
||||
initial: T_Local_State;
|
||||
initial: T_LocalState;
|
||||
}>,
|
||||
) {
|
||||
// TODO test that we default to {}
|
||||
this.#local_initial = config.initial ?? ({} as T_Local_State);
|
||||
// TODO check that we can't alter the initial after the fact
|
||||
this.#local_initial = config.initial ?? ({} as T_LocalState);
|
||||
}
|
||||
|
||||
// TODO memoize?
|
||||
get initial() {
|
||||
return this.#local_initial;
|
||||
}
|
||||
|
||||
createStore(
|
||||
options: Partial<{
|
||||
initial: T_LocalState;
|
||||
}> = {},
|
||||
) {
|
||||
const preloadedState = options.initial ?? this.initial;
|
||||
const store = configureStore({
|
||||
reducer: ((state) => state) as Reducer<T_LocalState, any>,
|
||||
preloadedState,
|
||||
});
|
||||
|
||||
return store;
|
||||
}
|
||||
}
|
||||
|
@ -3,20 +3,38 @@ import Updux from './index.js';
|
||||
const expectType = <T>(value: T) => value;
|
||||
|
||||
test('initial state', () => {
|
||||
const { initial } = new Updux({
|
||||
initial: {
|
||||
next_id: 1,
|
||||
todos: [],
|
||||
},
|
||||
const initial = {
|
||||
next_id: 1,
|
||||
todos: [],
|
||||
};
|
||||
const dux = new Updux({
|
||||
initial,
|
||||
});
|
||||
|
||||
expectType<{
|
||||
next_id: number;
|
||||
todos: unknown[];
|
||||
}>(initial);
|
||||
}>(dux.initial);
|
||||
|
||||
expect(initial).toEqual({
|
||||
next_id: 1,
|
||||
todos: [],
|
||||
expect(dux.initial).toEqual(initial);
|
||||
|
||||
const store = dux.createStore();
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
@ -27,7 +27,7 @@
|
||||
/* Modules */
|
||||
"module": "ES2020" /* Specify what module code is generated. */,
|
||||
// "rootDir": "./", /* Specify the root folder within your source files. */
|
||||
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
||||
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
|
||||
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
||||
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
||||
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
||||
|
Loading…
Reference in New Issue
Block a user