|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -import {BehaviorSubject, Subscription} from 'rxjs'; |
| 9 | +import {parallel} from './change-detection'; |
10 | 10 | import {
|
11 | 11 | AsyncFactoryFn,
|
12 | 12 | ComponentHarness,
|
@@ -37,109 +37,6 @@ type ParsedQueries<T extends ComponentHarness> = {
|
37 | 37 | harnessTypes: Set<ComponentHarnessConstructor<T>>,
|
38 | 38 | };
|
39 | 39 |
|
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 |
| -} |
142 |
| - |
143 | 40 | /**
|
144 | 41 | * Base harness environment class that can be extended to allow `ComponentHarness`es to be used in
|
145 | 42 | * different test environments (e.g. testbed, protractor, etc.). This class implements the
|
|
0 commit comments