@cleanweb/react 1.0.9 → 1.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,6 +10,10 @@ var ComponentMethods = /** @class */ (function () {
10
10
  exports.ComponentMethods = ComponentMethods;
11
11
  ;
12
12
  var useMethods = function (Methods, state, props) {
13
+ // @todo Switch to useRef. Vite HMR seems to sometimes reinitialize useMemo calls after a hot update,
14
+ // causing the instance to be unexpectedly recreated in the middle of the components lifecycle.
15
+ // But useRef and useState values appear to always be preserved whenever this happens.
16
+ // So those two are the only cross-render-persistence methods we can consider safe.
13
17
  var methods = (0, react_1.useMemo)(function () {
14
18
  // See useLogic implementation for a discussion of this type assertion.
15
19
  return new Methods();
@@ -12,9 +12,10 @@ declare class CleanStateBase<TState extends object> {
12
12
  }
13
13
  type TCleanStateInstance<TState extends object> = TState & CleanStateBase<TState>;
14
14
  export type TCleanState<TState extends object> = TCleanStateInstance<TState>;
15
- type Func = (...params: any[]) => any;
16
- type UseCleanState = <TState extends object, TProps extends object = object>(_initialState: ((props?: TProps) => TState) | TState, // TStateObjOrFactory,
17
- props?: typeof _initialState extends Func ? TProps : undefined) => TCleanState<TState>;
15
+ type TFunctionType = (...args: any) => object;
16
+ type TInitialState<StateParamType> = StateParamType extends TFunctionType ? ReturnType<StateParamType> : StateParamType;
17
+ type StateInitParameters<StateInitializer> = StateInitializer extends TFunctionType ? Parameters<StateInitializer> : [];
18
+ type UseCleanState = <StateInitializer extends TFunctionType | object>(_initialState: StateInitializer, ...props: StateInitParameters<StateInitializer>) => TCleanStateInstance<TInitialState<StateInitializer>>;
18
19
  export declare const useCleanState: UseCleanState;
19
20
  /**
20
21
  * Returns a value that is false before the component has been mounted,
@@ -92,8 +92,12 @@ var CleanStateBase = /** @class */ (function () {
92
92
  }());
93
93
  ;
94
94
  var CleanState = CleanStateBase;
95
- var useCleanState = function (_initialState, props) {
96
- var initialState = typeof _initialState === 'function' ? (0, react_1.useMemo)(function () { return _initialState(props); }, []) : _initialState;
95
+ var useCleanState = function (_initialState) {
96
+ var props = [];
97
+ for (var _i = 1; _i < arguments.length; _i++) {
98
+ props[_i - 1] = arguments[_i];
99
+ }
100
+ var initialState = typeof _initialState === 'function' ? (0, react_1.useMemo)(function () { return _initialState.apply(void 0, props); }, []) : _initialState;
97
101
  var cleanState = (0, react_1.useMemo)(function () { return new CleanState(initialState); }, []);
98
102
  CleanState.update.call(cleanState);
99
103
  return cleanState;
@@ -8,6 +8,6 @@ export declare class ComponentLogic<TState extends object, TProps extends object
8
8
  export interface ComponentLogicConstructor<TState extends object, TProps extends object, THooks extends object> extends Constructor<ComponentLogic<TState, TProps, THooks>> {
9
9
  getInitialState: (props?: TProps) => TState;
10
10
  }
11
- type UseLogic = <TState extends object, LogicClass extends ComponentLogicConstructor<TState, any, any>>(Methods: LogicClass, props: InstanceType<LogicClass>['props']) => InstanceType<LogicClass>;
11
+ type UseLogic = <LogicClass extends ComponentLogicConstructor<{}, object, any>>(Methods: LogicClass, props?: InstanceType<LogicClass>['props']) => InstanceType<LogicClass>;
12
12
  export declare const useLogic: UseLogic;
13
13
  export {};
@@ -12,6 +12,7 @@ exports.ComponentLogic = ComponentLogic;
12
12
  ;
13
13
  var useLogic = function (Methods, props) {
14
14
  var _a;
15
+ if (props === void 0) { props = {}; }
15
16
  var state = (0, state_1.useCleanState)(Methods.getInitialState, props);
16
17
  // There's apparently a bug? with Typescript that pegs the return type of "new Methods()" to "ComponentLogic<{}, {}, {}>",
17
18
  // completely ignoring the type specified for Methods in the function's type definition.
@@ -39,6 +39,9 @@ declare interface IVoidFunction<AsyncType extends 'async' | 'sync' | 'both' = 'b
39
39
  : Promise<void> | void
40
40
  }
41
41
 
42
+ declare type FunctionType = (...args: any) => any;
43
+
44
+
42
45
  declare interface Window {
43
46
  }
44
47
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleanweb/react",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "A suite of helpers for writing cleaner React function components.",
5
5
  "engines": {
6
6
  "node": ">=18"