trackLayoutChange<T> method
Tracks a layout change defined by fn
, and calls the callback
function
with the last stable value.
If framesToStabilize
is set, the callback will wait for the specified
number of animation frames before it considers the value to be stable.
If the value changes while waiting for stabilization, the animation frame
count restarts. The recommended value depends on the use case:
- visibility checks do not need animation frame stabilization,
- size-tracking properties (e.g. widths on resize) may wait for 3+ frames before reacting on the new size.
The callback
is assumed to do lightweight DOM updates only.
If you want to trigger both model changes and async operations, you must
set the runInAngularZone
flag.
Returns a subscription that allows pausing, resuming and canceling the observer.
Implementation
StreamSubscription<DomService> trackLayoutChange<T>(
T Function() fn, void Function(T) callback,
{int framesToStabilize = 1, bool runInAngularZone = false}) {
// TODO(google): Move layout checking into ruler service when landed.
var trackerCallback = callback;
if (runInAngularZone) {
trackerCallback = (T value) {
_ngZone.run(() => callback(value));
};
}
var tracker = _ChangeTracker(this, fn, trackerCallback, framesToStabilize);
return onLayoutChanged!.listen((_) => tracker._onLayoutObserve());
}