| 18 | */ |
| 19 | |
| 20 | export class PendingTasksInternal implements OnDestroy { |
| 21 | private taskId = 0; |
| 22 | private pendingTasks = new Set<number>(); |
| 23 | private destroyed = false; |
| 24 | private pendingTask = new BehaviorSubject<boolean>(false); |
| 25 | private debugTaskTracker = inject(DEBUG_TASK_TRACKER, {optional: true}); |
| 26 | |
| 27 | get hasPendingTasks(): boolean { |
| 28 | // Accessing the value of a closed `BehaviorSubject` throws an error. |
| 29 | return this.destroyed ? false : this.pendingTask.value; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * In case the service is about to be destroyed, return a self-completing observable. |
| 34 | * Otherwise, return the observable that emits the current state of pending tasks. |
| 35 | */ |
| 36 | get hasPendingTasksObservable(): Observable<boolean> { |
| 37 | if (this.destroyed) { |
| 38 | // Manually creating the observable pulls less symbols from RxJS than `of(false)`. |
| 39 | return new Observable<boolean>((subscriber) => { |
| 40 | subscriber.next(false); |
| 41 | subscriber.complete(); |
| 42 | }); |
| 43 | } |
| 44 | |
| 45 | return this.pendingTask; |
| 46 | } |
| 47 | |
| 48 | add(): number { |
| 49 | // Emitting a value to a closed subject throws an error. |
| 50 | if (!this.hasPendingTasks && !this.destroyed) { |
| 51 | this.pendingTask.next(true); |
| 52 | } |
| 53 | const taskId = this.taskId++; |
| 54 | this.pendingTasks.add(taskId); |
| 55 | this.debugTaskTracker?.add(taskId); |
| 56 | return taskId; |
| 57 | } |
| 58 | |
| 59 | has(taskId: number): boolean { |
| 60 | return this.pendingTasks.has(taskId); |
| 61 | } |
| 62 | |
| 63 | remove(taskId: number): void { |
| 64 | this.pendingTasks.delete(taskId); |
| 65 | this.debugTaskTracker?.remove(taskId); |
| 66 | if (this.pendingTasks.size === 0 && this.hasPendingTasks) { |
| 67 | this.pendingTask.next(false); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | ngOnDestroy(): void { |
| 72 | this.pendingTasks.clear(); |
| 73 | if (this.hasPendingTasks) { |
| 74 | this.pendingTask.next(false); |
| 75 | } |
| 76 | // We call `unsubscribe()` to release observers, as users may forget to |
| 77 | // unsubscribe manually when subscribing to `isStable`. We do not call |
nothing calls this directly
no test coverage detected
searching dependent graphs…