Skip to content

Commit 63bbb25

Browse files
committed
Simplify destructor flow
1 parent d3b8175 commit 63bbb25

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@westacks/vortex",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"license": "MIT",
55
"description": "Server-based routing for SPAs",
66
"author": "Dmytro Morozov <puny.flash@gmail.com>",

src/client.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { setPage, type Page } from "./page";
33
import { createRouter, destroyRouter } from "./router";
44

5-
type VortexDestructor<T> = (callback?: (app: Awaited<T>) => void) => void;
5+
type VortexDestructor = void | (() => void) | Promise<void | (() => void)>;
66

77
/**
88
* Initialize Vortex client.
@@ -12,7 +12,7 @@ type VortexDestructor<T> = (callback?: (app: Awaited<T>) => void) => void;
1212
* @param element Root element. Defaults to `#app`
1313
* @returns Destructor function
1414
*/
15-
export async function createVortex<T>(setup: (el: HTMLElement, page: Page, hydrate: boolean) => T, element: string|HTMLElement = "#app"): Promise<VortexDestructor<T>> {
15+
export async function createVortex(setup: (el: HTMLElement, page: Page, hydrate: boolean) => VortexDestructor, element: string|HTMLElement = "#app") {
1616
if (typeof element === 'string') {
1717
element = document.querySelector(element) as HTMLElement;
1818
}
@@ -26,10 +26,10 @@ export async function createVortex<T>(setup: (el: HTMLElement, page: Page, hydra
2626
setPage(page);
2727
createRouter();
2828

29-
const app = await setup(element, page, !!element.dataset?.ssr);
29+
const dispose = await setup(element, page, !!element.dataset?.ssr);
3030

31-
return (callback) => {
32-
callback instanceof Function && callback(app);
31+
return () => {
32+
dispose && dispose();
3333
destroyRouter();
3434
}
3535
}

0 commit comments

Comments
 (0)