-
Notifications
You must be signed in to change notification settings - Fork 399
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
feat(versioning): use native custom element lifecycle #3352
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,67 +8,58 @@ import { isUndefined } from '@lwc/shared'; | |
import type { LifecycleCallback } from '@lwc/engine-core'; | ||
|
||
const cachedConstructors = new Map<string, CustomElementConstructor>(); | ||
const elementsUpgradedOutsideLWC = new WeakSet<HTMLElement>(); | ||
|
||
let elementBeingUpgradedByLWC = false; | ||
let upgradeCallbackToUse: LifecycleCallback | undefined; | ||
let connectedCallbackToUse: LifecycleCallback | undefined; | ||
let disconnectedCallbackToUse: LifecycleCallback | undefined; | ||
|
||
const instancesToConnectedCallbacks = new WeakMap<HTMLElement, LifecycleCallback>(); | ||
const instancesToDisconnectedCallbacks = new WeakMap<HTMLElement, LifecycleCallback>(); | ||
|
||
// Creates a constructor that is intended to be used directly as a custom element, except that the upgradeCallback is | ||
// passed in to the constructor so LWC can reuse the same custom element constructor for multiple components. | ||
// Another benefit is that only LWC can create components that actually do anything – if you do | ||
// `customElements.define('x-foo')`, then you don't have access to the upgradeCallback, so it's a dummy custom element. | ||
// This class should be created once per tag name. | ||
const createUpgradableConstructor = ( | ||
connectedCallback?: LifecycleCallback, | ||
disconnectedCallback?: LifecycleCallback | ||
) => { | ||
const hasConnectedCallback = !isUndefined(connectedCallback); | ||
const hasDisconnectedCallback = !isUndefined(disconnectedCallback); | ||
|
||
const createUpgradableConstructor = () => { | ||
// TODO [#2972]: this class should expose observedAttributes as necessary | ||
class UpgradableConstructor extends HTMLElement { | ||
constructor(upgradeCallback: LifecycleCallback) { | ||
return class UpgradableConstructor extends HTMLElement { | ||
constructor() { | ||
super(); | ||
// If the element is not created using lwc.createElement(), e.g. `document.createElement('x-foo')`, | ||
// then elementBeingUpgraded will be false | ||
if (elementBeingUpgradedByLWC) { | ||
upgradeCallback(this); | ||
} else if (hasConnectedCallback || hasDisconnectedCallback) { | ||
// If this element has connected or disconnected callbacks, then we need to keep track of | ||
// instances that were created outside LWC (i.e. not created by `lwc.createElement()`). | ||
// If the element has no connected or disconnected callbacks, then we don't need to track this. | ||
elementsUpgradedOutsideLWC.add(this); | ||
|
||
// TODO [#2970]: LWC elements cannot be upgraded via new Ctor() | ||
// Do we want to support this? Throw an error? Currently for backwards compat it's a no-op. | ||
// Cache the callbacks in the weak maps | ||
instancesToConnectedCallbacks.set(this, connectedCallbackToUse!); | ||
instancesToDisconnectedCallbacks.set(this, disconnectedCallbackToUse!); | ||
upgradeCallbackToUse!(this); | ||
} | ||
// TODO [#2970]: LWC elements cannot be upgraded via new Ctor() | ||
// Do we want to support this? Throw an error? Currently for backwards compat it's a no-op. | ||
} | ||
} | ||
|
||
// Do not unnecessarily add a connectedCallback/disconnectedCallback, as it introduces perf overhead | ||
// See: https://github.com/salesforce/lwc/pull/3162#issuecomment-1311851174 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I'm doing here is a known perf regression (adding the We may be able to solve the perf regression if we do #3202 someday. |
||
if (hasConnectedCallback) { | ||
(UpgradableConstructor.prototype as any).connectedCallback = function () { | ||
if (!elementsUpgradedOutsideLWC.has(this)) { | ||
connectedCallback() { | ||
const connectedCallback = instancesToConnectedCallbacks.get(this); | ||
if (!isUndefined(connectedCallback)) { | ||
connectedCallback(this); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
if (hasDisconnectedCallback) { | ||
(UpgradableConstructor.prototype as any).disconnectedCallback = function () { | ||
if (!elementsUpgradedOutsideLWC.has(this)) { | ||
disconnectedCallback() { | ||
const disconnectedCallback = instancesToDisconnectedCallbacks.get(this); | ||
if (!isUndefined(disconnectedCallback)) { | ||
disconnectedCallback(this); | ||
} | ||
}; | ||
} | ||
|
||
return UpgradableConstructor; | ||
} | ||
}; | ||
}; | ||
|
||
export const createCustomElementUsingUpgradableConstructor = ( | ||
tagName: string, | ||
upgradeCallback: LifecycleCallback, | ||
connectedCallback?: LifecycleCallback, | ||
disconnectedCallback?: LifecycleCallback | ||
connectedCallback: LifecycleCallback, | ||
disconnectedCallback: LifecycleCallback | ||
) => { | ||
// use global custom elements registry | ||
let UpgradableConstructor = cachedConstructors.get(tagName); | ||
|
@@ -79,18 +70,21 @@ export const createCustomElementUsingUpgradableConstructor = ( | |
`Unexpected tag name "${tagName}". This name is a registered custom element, preventing LWC to upgrade the element.` | ||
); | ||
} | ||
UpgradableConstructor = createUpgradableConstructor( | ||
connectedCallback, | ||
disconnectedCallback | ||
); | ||
UpgradableConstructor = createUpgradableConstructor(); | ||
customElements.define(tagName, UpgradableConstructor); | ||
cachedConstructors.set(tagName, UpgradableConstructor); | ||
} | ||
|
||
elementBeingUpgradedByLWC = true; | ||
upgradeCallbackToUse = upgradeCallback; | ||
connectedCallbackToUse = connectedCallback; | ||
disconnectedCallbackToUse = disconnectedCallback; | ||
try { | ||
return new UpgradableConstructor(upgradeCallback); | ||
return new UpgradableConstructor(); | ||
} finally { | ||
elementBeingUpgradedByLWC = false; | ||
upgradeCallbackToUse = undefined; | ||
connectedCallbackToUse = undefined; | ||
disconnectedCallbackToUse = undefined; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would love to use the
integer
type here, but I could not get it to work, and I figured there's little point in debugging Circle CI oddities since we may move away from Circle CI anyway.