@cleanweb/react 1.1.1-beta.12 → 1.1.1-beta.14

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> {
2
+ export declare class ComponentMethods<TProps extends object = {}, TState extends TStateData | null = null> {
3
3
  props: TProps;
4
- state: TCleanState<TState>;
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,23 @@ 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
77
  methods.props = props;
72
- methods.state = state;
78
+ if (state)
79
+ methods.state = state;
73
80
  return methods;
74
81
  };
75
82
  exports.useMethods = useMethods;
@@ -91,7 +98,8 @@ testing: {
91
98
  return [4 /*yield*/, import('./state.js')];
92
99
  case 1:
93
100
  useCleanState = (_a.sent()).useCleanState;
94
- self = (0, exports.useMethods)(MyMethods, {}, useCleanState({}));
101
+ self = useMethods(MyMethods, {});
102
+ self.state;
95
103
  return [2 /*return*/];
96
104
  }
97
105
  });
@@ -4,7 +4,7 @@ type AsyncAllowedEffectCallback = () => Awaitable<IVoidFunction>;
4
4
  type UseMountCallbacks = <TInstance extends ComponentInstance<any, any, any>>(instance: TInstance) => void;
5
5
  export declare const useMountCallbacks: UseMountCallbacks;
6
6
  export declare const noOp: () => void;
7
- export declare class ComponentInstance<TProps extends o = WeakEmptyObject, TState extends TStateData = WeakEmptyObject, THooks extends o = WeakEmptyObject> extends ComponentLogic<TProps, TState, THooks> {
7
+ export declare class ComponentInstance<TProps extends o = {}, TState extends TStateData = WeakEmptyObject, THooks extends o = WeakEmptyObject> extends ComponentLogic<TProps, TState, THooks> {
8
8
  /**
9
9
  * Runs only _before_ first render, i.e before the component instance is mounted.
10
10
  * Useful for logic that is involved in determining what to render.
@@ -54,7 +54,7 @@ type InstanceClassParams = ConstructorParameters<typeof ComponentInstance<o, o,
54
54
  export interface IComponentInstanceClass<Instance extends ComponentInstance<o, o, o> = ComponentInstance, Params extends InstanceClassParams = InstanceClassParams> extends IComponentLogicClass<Instance, Params> {
55
55
  }
56
56
  type UseInstance = {
57
- <Class extends typeof ComponentInstance<HardEmptyObject, o, o>>(Methods: Class & IComponentInstanceClass<InstanceType<Class>>, props?: HardEmptyObject): InstanceType<Class>;
57
+ <Class extends typeof ComponentInstance<HardEmptyObject, o, o>>(Methods: Class & IComponentInstanceClass<InstanceType<Class>>): InstanceType<Class>;
58
58
  <Class extends typeof ComponentInstance<o, o, o>>(Methods: Class & IComponentInstanceClass<InstanceType<Class>>, props: InstanceType<Class>['props']): InstanceType<Class>;
59
59
  };
60
60
  export declare const useInstance: UseInstance;
@@ -117,7 +117,7 @@ var useInstance = function () {
117
117
  }
118
118
  var Component = args[0], _b = args[1], props = _b === void 0 ? {} : _b;
119
119
  // useHooks.
120
- var instance = (0, logic_1.useLogic)(Component, props); // Must spread rest parameter, rather than passing a single `props` argument directly.
120
+ var instance = (0, logic_1.useLogic)(Component, props);
121
121
  /**
122
122
  * Argument of type '
123
123
  * [
@@ -154,8 +154,16 @@ testing: {
154
154
  var A = /** @class */ (function (_super) {
155
155
  __extends(A, _super);
156
156
  function A() {
157
- return _super !== null && _super.apply(this, arguments) || this;
157
+ var _this = _super !== null && _super.apply(this, arguments) || this;
158
+ // k = this.props.o
159
+ _this.a = _this.state['_initialValues_'];
160
+ return _this;
161
+ // hard empty has every key
162
+ // weak empty has no key
163
+ // weak empty is not assignable to hard empty
158
164
  }
159
165
  return A;
160
166
  }(ComponentInstance));
167
+ var p = { k: '' };
168
+ var a = (0, exports.useInstance)(A, { o: '' });
161
169
  }
@@ -2,7 +2,7 @@ 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 = WeakEmpty, TState extends TStateData = WeakEmpty, // WeakEmpty,
5
+ export declare class ComponentLogic<TProps extends o = {}, TState extends TStateData = WeakEmpty, // WeakEmpty,
6
6
  THooks extends o = WeakEmpty> {
7
7
  state: TCleanState<TState>;
8
8
  props: TProps;
@@ -15,7 +15,7 @@ export interface IComponentLogicClass<Instance extends ComponentLogic<o, o, o> =
15
15
  getInitialState: (props?: Instance['props']) => ExtractCleanStateData<Instance['state']>;
16
16
  }
17
17
  type UseLogic = {
18
- <Class extends typeof ComponentLogic<HardEmptyObject, o, o>>(Methods: Class & IComponentLogicClass<InstanceType<Class>>, props?: HardEmptyObject): InstanceType<Class>;
18
+ <Class extends typeof ComponentLogic<HardEmptyObject, o, o>>(Methods: Class & IComponentLogicClass<InstanceType<Class>>): InstanceType<Class>;
19
19
  <Class extends typeof ComponentLogic<o, o, o>>(Methods: Class & IComponentLogicClass<InstanceType<Class>>, props: InstanceType<Class>['props']): InstanceType<Class>;
20
20
  };
21
21
  declare const useLogic: UseLogic;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleanweb/react",
3
- "version": "1.1.1-beta.12",
3
+ "version": "1.1.1-beta.14",
4
4
  "description": "A suite of helpers for writing cleaner React function components.",
5
5
  "engines": {
6
6
  "node": ">=18"