MCPcopy Index your code
hub / github.com/angular/angular / PendingTasks

Class PendingTasks

packages/core/src/pending_tasks.ts:42–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40 * @publicApi 20.0
41 */
42export class PendingTasks {
43 private readonly internalPendingTasks = inject(PendingTasksInternal);
44 private readonly scheduler = inject(ChangeDetectionScheduler);
45 private readonly errorHandler = inject(INTERNAL_APPLICATION_ERROR_HANDLER);
46 /**
47 * Adds a new task that should block application's stability.
48 * @returns A cleanup function that removes a task when called.
49 */
50 add(): () => void {
51 const taskId = this.internalPendingTasks.add();
52 return () => {
53 if (!this.internalPendingTasks.has(taskId)) {
54 // This pending task has already been cleared.
55 return;
56 }
57 // Notifying the scheduler will hold application stability open until the next tick.
58 this.scheduler.notify(NotificationSource.PendingTaskRemoved);
59 this.internalPendingTasks.remove(taskId);
60 };
61 }
62
63 /**
64 * Runs an asynchronous function and blocks the application's stability until the function completes.
65 *
66 * ```ts
67 * pendingTasks.run(async () => {
68 * const userData = await fetch('/api/user');
69 * this.userData.set(userData);
70 * });
71 * ```
72 *
73 * @param fn The asynchronous function to execute
74 * @developerPreview 19.0
75 */
76 run(fn: () => Promise<unknown>): void {
77 const removeTask = this.add();
78 try {
79 fn().catch(this.errorHandler).finally(removeTask);
80 } catch (err) {
81 this.errorHandler(err);
82 removeTask();
83 }
84 }
85
86 /** @nocollapse */
87 static ɵprov = /** @pureOrBreakMyCode */ /* @__PURE__ */ ɵɵdefineInjectable({
88 token: PendingTasks,
89 providedIn: 'root',
90 factory: () => new PendingTasks(),
91 });
92}

Callers

nothing calls this directly

Calls 2

injectFunction · 0.90
ɵɵdefineInjectableFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…