Ticker should dispatch creation and disposal events. (#137844)

This commit is contained in:
Kostia Sokolovskyi 2023-11-08 06:14:09 +01:00 committed by GitHub
parent 89692884a1
commit ca384b84e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -71,6 +71,15 @@ class Ticker {
_debugCreationStack = StackTrace.current;
return true;
}());
// TODO(polina-c): stop duplicating code across disposables
// https://github.com/flutter/flutter/issues/137435
if (kFlutterMemoryAllocationsEnabled) {
MemoryAllocations.instance.dispatchObjectCreated(
library: 'package:flutter/scheduler.dart',
className: '$Ticker',
object: this,
);
}
}
TickerFuture? _future;
@ -319,6 +328,12 @@ class Ticker {
/// with a [TickerCanceled] error.
@mustCallSuper
void dispose() {
// TODO(polina-c): stop duplicating code across disposables
// https://github.com/flutter/flutter/issues/137435
if (kFlutterMemoryAllocationsEnabled) {
MemoryAllocations.instance.dispatchObjectDisposed(object: this);
}
if (_future != null) {
final TickerFuture localFuture = _future!;
_future = null;

View File

@ -23,6 +23,7 @@ void main() {
}
final Ticker ticker = Ticker(handleTick);
addTearDown(ticker.dispose);
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isFalse);
@ -100,6 +101,7 @@ void main() {
testWidgetsWithLeakTracking('Ticker control test', (WidgetTester tester) async {
late Ticker ticker;
addTearDown(() => ticker.dispose());
void testFunction() {
ticker = Ticker((Duration _) { });
@ -154,6 +156,7 @@ void main() {
}
final Ticker ticker = Ticker(handleTick);
addTearDown(ticker.dispose);
ticker.start();
expect(ticker.isTicking, isTrue);
@ -179,6 +182,7 @@ void main() {
}
final Ticker ticker = Ticker(handleTick);
addTearDown(ticker.dispose);
ticker.start();
expect(tickCount, equals(0));
@ -198,4 +202,11 @@ void main() {
ticker.stop();
});
test('Ticker dispatches memory events', () async {
await expectLater(
await memoryEvents(() => Ticker((_) {}).dispose(), Ticker,),
areCreateAndDispose,
);
});
}