Skip to content

Commit

Permalink
[#74] Fix wrong typing caused in #72 (#75)
Browse files Browse the repository at this point in the history
* A word in README looks like a typo

* Make router lifecycle easier to use

* Export NextFunc

* Add an example of vue-router lifecycle

* Support `vm` in beforeRouteEnter lifecycle next function

* Support `vm` in beforeRouteLeave lifecycle next function

* Update README for more vue-router lifecycles

* Update README for better format

* Fix NextFuncVm boundary issue

* Merge beforeRouteUpdate & beforeRouteLeave

* Remove unnecessary

* Update README

* Optimize README

* Refer to vue-router's NavigationGuard definition

* Remove `NextFuncBool` from previous PR in README
  • Loading branch information
whitetrefoil authored and HerringtonDarkholme committed Jul 4, 2018
1 parent d0f9c83 commit d634d60
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ Lifecycle from `vue-router` is also supported as

```typescript
import {
Component, Lifecycle, NextFunc, NextFuncBool, NextFuncVm, p, Prop
Component, Lifecycle, NextFunc, NextFuncVm, p, Prop
} from 'av-ts'
import { Route } from 'vue-router'

Expand All @@ -323,21 +323,32 @@ import { Route } from 'vue-router'
export default class MyPage extends Vue {
@Prop id = p({ type: String, required: true })

// "beforeRouteEnter" can use "NextFuncVm<T>"
@Lifecycle
async beforeRouteEnter(to: Route, from: Route, next: NextFuncVm<MyPage>) {
next((vm) => {
console.log(vm.id)
})
if (normalCase) {
next()
} else if (redirection) {
next({ name: 'other-page' })
} else if (cancel) {
next(false)
} else if (needCallback) {
next((vm) => {
console.log(vm.id)
})
}
}


// "beforeRouteUpdate" & "beforeRouteLeave" can only use "NextFunc"
@Lifecycle
async beforeRouteUpdate(to: Route, from: Route, next: NextFunc) {
next()
}

@Lifecycle
async beforeRouteLeave(to: Route, from: Route, next: NextFuncBool) {
next(false)
if (normalCase) {
next()
} else if (redirection) {
next({ name: 'other-page' })
} else if (cancel) {
next(false)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Vue from 'vue'
import './src/global'

export {Component} from './src/core'
export {Lifecycle, NextFunc, NextFuncBool, NextFuncVm} from './src/lifecycle'
export {Lifecycle, NextFunc, NextFuncVm} from './src/lifecycle'
export {Prop, p} from './src/prop'
export {Render} from './src/render'
export {Transition} from './src/transition'
Expand Down
34 changes: 21 additions & 13 deletions src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Vue from 'vue'

import {Component} from './core'
import {$$Prop} from './interface'
import {createMap, ReadonlyPropertyDescriptor} from './util'
import {createMap, Map, ReadonlyPropertyDescriptor} from './util'

const LIFECYCLE_KEY = '$$Lifecycle' as $$Prop

Expand All @@ -13,22 +13,30 @@ export type Lifecycles =
'beforeUpdate' | 'updated' |
'activated' | 'deactivated'

export type BeforeRouteUpdateLifecycle = 'beforeRouteUpdate'
export type BeforeRouteLeaveLifecycle = 'beforeRouteLeave'
// Definitions of Location and RawLocation come from source of 'vue-router'
export interface Location {
name?: string;
path?: string;
hash?: string;
query?: Map<string>;
params?: Map<string>;
append?: boolean;
replace?: boolean;
}

export type RawLocation = string | Location;

export type RouterLifecycle = 'beforeRouteUpdate'|'beforeRouteLeave'
export type BeforeRouteEnterLifecycle = 'beforeRouteEnter'

export declare type NextFuncVmCallback<T extends Vue> = (vm: T) => void
export declare type NextFunc = () => void;
export declare type NextFuncBool = (ok?: false) => void;
export declare type NextFuncVm<T extends Vue = any> = (next?: NextFuncVmCallback<T>) => void;
export type BeforeRouteUpdateHandler = (to: any, from: any, next: NextFunc) => void
export type BeforeRouteLeaveHandler = (to: any, from: any, next: NextFuncBool) => void
export type BeforeRouteEnterHandler = (to: any, from: any, next: NextFuncVm) => void
export declare type NextFunc = (to?: RawLocation | false | void) => void;
export declare type NextFuncVm<T extends Vue> = (to?: RawLocation | false | ((vm: T) => any) | void) => void;
export type RouteHandler = (to: any, from: any, next: NextFunc) => void
export type RouteHandlerVm<T extends Vue> = (to: any, from: any, next: NextFuncVm<T>) => void

export function Lifecycle(target: Vue, life: Lifecycles, _: ReadonlyPropertyDescriptor<() => void>): void
export function Lifecycle(target: Vue, life: BeforeRouteUpdateLifecycle, _: ReadonlyPropertyDescriptor<BeforeRouteUpdateHandler>): void
export function Lifecycle(target: Vue, life: BeforeRouteLeaveLifecycle, _: ReadonlyPropertyDescriptor<BeforeRouteLeaveHandler>): void
export function Lifecycle(target: Vue, life: BeforeRouteEnterLifecycle, _: ReadonlyPropertyDescriptor<BeforeRouteEnterHandler>): void
export function Lifecycle(target: Vue, life: RouterLifecycle, _: ReadonlyPropertyDescriptor<RouteHandler>): void
export function Lifecycle<T extends Vue>(target: T, life: BeforeRouteEnterLifecycle, _: ReadonlyPropertyDescriptor<RouteHandlerVm<T>>): void
export function Lifecycle(target: Vue, life: string, _: ReadonlyPropertyDescriptor<(...args: any[]) => void>) {
let lifecycles = target[LIFECYCLE_KEY] = target[LIFECYCLE_KEY] || createMap()
lifecycles[life] = true
Expand Down

0 comments on commit d634d60

Please sign in to comment.