95cbb515d45ccb58ecf0e5ab6255ad63ee4ebadd1a0ec5329a99abab5ed486b155c5a0f5ad371615c2dea7a23e461e5da9a6e0b996937508d6e2b9e29ab08c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* @flow */
  2. import type Router from '../index'
  3. import { History } from './base'
  4. import { NavigationFailureType, isNavigationFailure } from '../util/errors'
  5. export class AbstractHistory extends History {
  6. index: number
  7. stack: Array<Route>
  8. constructor (router: Router, base: ?string) {
  9. super(router, base)
  10. this.stack = []
  11. this.index = -1
  12. }
  13. push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
  14. this.transitionTo(
  15. location,
  16. route => {
  17. this.stack = this.stack.slice(0, this.index + 1).concat(route)
  18. this.index++
  19. onComplete && onComplete(route)
  20. },
  21. onAbort
  22. )
  23. }
  24. replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
  25. this.transitionTo(
  26. location,
  27. route => {
  28. this.stack = this.stack.slice(0, this.index).concat(route)
  29. onComplete && onComplete(route)
  30. },
  31. onAbort
  32. )
  33. }
  34. go (n: number) {
  35. const targetIndex = this.index + n
  36. if (targetIndex < 0 || targetIndex >= this.stack.length) {
  37. return
  38. }
  39. const route = this.stack[targetIndex]
  40. this.confirmTransition(
  41. route,
  42. () => {
  43. const prev = this.current
  44. this.index = targetIndex
  45. this.updateRoute(route)
  46. this.router.afterHooks.forEach(hook => {
  47. hook && hook(route, prev)
  48. })
  49. },
  50. err => {
  51. if (isNavigationFailure(err, NavigationFailureType.duplicated)) {
  52. this.index = targetIndex
  53. }
  54. }
  55. )
  56. }
  57. getCurrentLocation () {
  58. const current = this.stack[this.stack.length - 1]
  59. return current ? current.fullPath : '/'
  60. }
  61. ensureURL () {
  62. // noop
  63. }
  64. }