vaderjs 2.0.2 → 2.0.3
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 +20 -23
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -205,11 +205,11 @@ interface SwitchProps {
|
|
|
205
205
|
}
|
|
206
206
|
|
|
207
207
|
// make children optional
|
|
208
|
-
export function Switch({children}
|
|
208
|
+
export function Switch({ children = [] }: SwitchProps) {
|
|
209
209
|
for (let child of children) {
|
|
210
210
|
if (child.props.when) {
|
|
211
211
|
return { type: "div", props: {
|
|
212
|
-
idKey:
|
|
212
|
+
idKey: crypto.randomUUID()
|
|
213
213
|
}, children: [child] };
|
|
214
214
|
}
|
|
215
215
|
}
|
|
@@ -310,9 +310,8 @@ export class Component {
|
|
|
310
310
|
useEffect(callback, dependencies = []) {
|
|
311
311
|
const callbackId = callback.toString(); // Unique ID based on callback string representation
|
|
312
312
|
|
|
313
|
-
// Ensure effect is tracked only once per callback function
|
|
314
313
|
if (!this.effectCalls.some((effect) => effect.id === callbackId)) {
|
|
315
|
-
//
|
|
314
|
+
// Add the initial effect call if it doesn't exist
|
|
316
315
|
this.effectCalls.push({
|
|
317
316
|
id: callbackId,
|
|
318
317
|
count: 0,
|
|
@@ -324,7 +323,7 @@ export class Component {
|
|
|
324
323
|
|
|
325
324
|
const effectCall = this.effectCalls.find((effect) => effect.id === callbackId);
|
|
326
325
|
|
|
327
|
-
const executeCallback =
|
|
326
|
+
const executeCallback = () => {
|
|
328
327
|
const now = Date.now();
|
|
329
328
|
const timeSinceLastCall = now - effectCall.lastCall;
|
|
330
329
|
|
|
@@ -342,21 +341,22 @@ export class Component {
|
|
|
342
341
|
|
|
343
342
|
effectCall.lastCall = now;
|
|
344
343
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
344
|
+
setTimeout(() => {
|
|
345
|
+
try {
|
|
346
|
+
effects.push(callbackId); // Track executed effects
|
|
347
|
+
callback(); // Execute the callback
|
|
348
|
+
} catch (error) {
|
|
349
|
+
console.error(error);
|
|
350
|
+
}
|
|
351
|
+
}, 0);
|
|
352
352
|
};
|
|
353
353
|
|
|
354
354
|
// First time: Run the effect and mark it as run
|
|
355
355
|
if (!effectCall.hasRun && dependencies.length === 0) {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
356
|
+
executeCallback();
|
|
357
|
+
effectCall.hasRun = true;
|
|
358
|
+
effectCall.dependencies = dependencies;
|
|
359
|
+
return;
|
|
360
360
|
}
|
|
361
361
|
|
|
362
362
|
// If there are no dependencies, do nothing after the first run
|
|
@@ -364,7 +364,7 @@ export class Component {
|
|
|
364
364
|
return;
|
|
365
365
|
}
|
|
366
366
|
|
|
367
|
-
// Check if dependencies have changed
|
|
367
|
+
// Check if dependencies have changed
|
|
368
368
|
let dependenciesChanged = false;
|
|
369
369
|
for (let i = 0; i < dependencies.length; i++) {
|
|
370
370
|
const previousDependencies = effectCall.dependencies || [];
|
|
@@ -378,14 +378,13 @@ export class Component {
|
|
|
378
378
|
|
|
379
379
|
// If dependencies have changed, run the effect and update dependencies
|
|
380
380
|
if (dependenciesChanged) {
|
|
381
|
-
executeCallback();
|
|
381
|
+
executeCallback();
|
|
382
382
|
effectCall.dependencies = dependencies;
|
|
383
383
|
}
|
|
384
384
|
}
|
|
385
385
|
|
|
386
|
-
|
|
387
386
|
|
|
388
|
-
|
|
387
|
+
useState(key, defaultValue, persist = false) {
|
|
389
388
|
let value = this.state[key] || defaultValue;
|
|
390
389
|
if(value === "true" || value === "false") {
|
|
391
390
|
value = JSON.parse(value);
|
|
@@ -404,9 +403,7 @@ export class Component {
|
|
|
404
403
|
sessionStorage.setItem(key, JSON.stringify({ value: newValue }));
|
|
405
404
|
}
|
|
406
405
|
this.forceUpdate(this.key);
|
|
407
|
-
};
|
|
408
|
-
value = typeof value === "boolean" ? value === "true" : value;
|
|
409
|
-
|
|
406
|
+
};
|
|
410
407
|
return [value, setValue];
|
|
411
408
|
}
|
|
412
409
|
useFetch(url, options) {
|