(source: Resource<T>)
| 35 | * during navigation transitions and handling rollback recovery. |
| 36 | */ |
| 37 | export function routerResource<T>(source: Resource<T>): Resource<T> & {reload(): boolean} { |
| 38 | ngDevMode && assertInInjectionContext(routerResource); |
| 39 | const injector = inject(Injector); |
| 40 | const router = injector.get(Router); |
| 41 | |
| 42 | const {snapshot: snapshotSignal, frozenSnapshot} = createTransactionalSnapshot( |
| 43 | source, |
| 44 | router, |
| 45 | injector, |
| 46 | ); |
| 47 | |
| 48 | const res = resourceFromSnapshots(snapshotSignal) as Resource<T> & {reload(): boolean}; |
| 49 | |
| 50 | if (typeof (source as any).reload === 'function') { |
| 51 | res.reload = function (): boolean { |
| 52 | // If the resource is currently frozen (e.g., during an active navigation transition |
| 53 | // or while recovering from a cancelled navigation), we reject manual reload requests. |
| 54 | // Triggering a reload during an active navigation (where the resource may already be |
| 55 | // reactively loading new parameters behind the scenes) would disrupt the router's resource |
| 56 | // tracking for the transition. Similarly, during a rollback recovery, the router is |
| 57 | // already managing the resource reload to restore the previous state. |
| 58 | if (frozenSnapshot() !== null) { |
| 59 | return false; |
| 60 | } |
| 61 | return (source as any).reload(); |
| 62 | }; |
| 63 | } else { |
| 64 | res.reload = () => false; |
| 65 | } |
| 66 | |
| 67 | return res; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Creates a signal that tracks the resource snapshot and handles transactional behavior |
no test coverage detected