Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit e642093

Browse files
author
farfromrefug
committed
fix: agnostic resolveComponentElement and typings to fit all cases
1 parent 37a77b0 commit e642093

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/dom/navigation.ts

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ViewBase, View, NavigatedData, NavigationTransition, Frame, BackstackEntry, Application } from "@nativescript/core";
1+
import { ViewBase, View, NavigatedData, NavigationTransition, Frame, BackstackEntry, Application, Page } from "@nativescript/core";
22
import FrameElement from "./native/FrameElement";
33
import { createElement, DocumentNode, logger as log } from "./basicdom";
44
import PageElement from "./native/PageElement";
@@ -39,18 +39,28 @@ export function resolveTarget(viewSpec: ViewSpec): View {
3939
}
4040
return viewSpec?.nativeView;
4141
}
42+
// interface ComponentInstanceInfo<T = any> { element: NativeViewElementNode<View>, pageInstance: SvelteComponent<T> }
4243

43-
export interface ComponentInstanceInfo<T = any> { element: NativeViewElementNode<View>, pageInstance: SvelteComponent<T> }
44+
export interface ComponentInstanceInfo<T extends ViewBase = View, U = SvelteComponent> {
45+
element: NativeViewElementNode<T>;
46+
viewInstance: U;
47+
}
4448

45-
export function resolveComponentElement<T>(pageSpec: PageSpec<T>, props?: T): ComponentInstanceInfo<T> {
46-
let dummy = createElement('fragment', window.document as unknown as DocumentNode);
47-
let pageInstance = new pageSpec({ target: dummy, props: props });
48-
let element = dummy.firstElement() as NativeViewElementNode<View>;
49-
return { element, pageInstance }
49+
export function resolveComponentElement<T, U extends ViewBase = View>(viewSpec: typeof SvelteComponent<T>, props?: T): ComponentInstanceInfo<U, SvelteComponent<T>> {
50+
const dummy = createElement('fragment', window.document as any);
51+
const viewInstance = new viewSpec({ target: dummy, props });
52+
const element = dummy.firstElement() as NativeViewElementNode<U>;
53+
return { element, viewInstance };
5054
}
55+
// export function resolveComponentElement<T>(pageSpec: PageSpec<T>, props?: T): ComponentInstanceInfo<T> {
56+
// let dummy = createElement('fragment', window.document as unknown as DocumentNode);
57+
// let pageInstance = new pageSpec({ target: dummy, props });
58+
// let element = dummy.firstElement() as NativeViewElementNode<View>;
59+
// return { element, pageInstance }
60+
// }
5161

5262
export function navigate<T>(options: NavigationOptions<T>): SvelteComponent<T> {
53-
let { frame, page, props = {}, ...navOptions } = options;
63+
let { frame, page, props, ...navOptions } = options;
5464

5565
let targetFrame = resolveFrame(frame);
5666

@@ -61,7 +71,7 @@ export function navigate<T>(options: NavigationOptions<T>): SvelteComponent<T> {
6171
throw new Error("navigate requires page to be set to the svelte component class that implements the page or reference to a page element");
6272
}
6373

64-
let { element, pageInstance } = resolveComponentElement(page, props);
74+
let { element, viewInstance } = resolveComponentElement<T, Page>(page, props);
6575

6676
if (!(element instanceof PageElement))
6777
throw new Error("navigate requires a svelte component with a page element at the root");
@@ -74,7 +84,7 @@ export function navigate<T>(options: NavigationOptions<T>): SvelteComponent<T> {
7484
// will remove all set `navigatedFrom` while we are enumerating to actually send them
7585
setTimeout(() => {
7686
nativePage.off('navigatedFrom', handler);
77-
pageInstance?.$destroy();
87+
viewInstance?.$destroy();
7888
}, 0);
7989
}
8090
};
@@ -89,7 +99,7 @@ export function navigate<T>(options: NavigationOptions<T>): SvelteComponent<T> {
8999
create: () => nativePage
90100
});
91101

92-
return pageInstance;
102+
return viewInstance;
93103
}
94104

95105
export interface BackNavigationOptions {
@@ -151,7 +161,7 @@ export function showModal<T, U>(modalOptions: ShowModalOptions<U>): Promise<T> {
151161
if (resolved) return;
152162
resolved = true;
153163
try {
154-
componentInstanceInfo.pageInstance.$destroy(); //don't let an exception in destroy kill the promise callback
164+
componentInstanceInfo.viewInstance.$destroy(); //don't let an exception in destroy kill the promise callback
155165
} finally {
156166
resolve(result);
157167
}

0 commit comments

Comments
 (0)