|
| 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 {assertInInjectionContext, Injector, ɵɵdefineInjectable} from '../di'; |
| 10 | +import {inject} from '../di/injector_compatibility'; |
| 11 | +import {RuntimeError, RuntimeErrorCode} from '../errors'; |
| 12 | +import {DestroyRef} from '../linker/destroy_ref'; |
| 13 | + |
| 14 | +import {isPlatformBrowser} from './util/misc_utils'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Options passed to `afterRender` and `afterNextRender`. |
| 18 | + * |
| 19 | + * @developerPreview |
| 20 | + */ |
| 21 | +export interface AfterRenderOptions { |
| 22 | + /** |
| 23 | + * The `Injector` to use during creation. |
| 24 | + * |
| 25 | + * If this is not provided, the current injection context will be used instead (via `inject`). |
| 26 | + */ |
| 27 | + injector?: Injector; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * A callback that runs after render. |
| 32 | + * |
| 33 | + * @developerPreview |
| 34 | + */ |
| 35 | +export interface AfterRenderRef { |
| 36 | + /** |
| 37 | + * Shut down the callback, preventing it from being called again. |
| 38 | + */ |
| 39 | + destroy(): void; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Register a callback to be invoked each time the application |
| 44 | + * finishes rendering. |
| 45 | + * |
| 46 | + * Note that the callback will run |
| 47 | + * - in the order it was registered |
| 48 | + * - once per render |
| 49 | + * - on browser platforms only |
| 50 | + * |
| 51 | + * <div class="alert is-important"> |
| 52 | + * |
| 53 | + * Components are not guaranteed to be [hydrated](guide/hydration) before the callback runs. |
| 54 | + * You must use caution when directly reading or writing the DOM and layout. |
| 55 | + * |
| 56 | + * </div> |
| 57 | + * |
| 58 | + * @param callback A callback function to register |
| 59 | + * |
| 60 | + * @usageNotes |
| 61 | + * |
| 62 | + * Use `afterRender` to read or write the DOM after each render. |
| 63 | + * |
| 64 | + * ### Example |
| 65 | + * ```ts |
| 66 | + * @Component({ |
| 67 | + * selector: 'my-cmp', |
| 68 | + * template: `<span #content>{{ ... }}</span>`, |
| 69 | + * }) |
| 70 | + * export class MyComponent { |
| 71 | + * @ViewChild('content') contentRef: ElementRef; |
| 72 | + * |
| 73 | + * constructor() { |
| 74 | + * afterRender(() => { |
| 75 | + * console.log('content height: ' + this.contentRef.nativeElement.scrollHeight); |
| 76 | + * }); |
| 77 | + * } |
| 78 | + * } |
| 79 | + * ``` |
| 80 | + * |
| 81 | + * @developerPreview |
| 82 | + */ |
| 83 | +export function afterRender(callback: VoidFunction, options?: AfterRenderOptions): AfterRenderRef { |
| 84 | + !options && assertInInjectionContext(afterRender); |
| 85 | + const injector = options?.injector ?? inject(Injector); |
| 86 | + |
| 87 | + if (!isPlatformBrowser(injector)) { |
| 88 | + return {destroy() {}}; |
| 89 | + } |
| 90 | + |
| 91 | + let destroy: VoidFunction|undefined; |
| 92 | + const unregisterFn = injector.get(DestroyRef).onDestroy(() => destroy?.()); |
| 93 | + const manager = injector.get(AfterRenderEventManager); |
| 94 | + const instance = new AfterRenderCallback(callback); |
| 95 | + |
| 96 | + destroy = () => { |
| 97 | + manager.unregister(instance); |
| 98 | + unregisterFn(); |
| 99 | + }; |
| 100 | + manager.register(instance); |
| 101 | + return {destroy}; |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Register a callback to be invoked the next time the application |
| 106 | + * finishes rendering. |
| 107 | + * |
| 108 | + * Note that the callback will run |
| 109 | + * - in the order it was registered |
| 110 | + * - on browser platforms only |
| 111 | + * |
| 112 | + * <div class="alert is-important"> |
| 113 | + * |
| 114 | + * Components are not guaranteed to be [hydrated](guide/hydration) before the callback runs. |
| 115 | + * You must use caution when directly reading or writing the DOM and layout. |
| 116 | + * |
| 117 | + * </div> |
| 118 | + * |
| 119 | + * @param callback A callback function to register |
| 120 | + * |
| 121 | + * @usageNotes |
| 122 | + * |
| 123 | + * Use `afterNextRender` to read or write the DOM once, |
| 124 | + * for example to initialize a non-Angular library. |
| 125 | + * |
| 126 | + * ### Example |
| 127 | + * ```ts |
| 128 | + * @Component({ |
| 129 | + * selector: 'my-chart-cmp', |
| 130 | + * template: `<div #chart>{{ ... }}</div>`, |
| 131 | + * }) |
| 132 | + * export class MyChartCmp { |
| 133 | + * @ViewChild('chart') chartRef: ElementRef; |
| 134 | + * chart: MyChart|null; |
| 135 | + * |
| 136 | + * constructor() { |
| 137 | + * afterNextRender(() => { |
| 138 | + * this.chart = new MyChart(this.chartRef.nativeElement); |
| 139 | + * }); |
| 140 | + * } |
| 141 | + * } |
| 142 | + * ``` |
| 143 | + * |
| 144 | + * @developerPreview |
| 145 | + */ |
| 146 | +export function afterNextRender( |
| 147 | + callback: VoidFunction, options?: AfterRenderOptions): AfterRenderRef { |
| 148 | + !options && assertInInjectionContext(afterNextRender); |
| 149 | + const injector = options?.injector ?? inject(Injector); |
| 150 | + |
| 151 | + if (!isPlatformBrowser(injector)) { |
| 152 | + return {destroy() {}}; |
| 153 | + } |
| 154 | + |
| 155 | + let destroy: VoidFunction|undefined; |
| 156 | + const unregisterFn = injector.get(DestroyRef).onDestroy(() => destroy?.()); |
| 157 | + const manager = injector.get(AfterRenderEventManager); |
| 158 | + const instance = new AfterRenderCallback(() => { |
| 159 | + destroy?.(); |
| 160 | + callback(); |
| 161 | + }); |
| 162 | + |
| 163 | + destroy = () => { |
| 164 | + manager.unregister(instance); |
| 165 | + unregisterFn(); |
| 166 | + }; |
| 167 | + manager.register(instance); |
| 168 | + return {destroy}; |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * A wrapper around a function to be used as an after render callback. |
| 173 | + * @private |
| 174 | + */ |
| 175 | +class AfterRenderCallback { |
| 176 | + private callback: VoidFunction; |
| 177 | + |
| 178 | + constructor(callback: VoidFunction) { |
| 179 | + this.callback = callback; |
| 180 | + } |
| 181 | + |
| 182 | + invoke() { |
| 183 | + this.callback(); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * Implements `afterRender` and `afterNextRender` callback manager logic. |
| 189 | + */ |
| 190 | +export class AfterRenderEventManager { |
| 191 | + private callbacks = new Set<AfterRenderCallback>(); |
| 192 | + private deferredCallbacks = new Set<AfterRenderCallback>(); |
| 193 | + private renderDepth = 0; |
| 194 | + private runningCallbacks = false; |
| 195 | + |
| 196 | + /** |
| 197 | + * Mark the beginning of a render operation (i.e. CD cycle). |
| 198 | + * Throws if called from an `afterRender` callback. |
| 199 | + */ |
| 200 | + begin() { |
| 201 | + if (this.runningCallbacks) { |
| 202 | + throw new RuntimeError( |
| 203 | + RuntimeErrorCode.RECURSIVE_APPLICATION_RENDER, |
| 204 | + ngDevMode && |
| 205 | + 'A new render operation began before the previous operation ended. ' + |
| 206 | + 'Did you trigger change detection from afterRender or afterNextRender?'); |
| 207 | + } |
| 208 | + |
| 209 | + this.renderDepth++; |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Mark the end of a render operation. Registered callbacks |
| 214 | + * are invoked if there are no more pending operations. |
| 215 | + */ |
| 216 | + end() { |
| 217 | + this.renderDepth--; |
| 218 | + |
| 219 | + if (this.renderDepth === 0) { |
| 220 | + try { |
| 221 | + this.runningCallbacks = true; |
| 222 | + for (const callback of this.callbacks) { |
| 223 | + callback.invoke(); |
| 224 | + } |
| 225 | + } finally { |
| 226 | + this.runningCallbacks = false; |
| 227 | + for (const callback of this.deferredCallbacks) { |
| 228 | + this.callbacks.add(callback); |
| 229 | + } |
| 230 | + this.deferredCallbacks.clear(); |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + register(callback: AfterRenderCallback) { |
| 236 | + // If we're currently running callbacks, new callbacks should be deferred |
| 237 | + // until the next render operation. |
| 238 | + const target = this.runningCallbacks ? this.deferredCallbacks : this.callbacks; |
| 239 | + target.add(callback); |
| 240 | + } |
| 241 | + |
| 242 | + unregister(callback: AfterRenderCallback) { |
| 243 | + this.callbacks.delete(callback); |
| 244 | + this.deferredCallbacks.delete(callback); |
| 245 | + } |
| 246 | + |
| 247 | + ngOnDestroy() { |
| 248 | + this.callbacks.clear(); |
| 249 | + this.deferredCallbacks.clear(); |
| 250 | + } |
| 251 | + |
| 252 | + /** @nocollapse */ |
| 253 | + static ɵprov = /** @pureOrBreakMyCode */ ɵɵdefineInjectable({ |
| 254 | + token: AfterRenderEventManager, |
| 255 | + providedIn: 'root', |
| 256 | + factory: () => new AfterRenderEventManager(), |
| 257 | + }); |
| 258 | +} |
0 commit comments