@xaendar/core 0.6.7 → 0.6.9
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/dist/xaendar-core.es.d.ts +9 -5
- package/dist/xaendar-core.es.js +20 -17
- package/package.json +3 -3
|
@@ -3,6 +3,7 @@ import { Beautify } from '@xaendar/types';
|
|
|
3
3
|
import { ClassDecorator as ClassDecorator_2 } from '@xaendar/types';
|
|
4
4
|
import { Constructor } from '@xaendar/types';
|
|
5
5
|
import { EffectOptions } from '@xaendar/signals';
|
|
6
|
+
import { Function as Function_2 } from '@xaendar/types';
|
|
6
7
|
import { NoArgsFunction } from '@xaendar/types';
|
|
7
8
|
import { NoArgsVoidFunction } from '@xaendar/types';
|
|
8
9
|
import { NoArgsVoidFunction as NoArgsVoidFunction_2 } from '@xaendar/types';
|
|
@@ -74,7 +75,7 @@ export declare type BaseWebComponentConstructor = Constructor<BaseWebComponent,
|
|
|
74
75
|
*/
|
|
75
76
|
declare type Block = {
|
|
76
77
|
condition?: NoArgsFunction<boolean>;
|
|
77
|
-
block:
|
|
78
|
+
block: Function_2<[HTMLElement], NoArgsVoidFunction[]>;
|
|
78
79
|
};
|
|
79
80
|
|
|
80
81
|
/**
|
|
@@ -156,11 +157,12 @@ export declare type EventOptions = Beautify<RequireOne<Omit<CustomEventInit, 'de
|
|
|
156
157
|
* Reactively iterates over a list of items, re-running the loop whenever the tracked condition changes.
|
|
157
158
|
* Previous iteration side-effects are cleaned up before each re-evaluation.
|
|
158
159
|
*
|
|
160
|
+
* @param parentNode - The parent HTML element where the conditional structure is applied.
|
|
159
161
|
* @param unwatchFns - Array collecting cleanup functions for the parent scope.
|
|
160
162
|
* @param condition - A reactive function that returns the array of items to iterate over.
|
|
161
|
-
* @param forFn - A callback invoked for each item, receiving the item and its index. Must return an array of cleanup functions.
|
|
163
|
+
* @param forFn - A callback invoked for each item, receiving the parent node, the item, and its index. Must return an array of cleanup functions.
|
|
162
164
|
*/
|
|
163
|
-
export declare function _for(unwatchFns: NoArgsVoidFunction[], condition: () => unknown[], forFn: (item: unknown, index: number) => NoArgsVoidFunction[]): void;
|
|
165
|
+
export declare function _for(parentNode: HTMLElement, unwatchFns: NoArgsVoidFunction[], condition: () => unknown[], forFn: (parentNode: HTMLElement, item: unknown, index: number) => NoArgsVoidFunction[]): void;
|
|
164
166
|
|
|
165
167
|
/**
|
|
166
168
|
* Creates a reactive conditional structure from a list of branches.
|
|
@@ -176,12 +178,13 @@ export declare function _for(unwatchFns: NoArgsVoidFunction[], condition: () =>
|
|
|
176
178
|
* state (which branch is active) and the related cleanup functions are only updated if
|
|
177
179
|
* the active branch has actually changed.
|
|
178
180
|
*
|
|
181
|
+
* @param parentNode - The parent HTML element where the conditional structure is applied.
|
|
179
182
|
* @param unwatchFns - Shared array that collects all active cleanup functions.
|
|
180
183
|
* Mutated in place: the created effect and branch functions are added to and
|
|
181
184
|
* removed from this array.
|
|
182
185
|
* @param blocks - Ordered list of conditional branches to evaluate.
|
|
183
186
|
*/
|
|
184
|
-
export declare function _if(unwatchFns: NoArgsVoidFunction[], blocks: Block[]): void;
|
|
187
|
+
export declare function _if(parentNode: HTMLElement, unwatchFns: NoArgsVoidFunction[], blocks: Block[]): void;
|
|
185
188
|
|
|
186
189
|
/**
|
|
187
190
|
* An `InputSignal` is a specialized `Signal.State` designed for use as a property signal in web components.
|
|
@@ -363,6 +366,7 @@ export { Signal_2 as Signal }
|
|
|
363
366
|
* Because it delegates to `_if`, the switch benefits from the same optimisation:
|
|
364
367
|
* the active branch is only re-rendered when it actually changes.
|
|
365
368
|
*
|
|
369
|
+
* @param parentNode - The parent HTML element where the conditional structure is applied.
|
|
366
370
|
* @param unwatchFns - Shared array that collects all active cleanup functions.
|
|
367
371
|
* Mutated in place by the underlying {@link _if} call.
|
|
368
372
|
* @param expression - Function that evaluates the switch expression. Called
|
|
@@ -373,7 +377,7 @@ export { Signal_2 as Signal }
|
|
|
373
377
|
* - `block`: function that applies the branch's side effects and returns
|
|
374
378
|
* its cleanup functions.
|
|
375
379
|
*/
|
|
376
|
-
export declare function _switch(unwatchFns: NoArgsVoidFunction[], expression: NoArgsFunction<unknown>, blocks: Array<{
|
|
380
|
+
export declare function _switch(parentNode: HTMLElement, unwatchFns: NoArgsVoidFunction[], expression: NoArgsFunction<unknown>, blocks: Array<{
|
|
377
381
|
condition: unknown[] | null;
|
|
378
382
|
block: NoArgsFunction<NoArgsVoidFunction[]>;
|
|
379
383
|
}>): void;
|
package/dist/xaendar-core.es.js
CHANGED
|
@@ -416,23 +416,24 @@ function signal(value, options) {
|
|
|
416
416
|
* state (which branch is active) and the related cleanup functions are only updated if
|
|
417
417
|
* the active branch has actually changed.
|
|
418
418
|
*
|
|
419
|
+
* @param parentNode - The parent HTML element where the conditional structure is applied.
|
|
419
420
|
* @param unwatchFns - Shared array that collects all active cleanup functions.
|
|
420
421
|
* Mutated in place: the created effect and branch functions are added to and
|
|
421
422
|
* removed from this array.
|
|
422
423
|
* @param blocks - Ordered list of conditional branches to evaluate.
|
|
423
424
|
*/
|
|
424
|
-
function _if(unwatchFns, blocks) {
|
|
425
|
+
function _if(parentNode, unwatchFns, blocks) {
|
|
425
426
|
let state = null;
|
|
426
427
|
let localUnwatchFns = new Array();
|
|
427
428
|
let fn;
|
|
428
429
|
switch (blocks.length) {
|
|
429
430
|
case 1:
|
|
430
|
-
fn = () => handleIf(blocks[0], state, unwatchFns, localUnwatchFns);
|
|
431
|
+
fn = () => handleIf(parentNode, blocks[0], state, unwatchFns, localUnwatchFns);
|
|
431
432
|
break;
|
|
432
433
|
case 2:
|
|
433
|
-
fn = () => handleIfElse(blocks[0], blocks[1], state, unwatchFns, localUnwatchFns);
|
|
434
|
+
fn = () => handleIfElse(parentNode, blocks[0], blocks[1], state, unwatchFns, localUnwatchFns);
|
|
434
435
|
break;
|
|
435
|
-
default: fn = () => handleIfElseIf(blocks, state, unwatchFns, localUnwatchFns);
|
|
436
|
+
default: fn = () => handleIfElseIf(parentNode, blocks, state, unwatchFns, localUnwatchFns);
|
|
436
437
|
}
|
|
437
438
|
const unlistener = effect(() => {
|
|
438
439
|
const retVal = fn();
|
|
@@ -457,8 +458,8 @@ function _if(unwatchFns, blocks) {
|
|
|
457
458
|
* @returns The new state and new cleanup functions if the active branch changed,
|
|
458
459
|
* otherwise `undefined`.
|
|
459
460
|
*/
|
|
460
|
-
function handleIf(ifBlock, state, unwatchFns, localUnwatchFns) {
|
|
461
|
-
return ifBlock.condition() ? checkAndUpdateState(state, 0, ifBlock.block, unwatchFns, localUnwatchFns) : checkAndUpdateState(state, null, () => [], unwatchFns, localUnwatchFns);
|
|
461
|
+
function handleIf(parentNode, ifBlock, state, unwatchFns, localUnwatchFns) {
|
|
462
|
+
return ifBlock.condition() ? checkAndUpdateState(parentNode, state, 0, ifBlock.block, unwatchFns, localUnwatchFns) : checkAndUpdateState(parentNode, state, null, () => [], unwatchFns, localUnwatchFns);
|
|
462
463
|
}
|
|
463
464
|
/**
|
|
464
465
|
* Handles the `if` / `else` case (exactly two branches).
|
|
@@ -474,8 +475,8 @@ function handleIf(ifBlock, state, unwatchFns, localUnwatchFns) {
|
|
|
474
475
|
* @returns The new state and new cleanup functions if the active branch changed,
|
|
475
476
|
* otherwise `undefined`.
|
|
476
477
|
*/
|
|
477
|
-
function handleIfElse(ifBlock, elseBlock, state, unwatchFns, localUnwatchFns) {
|
|
478
|
-
return ifBlock.condition() ? checkAndUpdateState(state, 0, ifBlock.block, unwatchFns, localUnwatchFns) : checkAndUpdateState(state, 1, elseBlock.block, unwatchFns, localUnwatchFns);
|
|
478
|
+
function handleIfElse(parentNode, ifBlock, elseBlock, state, unwatchFns, localUnwatchFns) {
|
|
479
|
+
return ifBlock.condition() ? checkAndUpdateState(parentNode, state, 0, ifBlock.block, unwatchFns, localUnwatchFns) : checkAndUpdateState(parentNode, state, 1, elseBlock.block, unwatchFns, localUnwatchFns);
|
|
479
480
|
}
|
|
480
481
|
/**
|
|
481
482
|
* Handles the general case of an `if` / `else if` / ... / `else` chain (three or more branches).
|
|
@@ -491,10 +492,10 @@ function handleIfElse(ifBlock, elseBlock, state, unwatchFns, localUnwatchFns) {
|
|
|
491
492
|
* @returns The new state and new cleanup functions if the active branch changed,
|
|
492
493
|
* otherwise `undefined`. Also returns `undefined` if no branch matches.
|
|
493
494
|
*/
|
|
494
|
-
function handleIfElseIf(blocks, state, unwatchFns, localUnwatchFns) {
|
|
495
|
+
function handleIfElseIf(parentNode, blocks, state, unwatchFns, localUnwatchFns) {
|
|
495
496
|
for (let i = 0; i < blocks.length; i++) {
|
|
496
497
|
const { condition, block } = blocks[i];
|
|
497
|
-
if (!condition || condition()) return checkAndUpdateState(state, i, block, unwatchFns, localUnwatchFns);
|
|
498
|
+
if (!condition || condition()) return checkAndUpdateState(parentNode, state, i, block, unwatchFns, localUnwatchFns);
|
|
498
499
|
}
|
|
499
500
|
}
|
|
500
501
|
/**
|
|
@@ -518,10 +519,10 @@ function handleIfElseIf(blocks, state, unwatchFns, localUnwatchFns) {
|
|
|
518
519
|
* @returns Il nuovo stato e le nuove funzioni di cleanup se il ramo è cambiato, altrimenti
|
|
519
520
|
* `undefined`.
|
|
520
521
|
*/
|
|
521
|
-
function checkAndUpdateState(state, newState, conditionalBlockFn, unwatchFns, localUnwatchFns) {
|
|
522
|
+
function checkAndUpdateState(parentNode, state, newState, conditionalBlockFn, unwatchFns, localUnwatchFns) {
|
|
522
523
|
if (state !== newState) {
|
|
523
524
|
unwatch(unwatchFns, localUnwatchFns);
|
|
524
|
-
localUnwatchFns = Signal.subtle.untrack(conditionalBlockFn);
|
|
525
|
+
localUnwatchFns = Signal.subtle.untrack(() => conditionalBlockFn(parentNode));
|
|
525
526
|
unwatchFns.push(...localUnwatchFns);
|
|
526
527
|
return {
|
|
527
528
|
state: newState,
|
|
@@ -551,18 +552,19 @@ function unwatch(unwatchFns, localUnwatchFns) {
|
|
|
551
552
|
* Reactively iterates over a list of items, re-running the loop whenever the tracked condition changes.
|
|
552
553
|
* Previous iteration side-effects are cleaned up before each re-evaluation.
|
|
553
554
|
*
|
|
555
|
+
* @param parentNode - The parent HTML element where the conditional structure is applied.
|
|
554
556
|
* @param unwatchFns - Array collecting cleanup functions for the parent scope.
|
|
555
557
|
* @param condition - A reactive function that returns the array of items to iterate over.
|
|
556
|
-
* @param forFn - A callback invoked for each item, receiving the item and its index. Must return an array of cleanup functions.
|
|
558
|
+
* @param forFn - A callback invoked for each item, receiving the parent node, the item, and its index. Must return an array of cleanup functions.
|
|
557
559
|
*/
|
|
558
|
-
function _for(unwatchFns, condition, forFn) {
|
|
560
|
+
function _for(parentNode, unwatchFns, condition, forFn) {
|
|
559
561
|
const localUnwatchFns = new Array();
|
|
560
562
|
const unlistener = effect(() => {
|
|
561
563
|
unwatch(unwatchFns, localUnwatchFns);
|
|
562
564
|
const items = condition();
|
|
563
565
|
Signal.subtle.untrack(() => {
|
|
564
566
|
for (let i = 0; i < items.length; i++) {
|
|
565
|
-
localUnwatchFns.push(...forFn(items[i], i));
|
|
567
|
+
localUnwatchFns.push(...forFn(parentNode, items[i], i));
|
|
566
568
|
unwatchFns.push(...localUnwatchFns);
|
|
567
569
|
}
|
|
568
570
|
});
|
|
@@ -605,6 +607,7 @@ function _iterationVariables(items, index, itemAlias, indexAlias) {
|
|
|
605
607
|
* Because it delegates to `_if`, the switch benefits from the same optimisation:
|
|
606
608
|
* the active branch is only re-rendered when it actually changes.
|
|
607
609
|
*
|
|
610
|
+
* @param parentNode - The parent HTML element where the conditional structure is applied.
|
|
608
611
|
* @param unwatchFns - Shared array that collects all active cleanup functions.
|
|
609
612
|
* Mutated in place by the underlying {@link _if} call.
|
|
610
613
|
* @param expression - Function that evaluates the switch expression. Called
|
|
@@ -615,8 +618,8 @@ function _iterationVariables(items, index, itemAlias, indexAlias) {
|
|
|
615
618
|
* - `block`: function that applies the branch's side effects and returns
|
|
616
619
|
* its cleanup functions.
|
|
617
620
|
*/
|
|
618
|
-
function _switch(unwatchFns, expression, blocks) {
|
|
619
|
-
_if(unwatchFns, blocks.map(({ condition, block }) => ({
|
|
621
|
+
function _switch(parentNode, unwatchFns, expression, blocks) {
|
|
622
|
+
_if(parentNode, unwatchFns, blocks.map(({ condition, block }) => ({
|
|
620
623
|
condition: condition ? () => condition.some((condition) => condition === expression()) : void 0,
|
|
621
624
|
block
|
|
622
625
|
})));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xaendar/core",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.9",
|
|
4
4
|
"description": "A library containing core utils such as webcomponent base classes and theming support",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"type": "module",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@xaendar/signals": "0.6.
|
|
20
|
-
"@xaendar/types": "0.6.
|
|
19
|
+
"@xaendar/signals": "0.6.9",
|
|
20
|
+
"@xaendar/types": "0.6.9"
|
|
21
21
|
}
|
|
22
22
|
}
|