vaderjs 2.0.1 → 2.0.2

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.
Files changed (2) hide show
  1. package/index.ts +27 -20
  2. 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 = [] }: SwitchProps) {
208
+ export function Switch({children}, key) {
209
209
  for (let child of children) {
210
210
  if (child.props.when) {
211
211
  return { type: "div", props: {
212
- idKey: crypto.randomUUID()
212
+ idKey: key || crypto.randomUUID()
213
213
  }, children: [child] };
214
214
  }
215
215
  }
@@ -310,8 +310,9 @@ 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
313
314
  if (!this.effectCalls.some((effect) => effect.id === callbackId)) {
314
- // Add the initial effect call if it doesn't exist
315
+ // Initialize the effect call tracking
315
316
  this.effectCalls.push({
316
317
  id: callbackId,
317
318
  count: 0,
@@ -323,7 +324,7 @@ export class Component {
323
324
 
324
325
  const effectCall = this.effectCalls.find((effect) => effect.id === callbackId);
325
326
 
326
- const executeCallback = () => {
327
+ const executeCallback = async () => {
327
328
  const now = Date.now();
328
329
  const timeSinceLastCall = now - effectCall.lastCall;
329
330
 
@@ -341,22 +342,21 @@ export class Component {
341
342
 
342
343
  effectCall.lastCall = now;
343
344
 
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);
345
+ try {
346
+ // Wait for async callback to finish before continuing
347
+ await callback();
348
+ effects.push(callbackId); // Track executed effects after callback completion
349
+ } catch (error) {
350
+ console.error("Effect callback failed:", error);
351
+ }
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
- executeCallback();
357
- effectCall.hasRun = true;
358
- effectCall.dependencies = dependencies;
359
- return;
356
+ executeCallback(); // Run async callback
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 by deep comparison (improve this if necessary)
368
368
  let dependenciesChanged = false;
369
369
  for (let i = 0; i < dependencies.length; i++) {
370
370
  const previousDependencies = effectCall.dependencies || [];
@@ -378,14 +378,20 @@ 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(); // Run async callback
382
382
  effectCall.dependencies = dependencies;
383
383
  }
384
384
  }
385
385
 
386
+
386
387
 
387
388
  useState(key, defaultValue, persist = false) {
388
389
  let value = this.state[key] || defaultValue;
390
+ if(value === "true" || value === "false") {
391
+ value = JSON.parse(value);
392
+ }
393
+ // if value is boolean store as string
394
+
389
395
  if (persist) {
390
396
  value = sessionStorage.getItem(key) ? JSON.parse(sessionStorage.getItem(key)).value : defaultValue;
391
397
  }
@@ -393,13 +399,14 @@ export class Component {
393
399
  if(typeof newValue === "function") {
394
400
  newValue = newValue(this.state[key]);
395
401
  }
396
- this.state[key] = newValue;
402
+ this.state[key] = typeof newValue === "boolean" ? newValue.toString() : newValue;
397
403
  if (persist) {
398
404
  sessionStorage.setItem(key, JSON.stringify({ value: newValue }));
399
405
  }
400
406
  this.forceUpdate(this.key);
401
407
  };
402
- value = this.state[key] || defaultValue;
408
+ value = typeof value === "boolean" ? value === "true" : value;
409
+
403
410
  return [value, setValue];
404
411
  }
405
412
  useFetch(url, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vaderjs",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "A simple and powerful JavaScript library for building modern web applications.",
5
5
  "bin": {
6
6
  "vaderjs": "./main.js"