From: Eduardo San Martin Morote Date: Mon, 24 Aug 2020 13:21:34 +0000 (+0200) Subject: refactor: avoid double filtering of arrays for guards X-Git-Tag: v4.0.0-beta.8~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f7ccde04348cabc8753fab98a5a925c86ddda18f;p=thirdparty%2Fvuejs%2Frouter.git refactor: avoid double filtering of arrays for guards --- diff --git a/src/router.ts b/src/router.ts index 31d70247..4ab4496a 100644 --- a/src/router.ts +++ b/src/router.ts @@ -566,20 +566,20 @@ export function createRouter(options: RouterOptions): Router { ): Promise { let guards: Lazy[] + const [ + leavingRecords, + updatingRecords, + enteringRecords, + ] = extractChangingRecords(to, from) + // all components here have been resolved once because we are leaving guards = extractComponentsGuards( - from.matched.filter(record => to.matched.indexOf(record) < 0).reverse(), + leavingRecords.reverse(), 'beforeRouteLeave', to, from ) - const [ - leavingRecords, - updatingRecords, - // enteringRecords, - ] = extractChangingRecords(to, from) - for (const record of leavingRecords) { for (const guard of record.leaveGuards) { guards.push(guardToPromiseFn(guard, to, from)) @@ -610,9 +610,7 @@ export function createRouter(options: RouterOptions): Router { .then(() => { // check in components beforeRouteUpdate guards = extractComponentsGuards( - to.matched.filter( - record => from.matched.indexOf(record as any) > -1 - ), + updatingRecords, 'beforeRouteUpdate', to, from @@ -655,10 +653,7 @@ export function createRouter(options: RouterOptions): Router { // check in-component beforeRouteEnter guards = extractComponentsGuards( - // the type doesn't matter as we are comparing an object per reference - to.matched.filter( - record => from.matched.indexOf(record as any) < 0 - ), + enteringRecords, 'beforeRouteEnter', to, from