return a Promise from goto - fixes #270

This commit is contained in:
Rich Harris
2018-06-27 17:32:20 -04:00
parent f821c19528
commit 57eeb5659a
3 changed files with 15 additions and 7 deletions

View File

@@ -133,7 +133,7 @@ function prepare_route(Page: ComponentConstructor, props: RouteData) {
});
}
function navigate(target: Target, id: number) {
function navigate(target: Target, id: number): Promise<any> {
if (id) {
// popstate or initial navigation
cid = id;
@@ -299,13 +299,17 @@ export function init(opts: { App: ComponentConstructor, target: Node, routes: Ro
export function goto(href: string, opts = { replaceState: false }) {
const target = select_route(new URL(href, document.baseURI));
let promise;
if (target) {
navigate(target, null);
promise = navigate(target, null);
if (history) history[opts.replaceState ? 'replaceState' : 'pushState']({ id: cid }, '', href);
} else {
window.location.href = href;
promise = new Promise(f => {}); // never resolves
}
return promise;
}
export function prefetchRoutes(pathnames: string[]) {