Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify UpgradableConstructor use of native lifecycle callbacks #3818

Closed
nolanlawson opened this issue Oct 20, 2023 · 0 comments · Fixed by #3862
Closed

Simplify UpgradableConstructor use of native lifecycle callbacks #3818

nolanlawson opened this issue Oct 20, 2023 · 0 comments · Fixed by #3862
Labels

Comments

@nolanlawson
Copy link
Contributor

nolanlawson commented Oct 20, 2023

(This bug only applies to native custom element lifecycle mode.)

Currently the UpgradableConstructor uses a WeakMap to keep track of lifecycle callbacks for FACE:

instancesToFormAssociatedCallbacks.set(this, formAssociatedCallbackToUse!);
instancesToFormDisabledCallbacks.set(this, formDisabledCallbackToUse!);
instancesToFormResetCallbacks.set(this, formResetCallbackToUse!);
instancesToFormStateRestoreCallbacks.set(this, formStateRestoreCallbackToUse!);

However, it doesn't do this for connected/disconnected callbacks:

if (hasConnectedCallback) {
(UpgradableConstructor.prototype as any).connectedCallback = function () {
if (!elementsUpgradedOutsideLWC.has(this)) {
connectedCallback(this);
}
};
}
if (hasDisconnectedCallback) {
(UpgradableConstructor.prototype as any).disconnectedCallback = function () {
if (!elementsUpgradedOutsideLWC.has(this)) {
disconnectedCallback(this);
}
};
}

This is technically a bug, since the connected/disconnected callbacks effectively are created once per constructor and never change per instance. So you might think that the connected/disconnected callbacks should use the same WeakMap system as the FACE callbacks, in order to fix the bug.

However, it doesn't actually matter in practice, because every callback closure just passes in this/elm, which is unique per instance:

connectedCallback = (elm: HTMLElement) => {
connectRootElement(elm);
};
disconnectedCallback = (elm: HTMLElement) => {
disconnectRootElement(elm);
};
formAssociatedCallback = (elm: HTMLElement) => {
runFormAssociatedCallback(elm);
};
formDisabledCallback = (elm: HTMLElement) => {
runFormDisabledCallback(elm);
};
formResetCallback = (elm: HTMLElement) => {
runFormResetCallback(elm);
};
formStateRestoreCallback = (elm: HTMLElement) => {
runFormStateRestoreCallback(elm);
};

(To get the vm, the underlying callbacks call getAssociatedVM(elm).)

Here is a test that confirms that the current behavior is correct: 687572e

So basically, the WeakMaps are useless at best and a perf hit at worst. It should be possible to greatly simplify this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant