@cleanweb/react 1.1.1-beta.13 → 1.1.1-beta.15

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,8 +1,12 @@
1
1
  import type { TCleanState, TStateData } from './state';
2
- export declare class ComponentMethods<TProps extends object, TState extends TStateData> {
3
- props: TProps;
4
- state: TCleanState<TState>;
2
+ export declare class ComponentMethods<TProps extends object = {}, TState extends TStateData | null = null> {
3
+ readonly props: TProps;
4
+ state: TState extends TStateData ? TCleanState<TState> : null;
5
5
  }
6
- type UseMethods = <Class extends typeof ComponentMethods<object, object>>(Methods: Class & Constructor<InstanceType<Class>>, props: InstanceType<Class>['props'], state: InstanceType<Class>['state']) => InstanceType<Class>;
7
- export declare const useMethods: UseMethods;
8
- export {};
6
+ type UseMethods = {
7
+ <Class extends typeof ComponentMethods<object, TStateData>>(Methods: Class & Constructor<InstanceType<Class>>, props: InstanceType<Class>['props'], state: InstanceType<Class>['state']): InstanceType<Class>;
8
+ <Class extends typeof ComponentMethods<object, null>>(Methods: Class & Constructor<InstanceType<Class>>, props: InstanceType<Class>['props'], state?: null): InstanceType<Class>;
9
+ <Class extends typeof ComponentMethods<HardEmptyObject, null>>(Methods: Class & Constructor<InstanceType<Class>>): InstanceType<Class>;
10
+ };
11
+ declare const useMethods: UseMethods;
12
+ export { useMethods };
@@ -60,16 +60,26 @@ var ComponentMethods = /** @class */ (function () {
60
60
  }());
61
61
  exports.ComponentMethods = ComponentMethods;
62
62
  ;
63
- var useMethods = function (Methods, props, state) {
64
- // @todo Switch to useRef. Vite HMR seems to sometimes reinitialize useMemo calls after a hot update,
65
- // causing the instance to be unexpectedly recreated in the middle of the components lifecycle.
63
+ var useMethods = function () {
64
+ var args = [];
65
+ for (var _i = 0; _i < arguments.length; _i++) {
66
+ args[_i] = arguments[_i];
67
+ }
68
+ var Methods = args[0], _a = args[1], props = _a === void 0 ? {} : _a, state = args[2];
69
+ // Vite HMR seems to sometimes reinitialize useMemo calls after a hot update,
70
+ // causing the instance to be unexpectedly recreated in the middle of the component's lifecycle.
66
71
  // But useRef and useState values appear to always be preserved whenever this happens.
67
72
  // So those two are the only cross-render-persistence methods we can consider safe.
73
+ // @todo Provide a way for users to reflect updated methods code on the existing instance after HMR.
68
74
  var methods = (0, react_1.useRef)((0, react_1.useMemo)(function () {
69
75
  return new Methods();
70
76
  }, [])).current;
71
- methods.props = props;
72
- methods.state = state;
77
+ /** A proxy variable to allow typechecking of the assignment to methods.props despite the need for "readonly" error suppression. */
78
+ var _propsProxy_;
79
+ // @ts-expect-error
80
+ methods.props = (_propsProxy_ = props);
81
+ if (state)
82
+ methods.state = state;
73
83
  return methods;
74
84
  };
75
85
  exports.useMethods = useMethods;
@@ -91,7 +101,8 @@ testing: {
91
101
  return [4 /*yield*/, import('./state.js')];
92
102
  case 1:
93
103
  useCleanState = (_a.sent()).useCleanState;
94
- self = (0, exports.useMethods)(MyMethods, {}, useCleanState({}));
104
+ self = useMethods(MyMethods, {});
105
+ self.state;
95
106
  return [2 /*return*/];
96
107
  }
97
108
  });
@@ -2,13 +2,12 @@ import type { TCleanState, ExtractCleanStateData, TStateData } from '../base/sta
2
2
  export type HardEmpty = HardEmptyObject;
3
3
  export type WeakEmpty = WeakEmptyObject;
4
4
  type o = object;
5
- export declare class ComponentLogic<TProps extends o = {}, TState extends TStateData = WeakEmpty, // WeakEmpty,
6
- THooks extends o = WeakEmpty> {
5
+ export declare class ComponentLogic<TProps extends object = {}, TState extends TStateData = WeakEmpty, THooks extends object = {}> {
7
6
  state: TCleanState<TState>;
8
- props: TProps;
9
- hooks: THooks;
7
+ readonly props: TProps;
8
+ readonly hooks: THooks;
10
9
  static getInitialState: (p?: object) => object;
11
- useHooks: THooks extends HardEmptyObject ? undefined | (() => HardEmptyObject | undefined) : () => THooks;
10
+ useHooks: THooks extends HardEmptyObject ? undefined | (() => THooks | void) : () => THooks;
12
11
  }
13
12
  type LogicClassParams = ConstructorParameters<typeof ComponentLogic>;
14
13
  export interface IComponentLogicClass<Instance extends ComponentLogic<o, o, o> = ComponentLogic, Params extends LogicClassParams = LogicClassParams> extends Constructor<Instance, Params> {
@@ -33,15 +33,22 @@ var useLogic = function () {
33
33
  for (var _i = 0; _i < arguments.length; _i++) {
34
34
  args[_i] = arguments[_i];
35
35
  }
36
- var Methods = args[0], _c = args[1], props = _c === void 0 ? {} : _c;
37
- var state = (0, state_1.useCleanState)(Methods.getInitialState, props);
38
- var methods = (0, react_1.useRef)((0, react_1.useMemo)(function () {
39
- return new Methods();
36
+ var Logic = args[0], _c = args[1], props = _c === void 0 ? {} : _c;
37
+ var state = (0, state_1.useCleanState)(Logic.getInitialState, props);
38
+ var self = (0, react_1.useRef)((0, react_1.useMemo)(function () {
39
+ return new Logic();
40
40
  }, [])).current;
41
- methods.state = state;
42
- methods.props = props;
43
- methods.hooks = (_b = (_a = methods.useHooks) === null || _a === void 0 ? void 0 : _a.call(methods)) !== null && _b !== void 0 ? _b : {};
44
- return methods;
41
+ /** A proxy variable to allow typechecking of the assignment to `self.props` despite the need for "readonly" error suppression. */
42
+ var _propsProxy_;
43
+ /** A proxy variable to allow typechecking of the assignment to `self.hooks` despite the need for "readonly" error suppression. */
44
+ var _hooksProxy_;
45
+ self.state = state;
46
+ // @ts-expect-error
47
+ self.props = (_propsProxy_ = props);
48
+ // @ts-expect-error
49
+ self.hooks = (_hooksProxy_ = (_b = (_a = self.useHooks) === null || _a === void 0 ? void 0 : _a.call(self)) !== null && _b !== void 0 ? _b : {} // @todo Improve UseLogic types to reflect that this may be undefined.
50
+ );
51
+ return self;
45
52
  };
46
53
  exports.useLogic = useLogic;
47
54
  testing: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleanweb/react",
3
- "version": "1.1.1-beta.13",
3
+ "version": "1.1.1-beta.15",
4
4
  "description": "A suite of helpers for writing cleaner React function components.",
5
5
  "engines": {
6
6
  "node": ">=18"