Asynchronous listen handler problem
elderapo opened this issue · comments
This works:
import { createRouter, defineRoute } from "type-route";
const { listen } = createRouter({
new: defineRoute("/new"),
old: defineRoute("/old")
});
listen(nextRoute => {
if (nextRoute.name === "old") {
return false;
}
});
// or
listen(async nextRoute => {
if (nextRoute.name === "old") {
return false as false;
}
});
but this doesn't:
import { createRouter, defineRoute } from "type-route";
const { listen } = createRouter({
new: defineRoute("/new"),
old: defineRoute("/old")
});
listen(async nextRoute => { // Type 'Promise<boolean>' is not assignable to type 'Promise<false | void>'.
if (nextRoute.name === "old") {
return false;
}
});
I think it would be better if NavigationHandler
had the following signature:
type SyncOrAsync<T> = T | Promise<T>
type NavigationHandler<T> = (nextRoute: Route<T>) => SyncOrAsync<boolean>
This should fix this issue and make the API more straightforward. Basically, if we return true
from the handler it means that route is "acceptable" and we want to continue and if we return false
we want to suppress route change for any reason.
Nice catch! This should be fixed in version 0.3.4
by this commit a916cfc.