Skip to content

Commit

Permalink
fix(runtime): handle lazy-instance promises for connected & disconnec…
Browse files Browse the repository at this point in the history
…ted callbacks
  • Loading branch information
khanhduy1407 committed Dec 3, 2023
1 parent e35899f commit f9cf35f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
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$));
}
}
};

0 comments on commit f9cf35f

Please sign in to comment.