]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: avoid double filtering of arrays for guards
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 24 Aug 2020 13:21:34 +0000 (15:21 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 24 Aug 2020 13:21:34 +0000 (15:21 +0200)
src/router.ts

index 31d702476dfbbd593c9d69124b19c03549319074..4ab4496a29090cd9c0605abbc3ad8fb978fffbdd 100644 (file)
@@ -566,20 +566,20 @@ export function createRouter(options: RouterOptions): Router {
   ): Promise<any> {
     let guards: Lazy<any>[]
 
+    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