vaderjs 1.9.2 → 1.9.4
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/index.ts +66 -59
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -285,77 +285,81 @@ export class Component {
|
|
|
285
285
|
return this.refs.find((r) => r.key == key).value;
|
|
286
286
|
}
|
|
287
287
|
useEffect(callback, dependencies = []) {
|
|
288
|
-
const callbackId = callback.toString();
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
288
|
+
const callbackId = callback.toString(); // Unique ID based on callback string representation
|
|
289
|
+
|
|
290
|
+
if (!this.effectCalls.some((effect) => effect.id === callbackId)) {
|
|
291
|
+
// Add the initial effect call if it doesn't exist
|
|
292
|
+
this.effectCalls.push({
|
|
293
|
+
id: callbackId,
|
|
294
|
+
count: 0,
|
|
295
|
+
lastCall: Date.now(),
|
|
296
|
+
hasRun: false, // Tracks if the effect has already run once
|
|
297
|
+
dependencies
|
|
298
|
+
});
|
|
298
299
|
}
|
|
299
|
-
|
|
300
|
-
const effectCall = this.effectCalls.find(effect => effect.id === callbackId);
|
|
301
|
-
|
|
300
|
+
|
|
301
|
+
const effectCall = this.effectCalls.find((effect) => effect.id === callbackId);
|
|
302
|
+
|
|
302
303
|
const executeCallback = () => {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
effectCall.lastCall = now;
|
|
319
|
-
|
|
320
|
-
// Execute the callback asynchronously
|
|
321
|
-
setTimeout(() => {
|
|
322
|
-
try {
|
|
323
|
-
effects.push(callbackId); // Track the effect
|
|
324
|
-
callback();
|
|
325
|
-
} catch (error) {
|
|
326
|
-
console.error(error);
|
|
304
|
+
const now = Date.now();
|
|
305
|
+
const timeSinceLastCall = now - effectCall.lastCall;
|
|
306
|
+
|
|
307
|
+
// Track call counts and handle potential over-calling issues
|
|
308
|
+
if (timeSinceLastCall < this.errorThreshold) {
|
|
309
|
+
effectCall.count += 1;
|
|
310
|
+
if (effectCall.count > this.maxIntervalCalls) {
|
|
311
|
+
throw new Error(
|
|
312
|
+
`Woah, way too many calls! Ensure you are not over-looping. Adjust maxIntervalCalls and errorThreshold as needed.`
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
} else {
|
|
316
|
+
effectCall.count = 1;
|
|
327
317
|
}
|
|
328
|
-
|
|
318
|
+
|
|
319
|
+
effectCall.lastCall = now;
|
|
320
|
+
|
|
321
|
+
setTimeout(() => {
|
|
322
|
+
try {
|
|
323
|
+
effects.push(callbackId); // Track executed effects
|
|
324
|
+
callback(); // Execute the callback
|
|
325
|
+
} catch (error) {
|
|
326
|
+
console.error(error);
|
|
327
|
+
}
|
|
328
|
+
}, 0);
|
|
329
329
|
};
|
|
330
|
-
|
|
331
|
-
//
|
|
330
|
+
|
|
331
|
+
// First time: Run the effect and mark it as run
|
|
332
|
+
if (!effectCall.hasRun && dependencies.length === 0) {
|
|
333
|
+
executeCallback();
|
|
334
|
+
effectCall.hasRun = true;
|
|
335
|
+
effectCall.dependencies = dependencies;
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// If there are no dependencies, do nothing after the first run
|
|
332
340
|
if (dependencies.length === 0) {
|
|
333
|
-
|
|
334
|
-
executeCallback();
|
|
335
|
-
this.effect.push(callbackId);
|
|
336
|
-
}
|
|
337
|
-
return; // Skip further processing for empty dependencies
|
|
341
|
+
return;
|
|
338
342
|
}
|
|
339
|
-
|
|
343
|
+
|
|
340
344
|
// Check if dependencies have changed
|
|
341
345
|
let dependenciesChanged = false;
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
346
|
+
for (let i = 0; i < dependencies.length; i++) {
|
|
347
|
+
const previousDependencies = effectCall.dependencies || [];
|
|
348
|
+
if (
|
|
349
|
+
JSON.stringify(previousDependencies[i]) !== JSON.stringify(dependencies[i])
|
|
350
|
+
) {
|
|
351
|
+
dependenciesChanged = true;
|
|
352
|
+
break;
|
|
349
353
|
}
|
|
350
|
-
}
|
|
351
354
|
}
|
|
352
|
-
|
|
353
|
-
// If dependencies changed,
|
|
355
|
+
|
|
356
|
+
// If dependencies have changed, run the effect and update dependencies
|
|
354
357
|
if (dependenciesChanged) {
|
|
355
|
-
|
|
356
|
-
|
|
358
|
+
executeCallback();
|
|
359
|
+
effectCall.dependencies = dependencies;
|
|
357
360
|
}
|
|
358
|
-
|
|
361
|
+
}
|
|
362
|
+
|
|
359
363
|
|
|
360
364
|
useState(key, defaultValue, persist = false) {
|
|
361
365
|
let value = this.state[key] || defaultValue;
|
|
@@ -363,6 +367,9 @@ export class Component {
|
|
|
363
367
|
value = sessionStorage.getItem(key) ? JSON.parse(sessionStorage.getItem(key)).value : defaultValue;
|
|
364
368
|
}
|
|
365
369
|
const setValue = (newValue) => {
|
|
370
|
+
if(typeof newValue === "function") {
|
|
371
|
+
newValue = newValue(this.state[key]);
|
|
372
|
+
}
|
|
366
373
|
this.state[key] = newValue;
|
|
367
374
|
if (persist) {
|
|
368
375
|
sessionStorage.setItem(key, JSON.stringify({ value: newValue }));
|