From 45249c1c4ba47dceb8be8506bc85ac9da93be018 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 2 May 2019 15:43:01 +0200 Subject: [PATCH] feat: type components for a route record --- src/router.ts | 49 ++++++++++++++++++++++++++-------------------- src/types/index.ts | 16 ++++++++++++--- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/router.ts b/src/router.ts index 7419763c..0c4922ef 100644 --- a/src/router.ts +++ b/src/router.ts @@ -103,16 +103,20 @@ export class Router { // TODO: ensure we are leaving since we could just be changing params or not changing anything // TODO: is it okay to resolve all matched component or should we do it in order await Promise.all( - from.matched.map(async ({ component }) => { + from.matched.map(async record => { // TODO: cache async routes per record - const resolvedComponent = await (typeof component === 'function' - ? component() - : component) - if (resolvedComponent.beforeRouteLeave) { - // TODO: handle the next callback - guards.push( - guardToPromiseFn(resolvedComponent.beforeRouteLeave, to, from) - ) + // TODO: handle components version. Probably repfactor in extractComponentGuards + if ('component' in record) { + const { component } = record + const resolvedComponent = await (typeof component === 'function' + ? component() + : component) + if (resolvedComponent.beforeRouteLeave) { + // TODO: handle the next callback + guards.push( + guardToPromiseFn(resolvedComponent.beforeRouteLeave, to, from) + ) + } } }) ) @@ -157,18 +161,21 @@ export class Router { await Promise.all( to.matched.map(async record => { // TODO: cache async routes per record - const { component } = record - const resolvedComponent = await (typeof component === 'function' - ? component() - : component) - if ( - resolvedComponent.beforeRouteEnter && - from.matched.indexOf(record) - ) { - // TODO: handle the next callback - guards.push( - guardToPromiseFn(resolvedComponent.beforeRouteEnter, to, from) - ) + // TODO: handle components version. Probably repfactor in extractComponentGuards + if ('component' in record) { + const { component } = record + const resolvedComponent = await (typeof component === 'function' + ? component() + : component) + if ( + resolvedComponent.beforeRouteEnter && + from.matched.indexOf(record) + ) { + // TODO: handle the next callback + guards.push( + guardToPromiseFn(resolvedComponent.beforeRouteEnter, to, from) + ) + } } }) ) diff --git a/src/types/index.ts b/src/types/index.ts index 59f84558..c170af8f 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -80,20 +80,30 @@ export interface RouteComponentInterface { // export type RouteComponent = TODO & RouteComponentInterface export type RouteComponent = { template?: string + render?: Function } & RouteComponentInterface // NOTE not sure the whole PropsTransformer thing can be usefull // since in callbacks we don't know where we are coming from // and I don't thin it's possible to filter out the route // by any means -export interface RouteRecord { + +interface RouteRecordCommon { path: string // | RegExp - component: RouteComponent | Lazy name?: string beforeEnter?: NavigationGuard - // props: PT } +interface RouteRecordSingleView extends RouteRecordCommon { + component: RouteComponent | Lazy +} + +interface RouteRecordMultipleViews extends RouteRecordCommon { + components: Record> +} + +export type RouteRecord = RouteRecordSingleView | RouteRecordMultipleViews + export const START_RECORD: RouteRecord = { path: '/', // @ts-ignore -- 2.39.5