@cleanweb/react 1.0.8 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
3
  exports.useMethods = exports.ComponentMethods = void 0;
15
4
  var react_1 = require("react");
@@ -21,13 +10,16 @@ var ComponentMethods = /** @class */ (function () {
21
10
  exports.ComponentMethods = ComponentMethods;
22
11
  ;
23
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.
24
17
  var methods = (0, react_1.useMemo)(function () {
25
18
  // See useLogic implementation for a discussion of this type assertion.
26
19
  return new Methods();
27
20
  }, []);
28
21
  methods.state = state;
29
22
  methods.props = props;
30
- // Return a gate object to "passthrough" all methods but filter out properties that should be private.
31
- return __assign(__assign({}, methods), { props: undefined, state: undefined });
23
+ return methods;
32
24
  };
33
25
  exports.useMethods = useMethods;
@@ -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 StateInitFunction = (props?: object) => object;
16
+ type TInitialState<StateParamType> = StateParamType extends StateInitFunction ? ReturnType<StateParamType> : StateParamType;
17
+ type StateInitParameters<StateInitializer> = StateInitializer extends StateInitFunction ? Parameters<StateInitializer> : [];
18
+ type UseCleanState = <StateInitializer extends StateInitFunction | object>(_initialState: StateInitializer, ...props: StateInitParameters<StateInitializer>) => TCleanState<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,
@@ -93,6 +93,7 @@ var CleanStateBase = /** @class */ (function () {
93
93
  ;
94
94
  var CleanState = CleanStateBase;
95
95
  var useCleanState = function (_initialState, props) {
96
+ if (props === void 0) { props = {}; }
96
97
  var initialState = typeof _initialState === 'function' ? (0, react_1.useMemo)(function () { return _initialState(props); }, []) : _initialState;
97
98
  var cleanState = (0, react_1.useMemo)(function () { return new CleanState(initialState); }, []);
98
99
  CleanState.update.call(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 {};
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
3
  exports.useLogic = exports.ComponentLogic = void 0;
15
4
  var react_1 = require("react");
@@ -23,6 +12,7 @@ exports.ComponentLogic = ComponentLogic;
23
12
  ;
24
13
  var useLogic = function (Methods, props) {
25
14
  var _a;
15
+ if (props === void 0) { props = {}; }
26
16
  var state = (0, state_1.useCleanState)(Methods.getInitialState, props);
27
17
  // There's apparently a bug? with Typescript that pegs the return type of "new Methods()" to "ComponentLogic<{}, {}, {}>",
28
18
  // completely ignoring the type specified for Methods in the function's type definition.
@@ -36,7 +26,6 @@ var useLogic = function (Methods, props) {
36
26
  methods.state = state;
37
27
  methods.props = props;
38
28
  methods.hooks = ((_a = methods.useHooks) === null || _a === void 0 ? void 0 : _a.call(methods)) || {};
39
- // Return a gate object to "passthrough" all methods but filter out properties that should be private.
40
- return __assign(__assign({}, methods), { useHooks: undefined });
29
+ return methods;
41
30
  };
42
31
  exports.useLogic = useLogic;
@@ -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.8",
3
+ "version": "1.0.10",
4
4
  "description": "A suite of helpers for writing cleaner React function components.",
5
5
  "engines": {
6
6
  "node": ">=18"