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

refactor(engine-core): encapsulate renderer as an object and allow it to be injectable in vnodes #2763

Merged
merged 16 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 117 additions & 100 deletions packages/@lwc/engine-core/src/framework/base-lightning-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,6 @@ import {

import { logError } from '../shared/logger';
import { getComponentTag } from '../shared/format';
import {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are now extracted from the renderer associated to the vm for a lightning element.

getChildren,
getChildNodes,
getFirstChild,
getFirstElementChild,
getLastChild,
getLastElementChild,
assertInstanceOfHTMLElement,
attachShadow,
addEventListener,
removeEventListener,
getAttribute,
removeAttribute,
setAttribute,
getBoundingClientRect,
isConnected,
getClassList,
dispatchEvent,
getElementsByClassName,
getElementsByTagName,
querySelector,
querySelectorAll,
} from '../renderer';

import { HTMLElementOriginalDescriptors } from './html-properties';
import { getWrappedComponentsListener } from './component';
Expand Down Expand Up @@ -220,6 +197,7 @@ export const LightningElement: LightningElementConstructor = function (
const { bridge } = def;

if (process.env.NODE_ENV !== 'production') {
const { assertInstanceOfHTMLElement } = vm.renderer;
assertInstanceOfHTMLElement(
vm.elm,
`Component creation requires a DOM element to be associated to ${vm}.`
Expand Down Expand Up @@ -270,6 +248,7 @@ function doAttachShadow(vm: VM): ShadowRoot {
mode,
shadowMode,
def: { ctor },
renderer: { attachShadow },
} = vm;

const shadowRoot = attachShadow(elm, {
Expand Down Expand Up @@ -303,7 +282,11 @@ LightningElement.prototype = {
constructor: LightningElement,

dispatchEvent(event: Event): boolean {
const { elm } = getAssociatedVM(this);
const vm = getAssociatedVM(this);
const {
elm,
renderer: { dispatchEvent },
} = vm;
return dispatchEvent(elm, event);
},

Expand All @@ -313,7 +296,10 @@ LightningElement.prototype = {
options?: boolean | AddEventListenerOptions
): void {
const vm = getAssociatedVM(this);
const { elm } = vm;
const {
elm,
renderer: { addEventListener },
} = vm;

if (process.env.NODE_ENV !== 'production') {
const vmBeingRendered = getVMBeingRendered();
Expand Down Expand Up @@ -341,51 +327,74 @@ LightningElement.prototype = {
options?: boolean | AddEventListenerOptions
): void {
const vm = getAssociatedVM(this);
const { elm } = vm;
const {
elm,
renderer: { removeEventListener },
} = vm;

const wrappedListener = getWrappedComponentsListener(vm, listener);
removeEventListener(elm, type, wrappedListener, options);
},

hasAttribute(name: string): boolean {
const { elm } = getAssociatedVM(this);
const vm = getAssociatedVM(this);
const {
elm,
renderer: { getAttribute },
} = vm;
return !isNull(getAttribute(elm, name));
},

hasAttributeNS(namespace: string | null, name: string): boolean {
const { elm } = getAssociatedVM(this);
const vm = getAssociatedVM(this);
const {
elm,
renderer: { getAttribute },
} = vm;
return !isNull(getAttribute(elm, name, namespace));
},

removeAttribute(name: string): void {
const { elm } = getAssociatedVM(this);

const vm = getAssociatedVM(this);
const {
elm,
renderer: { removeAttribute },
} = vm;
unlockAttribute(elm, name);
removeAttribute(elm, name);
lockAttribute(elm, name);
},

removeAttributeNS(namespace: string | null, name: string): void {
const { elm } = getAssociatedVM(this);

const {
elm,
renderer: { removeAttribute },
} = getAssociatedVM(this);
unlockAttribute(elm, name);
removeAttribute(elm, name, namespace);
lockAttribute(elm, name);
},

getAttribute(name: string): string | null {
const { elm } = getAssociatedVM(this);
const vm = getAssociatedVM(this);
const { elm } = vm;
const { getAttribute } = vm.renderer;
return getAttribute(elm, name);
},

getAttributeNS(namespace: string | null, name: string): string | null {
const { elm } = getAssociatedVM(this);
const vm = getAssociatedVM(this);
const { elm } = vm;
const { getAttribute } = vm.renderer;
return getAttribute(elm, name, namespace);
},

setAttribute(name: string, value: string): void {
const vm = getAssociatedVM(this);
const { elm } = vm;
const {
elm,
renderer: { setAttribute },
} = vm;

if (process.env.NODE_ENV !== 'production') {
assert.isFalse(
Expand All @@ -401,7 +410,10 @@ LightningElement.prototype = {

setAttributeNS(namespace: string | null, name: string, value: string): void {
const vm = getAssociatedVM(this);
const { elm } = vm;
const {
elm,
renderer: { setAttribute },
} = vm;

if (process.env.NODE_ENV !== 'production') {
assert.isFalse(
Expand All @@ -417,7 +429,10 @@ LightningElement.prototype = {

getBoundingClientRect(): ClientRect {
const vm = getAssociatedVM(this);
const { elm } = vm;
const {
elm,
renderer: { getBoundingClientRect },
} = vm;

if (process.env.NODE_ENV !== 'production') {
warnIfInvokedDuringConstruction(vm, 'getBoundingClientRect()');
Expand All @@ -427,13 +442,20 @@ LightningElement.prototype = {
},

get isConnected(): boolean {
const { elm } = getAssociatedVM(this);
const vm = getAssociatedVM(this);
const {
elm,
renderer: { isConnected },
} = vm;
return isConnected(elm);
},

get classList(): DOMTokenList {
const vm = getAssociatedVM(this);
const { elm } = vm;
const {
elm,
renderer: { getClassList },
} = vm;

if (process.env.NODE_ENV !== 'production') {
// TODO [#1290]: this still fails in dev but works in production, eventually, we should
Expand Down Expand Up @@ -467,6 +489,60 @@ LightningElement.prototype = {
return null;
},

get children() {
const vm = getAssociatedVM(this);
const renderer = vm.renderer;
if (process.env.NODE_ENV !== 'production') {
warnIfInvokedDuringConstruction(vm, 'children');
}
return renderer.getChildren(vm.elm);
},

get childNodes() {
const vm = getAssociatedVM(this);
const renderer = vm.renderer;
if (process.env.NODE_ENV !== 'production') {
warnIfInvokedDuringConstruction(vm, 'childNodes');
}
return renderer.getChildNodes(vm.elm);
},

get firstChild() {
const vm = getAssociatedVM(this);
const renderer = vm.renderer;
if (process.env.NODE_ENV !== 'production') {
warnIfInvokedDuringConstruction(vm, 'firstChild');
}
return renderer.getFirstChild(vm.elm);
},

get firstElementChild() {
const vm = getAssociatedVM(this);
const renderer = vm.renderer;
if (process.env.NODE_ENV !== 'production') {
warnIfInvokedDuringConstruction(vm, 'firstElementChild');
}
return renderer.getFirstElementChild(vm.elm);
},

get lastChild() {
const vm = getAssociatedVM(this);
const renderer = vm.renderer;
if (process.env.NODE_ENV !== 'production') {
warnIfInvokedDuringConstruction(vm, 'lastChild');
}
return renderer.getLastChild(vm.elm);
},

get lastElementChild() {
const vm = getAssociatedVM(this);
const renderer = vm.renderer;
if (process.env.NODE_ENV !== 'production') {
warnIfInvokedDuringConstruction(vm, 'lastElementChild');
}
return renderer.getLastElementChild(vm.elm);
},

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplication is a bit unfortunate. Before #2598 we had a way to consolidate some of this logic:

const childGetters: Array<
[
keyof HTMLElement,
keyof Pick<
Renderer,
| 'getChildren'
| 'getChildNodes'
| 'getFirstChild'
| 'getFirstElementChild'
| 'getLastChild'
| 'getLastElementChild'
>
]
> = [
['children', 'getChildren'],
['childNodes', 'getChildNodes'],
['firstChild', 'getFirstChild'],
['firstElementChild', 'getFirstElementChild'],
['lastChild', 'getLastChild'],
['lastElementChild', 'getLastElementChild'],
];

That said, I don't have a strong opinion, because gzip will remove a lot of the duplication, and maybe it's not worth all the TypeScript gymnastics.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also similar duplication below for querySelector et al.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, the main issue is the renaming of the methods... if we rename the methods in renderer.* then this becomes an easy step here, but that's not the case right now.

render(): Template {
const vm = getAssociatedVM(this);
return vm.def.template;
Expand All @@ -480,84 +556,25 @@ LightningElement.prototype = {

const queryAndChildGetterDescriptors: PropertyDescriptorMap = create(null);

// The reason we don't just call `import * as renderer from '../renderer'` here is that the bundle size
// is smaller if we reference each function individually. Otherwise Rollup will create one big frozen
// object representing the renderer, with a lot of methods we don't actually need.
const childGetters = [
'children',
'childNodes',
'firstChild',
'firstElementChild',
'lastChild',
'lastElementChild',
] as const;

function getChildGetter(methodName: typeof childGetters[number]) {

This comment was marked as resolved.

This comment was marked as resolved.

switch (methodName) {
case 'children':
return getChildren;
case 'childNodes':
return getChildNodes;
case 'firstChild':
return getFirstChild;
case 'firstElementChild':
return getFirstElementChild;
case 'lastChild':
return getLastChild;
case 'lastElementChild':
return getLastElementChild;
}
}

// Generic passthrough for child getters on HTMLElement to the relevant Renderer APIs
for (const childGetter of childGetters) {
queryAndChildGetterDescriptors[childGetter] = {
get(this: LightningElement) {
const vm = getAssociatedVM(this);
const { elm } = vm;

if (process.env.NODE_ENV !== 'production') {
warnIfInvokedDuringConstruction(vm, childGetter);
}

return getChildGetter(childGetter)(elm);
},
configurable: true,
enumerable: true,
};
}

const queryMethods = [
'getElementsByClassName',
'getElementsByTagName',
'querySelector',
'querySelectorAll',
] as const;
function getQueryMethod(methodName: typeof queryMethods[number]) {
switch (methodName) {
case 'getElementsByClassName':
return getElementsByClassName;
case 'getElementsByTagName':
return getElementsByTagName;
case 'querySelector':
return querySelector;
case 'querySelectorAll':
return querySelectorAll;
}
}

// Generic passthrough for query APIs on HTMLElement to the relevant Renderer APIs
for (const queryMethod of queryMethods) {
queryAndChildGetterDescriptors[queryMethod] = {
value(this: LightningElement, arg: string) {
const vm = getAssociatedVM(this);
const { elm } = vm;
const { elm, renderer } = vm;

if (process.env.NODE_ENV !== 'production') {
warnIfInvokedDuringConstruction(vm, `${queryMethod}()`);
}

return getQueryMethod(queryMethod)(elm, arg);
return renderer[queryMethod](elm, arg);
},
configurable: true,
enumerable: true,
Expand Down
Loading