MCPcopy Create free account
hub / github.com/angular/angular / createTransactionalSnapshot

Function createTransactionalSnapshot

packages/router/src/router_resource.ts:104–173  ·  view source on GitHub ↗

* Creates a signal that tracks the resource snapshot and handles transactional behavior * (freezing during navigation and rollback recovery).

(
  source: Resource<T>,
  router: Router,
  injector: Injector,
)

Source from the content-addressed store, hash-verified

102 * (freezing during navigation and rollback recovery).
103 */
104function createTransactionalSnapshot<T>(
105 source: Resource<T>,
106 router: Router,
107 injector: Injector,
108): {
109 snapshot: Signal<ResourceSnapshot<T>>;
110 frozenSnapshot: Signal<ResourceSnapshot<T> | null>;
111} {
112 // Holds a snapshot of the resource to keep the UI masked (frozen) during pending navigations
113 // or while recovering from a cancelled navigation.
114 const frozenSnapshot = signal<ResourceSnapshot<T> | null>(null);
115
116 // Tracks whether we are in a recovery phase after a cancelled navigation.
117 // The intended behavior is that on cancellation, the router reverts to the previous state.
118 // This reversion might trigger a new load of the previous state because the signal dependencies
119 // changed. If we were to release the frozen resource state immediately, the user would see a loading state
120 // for data they were just looking at. To avoid this "loading flash", we retain the frozen
121 // value (via frozenSnapshot) during this recovery load/reload until the resource settles.
122 const isRollbackRecoveryPending = signal(false);
123
124 const sub = router.events.subscribe((e) => {
125 if (e instanceof NavigationStart) {
126 isRollbackRecoveryPending.set(false);
127
128 if (frozenSnapshot() === null) {
129 // Freeze the snapshot at the start of navigation to keep the UI stable.
130 frozenSnapshot.set(source.snapshot());
131 }
132 } else if (e instanceof NavigationEnd) {
133 // Navigation succeeded, so we can unfreeze and use the live state.
134 frozenSnapshot.set(null);
135 isRollbackRecoveryPending.set(false);
136 } else if (e instanceof NavigationSkipped) {
137 // If a navigation is skipped while we have a frozen snapshot (e.g. navigating to the
138 // current URL to cancel an in-flight navigation), the in-flight navigation is aborted
139 // and parameter rollback begins. We must maintain the frozen snapshot until the rollback
140 // recovery load completes to prevent flashing a loading state.
141 if (frozenSnapshot() !== null) {
142 isRollbackRecoveryPending.set(true);
143 }
144 } else if (e instanceof NavigationCancel || e instanceof NavigationError) {
145 const isRollback =
146 e instanceof NavigationError ||
147 (e instanceof NavigationCancel &&
148 e.code !== NavigationCancellationCode.SupersededByNewNavigation &&
149 e.code !== NavigationCancellationCode.Redirect);
150
151 if (!isRollback) return;
152
153 isRollbackRecoveryPending.set(true);
154 }
155 });
156
157 injector.get(DestroyRef).onDestroy(() => sub.unsubscribe());
158
159 effect(
160 () => {
161 if (isRollbackRecoveryPending() && !source.isLoading()) {

Callers 1

routerResourceFunction · 0.85

Calls 9

signalFunction · 0.90
effectFunction · 0.90
computedFunction · 0.90
subscribeMethod · 0.65
setMethod · 0.65
snapshotMethod · 0.65
onDestroyMethod · 0.65
getMethod · 0.65
unsubscribeMethod · 0.65

Tested by

no test coverage detected