Skip to content

Commit

Permalink
Delay jumpTo* final return until actually complete
Browse files Browse the repository at this point in the history
  • Loading branch information
amake committed Dec 19, 2024
1 parent f3ef4da commit e9e04a7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
20 changes: 11 additions & 9 deletions lib/src/locator.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:org_flutter/src/controller.dart';
import 'package:org_flutter/src/locatable.dart';
Expand Down Expand Up @@ -84,14 +86,14 @@ class _OrgLocatorState extends State<OrgLocator> {
// Target widget is probably not currently visible, so make it visible and
// then listen for its key to become available.
_controller.ensureVisible(result.path);
_radioTargetKeys.listenOnce(() async {
return await _radioTargetKeys.listenOnce(() async {
final key = _radioTargetKeys.value[id];
if (await _makeVisible(key)) {
key!.currentState?.doHighlight();
return true;
}
return false;
});

return true;
}

Future<bool> _jumpToLinkTarget(String body) async {
Expand All @@ -109,14 +111,14 @@ class _OrgLocatorState extends State<OrgLocator> {
// Target widget is probably not currently visible, so make it visible and
// then listen for its key to become available.
_controller.ensureVisible(result.path);
_linkTargetKeys.listenOnce(() async {
return await _linkTargetKeys.listenOnce(() async {
final key = _linkTargetKeys.value[keyId];
if (await _makeVisible(key)) {
key!.currentState?.doHighlight();
return true;
}
return false;
});

return true;
}

Future<bool> _jumpToName(String name) async {
Expand All @@ -135,14 +137,14 @@ class _OrgLocatorState extends State<OrgLocator> {
// Target widget is probably not currently visible, so make it visible and
// then listen for its key to become available.
_controller.ensureVisible(result.path);
_nameKeys.listenOnce(() async {
return await _nameKeys.listenOnce(() async {
final key = _nameKeys.value[keyId];
if (await _makeVisible(key)) {
key!.currentState?.doHighlight();
return true;
}
return false;
});

return true;
}

@override
Expand Down
10 changes: 8 additions & 2 deletions lib/src/util/value_notifier.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/foundation.dart';

class SafeValueNotifier<T> extends ValueNotifier<T> {
Expand All @@ -15,12 +17,16 @@ class SafeValueNotifier<T> extends ValueNotifier<T> {
}

extension ValueNotifierUtil<T> on ValueNotifier<T> {
void listenOnce(VoidCallback callback) {
Future<U> listenOnce<U>(FutureOr<U> Function() callback) {
final result = Completer<U>();

void listener() {
callback();
result.complete(callback());
removeListener(listener);
}

addListener(listener);

return result.future;
}
}

0 comments on commit e9e04a7

Please sign in to comment.