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

fix(runtime): handle lazy-instance promises for connected & disconnected callbacks #4072

Merged
merged 8 commits into from
Jul 26, 2023
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
6 changes: 5 additions & 1 deletion src/runtime/connected-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ export const connectedCallback = (elm: d.HostElement) => {
addHostEventListeners(elm, hostRef, cmpMeta.$listeners$, false);

// fire off connectedCallback() on component instance
fireConnectedCallback(hostRef.$lazyInstance$);
if (hostRef?.$lazyInstance$) {
fireConnectedCallback(hostRef.$lazyInstance$);
} else if (hostRef?.$onReadyPromise$) {
hostRef.$onReadyPromise$.then(() => fireConnectedCallback(hostRef.$lazyInstance$));
}
}

endConnected();
Expand Down
23 changes: 16 additions & 7 deletions src/runtime/disconnected-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ import type * as d from '../declarations';
import { PLATFORM_FLAGS } from './runtime-constants';
import { safeCall } from './update-component';

export const disconnectedCallback = (elm: d.HostElement) => {
const disconnectInstance = (instance: any) => {
if (BUILD.lazyLoad && BUILD.disconnectedCallback) {
safeCall(instance, 'disconnectedCallback');
}
if (BUILD.cmpDidUnload) {
safeCall(instance, 'componentDidUnload');
}
};

export const disconnectedCallback = async (elm: d.HostElement) => {
if ((plt.$flags$ & PLATFORM_FLAGS.isTmpDisconnected) === 0) {
const hostRef = getHostRef(elm);
const instance: any = BUILD.lazyLoad ? hostRef.$lazyInstance$ : elm;

if (BUILD.hostListener) {
if (hostRef.$rmListeners$) {
Expand All @@ -17,11 +25,12 @@ export const disconnectedCallback = (elm: d.HostElement) => {
}
}

if (BUILD.lazyLoad && BUILD.disconnectedCallback) {
safeCall(instance, 'disconnectedCallback');
}
if (BUILD.cmpDidUnload) {
safeCall(instance, 'componentDidUnload');
if (!BUILD.lazyLoad) {
disconnectInstance(elm);
} else if (hostRef?.$lazyInstance$) {
disconnectInstance(hostRef.$lazyInstance$);
} else if (hostRef?.$onReadyPromise$) {
hostRef.$onReadyPromise$.then(() => disconnectInstance(hostRef.$lazyInstance$));
}
}
};
74 changes: 74 additions & 0 deletions src/runtime/test/lifecycle-sync.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,78 @@ describe('lifecycle sync', () => {
</cmp-root>
`);
});

it('call disconnectedCallback even if the element is immediately removed', async () => {
let connected = 0;
let disconnected = 0;

@Component({ tag: 'cmp-a' })
class CmpA {
connectedCallback() {
connected++;
}

disconnectedCallback() {
disconnected++;
}

render() {
return <Host></Host>;
}
}

const { doc, waitForChanges } = await newSpecPage({
components: [CmpA],
});

const a1 = doc.createElement('cmp-a');
doc.body.appendChild(a1);
a1.remove();

await waitForChanges();

expect(connected).toEqual(1);
expect(disconnected).toEqual(1);
});

it('calls disconnect and connect when an element is moved in the DOM', async () => {
let connected = 0;
let disconnected = 0;

@Component({ tag: 'cmp-a' })
class CmpA {
connectedCallback() {
connected++;
}

disconnectedCallback() {
disconnected++;
}

render() {
return <Host></Host>;
}
}

const { doc, waitForChanges } = await newSpecPage({
components: [CmpA],
});

const cmp = doc.createElement('cmp-a');
doc.body.appendChild(cmp);

await waitForChanges();

// Create a container we will move the component to
const container = doc.createElement('div');
doc.body.appendChild(container);

// Move the component
container.appendChild(cmp);

container.remove();

expect(connected).toEqual(2);
expect(disconnected).toEqual(2);
});
});