Skip to content

Commit

Permalink
fix: replace homegrown mutex with the synchronized package
Browse files Browse the repository at this point in the history
  • Loading branch information
benthillerkus committed Feb 23, 2023
1 parent bbec9dc commit 6ec3702
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
24 changes: 4 additions & 20 deletions lib/src/imperative.dart
Expand Up @@ -141,35 +141,19 @@ class TrayIcon {
_logger.fine("disposed", null, _disposedAt);
}

/// Mutex guard for [_isReal]
bool _makingReal = false;

final _waitingUntilMadeReal = Queue<Completer<void>>();
final _lock = Lock();

/// Ensures the icon has been constructed in native code.
///
/// This is deferred until usage, because constructors can't be `async`.
Future<void> _ensureIsReal() async {
if (_makingReal) {
final completer = Completer();
_waitingUntilMadeReal.addLast(completer);
await completer.future;
}
_makingReal = true;
try {
await _lock.synchronized(() async {
if (!_isReal) {
await _plugin.addTray(_id);
_isReal = true;
final njobs = _waitingUntilMadeReal.length;
while (_waitingUntilMadeReal.isNotEmpty) {
_waitingUntilMadeReal.removeFirst().complete();
}
_logger
.fine("made real (created in native code). unblocked $njobs tasks");
_logger.fine("made real (created in native code).");
}
} finally {
_makingReal = false;
}
});
}

/// Tests if this instance has already been disposed of.
Expand Down
3 changes: 2 additions & 1 deletion lib/src/plugin.dart
Expand Up @@ -2,14 +2,15 @@
/// However, note that trying to interfere with icons managed by a [TrayIcon]
/// will break stuff.
import 'dart:async';
import 'dart:collection';
import 'dart:math';
import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
import 'package:synchronized/extension.dart';
import 'package:synchronized/synchronized.dart';

import 'image.dart';
import 'stock_icon.dart';
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Expand Up @@ -17,6 +17,7 @@ dependencies:
sdk: flutter
logging: ^1.1.1
path: ^1.8.2
synchronized: ^3.0.1

dev_dependencies:
flutter_lints: ^2.0.1
Expand Down

0 comments on commit 6ec3702

Please sign in to comment.