@cleanweb/react 2.1.5 → 2.1.6

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.
@@ -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], _a = args[1], props = _a === void 0 ? {} : _a, state = args[2];
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
- if (process.env.NODE_ENV === 'development') {
99
- var rerender_1 = (0, helpers_1.useRerender)();
100
- (0, react_1.useEffect)(function () {
101
- var _a;
102
- if (instanceRef.current === latestInstance)
103
- return;
104
- console.log([
105
- 'HMR-updated component class detected.',
106
- 'Creating a new instance with the updated class.',
107
- 'All stateful values will be copied over.\n\n',
108
- 'Note that this mechanism only works in the `development` environment during HMR.',
109
- 'In production, the class argument will be ignored after the first render.\n\n',
110
- 'If this wasn\'t an HMR update, you should refactor your code to make sure',
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
- var methods = instanceRef.current;
130
- /**
131
- * A proxy variable to allow typechecking of the assignment
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, _b;
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], _c = args[1], props = _c === void 0 ? {} : _c;
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
- if (process.env.NODE_ENV === 'development') {
112
- if (instanceRef.current !== latestInstance) {
113
- console.log([
114
- 'HMR-updated component class detected.',
115
- 'Creating a new instance with the updated class.',
116
- 'All stateful values will be copied over.\n\n',
117
- 'Note that this mechanism only works in the `development` environment during HMR.',
118
- 'In production, the class argument will be ignored after the first render.\n\n',
119
- 'If this wasn\'t an HMR update, you should refactor your code to make sure',
120
- 'all clean-react hooks receive the same class argument on every render.'
121
- ].join(' '));
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
- (_a = latestInstance._onHmrUpdate) === null || _a === void 0 ? void 0 : _a.call(latestInstance, oldInstance_1);
129
- instanceRef.current = latestInstance;
130
- Reflect.ownKeys(oldInstance_1).forEach(function (_key) {
131
- var key = _key;
132
- delete oldInstance_1[key];
133
- });
134
- Object.setPrototypeOf(oldInstance_1, latestInstance);
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
- var self = instanceRef.current;
138
- /**
139
- * A proxy variable to allow typechecking of the assignment
140
- * to a readonly property,
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleanweb/react",
3
- "version": "2.1.5",
3
+ "version": "2.1.6",
4
4
  "description": "A suite of helpers for writing cleaner React function components.",
5
5
  "engines": {
6
6
  "node": ">=18"