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
|
yarn-error.log
|
||||||
GPUCache/
|
GPUCache/
|
||||||
updux-2.0.0.tgz
|
updux-2.0.0.tgz
|
||||||
|
pnpm-lock.yaml
|
||||||
|
@ -3,6 +3,9 @@
|
|||||||
version: '3'
|
version: '3'
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
|
test: vitest run src
|
||||||
|
test:dev: vitest src
|
||||||
|
|
||||||
lint:fix:delta:
|
lint:fix:delta:
|
||||||
vars:
|
vars:
|
||||||
FILES:
|
FILES:
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/yanick/updux#readme",
|
"homepage": "https://github.com/yanick/updux#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@reduxjs/toolkit": "^1.9.3",
|
||||||
"@vitest/browser": "^0.23.1",
|
"@vitest/browser": "^0.23.1",
|
||||||
"@vitest/ui": "^0.23.1",
|
"@vitest/ui": "^0.23.1",
|
||||||
"eslint": "^8.22.0",
|
"eslint": "^8.22.0",
|
||||||
@ -36,6 +37,7 @@
|
|||||||
"eslint-plugin-todo-plz": "^1.2.1",
|
"eslint-plugin-todo-plz": "^1.2.1",
|
||||||
"jsdoc-to-markdown": "^7.1.1",
|
"jsdoc-to-markdown": "^7.1.1",
|
||||||
"prettier": "^2.7.1",
|
"prettier": "^2.7.1",
|
||||||
|
"redux-toolkit": "^1.1.2",
|
||||||
"typescript": "^4.9.5",
|
"typescript": "^4.9.5",
|
||||||
"vitest": "0.23.1"
|
"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;
|
#local_initial: T_Local_State;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
config: Partial<{
|
config: Partial<{
|
||||||
initial: T_Local_State;
|
initial: T_LocalState;
|
||||||
}>,
|
}>,
|
||||||
) {
|
) {
|
||||||
// TODO test that we default to {}
|
// TODO check that we can't alter the initial after the fact
|
||||||
this.#local_initial = config.initial ?? ({} as T_Local_State);
|
this.#local_initial = config.initial ?? ({} as T_LocalState);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO memoize?
|
// TODO memoize?
|
||||||
get initial() {
|
get initial() {
|
||||||
return this.#local_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;
|
const expectType = <T>(value: T) => value;
|
||||||
|
|
||||||
test('initial state', () => {
|
test('initial state', () => {
|
||||||
const { initial } = new Updux({
|
const initial = {
|
||||||
initial: {
|
|
||||||
next_id: 1,
|
next_id: 1,
|
||||||
todos: [],
|
todos: [],
|
||||||
},
|
};
|
||||||
|
const dux = new Updux({
|
||||||
|
initial,
|
||||||
});
|
});
|
||||||
|
|
||||||
expectType<{
|
expectType<{
|
||||||
next_id: number;
|
next_id: number;
|
||||||
todos: unknown[];
|
todos: unknown[];
|
||||||
}>(initial);
|
}>(dux.initial);
|
||||||
|
|
||||||
expect(initial).toEqual({
|
expect(dux.initial).toEqual(initial);
|
||||||
next_id: 1,
|
|
||||||
todos: [],
|
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 */
|
/* Modules */
|
||||||
"module": "ES2020" /* Specify what module code is generated. */,
|
"module": "ES2020" /* Specify what module code is generated. */,
|
||||||
// "rootDir": "./", /* Specify the root folder within your source files. */
|
// "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. */
|
// "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. */
|
// "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. */
|
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
||||||
|
Loading…
Reference in New Issue
Block a user