]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: rename distance to delta
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 28 Apr 2020 15:30:38 +0000 (17:30 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 28 Apr 2020 15:30:38 +0000 (17:30 +0200)
src/history/common.ts
src/history/html5.ts
src/history/memory.ts
src/router.ts

index 2992e4bba0c300c45a90b491eed8991af1adf5ee..f5a6abef4055530cecf56353e057b16ab32aac8d 100644 (file)
@@ -38,7 +38,7 @@ export enum NavigationDirection {
 export interface NavigationInformation {
   type: NavigationType
   direction: NavigationDirection
-  distance: number
+  delta: number
 }
 
 export interface NavigationCallback {
@@ -110,12 +110,12 @@ export interface RouterHistory {
    * myHistory.go(1) // equivalent to window.history.forward()
    * ```
    *
-   * @param distance - distance to travel. If distance is \< 0, it will go back,
-   * if it's \> 0, it will go forward
+   * @param delta - distance to travel. If delta is \< 0, it will go back,
+   * if it's \> 0, it will go forward by that amount of entries.
    * @param triggerListeners - whether this should trigger listeners attached to
    * the history
    */
-  go(distance: number, triggerListeners?: boolean): void
+  go(delta: number, triggerListeners?: boolean): void
 
   /**
    * Attach a listener to the History implementation that is triggered when the
index 0a8b8a1432c82e427383f3543a0991ff8d63cc9f..6ec400daaa9a63e8eb0d80947b3dd0fc373b3b9e 100644 (file)
@@ -84,19 +84,19 @@ function useHistoryListeners(
     const deltaFromCurrent = fromState
       ? state.position - fromState.position
       : ''
-    const distance = deltaFromCurrent || 0
+    const delta = deltaFromCurrent || 0
     // console.log({ deltaFromCurrent })
-    // Here we could also revert the navigation by calling history.go(-distance)
+    // Here we could also revert the navigation by calling history.go(-delta)
     // this listener will have to be adapted to not trigger again and to wait for the url
     // to be updated before triggering the listeners. Some kind of validation function would also
     // need to be passed to the listeners so the navigation can be accepted
     // call all listeners
     listeners.forEach(listener => {
       listener(location.value, from, {
-        distance,
+        delta,
         type: NavigationType.pop,
-        direction: distance
-          ? distance > 0
+        direction: delta
+          ? delta > 0
             ? NavigationDirection.forward
             : NavigationDirection.back
           : NavigationDirection.unknown,
@@ -275,9 +275,9 @@ export default function createWebHistory(base?: string): RouterHistory {
     historyNavigation.location,
     historyNavigation.replace
   )
-  function go(distance: number, triggerListeners = true) {
+  function go(delta: number, triggerListeners = true) {
     if (!triggerListeners) historyListeners.pauseListeners()
-    history.go(distance)
+    history.go(delta)
   }
   const routerHistory: RouterHistory = {
     // it's overridden right after
index 02db6e3381c17bad3e6e0a15ab0cdbae0ad4f914..83f447f6d39b5c78e7f15c36adff9c0c2b9b8ce8 100644 (file)
@@ -37,14 +37,11 @@ export default function createMemoryHistory(base: string = ''): RouterHistory {
   function triggerListeners(
     to: HistoryLocationNormalized,
     from: HistoryLocationNormalized,
-    {
-      direction,
-      distance,
-    }: Pick<NavigationInformation, 'direction' | 'distance'>
+    { direction, delta }: Pick<NavigationInformation, 'direction' | 'delta'>
   ): void {
     const info: NavigationInformation = {
       direction,
-      distance,
+      delta,
       type: NavigationType.pop,
     }
     for (let callback of listeners) {
@@ -81,18 +78,18 @@ export default function createMemoryHistory(base: string = ''): RouterHistory {
       listeners = []
     },
 
-    go(distance, shouldTrigger = true) {
+    go(delta, shouldTrigger = true) {
       const from = this.location
       const direction: NavigationDirection =
-        // we are considering distance === 0 going forward, but in abstract mode
-        // using 0 for the distance doesn't make sense like it does in html5 where
+        // we are considering delta === 0 going forward, but in abstract mode
+        // using 0 for the delta doesn't make sense like it does in html5 where
         // it reloads the page
-        distance < 0 ? NavigationDirection.back : NavigationDirection.forward
-      position = Math.max(0, Math.min(position + distance, queue.length - 1))
+        delta < 0 ? NavigationDirection.back : NavigationDirection.forward
+      position = Math.max(0, Math.min(position + delta, queue.length - 1))
       if (shouldTrigger) {
         triggerListeners(this.location, from, {
           direction,
-          distance,
+          delta,
         })
       }
     },
index 7544fd53cd248e55da6a752436ce159a793de475..1d0702dd6533626ffc87c9f105596a4e2b927f31 100644 (file)
@@ -132,7 +132,7 @@ export interface Router {
   // merged
   back(): void
   forward(): void
-  go(distance: number): void
+  go(delta: number): void
 
   beforeEach(guard: NavigationGuardWithThis<undefined>): () => void
   beforeResolve(guard: NavigationGuardWithThis<undefined>): () => void
@@ -574,7 +574,7 @@ export function createRouter({
 
     if (isBrowser) {
       saveScrollPosition(
-        getScrollKey(from.fullPath, info.distance),
+        getScrollKey(from.fullPath, info.delta),
         computeScrollPosition()
       )
     }
@@ -595,7 +595,7 @@ export function createRouter({
           return error as NavigationFailure
         }
         if (error.type === ErrorTypes.NAVIGATION_GUARD_REDIRECT) {
-          history.go(-info.distance, false)
+          history.go(-info.delta, false)
           // the error is already handled by router.push we just want to avoid
           // logging the error
           pushWithRedirect(
@@ -607,7 +607,7 @@ export function createRouter({
           return Promise.reject()
         }
         // TODO: test on different browsers ensure consistent behavior
-        history.go(-info.distance, false)
+        history.go(-info.delta, false)
         // unrecognized error, transfer to the global handler
         return triggerError(error)
       })
@@ -622,7 +622,7 @@ export function createRouter({
           )
 
         // revert the navigation
-        if (failure) history.go(-info.distance, false)
+        if (failure) history.go(-info.delta, false)
 
         triggerAfterEach(
           toLocation as RouteLocationNormalizedLoaded,