From 34d7390b946644a128ab6fd03fd821a91fd4782c Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 21 Jul 2020 11:46:40 +0200 Subject: [PATCH] feat(guards): wip context support in multi apps --- e2e/multi-app/index.ts | 69 +++++++++++++++++++++++++++++++++++++---- e2e/specs/multi-app.js | 24 ++++++++++++++ src/navigationGuards.ts | 13 ++++++++ 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/e2e/multi-app/index.ts b/e2e/multi-app/index.ts index a8431bc1..3a975f32 100644 --- a/e2e/multi-app/index.ts +++ b/e2e/multi-app/index.ts @@ -1,15 +1,60 @@ -import { createRouter, createWebHistory } from '../../src' +import { createRouter, createWebHistory, onBeforeRouteUpdate } from '../../src' import { RouteComponent } from '../../src/types' -import { createApp, ref, watchEffect, App } from 'vue' +import { createApp, ref, watchEffect, App, inject } from 'vue' const Home: RouteComponent = { template: `
Home
`, } const User: RouteComponent = { - template: `
User {{ $route.params.id }}
`, + template: `
User {{ $route.params.id }}. Updated {{ count }}
`, + data: () => ({ count: 0 }), + + beforeRouteEnter(to, from, next) { + next(vm => { + // @ts-ignore + console.log('enter from ', vm.id) + // @ts-ignore + }) + }, + + beforeRouteUpdate(to, from, next) { + // this.count++ + next() + }, + + setup() { + const id = inject('id')! + + if (id !== 1) + onBeforeRouteUpdate(function (to, from, next) { + // @ts-ignore + console.log('update from ', id, this.id) + // @ts-ignore + // this.count++ + next() + }) + + return { id } + }, } +let looper = [1, 2, 3] + +const NamedViews: RouteComponent[] = looper.map(i => ({ + name: 'part-' + i, + + template: `
Part ${i}. Updated {{ count }}
`, + + data: () => ({ count: 0 }), + + beforeRouteUpdate(to, from, next) { + // @ts-ignore + // this.count++ + next() + }, +})) + // path popstate listeners to track the call count let activePopStateListeners = ref(0) let guardCallCount = ref(0) @@ -43,7 +88,19 @@ const router = createRouter({ history: createWebHistory('/' + __dirname), routes: [ { path: '/', component: Home }, - { path: '/users/:id', component: User }, + { + path: '/users/:id', + components: { + default: User, + ...NamedViews.reduce( + (routeComponents, component) => ({ + ...routeComponents, + [component.name!]: component, + }), + {} as Record + ), + }, + }, ], }) @@ -52,8 +109,6 @@ router.beforeEach((to, from, next) => { next() }) -let looper = [1, 2, 3] - let apps: Array = [null, null, null] looper.forEach((n, i) => { @@ -71,10 +126,12 @@ looper.forEach((n, i) => { + `, })) app.use(router) + app.provide('id', n) app.mount('#app-' + n) }) diff --git a/e2e/specs/multi-app.js b/e2e/specs/multi-app.js index 07fe8957..97b94f2d 100644 --- a/e2e/specs/multi-app.js +++ b/e2e/specs/multi-app.js @@ -97,4 +97,28 @@ module.exports = { .end() }, + + /** @type {import('nightwatch').NightwatchTest} */ + 'supports navigation guards context with multiple apps': function (browser) { + browser + .url(baseURL) + .assert.urlEquals(baseURL + '/') + + // mount multiple apps and expect to have one listener only + .click('#mount1') + .assert.containsText('#app-1 .home', 'Home') + // toggle multiple times + .click('#app-1 li:nth-child(2) a') + .assert.containsText('#app-1 .count', '0') + .click('#app-1 li:nth-child(3) a') + .assert.containsText('#app-1 .count', '1') + .click('#mount2') + .assert.containsText('#app-2 .user', 'User 2') + .click('#app-1 li:nth-child(2) a') + // first one keeps updating + .assert.containsText('#app-1 .count', '2') + // second app only updated once + .assert.containsText('#app-2 .count', '1') + .click('#mount3') + }, } diff --git a/src/navigationGuards.ts b/src/navigationGuards.ts index 53bee104..d1d61f34 100644 --- a/src/navigationGuards.ts +++ b/src/navigationGuards.ts @@ -260,3 +260,16 @@ function isRouteComponent( '__vccOpts' in component ) } + +/** + * 1. beforeRouteEnter callbacks + * 2. Dictionary of instances per view name + */ +export type GuardManagerEntry = [ + NavigationGuardNextCallback[], + Record +] + +export function createGuardManager() { + return new Map() +} -- 2.39.5