what-core 0.6.8 → 0.6.9

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/dist/testing.js CHANGED
@@ -115,38 +115,11 @@ function _createEffect(fn, lazy) {
115
115
  function _runEffect(e) {
116
116
  if (e.disposed) return;
117
117
  if (e._stable) {
118
- if (e._cleanup) {
119
- try {
120
- e._cleanup();
121
- } catch (err) {
122
- if (__DEV__) console.warn("[what] Error in effect cleanup:", err);
123
- }
124
- e._cleanup = null;
125
- }
126
- const prev2 = currentEffect;
127
- currentEffect = null;
128
- try {
129
- const result = e.fn();
130
- if (typeof result === "function") e._cleanup = result;
131
- } catch (err) {
132
- if (__devtools?.onError) __devtools.onError(err, { type: "effect", effect: e });
133
- if (__DEV__) console.warn("[what] Error in stable effect:", err);
134
- } finally {
135
- currentEffect = prev2;
136
- }
137
- if (__DEV__ && __devtools?.onEffectRun) __devtools.onEffectRun(e);
118
+ runStableEffect(e);
138
119
  return;
139
120
  }
140
121
  cleanup(e);
141
- if (e._cleanup) {
142
- try {
143
- e._cleanup();
144
- } catch (err) {
145
- if (__devtools?.onError) __devtools.onError(err, { type: "effect-cleanup", effect: e });
146
- if (__DEV__) console.warn("[what] Error in effect cleanup:", err);
147
- }
148
- e._cleanup = null;
149
- }
122
+ runEffectCleanup(e, "effect cleanup");
150
123
  const prev = currentEffect;
151
124
  currentEffect = e;
152
125
  try {
@@ -167,15 +140,37 @@ function _disposeEffect(e) {
167
140
  e.disposed = true;
168
141
  if (__DEV__ && __devtools) __devtools.onEffectDispose(e);
169
142
  cleanup(e);
170
- if (e._cleanup) {
171
- try {
172
- e._cleanup();
173
- } catch (err) {
174
- if (__DEV__) console.warn("[what] Error in effect cleanup on dispose:", err);
175
- }
176
- e._cleanup = null;
143
+ runEffectCleanup(e, "effect cleanup on dispose");
144
+ }
145
+ function reportEffectCleanupError(err, e, phase) {
146
+ if (__devtools?.onError) __devtools.onError(err, { type: "effect-cleanup", effect: e, phase });
147
+ if (__DEV__) console.warn(`[what] Error in ${phase}:`, err);
148
+ }
149
+ function runEffectCleanup(e, phase) {
150
+ if (!e._cleanup) return;
151
+ const cleanupFn = e._cleanup;
152
+ e._cleanup = null;
153
+ try {
154
+ cleanupFn();
155
+ } catch (err) {
156
+ reportEffectCleanupError(err, e, phase);
177
157
  }
178
158
  }
159
+ function runStableEffect(e) {
160
+ const prev = currentEffect;
161
+ currentEffect = null;
162
+ try {
163
+ runEffectCleanup(e, "stable effect cleanup");
164
+ const result = e.fn();
165
+ if (typeof result === "function") e._cleanup = result;
166
+ } catch (err) {
167
+ if (__devtools?.onError) __devtools.onError(err, { type: "effect", effect: e });
168
+ if (__DEV__) console.warn("[what] Error in stable effect:", err);
169
+ } finally {
170
+ currentEffect = prev;
171
+ }
172
+ if (__DEV__ && __devtools?.onEffectRun) __devtools.onEffectRun(e);
173
+ }
179
174
  function cleanup(e) {
180
175
  const deps = e.deps;
181
176
  for (let i = 0; i < deps.length; i++) deps[i].delete(e);
@@ -193,23 +188,7 @@ function notify(subs) {
193
188
  if (e._onNotify) {
194
189
  e._onNotify();
195
190
  } else if (batchDepth === 0 && e._stable) {
196
- const prev = currentEffect;
197
- currentEffect = null;
198
- try {
199
- const result = e.fn();
200
- if (typeof result === "function") {
201
- if (e._cleanup) try {
202
- e._cleanup();
203
- } catch (err) {
204
- }
205
- e._cleanup = result;
206
- }
207
- } catch (err) {
208
- if (__devtools?.onError) __devtools.onError(err, { type: "effect", effect: e });
209
- if (__DEV__) console.warn("[what] Error in stable effect:", err);
210
- } finally {
211
- currentEffect = prev;
212
- }
191
+ runStableEffect(e);
213
192
  } else if (!e._pending) {
214
193
  e._pending = true;
215
194
  const level = e._level;
@@ -231,23 +210,7 @@ function notify(subs) {
231
210
  if (e._onNotify) {
232
211
  e._onNotify();
233
212
  } else if (batchDepth === 0 && e._stable) {
234
- const prev = currentEffect;
235
- currentEffect = null;
236
- try {
237
- const result = e.fn();
238
- if (typeof result === "function") {
239
- if (e._cleanup) try {
240
- e._cleanup();
241
- } catch (err) {
242
- }
243
- e._cleanup = result;
244
- }
245
- } catch (err) {
246
- if (__devtools?.onError) __devtools.onError(err, { type: "effect", effect: e });
247
- if (__DEV__) console.warn("[what] Error in stable effect:", err);
248
- } finally {
249
- currentEffect = prev;
250
- }
213
+ runStableEffect(e);
251
214
  } else if (!e._pending) {
252
215
  e._pending = true;
253
216
  const level = e._level;
@@ -996,27 +959,6 @@ function applyProps(el, newProps, oldProps, isSvg) {
996
959
  }
997
960
  }
998
961
  function setProp(el, key, value, isSvg) {
999
- if (!isSafeUrlAttributeValue(key, value)) {
1000
- if (typeof console !== "undefined") {
1001
- console.warn(`[what] Blocked unsafe URL in "${key}" attribute: ${value}`);
1002
- }
1003
- el.removeAttribute(getDomAttributeName(key));
1004
- return;
1005
- }
1006
- if (typeof value === "function" && !(key.startsWith("on") && key.length > 2) && key !== "ref") {
1007
- if (!el._propEffects) el._propEffects = {};
1008
- if (el._propEffects[key]) {
1009
- try {
1010
- el._propEffects[key]();
1011
- } catch (e) {
1012
- }
1013
- }
1014
- el._propEffects[key] = effect(() => {
1015
- const resolved = value();
1016
- setProp(el, key, resolved, isSvg);
1017
- });
1018
- return;
1019
- }
1020
962
  if (key.startsWith("on") && key.length > 2) {
1021
963
  let eventName = key.slice(2);
1022
964
  let useCapture = false;
@@ -1041,6 +983,32 @@ function setProp(el, key, value, isSvg) {
1041
983
  el.addEventListener(event, wrappedHandler, eventOpts || useCapture || void 0);
1042
984
  return;
1043
985
  }
986
+ if (key === "ref") {
987
+ if (typeof value === "function") value(el);
988
+ else if (value) value.current = el;
989
+ return;
990
+ }
991
+ if (!isSafeUrlAttributeValue(key, value)) {
992
+ if (typeof console !== "undefined") {
993
+ console.warn(`[what] Blocked unsafe URL in "${key}" attribute: ${value}`);
994
+ }
995
+ el.removeAttribute(getDomAttributeName(key));
996
+ return;
997
+ }
998
+ if (typeof value === "function") {
999
+ if (!el._propEffects) el._propEffects = {};
1000
+ if (el._propEffects[key]) {
1001
+ try {
1002
+ el._propEffects[key]();
1003
+ } catch (e) {
1004
+ }
1005
+ }
1006
+ el._propEffects[key] = effect(() => {
1007
+ const resolved = value();
1008
+ setProp(el, key, resolved, isSvg);
1009
+ });
1010
+ return;
1011
+ }
1044
1012
  if (key === "className" || key === "class") {
1045
1013
  if (isSvg) {
1046
1014
  el.setAttribute("class", value || "");