|
| 1 | +import { Injectable } from "@angular/core"; |
| 2 | +import { RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle } from "@angular/router"; |
| 3 | + |
| 4 | +import { routeReuseStrategyLog as log } from "../trace"; |
| 5 | +import { NSLocationStrategy } from "./ns-location-strategy"; |
| 6 | +import { pageRouterActivatedSymbol, destroyComponentRef } from "./page-router-outlet"; |
| 7 | + |
| 8 | +interface CacheItem { |
| 9 | + key: string; |
| 10 | + state: DetachedRouteHandle; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Detached state cache |
| 15 | + */ |
| 16 | +class DetachedStateCache { |
| 17 | + private cache = new Array<CacheItem>(); |
| 18 | + |
| 19 | + public get length(): number { |
| 20 | + return this.cache.length; |
| 21 | + } |
| 22 | + |
| 23 | + public push(cacheItem: CacheItem) { |
| 24 | + this.cache.push(cacheItem); |
| 25 | + } |
| 26 | + |
| 27 | + public pop(): CacheItem { |
| 28 | + return this.cache.pop(); |
| 29 | + } |
| 30 | + |
| 31 | + public peek(): CacheItem { |
| 32 | + return this.cache[this.cache.length - 1]; |
| 33 | + } |
| 34 | + |
| 35 | + public clear() { |
| 36 | + log(`DetachedStateCache.clear() ${this.cache.length} items will be destroyed`); |
| 37 | + |
| 38 | + while (this.cache.length > 0) { |
| 39 | + const state = <any>this.cache.pop().state; |
| 40 | + if (!state.componentRef) { |
| 41 | + throw new Error("No componentRed found in DetachedRouteHandle"); |
| 42 | + } |
| 43 | + |
| 44 | + destroyComponentRef(state.componentRef); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Does not detach any subtrees. Reuses routes as long as their route config is the same. |
| 51 | + */ |
| 52 | +@Injectable() |
| 53 | +export class NsRouteReuseStrategy implements RouteReuseStrategy { |
| 54 | + private cache: DetachedStateCache = new DetachedStateCache(); |
| 55 | + |
| 56 | + constructor(private location: NSLocationStrategy) { } |
| 57 | + |
| 58 | + shouldDetach(route: ActivatedRouteSnapshot): boolean { |
| 59 | + const key = getSnapshotKey(route); |
| 60 | + const isPageActivated = route[pageRouterActivatedSymbol]; |
| 61 | + const isBack = this.location._isPageNavigatingBack(); |
| 62 | + const shouldDetach = !isBack && isPageActivated; |
| 63 | + |
| 64 | + log(`shouldDetach isBack: ${isBack} key: ${key} result: ${shouldDetach}`); |
| 65 | + |
| 66 | + return shouldDetach; |
| 67 | + } |
| 68 | + |
| 69 | + shouldAttach(route: ActivatedRouteSnapshot): boolean { |
| 70 | + const key = getSnapshotKey(route); |
| 71 | + const isBack = this.location._isPageNavigatingBack(); |
| 72 | + const shouldDetach = isBack && this.cache.peek().key === key; |
| 73 | + |
| 74 | + log(`shouldAttach isBack: ${isBack} key: ${key} result: ${shouldDetach}`); |
| 75 | + |
| 76 | + return shouldDetach; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + store(route: ActivatedRouteSnapshot, state: DetachedRouteHandle): void { |
| 81 | + const key = getSnapshotKey(route); |
| 82 | + log(`store key: ${key}, state: ${state}`); |
| 83 | + |
| 84 | + if (state) { |
| 85 | + this.cache.push({ key, state }); |
| 86 | + } else { |
| 87 | + const topItem = this.cache.peek(); |
| 88 | + if (topItem.key === key) { |
| 89 | + this.cache.pop(); |
| 90 | + } else { |
| 91 | + throw new Error("Trying to pop from DetachedStateCache but keys don't match. " + |
| 92 | + `expected: ${topItem.key} actual: ${key}`); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle | null { |
| 98 | + const key = getSnapshotKey(route); |
| 99 | + const isBack = this.location._isPageNavigatingBack(); |
| 100 | + const cachedItem = this.cache.peek(); |
| 101 | + |
| 102 | + let state = null; |
| 103 | + if (isBack && cachedItem && cachedItem.key === key) { |
| 104 | + state = cachedItem.state; |
| 105 | + } |
| 106 | + |
| 107 | + log(`retrieved isBack: ${isBack} key: ${key} state: ${state}`); |
| 108 | + |
| 109 | + return state; |
| 110 | + } |
| 111 | + |
| 112 | + shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean { |
| 113 | + const shouldReuse = future.routeConfig === curr.routeConfig; |
| 114 | + |
| 115 | + if (shouldReuse && curr && curr[pageRouterActivatedSymbol]) { |
| 116 | + // When reusing route - copy the pageRouterActivated to the new snapshot |
| 117 | + // Its needed in shouldDetach to determine if the route should be detached. |
| 118 | + future[pageRouterActivatedSymbol] = curr[pageRouterActivatedSymbol]; |
| 119 | + } |
| 120 | + |
| 121 | + log(`shouldReuseRoute result: ${shouldReuse}`); |
| 122 | + |
| 123 | + return shouldReuse; |
| 124 | + } |
| 125 | + |
| 126 | + clearCache() { |
| 127 | + this.cache.clear(); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +function getSnapshotKey(snapshot: ActivatedRouteSnapshot): string { |
| 132 | + return snapshot.pathFromRoot.join("->"); |
| 133 | +} |
0 commit comments