Adjust lifecycleHooks.setup to allow async callbacks (#2388)
This commit is contained in:
parent
09a946574b
commit
f604575b41
@ -11,8 +11,8 @@ type ComponentAPIDestroy<T> = (api: T) => void;
|
|||||||
type SetLifecycleHooksAction<T> = (api: T) => void;
|
type SetLifecycleHooksAction<T> = (api: T) => void;
|
||||||
|
|
||||||
export interface LifecycleHooks<T> {
|
export interface LifecycleHooks<T> {
|
||||||
onMount(callback: ComponentAPIMount<T>): Callback;
|
onMount(callback: ComponentAPIMount<T>): Callback | Promise<Callback>;
|
||||||
onDestroy(callback: ComponentAPIDestroy<T>): Callback;
|
onDestroy(callback: ComponentAPIDestroy<T>): Callback | Promise<Callback>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,22 +27,25 @@ function lifecycleHooks<T>(): [LifecycleHooks<T>, T[], SetLifecycleHooksAction<T
|
|||||||
|
|
||||||
function setup(api: T): void {
|
function setup(api: T): void {
|
||||||
svelteOnMount(() => {
|
svelteOnMount(() => {
|
||||||
const cleanups: Callback[] = [];
|
const cleanups: Promise<void | Callback>[] = [];
|
||||||
|
|
||||||
for (const callback of mountCallbacks) {
|
for (const mountCallback of mountCallbacks) {
|
||||||
const cleanup = callback(api);
|
// Promise.resolve doesn't care whether it's a promise or sync callback
|
||||||
|
cleanups.push(
|
||||||
if (cleanup) {
|
Promise.resolve(mountCallback).then((callback) => {
|
||||||
cleanups.push(cleanup);
|
return callback(api);
|
||||||
}
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// onMount seems to be called in reverse order
|
// onMount seems to be called in reverse order
|
||||||
instances.unshift(api);
|
instances.unshift(api);
|
||||||
|
|
||||||
return () => {
|
return async () => {
|
||||||
for (const cleanup of cleanups) {
|
for (const cleanup of await Promise.all(cleanups)) {
|
||||||
cleanup();
|
if (cleanup) {
|
||||||
|
cleanup();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@ -50,8 +53,10 @@ function lifecycleHooks<T>(): [LifecycleHooks<T>, T[], SetLifecycleHooksAction<T
|
|||||||
svelteOnDestroy(() => {
|
svelteOnDestroy(() => {
|
||||||
removeItem(instances, api);
|
removeItem(instances, api);
|
||||||
|
|
||||||
for (const callback of destroyCallbacks) {
|
for (const destroyCallback of destroyCallbacks) {
|
||||||
callback(api);
|
Promise.resolve(destroyCallback).then((callback) => {
|
||||||
|
callback(api);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user