Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
polina-c committed Jul 25, 2023
1 parent b3499cd commit ab158a7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import 'model.dart';
///
/// If there are leaks, reports them to the enabled outputs:
/// listener, console and DevTools.
class LeakChecker {
LeakChecker({
class LeakReporter {
LeakReporter({
required this.leakProvider,
required this.checkPeriod,
required this.onLeaks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import '../devtools_integration/_registration.dart';
import '../shared/_primitives.dart';
import '../shared/shared_model.dart';
import '_dispatcher.dart' as dispatcher;
import '_leak_checker.dart';
import '_leak_reporter.dart';
import '_object_tracker.dart';
import 'model.dart';

class LeakTracker {
// static LeakTracker? _leakTracker;

// LeakProvider?
final ObjectTracker _objectTracker;
final LeakReporter _leakChecker;
}
6 changes: 3 additions & 3 deletions pkgs/leak_tracker/lib/src/leak_tracking/leak_tracker___.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import '../devtools_integration/_registration.dart';
import '../shared/_primitives.dart';
import '../shared/shared_model.dart';
import '_dispatcher.dart' as dispatcher;
import '_leak_checker.dart';
import '_leak_reporter.dart';
import '_object_tracker.dart';
import 'model.dart';

class LeakTracker {}

final _objectTracker = ObjectRef<ObjectTracker?>(null);

LeakChecker? _leakChecker;
LeakReporter? _leakChecker;

ObjectTracker _theObjectTracker() {
// TODO(polina-c): return both tracker and checker when tuples get released.
Expand Down Expand Up @@ -55,7 +55,7 @@ void enableLeakTracking({

_objectTracker.value = newTracker;

_leakChecker = LeakChecker(
_leakChecker = LeakReporter(
leakProvider: newTracker,
checkPeriod: theConfig.checkPeriod,
onLeaks: theConfig.onLeaks,
Expand Down
69 changes: 66 additions & 3 deletions pkgs/leak_tracker/lib/src/leak_tracking/leak_tracking.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,78 @@ import '../devtools_integration/_registration.dart';
import '../shared/_primitives.dart';
import '../shared/shared_model.dart';
import '_dispatcher.dart' as dispatcher;
import '_leak_checker.dart';
import '_leak_reporter.dart';
import '_object_tracker.dart';
import 'model.dart';

abstract class LeakTracking {
static LeakTracker? _leakTracker;

/// Leak provider, used in integration with DevTools.
/// Leak provider, used for integration with DevTools.
///
/// It should be updated every time leak tracking is reconfigured.
static final _leakProvider = ObjectRef<LeakProvider?>(null);
static final _leakProvider = ObjectRef<WeakReference<LeakProvider>?>(null);

/// Enables leak tracking for the application.
///
/// The leak tracking will function only for debug/profile/developer mode.
/// See usage guidance at https://github.com/dart-lang/leak_tracker.
///
/// If [resetIfAlreadyEnabled] is true and leak tracking is already on,
/// the tracking will be reset with new configuration.
///
/// If [resetIfAlreadyEnabled] is true and leak tracking is already on,
/// [StateError] will be thrown.
void enableLeakTracking({
LeakTrackingConfiguration? config,
bool resetIfAlreadyEnabled = false,
}) {
assert(() {
final theConfig = config ??= const LeakTrackingConfiguration();
if (_leakTracker != null) {
if (!resetIfAlreadyEnabled) {
throw StateError('Leak tracking is already enabled.');
}
disableLeakTracking();
}

final objectTracker = ObjectTracker(
leakDiagnosticConfig: theConfig.leakDiagnosticConfig,
disposalTime: theConfig.disposalTime,
numberOfGcCycles: theConfig.numberOfGcCycles,
);

final leakChecker = LeakReporter(
leakProvider: objectTracker,
checkPeriod: theConfig.checkPeriod,
onLeaks: theConfig.onLeaks,
stdoutSink: theConfig.stdoutLeaks ? StdoutSummarySink() : null,
devToolsSink: theConfig.notifyDevTools ? DevToolsSummarySink() : null,
);

_leakProvider.value = WeakReference(objectTracker);

if (theConfig.notifyDevTools) {
setupDevToolsIntegration(_leakProvider);
} else {
registerLeakTrackingServiceExtension();
}
return true;
}());
}

/// Disables leak tracking for the application.
///
/// See usage guidance at https://github.com/dart-lang/leak_tracker.
void disableLeakTracking() {
assert(() {
_leakProvider.value?.dispose();
_leakChecker?.dispose();
_leakChecker = null;
_objectTracker.value?.dispose();
_objectTracker.value = null;

return true;
}());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:leak_tracker/leak_tracker.dart';
import 'package:leak_tracker/src/leak_tracking/_leak_checker.dart';
import 'package:leak_tracker/src/leak_tracking/_leak_reporter.dart';
import 'package:test/test.dart';

// Enum-like static classes are ok.
Expand Down Expand Up @@ -31,21 +31,21 @@ void main() {
const period = Duration(milliseconds: 5);
late final doublePeriod = Duration(microseconds: period.inMicroseconds);

LeakChecker leakChecker({
LeakReporter leakChecker({
required bool checkPeriodically,
required bool hasListener,
required bool hasStdout,
required bool hasDevtools,
}) =>
LeakChecker(
LeakReporter(
leakProvider: leakProvider,
checkPeriod: checkPeriodically ? period : null,
onLeaks: hasListener ? (summary) => listened.store.add(summary) : null,
stdoutSink: hasStdout ? stdout : null,
devToolsSink: hasDevtools ? devtools : null,
);

LeakChecker defaultLeakChecker() => leakChecker(
LeakReporter defaultLeakChecker() => leakChecker(
checkPeriodically: true,
hasListener: false,
hasStdout: true,
Expand Down

0 comments on commit ab158a7

Please sign in to comment.