vaderjs 1.9.1 → 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.
- package/index.ts +70 -61
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -140,7 +140,7 @@ export const e = (element, props, ...children) => {
|
|
|
140
140
|
return instance.render(props);
|
|
141
141
|
case typeof element === "function":
|
|
142
142
|
instance = memoizeClassComponent(Component, element.name);
|
|
143
|
-
element = element.bind(instance);
|
|
143
|
+
element = element.bind(instance);
|
|
144
144
|
instance.render = (props) => element(props);
|
|
145
145
|
if (element.name.toLowerCase() == "default") {
|
|
146
146
|
throw new Error("Function name must be unique");
|
|
@@ -153,6 +153,7 @@ export const e = (element, props, ...children) => {
|
|
|
153
153
|
firstEl = { type: "div", props: { key: instance.key, ...props }, children };
|
|
154
154
|
firstEl.props = { key: instance.key, ...firstEl.props, ...props };
|
|
155
155
|
firstEl.props["idKey"] = instance.key;
|
|
156
|
+
instance.props = firstEl.props;
|
|
156
157
|
return firstEl;
|
|
157
158
|
default:
|
|
158
159
|
let el = { type: element, props: props || {}, children: children || [] };
|
|
@@ -284,77 +285,81 @@ export class Component {
|
|
|
284
285
|
return this.refs.find((r) => r.key == key).value;
|
|
285
286
|
}
|
|
286
287
|
useEffect(callback, dependencies = []) {
|
|
287
|
-
const callbackId = callback.toString();
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
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
|
+
});
|
|
297
299
|
}
|
|
298
|
-
|
|
299
|
-
const effectCall = this.effectCalls.find(effect => effect.id === callbackId);
|
|
300
|
-
|
|
300
|
+
|
|
301
|
+
const effectCall = this.effectCalls.find((effect) => effect.id === callbackId);
|
|
302
|
+
|
|
301
303
|
const executeCallback = () => {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
effectCall.lastCall = now;
|
|
318
|
-
|
|
319
|
-
// Execute the callback asynchronously
|
|
320
|
-
setTimeout(() => {
|
|
321
|
-
try {
|
|
322
|
-
effects.push(callbackId); // Track the effect
|
|
323
|
-
callback();
|
|
324
|
-
} catch (error) {
|
|
325
|
-
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;
|
|
326
317
|
}
|
|
327
|
-
|
|
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);
|
|
328
329
|
};
|
|
329
|
-
|
|
330
|
-
//
|
|
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
|
|
331
340
|
if (dependencies.length === 0) {
|
|
332
|
-
|
|
333
|
-
executeCallback();
|
|
334
|
-
this.effect.push(callbackId);
|
|
335
|
-
}
|
|
336
|
-
return; // Skip further processing for empty dependencies
|
|
341
|
+
return;
|
|
337
342
|
}
|
|
338
|
-
|
|
343
|
+
|
|
339
344
|
// Check if dependencies have changed
|
|
340
345
|
let dependenciesChanged = false;
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
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;
|
|
348
353
|
}
|
|
349
|
-
}
|
|
350
354
|
}
|
|
351
|
-
|
|
352
|
-
// If dependencies changed,
|
|
355
|
+
|
|
356
|
+
// If dependencies have changed, run the effect and update dependencies
|
|
353
357
|
if (dependenciesChanged) {
|
|
354
|
-
|
|
355
|
-
|
|
358
|
+
executeCallback();
|
|
359
|
+
effectCall.dependencies = dependencies;
|
|
356
360
|
}
|
|
357
|
-
|
|
361
|
+
}
|
|
362
|
+
|
|
358
363
|
|
|
359
364
|
useState(key, defaultValue, persist = false) {
|
|
360
365
|
let value = this.state[key] || defaultValue;
|
|
@@ -362,6 +367,9 @@ export class Component {
|
|
|
362
367
|
value = sessionStorage.getItem(key) ? JSON.parse(sessionStorage.getItem(key)).value : defaultValue;
|
|
363
368
|
}
|
|
364
369
|
const setValue = (newValue) => {
|
|
370
|
+
if(typeof newValue === "function") {
|
|
371
|
+
newValue = newValue(this.state[key]);
|
|
372
|
+
}
|
|
365
373
|
this.state[key] = newValue;
|
|
366
374
|
if (persist) {
|
|
367
375
|
sessionStorage.setItem(key, JSON.stringify({ value: newValue }));
|
|
@@ -640,7 +648,7 @@ export class Component {
|
|
|
640
648
|
// Handle functional components
|
|
641
649
|
let component = memoizeClassComponent(Component, child.name);
|
|
642
650
|
component.Mounted = true;
|
|
643
|
-
component.render = child;
|
|
651
|
+
component.render = (props) => child(props);
|
|
644
652
|
let componentElement = component.toElement();
|
|
645
653
|
el.appendChild(componentElement);
|
|
646
654
|
} else if (typeof child === "object") {
|
|
@@ -707,6 +715,7 @@ export function render(element, container) {
|
|
|
707
715
|
let memoizedInstance = memoizeClassComponent(Component, element.name);
|
|
708
716
|
memoizedInstance.Mounted = true;
|
|
709
717
|
element = element.bind(memoizedInstance);
|
|
718
|
+
|
|
710
719
|
memoizedInstance.render = (props) => element(props);
|
|
711
720
|
if (element.name == "default") {
|
|
712
721
|
throw new Error("Function name Must be a unique function name as it is used for a element key");
|