pawajs 1.4.19 → 1.4.20
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/README.md +2 -2
- package/index.js +73 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -343,13 +343,13 @@ useValidateComponent(TodoList, {
|
|
|
343
343
|
### Core Functions
|
|
344
344
|
- `pawaStartApp(rootElement, initialContext)`: Initializes the PawaJS application on a given root DOM element.
|
|
345
345
|
- `RegisterComponent(...components)`: Registers one or more components to be used in templates.
|
|
346
|
-
- `$state(initialValue, localStorageKey?)`: Creates a new reactive state object.
|
|
346
|
+
- `$state(initialValue, localStorageKey?)`: Creates a new reactive state object. Used inside or outside Component (module export)
|
|
347
347
|
- `PluginSystem(plugin)`: Registers a plugin to extend PawaJS functionality (e.g., Routers, Global Stores).
|
|
348
348
|
- `html`: A tagged template literal for syntax highlighting and potential future optimizations.
|
|
349
349
|
|
|
350
350
|
### Component Hooks
|
|
351
351
|
- `useInsert(object)`: Exposes data and functions from a component's setup to its template.
|
|
352
|
-
- `runEffect(callback, dependencies?)`: Runs a side effect after or before the component renders, and re-runs it when its dependencies change .
|
|
352
|
+
- `runEffect(callback, dependencies?)`: Runs a side effect after or before the component renders, and re-runs it when its dependencies change . Used inside or outside Component
|
|
353
353
|
- `useContext(contextObject)` & `setContext()`: A mechanism for providing and consuming data throughout a component tree.
|
|
354
354
|
- `useRef()`: Creates a reference object that can be attached to a DOM element using the `ref` directive.
|
|
355
355
|
- `useValidateComponent(Component, rules)`: Defines validation rules for a component's props.
|
package/index.js
CHANGED
|
@@ -418,39 +418,88 @@ export const RegisterComponent = (...args) => {
|
|
|
418
418
|
*
|
|
419
419
|
* null- for Mount hook
|
|
420
420
|
* @returns {void}
|
|
421
|
-
*/
|
|
422
|
-
export const runEffect = (callback, deps) => {
|
|
421
|
+
*/export const runEffect = (callback, deps) => {
|
|
423
422
|
if (client) {
|
|
424
423
|
if (stateContext._hasRun) {
|
|
425
424
|
return
|
|
426
425
|
}
|
|
426
|
+
|
|
427
|
+
// Detect if inside a component
|
|
428
|
+
// stateContext._name is set in setStateContext
|
|
429
|
+
// default stub has no _name
|
|
430
|
+
const insideComponent = !!stateContext._name ||
|
|
431
|
+
!!stateContext.component
|
|
432
|
+
|
|
427
433
|
if (stateContext) {
|
|
428
|
-
if (!stateContext._hook) {
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
if (!stateContext._hook.
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
if (!stateContext._hook.beforeMount) {
|
|
435
|
-
stateContext._hook.beforeMount = []
|
|
436
|
-
}
|
|
437
|
-
if (!stateContext._hook.reactiveEffect) {
|
|
438
|
-
stateContext._hook.reactiveEffect = []
|
|
439
|
-
}
|
|
440
|
-
if (!stateContext._hook.effect) {
|
|
441
|
-
stateContext._hook.effect = []
|
|
442
|
-
}
|
|
434
|
+
if (!stateContext._hook) stateContext._hook = {}
|
|
435
|
+
if (!stateContext._hook.isMount) stateContext._hook.isMount = []
|
|
436
|
+
if (!stateContext._hook.beforeMount) stateContext._hook.beforeMount = []
|
|
437
|
+
if (!stateContext._hook.reactiveEffect) stateContext._hook.reactiveEffect = []
|
|
438
|
+
if (!stateContext._hook.effect) stateContext._hook.effect = []
|
|
439
|
+
|
|
443
440
|
if (deps === undefined || deps === null) {
|
|
444
|
-
|
|
441
|
+
if (insideComponent) {
|
|
442
|
+
// Inside component — register for later
|
|
443
|
+
stateContext._hook.isMount.push(callback)
|
|
444
|
+
} else {
|
|
445
|
+
// Outside component — execute immediately
|
|
446
|
+
// after current execution context via microtask
|
|
447
|
+
Promise.resolve().then(() => {
|
|
448
|
+
const cleanup = callback()
|
|
449
|
+
if (typeof cleanup === 'function') {
|
|
450
|
+
// Register global cleanup on page unload
|
|
451
|
+
window.addEventListener('beforeunload', cleanup, { once: true })
|
|
452
|
+
}
|
|
453
|
+
})
|
|
454
|
+
}
|
|
445
455
|
} else if (typeof deps === 'object' && !Array.isArray(deps)) {
|
|
446
|
-
|
|
456
|
+
// readonly reactive effect — needs component context
|
|
457
|
+
// silently ignore outside component
|
|
458
|
+
if (insideComponent) {
|
|
459
|
+
stateContext._hook.reactiveEffect.push({ deps, effect: callback })
|
|
460
|
+
}else{
|
|
461
|
+
|
|
462
|
+
let terminateEffect=new Set()
|
|
463
|
+
const mainFunction=callback()
|
|
464
|
+
createEffect(()=>{
|
|
465
|
+
const cleanUp=mainFunction?.()
|
|
466
|
+
if (typeof cleanUp === 'function') {
|
|
467
|
+
return cleanUp
|
|
468
|
+
}
|
|
469
|
+
},{
|
|
470
|
+
_terminateEffects:terminateEffect
|
|
471
|
+
})
|
|
472
|
+
if (terminateEffect.size > 0) {
|
|
473
|
+
window.addEventListener('beforeunload',()=>{
|
|
474
|
+
terminateEffect.forEach(fn =>{
|
|
475
|
+
fn()
|
|
476
|
+
})
|
|
477
|
+
})
|
|
478
|
+
}
|
|
479
|
+
}
|
|
447
480
|
} else if (Array.isArray(deps)) {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
481
|
+
// watch — needs component context
|
|
482
|
+
// silently ignore outside component
|
|
483
|
+
if (insideComponent) {
|
|
484
|
+
stateContext._hook.effect.push({ deps, effect: callback })
|
|
485
|
+
}else{
|
|
486
|
+
//used pawajs stateWatch
|
|
487
|
+
const cleanUp=stateWatch(callback,deps)
|
|
488
|
+
window.addEventListener('beforeunload',cleanUp,{once:true})
|
|
489
|
+
}
|
|
452
490
|
} else if (typeof deps === 'number') {
|
|
453
|
-
|
|
491
|
+
if (insideComponent) {
|
|
492
|
+
// Inside component — register for later
|
|
493
|
+
stateContext._hook.beforeMount.push(callback)
|
|
494
|
+
} else {
|
|
495
|
+
// Outside component — execute after deps ms
|
|
496
|
+
setTimeout(() => {
|
|
497
|
+
const cleanup = callback()
|
|
498
|
+
if (typeof cleanup === 'function') {
|
|
499
|
+
window.addEventListener('beforeunload', cleanup, { once: true })
|
|
500
|
+
}
|
|
501
|
+
}, deps)
|
|
502
|
+
}
|
|
454
503
|
}
|
|
455
504
|
}
|
|
456
505
|
}
|