@cleanweb/react 1.1.1-beta.13 → 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.
- package/build/base/methods.d.ts +9 -5
- package/build/base/methods.js +13 -5
- package/package.json +1 -1
package/build/base/methods.d.ts
CHANGED
@@ -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 =
|
7
|
-
|
8
|
-
|
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 };
|
package/build/base/methods.js
CHANGED
@@ -60,16 +60,23 @@ var ComponentMethods = /** @class */ (function () {
|
|
60
60
|
}());
|
61
61
|
exports.ComponentMethods = ComponentMethods;
|
62
62
|
;
|
63
|
-
var useMethods = function (
|
64
|
-
|
65
|
-
|
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
|
-
|
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 =
|
101
|
+
self = useMethods(MyMethods, {});
|
102
|
+
self.state;
|
95
103
|
return [2 /*return*/];
|
96
104
|
}
|
97
105
|
});
|