Skip to content

Commit

Permalink
Ticker should dispatch creation and disposal events. (flutter#137844)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksokolovskyi authored Nov 8, 2023
1 parent 8969288 commit ca384b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/flutter/lib/src/scheduler/ticker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions packages/flutter/test/scheduler/ticker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void main() {
}

final Ticker ticker = Ticker(handleTick);
addTearDown(ticker.dispose);

expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isFalse);
Expand Down Expand Up @@ -100,6 +101,7 @@ void main() {

testWidgetsWithLeakTracking('Ticker control test', (WidgetTester tester) async {
late Ticker ticker;
addTearDown(() => ticker.dispose());

void testFunction() {
ticker = Ticker((Duration _) { });
Expand Down Expand Up @@ -154,6 +156,7 @@ void main() {
}

final Ticker ticker = Ticker(handleTick);
addTearDown(ticker.dispose);
ticker.start();

expect(ticker.isTicking, isTrue);
Expand All @@ -179,6 +182,7 @@ void main() {
}

final Ticker ticker = Ticker(handleTick);
addTearDown(ticker.dispose);
ticker.start();

expect(tickCount, equals(0));
Expand All @@ -198,4 +202,11 @@ void main() {

ticker.stop();
});

test('Ticker dispatches memory events', () async {
await expectLater(
await memoryEvents(() => Ticker((_) {}).dispose(), Ticker,),
areCreateAndDispose,
);
});
}

0 comments on commit ca384b8

Please sign in to comment.