|
| 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 {BehaviorSubject, Observable} from 'rxjs'; |
| 10 | +import {ProxyZone, ProxyZoneStatic} from './proxy-zone-types'; |
| 11 | +import {HasTaskState, Zone, ZoneDelegate} from './zone-types'; |
| 12 | + |
| 13 | +/** Current state of the intercepted zone. */ |
| 14 | +export interface TaskState { |
| 15 | + /** Whether the zone is stable (i.e. no microtasks and macrotasks). */ |
| 16 | + stable: boolean; |
| 17 | +} |
| 18 | + |
| 19 | +/** Unique symbol that is used to patch a property to a proxy zone. */ |
| 20 | +const stateObservableSymbol = Symbol('ProxyZone_PATCHED#stateObservable'); |
| 21 | + |
| 22 | +/** Type that describes a potentially patched proxy zone instance. */ |
| 23 | +type PatchedProxyZone = ProxyZone & { |
| 24 | + [stateObservableSymbol]: undefined|Observable<TaskState>; |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * Interceptor that can be set up in a `ProxyZone` instance. The interceptor |
| 29 | + * will keep track of the task state and emit whenever the state changes. |
| 30 | + * |
| 31 | + * This serves as a workaround for https://github.com/angular/angular/issues/32896. |
| 32 | + */ |
| 33 | +export class TaskStateZoneInterceptor { |
| 34 | + /** Subject that can be used to emit a new state change. */ |
| 35 | + private _stateSubject: BehaviorSubject<TaskState> = new BehaviorSubject<TaskState>( |
| 36 | + this._lastState ? this._getTaskStateFromInternalZoneState(this._lastState) : {stable: true}); |
| 37 | + |
| 38 | + /** Public observable that emits whenever the task state changes. */ |
| 39 | + readonly state: Observable<TaskState> = this._stateSubject.asObservable(); |
| 40 | + |
| 41 | + constructor(private _lastState: HasTaskState|null) {} |
| 42 | + |
| 43 | + /** This will be called whenever the task state changes in the intercepted zone. */ |
| 44 | + onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState) { |
| 45 | + if (current === target) { |
| 46 | + this._stateSubject.next(this._getTaskStateFromInternalZoneState(hasTaskState)); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** Gets the task state from the internal ZoneJS task state. */ |
| 51 | + private _getTaskStateFromInternalZoneState(state: HasTaskState): TaskState { |
| 52 | + return {stable: !state.macroTask && !state.microTask}; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Sets up the custom task state Zone interceptor in the `ProxyZone`. Throws if |
| 57 | + * no `ProxyZone` could be found. |
| 58 | + * @returns an observable that emits whenever the task state changes. |
| 59 | + */ |
| 60 | + static setup(): Observable<TaskState> { |
| 61 | + if (Zone === undefined) { |
| 62 | + throw Error('Could not find ZoneJS. For test harnesses running in TestBed, ' + |
| 63 | + 'ZoneJS needs to be installed.'); |
| 64 | + } |
| 65 | + |
| 66 | + // tslint:disable-next-line:variable-name |
| 67 | + const ProxyZoneSpec = (Zone as any)['ProxyZoneSpec'] as ProxyZoneStatic|undefined; |
| 68 | + |
| 69 | + // If there is no "ProxyZoneSpec" installed, we throw an error and recommend |
| 70 | + // setting up the proxy zone by pulling in the testing bundle. |
| 71 | + if (!ProxyZoneSpec) { |
| 72 | + throw Error( |
| 73 | + 'ProxyZoneSpec is needed for the test harnesses but could not be found. ' + |
| 74 | + 'Please make sure that your environment includes zone.js/dist/zone-testing.js'); |
| 75 | + } |
| 76 | + |
| 77 | + // Ensure that there is a proxy zone instance set up, and get |
| 78 | + // a reference to the instance if present. |
| 79 | + const zoneSpec = ProxyZoneSpec.assertPresent() as PatchedProxyZone; |
| 80 | + |
| 81 | + // If there already is a delegate registered in the proxy zone, and it |
| 82 | + // is type of the custom task state interceptor, we just use that state |
| 83 | + // observable. This allows us to only intercept Zone once per test |
| 84 | + // (similar to how `fakeAsync` or `async` work). |
| 85 | + if (zoneSpec[stateObservableSymbol]) { |
| 86 | + return zoneSpec[stateObservableSymbol]!; |
| 87 | + } |
| 88 | + |
| 89 | + // Since we intercept on environment creation and the fixture has been |
| 90 | + // created before, we might have missed tasks scheduled before. Fortunately |
| 91 | + // the proxy zone keeps track of the previous task state, so we can just pass |
| 92 | + // this as initial state to the task zone interceptor. |
| 93 | + const interceptor = new TaskStateZoneInterceptor(zoneSpec.lastTaskState); |
| 94 | + const zoneSpecOnHasTask = zoneSpec.onHasTask; |
| 95 | + |
| 96 | + // We setup the task state interceptor in the `ProxyZone`. Note that we cannot register |
| 97 | + // the interceptor as a new proxy zone delegate because it would mean that other zone |
| 98 | + // delegates (e.g. `FakeAsyncTestZone` or `AsyncTestZone`) can accidentally overwrite/disable |
| 99 | + // our interceptor. Since we just intend to monitor the task state of the proxy zone, it is |
| 100 | + // sufficient to just patch the proxy zone. This also avoids that we interfere with the task |
| 101 | + // queue scheduling logic. |
| 102 | + zoneSpec.onHasTask = function() { |
| 103 | + zoneSpecOnHasTask.apply(zoneSpec, arguments); |
| 104 | + interceptor.onHasTask.apply(interceptor, arguments); |
| 105 | + }; |
| 106 | + |
| 107 | + return zoneSpec[stateObservableSymbol] = interceptor.state; |
| 108 | + } |
| 109 | +} |
0 commit comments