* Batches together triggering of change detection over the duration of the given function. * @param fn The function to call with batched change detection. * @param triggerBeforeAndAfter Optionally trigger change detection once before and after the batch * operation. If false, change detection w
(fn: () => Promise<T>, triggerBeforeAndAfter: boolean)
| 67 | * @return The result of the given function. |
| 68 | */ |
| 69 | async function batchChangeDetection<T>(fn: () => Promise<T>, triggerBeforeAndAfter: boolean) { |
| 70 | // If change detection batching is already in progress, just run the function. |
| 71 | if (autoChangeDetectionSubject.getValue().isDisabled) { |
| 72 | return await fn(); |
| 73 | } |
| 74 | |
| 75 | // If nothing is handling change detection batching, install the default handler. |
| 76 | if (!autoChangeDetectionSubscription) { |
| 77 | handleAutoChangeDetectionStatus(defaultAutoChangeDetectionHandler); |
| 78 | } |
| 79 | |
| 80 | if (triggerBeforeAndAfter) { |
| 81 | await new Promise(resolve => |
| 82 | autoChangeDetectionSubject.next({ |
| 83 | isDisabled: true, |
| 84 | onDetectChangesNow: resolve as () => void, |
| 85 | }), |
| 86 | ); |
| 87 | // The function passed in may throw (e.g. if the user wants to make an expectation of an error |
| 88 | // being thrown. If this happens, we need to make sure we still re-enable change detection, so |
| 89 | // we wrap it in a `finally` block. |
| 90 | try { |
| 91 | return await fn(); |
| 92 | } finally { |
| 93 | await new Promise(resolve => |
| 94 | autoChangeDetectionSubject.next({ |
| 95 | isDisabled: false, |
| 96 | onDetectChangesNow: resolve as () => void, |
| 97 | }), |
| 98 | ); |
| 99 | } |
| 100 | } else { |
| 101 | autoChangeDetectionSubject.next({isDisabled: true}); |
| 102 | // The function passed in may throw (e.g. if the user wants to make an expectation of an error |
| 103 | // being thrown. If this happens, we need to make sure we still re-enable change detection, so |
| 104 | // we wrap it in a `finally` block. |
| 105 | try { |
| 106 | return await fn(); |
| 107 | } finally { |
| 108 | autoChangeDetectionSubject.next({isDisabled: false}); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Disables the harness system's auto change detection for the duration of the given function. |
no test coverage detected
searching dependent graphs…