Skip to content

Commit

Permalink
fix(packages): failure to return in some fast new cases results in no…
Browse files Browse the repository at this point in the history
… initialization . close #8
  • Loading branch information
Ohh-889 committed Sep 14, 2024
1 parent b29bceb commit cfe46ea
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/simple-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function createRouter({ beforeEach, initRoutes, mode, opt, getReactRoutes

let currentRoute = transformLocationToRoute(reactRouter.state.location, reactRouter.state.matches);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
let listeners: (() => void)[] = [];

reactRouter.getBlocker('beforeGuard', onBeforeRouteChange);
Expand Down Expand Up @@ -99,7 +100,7 @@ export function createRouter({ beforeEach, initRoutes, mode, opt, getReactRoutes
return true;
}
} else {
const finalRoute = to.matched[to.matched.length - 1];
const finalRoute = to.matched.at(-1);

const finalPath = getFullPath(finalRoute);

Expand Down Expand Up @@ -212,13 +213,9 @@ export function createRouter({ beforeEach, initRoutes, mode, opt, getReactRoutes
async function initReady(): Promise<boolean> {
return new Promise((resolved, reject) => {
init(currentRoute.fullPath, blockerOrJump)
.then(res => {
if (!res) {
reactRouter.initialize();
resolved(true);
} else {
reject(new Error('init failed'));
}
.then(() => {
reactRouter.initialize();
resolved(true);
})
.catch(e => {
reject(e);
Expand Down Expand Up @@ -291,9 +288,9 @@ export function createRouter({ beforeEach, initRoutes, mode, opt, getReactRoutes
}

function subscribe(listener: () => void) {
listeners = [...listeners, listener];
listeners = [listener];
return () => {
listeners = listeners.filter(l => l !== listener);
listeners = [];
};
}

Expand Down

0 comments on commit cfe46ea

Please sign in to comment.