vaderjs 1.9.2 → 1.9.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.
Files changed (2) hide show
  1. package/index.ts +66 -59
  2. 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
- // Initialize effect tracking
291
- if (!this.effectCalls.some(effect => effect.id === callbackId)) {
292
- this.effectCalls.push({
293
- id: callbackId,
294
- count: 0,
295
- lastCall: Date.now(),
296
- runOnce: dependencies.length === 0, // Flag to run only once if no dependencies
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
- const now = Date.now();
304
- const timeSinceLastCall = now - effectCall.lastCall;
305
-
306
- // Rate-limiting logic
307
- if (timeSinceLastCall < this.errorThreshold) {
308
- effectCall.count += 1;
309
- if (effectCall.count > this.maxIntervalCalls) {
310
- throw new Error(
311
- `Woah, way too many calls! Ensure you are not over-looping. Adjust maxIntervalCalls and errorThreshold as needed.`
312
- );
313
- }
314
- } else {
315
- effectCall.count = 1; // Reset count for a new interval
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
- }, 0);
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
- // Handle empty dependencies: run only once
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
- if (this.Mounted && !effects.includes(callbackId)) {
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
- if (dependencies.length !== this.effect.length) {
343
- dependenciesChanged = true;
344
- } else {
345
- for (let i = 0; i < dependencies.length; i++) {
346
- if (this.effect[i] !== dependencies[i]) {
347
- dependenciesChanged = true;
348
- break;
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, update and execute the callback
355
+
356
+ // If dependencies have changed, run the effect and update dependencies
354
357
  if (dependenciesChanged) {
355
- this.effect = [...dependencies]; // Update tracked dependencies
356
- executeCallback();
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 }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vaderjs",
3
- "version": "1.9.2",
3
+ "version": "1.9.3",
4
4
  "description": "A simple and powerful JavaScript library for building modern web applications.",
5
5
  "bin": {
6
6
  "vaderjs": "./main.js"