@cleanweb/react 2.1.5 → 2.1.7-beta.0
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.js +26 -47
- package/build/classy/logic/index.js +31 -45
- package/build/helpers/index.d.ts +3 -0
- package/package.json +1 -1
package/build/base/methods.js
CHANGED
@@ -4,8 +4,6 @@
|
|
4
4
|
*/
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
exports.useMethods = exports.ComponentMethods = void 0;
|
7
|
-
// JSDoc references
|
8
|
-
var helpers_1 = require("../helpers");
|
9
7
|
// Values
|
10
8
|
var react_1 = require("react");
|
11
9
|
/**
|
@@ -81,11 +79,12 @@ exports.ComponentMethods = ComponentMethods;
|
|
81
79
|
* `state` should be an instance of `CleanState` created with {@link useCleanState}.
|
82
80
|
*/
|
83
81
|
var useMethods = function () {
|
82
|
+
var _a;
|
84
83
|
var args = [];
|
85
84
|
for (var _i = 0; _i < arguments.length; _i++) {
|
86
85
|
args[_i] = arguments[_i];
|
87
86
|
}
|
88
|
-
var Methods = args[0],
|
87
|
+
var Methods = args[0], _b = args[1], props = _b === void 0 ? {} : _b, state = args[2];
|
89
88
|
// Vite HMR seems to sometimes reinitialize useMemo calls after a hot update,
|
90
89
|
// causing the instance to be unexpectedly recreated in the middle of the component's lifecycle.
|
91
90
|
// But useRef and useState values appear to always be preserved whenever this happens.
|
@@ -95,52 +94,32 @@ var useMethods = function () {
|
|
95
94
|
// latestInstance is only extracted into a separate variable for use in dev mode during HMR.
|
96
95
|
var latestInstance = (0, react_1.useMemo)(function () { return new Methods(); }, [Methods]);
|
97
96
|
var instanceRef = (0, react_1.useRef)(latestInstance);
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
'all clean-react hooks receive the same class argument on every render.'
|
112
|
-
].join(' '));
|
113
|
-
var oldInstance = instanceRef.current;
|
114
|
-
latestInstance._hmrPreserveKeys.forEach(function (_key) {
|
115
|
-
var key = _key;
|
116
|
-
// @ts-expect-error We're assigning to readonly properties. Also, Typescript doesn't know that the type of the left and right side will always match, due to the dynamic access.
|
117
|
-
latestInstance[key] = oldInstance[key];
|
118
|
-
});
|
119
|
-
(_a = latestInstance._onHmrUpdate) === null || _a === void 0 ? void 0 : _a.call(latestInstance, oldInstance);
|
120
|
-
Reflect.ownKeys(oldInstance).forEach(function (_key) {
|
121
|
-
var key = _key;
|
122
|
-
delete oldInstance[key];
|
123
|
-
});
|
124
|
-
Object.setPrototypeOf(oldInstance, latestInstance);
|
125
|
-
instanceRef.current = latestInstance;
|
126
|
-
rerender_1();
|
97
|
+
var refreshState = function () {
|
98
|
+
// @ts-expect-error
|
99
|
+
instanceRef.current.props = props;
|
100
|
+
// @ts-expect-error
|
101
|
+
if (state)
|
102
|
+
instanceRef.current.state = state;
|
103
|
+
};
|
104
|
+
if (process.env.NODE_ENV === 'development' && instanceRef.current !== latestInstance) {
|
105
|
+
var oldInstance_1 = instanceRef.current;
|
106
|
+
latestInstance._hmrPreserveKeys.forEach(function (_key) {
|
107
|
+
var key = _key;
|
108
|
+
// @ts-expect-error We're assigning to readonly properties. Also, Typescript doesn't know that the type of the left and right side will always match, due to the dynamic access.
|
109
|
+
latestInstance[key] = oldInstance_1[key];
|
127
110
|
});
|
111
|
+
Reflect.ownKeys(oldInstance_1).forEach(function (_key) {
|
112
|
+
var key = _key;
|
113
|
+
delete oldInstance_1[key];
|
114
|
+
});
|
115
|
+
Object.setPrototypeOf(oldInstance_1, latestInstance);
|
116
|
+
instanceRef.current = latestInstance;
|
117
|
+
refreshState();
|
118
|
+
(_a = latestInstance._onHmrUpdate) === null || _a === void 0 ? void 0 : _a.call(latestInstance, oldInstance_1);
|
128
119
|
}
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
* to a readonly property,
|
133
|
-
* despite the need for "readonly" error suppression.
|
134
|
-
*/
|
135
|
-
var _propsProxy;
|
136
|
-
/** @see {@link _propsProxy} */
|
137
|
-
var _stateProxy;
|
138
|
-
// @ts-expect-error
|
139
|
-
methods.props = (_propsProxy = props);
|
140
|
-
// @ts-expect-error
|
141
|
-
if (state)
|
142
|
-
methods.state = (_stateProxy = state);
|
143
|
-
return methods;
|
120
|
+
else
|
121
|
+
refreshState();
|
122
|
+
return instanceRef.current;
|
144
123
|
};
|
145
124
|
exports.useMethods = useMethods;
|
146
125
|
/** /testing: {
|
@@ -97,61 +97,47 @@ exports.ComponentLogic = ComponentLogic;
|
|
97
97
|
* @see https://cleanjsweb.github.io/neat-react/functions/API.useLogic.html
|
98
98
|
*/
|
99
99
|
var useLogic = function () {
|
100
|
-
var _a
|
100
|
+
var _a;
|
101
101
|
var args = [];
|
102
102
|
for (var _i = 0; _i < arguments.length; _i++) {
|
103
103
|
args[_i] = arguments[_i];
|
104
104
|
}
|
105
|
-
var Logic = args[0],
|
105
|
+
var Logic = args[0], _b = args[1], props = _b === void 0 ? {} : _b;
|
106
106
|
// In production, we only use the latestInstance the first time, and it's ignored every other time.
|
107
107
|
// This means changing the class at runtime will have no effect in production.
|
108
108
|
// latestInstance is only extracted into a separate variable for use in dev mode during HMR.
|
109
109
|
var latestInstance = (0, react_1.useMemo)(function () { return new Logic(); }, [Logic]);
|
110
|
+
// const latestInstance = useMemo(() => new Logic(), []);
|
110
111
|
var instanceRef = (0, react_1.useRef)(latestInstance);
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
}
|
112
|
+
var refreshState = function () {
|
113
|
+
var _a;
|
114
|
+
// @ts-expect-error
|
115
|
+
instanceRef.current.props = props;
|
116
|
+
// @ts-expect-error
|
117
|
+
instanceRef.current.state = (0, state_1.useCleanState)(instanceRef.current.getInitialState, props);
|
118
|
+
// @ts-expect-error
|
119
|
+
instanceRef.current.hooks = (_a = instanceRef.current.useHooks()) !== null && _a !== void 0 ? _a : {};
|
120
|
+
};
|
121
|
+
if (process.env.NODE_ENV === 'development' && instanceRef.current !== latestInstance) {
|
122
|
+
var oldInstance_1 = instanceRef.current;
|
123
|
+
latestInstance._hmrPreserveKeys.forEach(function (_key) {
|
124
|
+
var key = _key;
|
125
|
+
// @ts-expect-error We're assigning to readonly properties. Also, Typescript doesn't know that the type of the left and right side will always match, due to the dynamic access.
|
126
|
+
latestInstance[key] = oldInstance_1[key];
|
127
|
+
});
|
128
|
+
Reflect.ownKeys(oldInstance_1).forEach(function (_key) {
|
129
|
+
var key = _key;
|
130
|
+
delete oldInstance_1[key];
|
131
|
+
});
|
132
|
+
Object.setPrototypeOf(oldInstance_1, latestInstance);
|
133
|
+
instanceRef.current = latestInstance;
|
134
|
+
refreshState();
|
135
|
+
(_a = latestInstance._onHmrUpdate) === null || _a === void 0 ? void 0 : _a.call(latestInstance, oldInstance_1);
|
136
136
|
}
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
* despite the need for "readonly" error suppression.
|
142
|
-
*/
|
143
|
-
var _propsProxy;
|
144
|
-
/** @see {@link _propsProxy} */
|
145
|
-
var _stateProxy;
|
146
|
-
/** @see {@link _propsProxy} */
|
147
|
-
var _hooksProxy;
|
148
|
-
// @ts-expect-error
|
149
|
-
self.props = (_propsProxy = props);
|
150
|
-
// @ts-expect-error
|
151
|
-
self.state = (_stateProxy = (0, state_1.useCleanState)(self.getInitialState, props));
|
152
|
-
// @ts-expect-error
|
153
|
-
self.hooks = (_hooksProxy = (_b = self.useHooks()) !== null && _b !== void 0 ? _b : {});
|
154
|
-
return self;
|
137
|
+
else
|
138
|
+
refreshState();
|
139
|
+
return instanceRef.current;
|
140
|
+
;
|
155
141
|
};
|
156
142
|
exports.useLogic = useLogic;
|
157
143
|
/** /
|
package/build/helpers/index.d.ts
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
* <!-- @ mergeModuleWith API -->
|
3
3
|
* @module Helpers
|
4
4
|
*/
|
5
|
+
import type { ComponentLogic } from '../classy';
|
5
6
|
export * from './mount-state';
|
6
7
|
export * from './rerender';
|
7
8
|
export * from './use-component';
|
@@ -11,3 +12,5 @@ export * from './type-guards';
|
|
11
12
|
* It returns (void) without performing any operations.
|
12
13
|
*/
|
13
14
|
export declare const noOp: () => void;
|
15
|
+
export type StateFragment<TComponent extends ComponentLogic<any>> = Partial<ReturnType<TComponent['getInitialState']>>;
|
16
|
+
export type { StateFragment as SF };
|