Skip to content

Commit ee4aa0b

Browse files
dwirzDominique Wirz
and
Dominique Wirz
authoredDec 9, 2024
fix: Change hasHostListenerAttached from var to protoype property (#6074)
* fix: Change hasHostListenerAttached from var to protoype property * prefix property with __ to indicate internal usage --------- Co-authored-by: Dominique Wirz <dominique@smartive.ch>
1 parent 6c302ec commit ee4aa0b

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed
 

‎src/runtime/bootstrap-custom-element.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ export const proxyCustomElement = (Cstr: any, compactMeta: d.ComponentRuntimeMet
6767

6868
const originalConnectedCallback = Cstr.prototype.connectedCallback;
6969
const originalDisconnectedCallback = Cstr.prototype.disconnectedCallback;
70-
let hasHostListenerAttached = false;
7170
Object.assign(Cstr.prototype, {
71+
__hasHostListenerAttached: false,
7272
__registerHost() {
7373
registerHost(this, cmpMeta);
7474
},
7575
connectedCallback() {
76-
if (!hasHostListenerAttached) {
76+
if (!this.__hasHostListenerAttached) {
7777
const hostRef = getHostRef(this);
7878
addHostEventListeners(this, hostRef, cmpMeta.$listeners$, false);
79-
hasHostListenerAttached = true;
79+
this.__hasHostListenerAttached = true;
8080
}
8181

8282
connectedCallback(this);

‎test/wdio/event-listener-capture/event-re-register.test.tsx

+68
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,72 @@ describe('event-listener-capture using lazy load components', function () {
4444
// check if event fired 6 times
4545
await expect(reattach).toHaveText(expect.stringContaining('Event fired times: 6'));
4646
});
47+
48+
it('should attach keydown event listener once per component', async () => {
49+
const elem = document.createElement('event-re-register') as HTMLElement;
50+
elem.setAttribute('id', 'elem1');
51+
document.body.appendChild(elem);
52+
53+
const reattach = $('#elem1');
54+
await expect(reattach).toBePresent();
55+
56+
// focus on element 1
57+
await reattach.click();
58+
await browser.action('key').down('a').pause(100).up('a').perform();
59+
await browser.action('key').down('a').pause(100).up('a').perform();
60+
await browser.action('key').down('a').pause(100).up('a').perform();
61+
62+
// check if event fired 3 times on first element
63+
await expect(reattach).toHaveText(expect.stringContaining('Event fired times: 3'), {
64+
message: 'Second element should have fired 3 times',
65+
});
66+
67+
const elem2 = document.createElement('event-re-register') as HTMLElement;
68+
elem2.setAttribute('id', 'elem2');
69+
document.body.appendChild(elem2);
70+
71+
const reattach2 = $('#elem2');
72+
await expect(reattach2).toBePresent();
73+
74+
// focus on element 2
75+
await reattach2.click();
76+
await browser.action('key').down('a').pause(100).up('a').perform();
77+
await browser.action('key').down('a').pause(100).up('a').perform();
78+
await browser.action('key').down('a').pause(100).up('a').perform();
79+
80+
// check if event fired 3 times on second element
81+
await expect(reattach2).toHaveText(expect.stringContaining('Event fired times: 3'), {
82+
message: 'Second element should have fired 3 times',
83+
});
84+
85+
// remove node from DOM
86+
elem.remove();
87+
elem2.remove();
88+
89+
// reattach node to DOM
90+
document.body.appendChild(elem);
91+
document.body.appendChild(elem2);
92+
93+
// retrigger event
94+
await reattach.click();
95+
await browser.action('key').down('a').pause(100).up('a').perform();
96+
await browser.action('key').down('a').pause(100).up('a').perform();
97+
await browser.action('key').down('a').pause(100).up('a').perform();
98+
99+
// check if event fired 6 times on first element
100+
await expect(reattach).toHaveText(expect.stringContaining('Event fired times: 6'), {
101+
message: 'First element should have fired 3 times',
102+
});
103+
104+
// retrigger event on element 2
105+
await reattach2.click();
106+
await browser.action('key').down('a').pause(100).up('a').perform();
107+
await browser.action('key').down('a').pause(100).up('a').perform();
108+
await browser.action('key').down('a').pause(100).up('a').perform();
109+
110+
// check if event fired 3 times on second element
111+
await expect(reattach2).toHaveText(expect.stringContaining('Event fired times: 6'), {
112+
message: 'Second element should have fired 3 times',
113+
});
114+
});
47115
});

0 commit comments

Comments
 (0)
Please sign in to comment.