p-elements-core 1.2.32-rc4 → 1.2.32-rc5
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/demo/sample.js +1 -1
- package/dist/p-elements-core-modern.js +1 -1
- package/dist/p-elements-core.js +1 -1
- package/package.json +1 -1
- package/src/custom-element.ts +12 -3
- package/src/decorators/property.ts +87 -53
- package/src/sample/sample.tsx +3 -3
|
@@ -78,7 +78,7 @@ export interface PropertyOptions {
|
|
|
78
78
|
*
|
|
79
79
|
* @typedef {PropertyOptions & { name: string }} PropertyOptionsWithName
|
|
80
80
|
*/
|
|
81
|
-
export type PropertyOptionsWithName = PropertyOptions & {name: string};
|
|
81
|
+
export type PropertyOptionsWithName = PropertyOptions & { name: string };
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
84
|
* Track which elements are currently updating attributes from property setters.
|
|
@@ -98,7 +98,7 @@ const settingAttributes = new WeakSet<HTMLElement>();
|
|
|
98
98
|
*/
|
|
99
99
|
const pendingAttributeReflections = new WeakMap<
|
|
100
100
|
HTMLElement,
|
|
101
|
-
Map<string, {value: any; type: any; converter?: AttributeConverter<any>}>
|
|
101
|
+
Map<string, { value: any; type: any; converter?: AttributeConverter<any> }>
|
|
102
102
|
>();
|
|
103
103
|
|
|
104
104
|
/**
|
|
@@ -111,7 +111,7 @@ const pendingAttributeReflections = new WeakMap<
|
|
|
111
111
|
*/
|
|
112
112
|
const pendingUpdates = new WeakMap<
|
|
113
113
|
HTMLElement,
|
|
114
|
-
Array<{propertyKey: string; oldValue: any; newValue: any}>
|
|
114
|
+
Array<{ propertyKey: string; oldValue: any; newValue: any }>
|
|
115
115
|
>();
|
|
116
116
|
|
|
117
117
|
/**
|
|
@@ -399,6 +399,19 @@ function updateAttribute(
|
|
|
399
399
|
}
|
|
400
400
|
}
|
|
401
401
|
|
|
402
|
+
const alreadLogedNodeNames = new Set<string>();
|
|
403
|
+
|
|
404
|
+
function logConnectedCallback(nodeName: string) {
|
|
405
|
+
if (!alreadLogedNodeNames.has(nodeName)) {
|
|
406
|
+
alreadLogedNodeNames.add(nodeName);
|
|
407
|
+
if (typeof console !== "undefined" && console.info) {
|
|
408
|
+
console.info(
|
|
409
|
+
`[CustomElement] connectedCallback called for <${nodeName.toLocaleLowerCase()}>`,
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
402
415
|
/**
|
|
403
416
|
* Class field decorator that creates a reactive property with automatic attribute syncing.
|
|
404
417
|
*
|
|
@@ -470,7 +483,13 @@ function updateAttribute(
|
|
|
470
483
|
export function property(
|
|
471
484
|
options: PropertyOptions = {},
|
|
472
485
|
): (target: any, propertyKey: string) => void {
|
|
473
|
-
const {
|
|
486
|
+
const {
|
|
487
|
+
type: rawType,
|
|
488
|
+
attribute = true,
|
|
489
|
+
reflect = false,
|
|
490
|
+
converter,
|
|
491
|
+
readonly = false,
|
|
492
|
+
} = options;
|
|
474
493
|
|
|
475
494
|
const type = normalizeType(rawType);
|
|
476
495
|
|
|
@@ -483,10 +502,15 @@ export function property(
|
|
|
483
502
|
// Only patch once per class
|
|
484
503
|
if (!target.__p_elements_core_lifecycle_patch_applied) {
|
|
485
504
|
// Patch connectedCallback
|
|
486
|
-
if (
|
|
505
|
+
if (
|
|
506
|
+
Object.prototype.hasOwnProperty.call(target, "connectedCallback") &&
|
|
507
|
+
typeof target.connectedCallback === "function"
|
|
508
|
+
) {
|
|
487
509
|
const originalConnected = target.connectedCallback;
|
|
488
|
-
target.connectedCallback = function patchedConnectedCallback(
|
|
489
|
-
|
|
510
|
+
target.connectedCallback = function patchedConnectedCallback(
|
|
511
|
+
...args: any[]
|
|
512
|
+
) {
|
|
513
|
+
const PATCHED_FLAG = "__p_elements_core_pending_updates_called";
|
|
490
514
|
if (!this[PATCHED_FLAG]) {
|
|
491
515
|
let superCalled = false;
|
|
492
516
|
const origProcess = processPendingUpdates;
|
|
@@ -497,28 +521,36 @@ export function property(
|
|
|
497
521
|
}
|
|
498
522
|
return origProcess(element);
|
|
499
523
|
}
|
|
500
|
-
(globalThis as any).__origProcessPendingUpdates =
|
|
501
|
-
|
|
524
|
+
(globalThis as any).__origProcessPendingUpdates =
|
|
525
|
+
processPendingUpdates;
|
|
526
|
+
(globalThis as any).processPendingUpdates =
|
|
527
|
+
wrappedProcessPendingUpdates;
|
|
502
528
|
try {
|
|
503
529
|
if (originalConnected) {
|
|
504
530
|
originalConnected.apply(this, args);
|
|
505
531
|
}
|
|
506
532
|
} finally {
|
|
507
|
-
(globalThis as any).processPendingUpdates = (
|
|
533
|
+
(globalThis as any).processPendingUpdates = (
|
|
534
|
+
globalThis as any
|
|
535
|
+
).__origProcessPendingUpdates;
|
|
508
536
|
delete (globalThis as any).__origProcessPendingUpdates;
|
|
509
537
|
}
|
|
510
538
|
if (!superCalled) {
|
|
511
539
|
let baseProto = Object.getPrototypeOf(target);
|
|
512
540
|
let baseConnected = baseProto && baseProto.connectedCallback;
|
|
513
|
-
if (typeof baseConnected ===
|
|
541
|
+
if (typeof baseConnected === "function") {
|
|
514
542
|
baseConnected.apply(this, args);
|
|
515
|
-
if (typeof console !==
|
|
516
|
-
|
|
543
|
+
if (typeof console !== "undefined" && console.info) {
|
|
544
|
+
// log this once per nodeName
|
|
545
|
+
logConnectedCallback(this.nodeName);
|
|
546
|
+
|
|
517
547
|
}
|
|
518
548
|
} else {
|
|
519
549
|
processPendingUpdates(this);
|
|
520
|
-
if (typeof console !==
|
|
521
|
-
console.info(
|
|
550
|
+
if (typeof console !== "undefined" && console.info) {
|
|
551
|
+
console.info(
|
|
552
|
+
`[p-elements-core] connectedCallback: called processPendingUpdates automatically for <${this.nodeName}>`,
|
|
553
|
+
);
|
|
522
554
|
}
|
|
523
555
|
}
|
|
524
556
|
}
|
|
@@ -530,23 +562,26 @@ export function property(
|
|
|
530
562
|
}
|
|
531
563
|
|
|
532
564
|
// Patch disconnectedCallback
|
|
533
|
-
if (
|
|
565
|
+
if (
|
|
566
|
+
Object.prototype.hasOwnProperty.call(target, "disconnectedCallback") &&
|
|
567
|
+
typeof target.disconnectedCallback === "function"
|
|
568
|
+
) {
|
|
534
569
|
const originalDisconnected = target.disconnectedCallback;
|
|
535
|
-
target.disconnectedCallback = function patchedDisconnectedCallback(
|
|
536
|
-
|
|
570
|
+
target.disconnectedCallback = function patchedDisconnectedCallback(
|
|
571
|
+
...args: any[]
|
|
572
|
+
) {
|
|
573
|
+
const PATCHED_FLAG = "__p_elements_core_disconnected_called";
|
|
537
574
|
if (!this[PATCHED_FLAG]) {
|
|
538
575
|
let superCalled = false;
|
|
539
576
|
const baseProto = Object.getPrototypeOf(target);
|
|
540
|
-
const baseDisconnected =
|
|
577
|
+
const baseDisconnected =
|
|
578
|
+
baseProto && baseProto.disconnectedCallback;
|
|
541
579
|
if (originalDisconnected) {
|
|
542
580
|
originalDisconnected.apply(this, args);
|
|
543
581
|
superCalled = true;
|
|
544
582
|
}
|
|
545
|
-
if (!superCalled && typeof baseDisconnected ===
|
|
583
|
+
if (!superCalled && typeof baseDisconnected === "function") {
|
|
546
584
|
baseDisconnected.apply(this, args);
|
|
547
|
-
if (typeof console !== 'undefined' && console.info) {
|
|
548
|
-
console.info(`[p-elements-core] disconnectedCallback: called base automatically for <${this.nodeName}>`);
|
|
549
|
-
}
|
|
550
585
|
}
|
|
551
586
|
this[PATCHED_FLAG] = true;
|
|
552
587
|
} else if (originalDisconnected) {
|
|
@@ -556,29 +591,35 @@ export function property(
|
|
|
556
591
|
}
|
|
557
592
|
|
|
558
593
|
// Patch attributeChangedCallback
|
|
559
|
-
if (
|
|
594
|
+
if (
|
|
595
|
+
Object.prototype.hasOwnProperty.call(
|
|
596
|
+
target,
|
|
597
|
+
"attributeChangedCallback",
|
|
598
|
+
) &&
|
|
599
|
+
typeof target.attributeChangedCallback === "function"
|
|
600
|
+
) {
|
|
560
601
|
const originalAttributeChanged = target.attributeChangedCallback;
|
|
561
|
-
target.attributeChangedCallback =
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
602
|
+
target.attributeChangedCallback =
|
|
603
|
+
function patchedAttributeChangedCallback(...args: any[]) {
|
|
604
|
+
const PATCHED_FLAG = "__p_elements_core_attribute_changed_called";
|
|
605
|
+
|
|
606
|
+
if (!this[PATCHED_FLAG]) {
|
|
607
|
+
let superCalled = false;
|
|
608
|
+
const baseProto = Object.getPrototypeOf(target);
|
|
609
|
+
const baseAttributeChanged =
|
|
610
|
+
baseProto && baseProto.attributeChangedCallback;
|
|
611
|
+
if (originalAttributeChanged) {
|
|
612
|
+
originalAttributeChanged.apply(this, args);
|
|
613
|
+
superCalled = true;
|
|
614
|
+
}
|
|
615
|
+
if (!superCalled && typeof baseAttributeChanged === "function") {
|
|
616
|
+
baseAttributeChanged.apply(this, args);
|
|
617
|
+
}
|
|
618
|
+
this[PATCHED_FLAG] = true;
|
|
619
|
+
} else if (originalAttributeChanged) {
|
|
568
620
|
originalAttributeChanged.apply(this, args);
|
|
569
|
-
superCalled = true;
|
|
570
|
-
}
|
|
571
|
-
if (!superCalled && typeof baseAttributeChanged === 'function') {
|
|
572
|
-
baseAttributeChanged.apply(this, args);
|
|
573
|
-
if (typeof console !== 'undefined' && console.info) {
|
|
574
|
-
console.info(`[p-elements-core] attributeChangedCallback: called base automatically for <${this.nodeName}>`);
|
|
575
|
-
}
|
|
576
621
|
}
|
|
577
|
-
|
|
578
|
-
} else if (originalAttributeChanged) {
|
|
579
|
-
originalAttributeChanged.apply(this, args);
|
|
580
|
-
}
|
|
581
|
-
};
|
|
622
|
+
};
|
|
582
623
|
}
|
|
583
624
|
|
|
584
625
|
target.__p_elements_core_lifecycle_patch_applied = true;
|
|
@@ -640,7 +681,6 @@ export function property(
|
|
|
640
681
|
isRenderOnSet = true;
|
|
641
682
|
}
|
|
642
683
|
|
|
643
|
-
|
|
644
684
|
// On first set, check if an HTML attribute exists and use that instead of the default
|
|
645
685
|
if (
|
|
646
686
|
!instanceMap.has(this) &&
|
|
@@ -648,7 +688,6 @@ export function property(
|
|
|
648
688
|
this.hasAttribute &&
|
|
649
689
|
this.hasAttribute(attributeName)
|
|
650
690
|
) {
|
|
651
|
-
|
|
652
691
|
const attrValue = this.getAttribute(attributeName);
|
|
653
692
|
if (attrValue !== null) {
|
|
654
693
|
if (converter && converter.fromAttribute) {
|
|
@@ -664,7 +703,6 @@ export function property(
|
|
|
664
703
|
|
|
665
704
|
// Trigger update for initial HTML attribute only if connected
|
|
666
705
|
if (this.isConnected) {
|
|
667
|
-
|
|
668
706
|
if (typeof this.renderNow === "function") {
|
|
669
707
|
this.renderNow();
|
|
670
708
|
}
|
|
@@ -673,7 +711,6 @@ export function property(
|
|
|
673
711
|
this.updated(propertyKey, oldValue, convertedValue);
|
|
674
712
|
}
|
|
675
713
|
} else {
|
|
676
|
-
|
|
677
714
|
// Queue for later when element connects
|
|
678
715
|
if (this.updated) {
|
|
679
716
|
if (!pendingUpdates.has(this)) {
|
|
@@ -681,7 +718,7 @@ export function property(
|
|
|
681
718
|
}
|
|
682
719
|
pendingUpdates
|
|
683
720
|
.get(this)!
|
|
684
|
-
.push({propertyKey, oldValue, newValue: convertedValue});
|
|
721
|
+
.push({ propertyKey, oldValue, newValue: convertedValue });
|
|
685
722
|
}
|
|
686
723
|
}
|
|
687
724
|
|
|
@@ -731,8 +768,6 @@ export function property(
|
|
|
731
768
|
converter,
|
|
732
769
|
});
|
|
733
770
|
}
|
|
734
|
-
|
|
735
|
-
|
|
736
771
|
}
|
|
737
772
|
|
|
738
773
|
// Trigger update only if connected
|
|
@@ -752,15 +787,14 @@ export function property(
|
|
|
752
787
|
}
|
|
753
788
|
pendingUpdates
|
|
754
789
|
.get(this)!
|
|
755
|
-
.push({propertyKey, oldValue, newValue: convertedValue});
|
|
790
|
+
.push({ propertyKey, oldValue, newValue: convertedValue });
|
|
756
791
|
}
|
|
757
792
|
}
|
|
758
793
|
|
|
759
794
|
// Call renderNow if @RenderOnSet is used and value changed
|
|
760
|
-
if (isRenderOnSet && typeof this.renderNow === "function"
|
|
795
|
+
if (isRenderOnSet && typeof this.renderNow === "function") {
|
|
761
796
|
this.renderNow();
|
|
762
797
|
}
|
|
763
|
-
|
|
764
798
|
},
|
|
765
799
|
configurable: true,
|
|
766
800
|
});
|
package/src/sample/sample.tsx
CHANGED
|
@@ -11,7 +11,7 @@ import {CustomElementController} from "../custom-element-controller";
|
|
|
11
11
|
|
|
12
12
|
@customElementConfig({tagName: "render-on-set"})
|
|
13
13
|
export class RenderOnSet extends CustomElement {
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
@PropertyRenderOnSet
|
|
16
16
|
public name: string = "foo";
|
|
17
17
|
|
|
@@ -22,11 +22,11 @@ export class RenderOnSet extends CustomElement {
|
|
|
22
22
|
#initDone = false;
|
|
23
23
|
|
|
24
24
|
connectedCallback(): void {
|
|
25
|
-
|
|
25
|
+
|
|
26
26
|
if (this.#initDone) {
|
|
27
27
|
return
|
|
28
28
|
}
|
|
29
|
-
this.#initDone = true;
|
|
29
|
+
this.#initDone = true;
|
|
30
30
|
let template = this.templateFromString(`<style>div{color: lime}</style><div />`);
|
|
31
31
|
this.shadowRoot.appendChild(template);
|
|
32
32
|
this.createProjector(this.shadowRoot.querySelector("div"), this.render);
|