Skip to content

Commit 24455ac

Browse files
custom element
1 parent 410c933 commit 24455ac

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/oc-client.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export function createOc(oc) {
5252
let RETRY_SEND_NUMBER = ocConf.retrySendNumber || true;
5353
let POLLING_INTERVAL = ocConf.pollingInterval || 500;
5454
let OC_TAG = ocConf.tag || "oc-component";
55+
let MANAGE_LIFECYCLES = isBool(ocConf.manageLifecycles)
56+
? ocConf.manageLifecycles
57+
: false;
5558
let MESSAGES_ERRORS_HREF_MISSING = "Href parameter missing";
5659
let MESSAGES_ERRORS_RETRY_FAILED =
5760
"Failed to load % component " + RETRY_LIMIT + " times. Giving up";
@@ -663,5 +666,46 @@ export function createOc(oc) {
663666
// render the components
664667
loadAfterReady();
665668

669+
if (window.customElements) {
670+
window.customElements.define(
671+
OC_TAG,
672+
class extends HTMLElement {
673+
#connected = false;
674+
#manageLifecycle = false;
675+
constructor() {
676+
super();
677+
if (
678+
MANAGE_LIFECYCLES ||
679+
this.getAttribute("manage-lifecycle") == "true" ||
680+
this.getAttribute("manage-lifecycle") == ""
681+
) {
682+
this.#manageLifecycle = true;
683+
}
684+
}
685+
686+
connectedCallback() {
687+
this.#connected = true;
688+
if (this.#manageLifecycle) {
689+
oc.renderNestedComponent(this, () => {});
690+
}
691+
}
692+
693+
disconnectedCallback() {
694+
if (this.#connected) {
695+
this.#connected = false;
696+
const shouldUnmount =
697+
this.#manageLifecycle &&
698+
this.unmount &&
699+
this.getAttribute("data-rendered") == "true";
700+
if (shouldUnmount) {
701+
this.unmount();
702+
this.removeAttribute("data-rendered");
703+
}
704+
}
705+
}
706+
},
707+
);
708+
}
709+
666710
return oc;
667711
}

0 commit comments

Comments
 (0)