Skip to content

Commit f3788c0

Browse files
committed
fix: use native connected callback
1 parent c97472d commit f3788c0

File tree

1 file changed

+7
-59
lines changed

1 file changed

+7
-59
lines changed

packages/@lwc/engine-dom/src/apis/create-element.ts

Lines changed: 7 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@
44
* SPDX-License-Identifier: MIT
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
7-
import {
8-
assert,
9-
assign,
10-
isFunction,
11-
isNull,
12-
isObject,
13-
isUndefined,
14-
toString,
15-
StringToLowerCase,
16-
} from '@lwc/shared';
7+
import { isFunction, isNull, isObject, toString, StringToLowerCase } from '@lwc/shared';
178
import {
189
createVM,
1910
connectRootElement,
@@ -22,53 +13,6 @@ import {
2213
} from '@lwc/engine-core';
2314
import { getUpgradableElement } from '../renderer';
2415

25-
// TODO [#2472]: Remove this workaround when appropriate.
26-
// eslint-disable-next-line lwc-internal/no-global-node
27-
const _Node = Node;
28-
29-
type NodeSlotCallback = (element: Node) => void;
30-
31-
const ConnectingSlot = new WeakMap<any, NodeSlotCallback>();
32-
const DisconnectingSlot = new WeakMap<any, NodeSlotCallback>();
33-
34-
function callNodeSlot(node: Node, slot: WeakMap<any, NodeSlotCallback>): Node {
35-
if (process.env.NODE_ENV !== 'production') {
36-
assert.isTrue(node, `callNodeSlot() should not be called for a non-object`);
37-
}
38-
39-
const fn = slot.get(node);
40-
41-
if (!isUndefined(fn)) {
42-
fn(node);
43-
}
44-
45-
return node; // for convenience
46-
}
47-
48-
// Monkey patching Node methods to be able to detect the insertions and removal of root elements
49-
// created via createElement.
50-
const { appendChild, insertBefore, removeChild, replaceChild } = _Node.prototype;
51-
assign(_Node.prototype, {
52-
appendChild(newChild) {
53-
const appendedNode = appendChild.call(this, newChild);
54-
return callNodeSlot(appendedNode, ConnectingSlot);
55-
},
56-
insertBefore(newChild, referenceNode) {
57-
const insertedNode = insertBefore.call(this, newChild, referenceNode);
58-
return callNodeSlot(insertedNode, ConnectingSlot);
59-
},
60-
removeChild(oldChild) {
61-
const removedNode = removeChild.call(this, oldChild);
62-
return callNodeSlot(removedNode, DisconnectingSlot);
63-
},
64-
replaceChild(newChild, oldChild) {
65-
const replacedNode = replaceChild.call(this, newChild, oldChild);
66-
callNodeSlot(replacedNode, DisconnectingSlot);
67-
callNodeSlot(newChild, ConnectingSlot);
68-
return replacedNode;
69-
},
70-
} as Pick<Node, 'appendChild' | 'insertBefore' | 'removeChild' | 'replaceChild'>);
71-
7216
/**
7317
* EXPERIMENTAL: This function is almost identical to document.createElement with the slightly
7418
* difference that in the options, you can pass the `is` property set to a Constructor instead of
@@ -124,10 +68,14 @@ export function createElement(
12468
mode: options.mode !== 'closed' ? 'open' : 'closed',
12569
owner: null,
12670
});
127-
ConnectingSlot.set(elm, connectRootElement);
128-
DisconnectingSlot.set(elm, disconnectRootElement);
12971
wasComponentUpgraded = true;
13072
}
73+
connectedCallback() {
74+
connectRootElement(this);
75+
}
76+
disconnectedCallback() {
77+
disconnectRootElement(this);
78+
}
13179
}
13280
const element = new UpgradableConstructor(UserElement.prototype.constructor);
13381
if (!wasComponentUpgraded) {

0 commit comments

Comments
 (0)