This commit is contained in:
Yanick Champoux 2021-10-17 18:41:56 -04:00
parent 7cd7e27805
commit 55812ed6d8
6 changed files with 48 additions and 27 deletions

View File

@ -66,7 +66,7 @@
"url": "https://github.com/yanick/updux/issues" "url": "https://github.com/yanick/updux/issues"
}, },
"homepage": "https://github.com/yanick/updux#readme", "homepage": "https://github.com/yanick/updux#readme",
"types": "./types/index.d.ts", "types": "./dist/index.d.ts",
"prettier": { "prettier": {
"tabWidth": 4, "tabWidth": 4,
"singleQuote": true, "singleQuote": true,

View File

@ -152,10 +152,7 @@ export class Updux {
return this.#memoInitial(this.#initial, this.#subduxes); return this.#memoInitial(this.#initial, this.#subduxes);
} }
/** get actions(): Record<string, Function> {
* @return {Record<string,Function>}
*/
get actions() {
return this.#memoActions(this.#actions, this.#subduxes); return this.#memoActions(this.#actions, this.#subduxes);
} }
@ -183,7 +180,7 @@ export class Updux {
this.#reactions = [...this.#reactions, subscription]; this.#reactions = [...this.#reactions, subscription];
} }
setAction(type, payloadFunc) { setAction(type, payloadFunc?: Function) {
const theAction = action(type, payloadFunc); const theAction = action(type, payloadFunc);
this.#actions = { ...this.#actions, [type]: theAction }; this.#actions = { ...this.#actions, [type]: theAction };
@ -325,10 +322,10 @@ export class Updux {
}; };
} }
createStore(initial) { createStore(initial?: unknown) {
const store : { const store : {
getState: Function, getState: Function & Record<string,Function>,
dispatch: Function, dispatch: Function & Record<string,Function>,
selectors: Record<string,Function>, selectors: Record<string,Function>,
actions: Record<string,Function>, actions: Record<string,Function>,
} = reduxCreateStore( } = reduxCreateStore(

View File

@ -1,17 +0,0 @@
export function action(type, payloadFunction = null) {
const generator = function (payloadArg) {
const result = { type };
if (payloadFunction) {
result.payload = payloadFunction(payloadArg);
} else {
if (payloadArg !== undefined) result.payload = payloadArg;
}
return result;
};
generator.type = type;
return generator;
}

39
src/actions.ts Normal file
View File

@ -0,0 +1,39 @@
export type Action<T extends string=string,TPayload=unknown> = {
type: T; meta?: Record<string,unknown>; } & (
{ payload?: TPayload }
)
export type ActionGenerator<TType extends string = string, TPayloadGen = undefined> = {
type: TType;
} & (TPayloadGen extends (...args:any) => any
? (...args: Parameters<TPayloadGen>) => {
type: TType;
payload: ReturnType<TPayloadGen>;
}
: (...args: any[]) => { type: TType; payload?: unknown });
// interface Action {
// <T extends string = string>(actionType: T): ActionGenerator<T>;
// <T extends string = string, F extends Function = Function>(
// actionType: T,
// payloadGen: F
// ): ActionGenerator<T, F>;
// }
export function action(type, payloadFunction = null) {
const generator = function (...payloadArg) {
const result :Action = { type };
if (payloadFunction) {
result.payload = payloadFunction(...payloadArg);
} else {
if (payloadArg[0] !== undefined) result.payload = payloadArg[0];
}
return result;
};
generator.type = type;
return generator;
}

View File

@ -5,7 +5,7 @@ export function buildSelectors(localSelectors, splatSelector = {}, subduxes ={})
if (!selectors) return {}; if (!selectors) return {};
if (slice === '*') return {}; if (slice === '*') return {};
return mapValues(selectors, (func) => (state) => func(state[slice])); return mapValues(selectors, (func: Function) => (state) => func(state[slice]));
}); });
let splat = {}; let splat = {};

View File

@ -8,6 +8,8 @@ export function buildUpreducer(initial, mutations, subduxes = {}) {
: null; : null;
return (action) => (state) => { return (action) => (state) => {
if( !action?.type ) throw new Error("upreducer called with a bad action");
let newState = state ?? initial; let newState = state ?? initial;
if (subReducers) { if (subReducers) {