|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {EnvironmentInjector, ɵPendingTasks as PendingTasks, ApplicationRef} from '@angular/core'; |
| 10 | +import { |
| 11 | + BehaviorSubject, |
| 12 | + EMPTY, |
| 13 | + Subject, |
| 14 | + catchError, |
| 15 | + delay, |
| 16 | + config, |
| 17 | + finalize, |
| 18 | + firstValueFrom, |
| 19 | + interval, |
| 20 | + of, |
| 21 | +} from 'rxjs'; |
| 22 | + |
| 23 | +import {pendingUntilEvent} from '@angular/core/rxjs-interop'; |
| 24 | +import {TestBed} from '@angular/core/testing'; |
| 25 | + |
| 26 | +describe('pendingUntilEvent', () => { |
| 27 | + let taskService: PendingTasks; |
| 28 | + let injector: EnvironmentInjector; |
| 29 | + let appRef: ApplicationRef; |
| 30 | + beforeEach(() => { |
| 31 | + taskService = TestBed.inject(PendingTasks); |
| 32 | + injector = TestBed.inject(EnvironmentInjector); |
| 33 | + appRef = TestBed.inject(ApplicationRef); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should not block stability until subscription', async () => { |
| 37 | + const originalSource = new BehaviorSubject(0); |
| 38 | + const delayedSource = originalSource.pipe(delay(5), pendingUntilEvent(injector)); |
| 39 | + expect(taskService.hasPendingTasks.value).toEqual(false); |
| 40 | + |
| 41 | + const emitPromise = firstValueFrom(delayedSource); |
| 42 | + expect(taskService.hasPendingTasks.value).toEqual(true); |
| 43 | + |
| 44 | + await expectAsync(emitPromise).toBeResolvedTo(0); |
| 45 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('runs the subscription body before stability', async () => { |
| 49 | + const source = of(1).pipe(pendingUntilEvent(injector)); |
| 50 | + |
| 51 | + // stable before subscription |
| 52 | + expect(taskService.hasPendingTasks.value).toEqual(false); |
| 53 | + source.subscribe(() => { |
| 54 | + // unstable within synchronous subscription body |
| 55 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 56 | + }); |
| 57 | + // stable after above synchronous subscription execution |
| 58 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('only blocks stability until first emit', async () => { |
| 62 | + const intervalSource = interval(5).pipe(pendingUntilEvent(injector)); |
| 63 | + expect(taskService.hasPendingTasks.value).toEqual(false); |
| 64 | + |
| 65 | + await new Promise<void>(async (resolve) => { |
| 66 | + const subscription = intervalSource.subscribe(async (v) => { |
| 67 | + if (v === 0) { |
| 68 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 69 | + } else { |
| 70 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 71 | + } |
| 72 | + if (v === 3) { |
| 73 | + subscription.unsubscribe(); |
| 74 | + resolve(); |
| 75 | + } |
| 76 | + }); |
| 77 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should unblock stability on complete (but no emit)', async () => { |
| 82 | + const sub = new Subject(); |
| 83 | + sub.asObservable().pipe(pendingUntilEvent(injector)).subscribe(); |
| 84 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 85 | + sub.complete(); |
| 86 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should unblock stability on unsubscribe before emit', async () => { |
| 90 | + const sub = new Subject(); |
| 91 | + const subscription = sub.asObservable().pipe(pendingUntilEvent(injector)).subscribe(); |
| 92 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 93 | + subscription.unsubscribe(); |
| 94 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 95 | + }); |
| 96 | + |
| 97 | + // Note that we cannot execute `finalize` operators that appear _after_ ours before |
| 98 | + // removing the pending task. We need to register the finalize operation on the subscription |
| 99 | + // as soon as the operator executes. A `finalize` operator later on in the stream will |
| 100 | + // be appear later in the finalizers list. These finalizers are both registered and executed |
| 101 | + // serially. We cannot execute our finalizer after other finalizers in the pipeline. |
| 102 | + it('should execute user finalize body before stability (as long as it appears first)', async () => { |
| 103 | + const sub = new Subject(); |
| 104 | + let finalizeExecuted = false; |
| 105 | + const subscription = sub |
| 106 | + .asObservable() |
| 107 | + .pipe( |
| 108 | + finalize(() => { |
| 109 | + finalizeExecuted = true; |
| 110 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 111 | + }), |
| 112 | + pendingUntilEvent(injector), |
| 113 | + ) |
| 114 | + .subscribe(); |
| 115 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 116 | + subscription.unsubscribe(); |
| 117 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 118 | + expect(finalizeExecuted).toBe(true); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should not throw if application is destroyed before emit', async () => { |
| 122 | + const sub = new Subject<void>(); |
| 123 | + sub.asObservable().pipe(pendingUntilEvent(injector)).subscribe(); |
| 124 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 125 | + TestBed.resetTestingModule(); |
| 126 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 127 | + sub.next(); |
| 128 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should unblock stability on error before emit', async () => { |
| 132 | + const sub = new Subject<void>(); |
| 133 | + sub |
| 134 | + .asObservable() |
| 135 | + .pipe( |
| 136 | + pendingUntilEvent(injector), |
| 137 | + catchError(() => EMPTY), |
| 138 | + ) |
| 139 | + .subscribe(); |
| 140 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 141 | + sub.error(new Error('error in pipe')); |
| 142 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 143 | + sub.next(); |
| 144 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should unblock stability on error in subscription', async () => { |
| 148 | + function nextUncaughtError() { |
| 149 | + return new Promise((resolve) => { |
| 150 | + config.onUnhandledError = (e) => { |
| 151 | + config.onUnhandledError = null; |
| 152 | + resolve(e); |
| 153 | + }; |
| 154 | + }); |
| 155 | + } |
| 156 | + const sub = new Subject<void>(); |
| 157 | + sub |
| 158 | + .asObservable() |
| 159 | + .pipe(pendingUntilEvent(injector)) |
| 160 | + .subscribe({ |
| 161 | + next: () => { |
| 162 | + throw new Error('oh noes'); |
| 163 | + }, |
| 164 | + }); |
| 165 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 166 | + const errorPromise = nextUncaughtError(); |
| 167 | + sub.next(); |
| 168 | + await expectAsync(errorPromise).toBeResolved(); |
| 169 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 170 | + |
| 171 | + const errorPromise2 = nextUncaughtError(); |
| 172 | + sub.next(); |
| 173 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 174 | + await expectAsync(errorPromise2).toBeResolved(); |
| 175 | + }); |
| 176 | + |
| 177 | + it('finalize and complete are delivered correctly', () => { |
| 178 | + const sub = new Subject<void>(); |
| 179 | + let log: string[] = []; |
| 180 | + const obs1 = sub.asObservable().pipe( |
| 181 | + pendingUntilEvent(injector), |
| 182 | + finalize(() => { |
| 183 | + log.push('finalize'); |
| 184 | + }), |
| 185 | + ); |
| 186 | + |
| 187 | + // complete after subscription |
| 188 | + obs1.subscribe({ |
| 189 | + complete: () => { |
| 190 | + log.push('complete'); |
| 191 | + }, |
| 192 | + }); |
| 193 | + sub.complete(); |
| 194 | + expect(log).toEqual(['complete', 'finalize']); |
| 195 | + |
| 196 | + // already completed before subscription |
| 197 | + log.length = 0; |
| 198 | + obs1.subscribe({ |
| 199 | + complete: () => { |
| 200 | + log.push('complete'); |
| 201 | + }, |
| 202 | + }); |
| 203 | + expect(log).toEqual(['complete', 'finalize']); |
| 204 | + |
| 205 | + log.length = 0; |
| 206 | + new Subject() |
| 207 | + .asObservable() |
| 208 | + .pipe( |
| 209 | + pendingUntilEvent(injector), |
| 210 | + finalize(() => { |
| 211 | + log.push('finalize'); |
| 212 | + }), |
| 213 | + ) |
| 214 | + .subscribe({ |
| 215 | + complete: () => { |
| 216 | + log.push('complete'); |
| 217 | + }, |
| 218 | + }) |
| 219 | + .unsubscribe(); |
| 220 | + expect(log).toEqual(['finalize']); |
| 221 | + }); |
| 222 | + |
| 223 | + it('should block stability for each new subscriber', async () => { |
| 224 | + const sub = new Subject<void>(); |
| 225 | + const observable = sub.asObservable().pipe(delay(5), pendingUntilEvent(injector)); |
| 226 | + |
| 227 | + observable.subscribe(); |
| 228 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 229 | + sub.next(); |
| 230 | + observable.subscribe(); |
| 231 | + // first subscription unblocks |
| 232 | + await new Promise((r) => setTimeout(r, 5)); |
| 233 | + // still pending because the other subscribed after the emit |
| 234 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 235 | + |
| 236 | + sub.next(); |
| 237 | + await new Promise((r) => setTimeout(r, 3)); |
| 238 | + observable.subscribe(); |
| 239 | + sub.next(); |
| 240 | + // second subscription unblocks |
| 241 | + await new Promise((r) => setTimeout(r, 2)); |
| 242 | + // still pending because third subscription delay not finished |
| 243 | + expect(taskService.hasPendingTasks.value).toBe(true); |
| 244 | + |
| 245 | + // finishes third subscription |
| 246 | + await new Promise((r) => setTimeout(r, 3)); |
| 247 | + await expectAsync(appRef.whenStable()).toBeResolved(); |
| 248 | + }); |
| 249 | +}); |
0 commit comments