|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 |
|
| 9 | +import {BehaviorSubject, Subscription} from 'rxjs'; |
9 | 10 | import {
|
10 | 11 | AsyncFactoryFn,
|
11 | 12 | ComponentHarness,
|
@@ -36,7 +37,108 @@ type ParsedQueries<T extends ComponentHarness> = {
|
36 | 37 | harnessTypes: Set<ComponentHarnessConstructor<T>>,
|
37 | 38 | };
|
38 | 39 |
|
39 |
| -let isCDBatching = false; |
| 40 | +/** Represents the status of change detection batching. */ |
| 41 | +export interface ChangeDetectionBatchingStatus { |
| 42 | + /** Whether change detection is batching. */ |
| 43 | + isBatching: boolean; |
| 44 | + /** |
| 45 | + * An optional callback, if present it indicates that change detection should be run immediately, |
| 46 | + * while handling the batching status change. The callback should then be called as soon as change |
| 47 | + * detection is done. |
| 48 | + */ |
| 49 | + onDetectChangesNow?: () => void; |
| 50 | +} |
| 51 | + |
| 52 | +/** Subject used to dispatch and listen for changes to the change detection batching status . */ |
| 53 | +const batchChangeDetectionSubject = new BehaviorSubject<ChangeDetectionBatchingStatus>({ |
| 54 | + isBatching: false |
| 55 | +}); |
| 56 | + |
| 57 | +/** The current subscription to `batchChangeDetectionSubject`. */ |
| 58 | +let batchChangeDetectionSubscription: Subscription | null; |
| 59 | + |
| 60 | +/** |
| 61 | + * The default handler for change detection batching status changes. This handler will be used if |
| 62 | + * the specific environment does not install its own. |
| 63 | + * @param status The new change detection batching status. |
| 64 | + */ |
| 65 | +function defaultBatchChangeDetectionHandler(status: ChangeDetectionBatchingStatus) { |
| 66 | + status.onDetectChangesNow?.(); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Allows a test `HarnessEnvironment` to install its own handler for change detection batching |
| 71 | + * status changes. |
| 72 | + * @param handler The handler for the change detection batching status. |
| 73 | + */ |
| 74 | +export function handleChangeDetectionBatching( |
| 75 | + handler: (status: ChangeDetectionBatchingStatus) => void) { |
| 76 | + stopHandlingChangeDetectionBatching(); |
| 77 | + batchChangeDetectionSubscription = batchChangeDetectionSubject.subscribe(handler); |
| 78 | +} |
| 79 | + |
| 80 | +/** Allows a `HarnessEnvironment` to stop handling change detection batching status changes. */ |
| 81 | +export function stopHandlingChangeDetectionBatching() { |
| 82 | + batchChangeDetectionSubscription?.unsubscribe(); |
| 83 | + batchChangeDetectionSubscription = null; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Batches together triggering of change detection over the duration of the given function. |
| 88 | + * @param fn The function to call with batched change detection. |
| 89 | + * @param triggerBeforeAndAfter Optionally trigger change detection once before and after the batch |
| 90 | + * operation. If false, change detection will not be triggered. |
| 91 | + * @return The result of the given function. |
| 92 | + */ |
| 93 | +async function batchChangeDetection<T>(fn: () => Promise<T>, triggerBeforeAndAfter: boolean) { |
| 94 | + // If change detection batching is already in progress, just run the function. |
| 95 | + if (batchChangeDetectionSubject.getValue().isBatching) { |
| 96 | + return await fn(); |
| 97 | + } |
| 98 | + |
| 99 | + // If nothing is handling change detection batching, install the default handler. |
| 100 | + if (!batchChangeDetectionSubscription) { |
| 101 | + batchChangeDetectionSubject.subscribe(defaultBatchChangeDetectionHandler); |
| 102 | + } |
| 103 | + |
| 104 | + if (triggerBeforeAndAfter) { |
| 105 | + await new Promise(resolve => batchChangeDetectionSubject.next({ |
| 106 | + isBatching: true, |
| 107 | + onDetectChangesNow: resolve, |
| 108 | + })); |
| 109 | + const result = await fn(); |
| 110 | + await new Promise(resolve => batchChangeDetectionSubject.next({ |
| 111 | + isBatching: false, |
| 112 | + onDetectChangesNow: resolve, |
| 113 | + })); |
| 114 | + return result; |
| 115 | + } else { |
| 116 | + batchChangeDetectionSubject.next({isBatching: true}); |
| 117 | + const result = await fn(); |
| 118 | + batchChangeDetectionSubject.next({isBatching: false}); |
| 119 | + return result; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Disables the harness system's auto change detection for the duration of the given function. |
| 125 | + * @param fn The function to disable auto change detection for. |
| 126 | + * @return The result of the given function. |
| 127 | + */ |
| 128 | +export async function noAutoChangeDetection<T>(fn: () => Promise<T>) { |
| 129 | + return batchChangeDetection(fn, false); |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Resolves the given list of async values in parallel (i.e. via Promise.all) while batching change |
| 134 | + * detection over the entire operation such that change detection occurs exactly once before |
| 135 | + * resolving the values and once after. |
| 136 | + * @param values The async values to resolve in parallel with batched change detection. |
| 137 | + * @return The resolved values. |
| 138 | + */ |
| 139 | +export async function parallel<T>(values: Iterable<T | PromiseLike<T>>) { |
| 140 | + return batchChangeDetection(() => Promise.all(values), true); |
| 141 | +} |
40 | 142 |
|
41 | 143 | /**
|
42 | 144 | * Base harness environment class that can be extended to allow `ComponentHarness`es to be used in
|
@@ -121,33 +223,6 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
|
121 | 223 | return (await this.getAllRawElements(selector)).map(e => this.createEnvironment(e));
|
122 | 224 | }
|
123 | 225 |
|
124 |
| - protected isCDBatching() { |
125 |
| - return isCDBatching; |
126 |
| - } |
127 |
| - |
128 |
| - async batchCD<T>(fn: () => Promise<T>) { |
129 |
| - // Flipping on the `isCDBatching` flag will turn all calls to `forceStabilize` into no-ops to |
130 |
| - // prevent triggering redundant change detection. However, we want to make sure we trigger |
131 |
| - // change detection exactly once before flipping this flag to ensure that any pending changes |
132 |
| - // are flushed before the harness tries to read state from DOM elements. |
133 |
| - const alreadyBatching = isCDBatching; |
134 |
| - if (!alreadyBatching) { |
135 |
| - await this.forceStabilize(); |
136 |
| - isCDBatching = true; |
137 |
| - } |
138 |
| - |
139 |
| - const result = await fn(); |
140 |
| - |
141 |
| - // We also want to run change detection exactly once after the batched operation is complete. |
142 |
| - // This will ensure that any changes from interacting with DOM elements are properly flushed. |
143 |
| - if (!alreadyBatching) { |
144 |
| - isCDBatching = false; |
145 |
| - await this.forceStabilize(); |
146 |
| - } |
147 |
| - |
148 |
| - return result; |
149 |
| - } |
150 |
| - |
151 | 226 | /** Creates a `ComponentHarness` for the given harness type with the given raw host element. */
|
152 | 227 | protected createComponentHarness<T extends ComponentHarness>(
|
153 | 228 | harnessType: ComponentHarnessConstructor<T>, element: E): T {
|
@@ -198,18 +273,17 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
|
198 | 273 | // may trigger change detection by reading state from DOM elements. If not batched these change
|
199 | 274 | // detections would be triggered once per potential match element which could cause significant
|
200 | 275 | // slowdown.
|
201 |
| - const perElementMatches = await this.batchCD(() => |
202 |
| - Promise.all(rawElements.map(async rawElement => { |
203 |
| - const testElement = this.createTestElement(rawElement); |
204 |
| - const allResultsForElement = await Promise.all( |
205 |
| - // For each query, get `null` if it doesn't match, or a `TestElement` or |
206 |
| - // `ComponentHarness` as appropriate if it does match. This gives us everything that |
207 |
| - // matches the current raw element, but it may contain duplicate entries (e.g. |
208 |
| - // multiple `TestElement` or multiple `ComponentHarness` of the same type). |
209 |
| - allQueries.map(query => this._getQueryResultForElement( |
210 |
| - query, rawElement, testElement, skipSelectorCheck))); |
211 |
| - return _removeDuplicateQueryResults(allResultsForElement); |
212 |
| - }))); |
| 276 | + const perElementMatches = await parallel(rawElements.map(async rawElement => { |
| 277 | + const testElement = this.createTestElement(rawElement); |
| 278 | + const allResultsForElement = await Promise.all( |
| 279 | + // For each query, get `null` if it doesn't match, or a `TestElement` or |
| 280 | + // `ComponentHarness` as appropriate if it does match. This gives us everything that |
| 281 | + // matches the current raw element, but it may contain duplicate entries (e.g. |
| 282 | + // multiple `TestElement` or multiple `ComponentHarness` of the same type). |
| 283 | + allQueries.map(query => this._getQueryResultForElement( |
| 284 | + query, rawElement, testElement, skipSelectorCheck))); |
| 285 | + return _removeDuplicateQueryResults(allResultsForElement); |
| 286 | + })); |
213 | 287 | return ([] as any).concat(...perElementMatches);
|
214 | 288 | }
|
215 | 289 |
|
|
0 commit comments