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;
|
||||
|
||||
export interface LifecycleHooks<T> {
|
||||
onMount(callback: ComponentAPIMount<T>): Callback;
|
||||
onDestroy(callback: ComponentAPIDestroy<T>): Callback;
|
||||
onMount(callback: ComponentAPIMount<T>): Callback | Promise<Callback>;
|
||||
onDestroy(callback: ComponentAPIDestroy<T>): Callback | Promise<Callback>;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -27,31 +27,36 @@ function lifecycleHooks<T>(): [LifecycleHooks<T>, T[], SetLifecycleHooksAction<T
|
||||
|
||||
function setup(api: T): void {
|
||||
svelteOnMount(() => {
|
||||
const cleanups: Callback[] = [];
|
||||
const cleanups: Promise<void | Callback>[] = [];
|
||||
|
||||
for (const callback of mountCallbacks) {
|
||||
const cleanup = callback(api);
|
||||
|
||||
if (cleanup) {
|
||||
cleanups.push(cleanup);
|
||||
}
|
||||
for (const mountCallback of mountCallbacks) {
|
||||
// Promise.resolve doesn't care whether it's a promise or sync callback
|
||||
cleanups.push(
|
||||
Promise.resolve(mountCallback).then((callback) => {
|
||||
return callback(api);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// onMount seems to be called in reverse order
|
||||
instances.unshift(api);
|
||||
|
||||
return () => {
|
||||
for (const cleanup of cleanups) {
|
||||
return async () => {
|
||||
for (const cleanup of await Promise.all(cleanups)) {
|
||||
if (cleanup) {
|
||||
cleanup();
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
svelteOnDestroy(() => {
|
||||
removeItem(instances, api);
|
||||
|
||||
for (const callback of destroyCallbacks) {
|
||||
for (const destroyCallback of destroyCallbacks) {
|
||||
Promise.resolve(destroyCallback).then((callback) => {
|
||||
callback(api);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user