mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Preparation for landing https://dart-review.googlesource.com/c/sdk/+/443460 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
131 lines
4.3 KiB
Dart
131 lines
4.3 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'dart:async';
|
|
import 'dart:collection';
|
|
|
|
import 'package:analyzer/dart/analysis/results.dart';
|
|
import 'package:analyzer/dart/ast/ast.dart';
|
|
import 'package:analyzer/dart/ast/token.dart';
|
|
import 'package:analyzer/dart/element/element.dart';
|
|
import 'package:analyzer/dart/element/type.dart';
|
|
import 'package:analyzer/source/source.dart';
|
|
import 'package:collection/collection.dart';
|
|
|
|
import 'dependency_graph.dart';
|
|
|
|
extension TokenExtension on Token {
|
|
/// Convenience getter to identify tokens for private fields and functions.
|
|
bool get isPrivate => toString().startsWith('_');
|
|
|
|
/// Convenience getter to identify WidgetBuilder types.
|
|
bool get isWidgetBuilder => toString() == 'WidgetBuilder';
|
|
|
|
/// Convenience getter to identify Widget types.
|
|
bool get isWidget => toString() == 'Widget';
|
|
}
|
|
|
|
extension on InterfaceType {
|
|
bool isType({required String typeName, required Uri uri}) {
|
|
if (getDisplayString() == typeName && element.library.uri == uri) {
|
|
return true;
|
|
}
|
|
return allSupertypes.firstWhereOrNull((e) {
|
|
return e.getDisplayString() == typeName && e.element.library.uri == uri;
|
|
}) !=
|
|
null;
|
|
}
|
|
}
|
|
|
|
extension AnnotationExtension on Annotation {
|
|
static final Uri widgetPreviewsLibraryUri = Uri.parse(
|
|
'package:flutter/src/widget_previews/widget_previews.dart',
|
|
);
|
|
|
|
bool _isPreviewType(String typeName) {
|
|
final Element? element = elementAnnotation!.element;
|
|
if (element is ConstructorElement) {
|
|
final InterfaceType type = element.enclosingElement.thisType;
|
|
return type.isType(typeName: typeName, uri: widgetPreviewsLibraryUri);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// Convenience getter to identify `@Preview` annotations
|
|
bool get isPreview => _isPreviewType('Preview');
|
|
|
|
/// Convenience getter to identify `@MultiPreview` annotations
|
|
bool get isMultiPreview => _isPreviewType('MultiPreview');
|
|
}
|
|
|
|
/// Convenience getters for examining [String] paths.
|
|
extension StringExtension on String {
|
|
bool get isDartFile => endsWith('.dart');
|
|
bool get isPubspec => endsWith('pubspec.yaml');
|
|
bool get doesContainDartTool => contains('.dart_tool');
|
|
}
|
|
|
|
extension LibraryElementExtension on LibraryElement {
|
|
/// Convenience method to package path and [uri] into a [PreviewPath]
|
|
PreviewPath toPreviewPath() => (path: firstFragment.source.fullName, uri: uri);
|
|
}
|
|
|
|
extension ParsedUnitResultExtension on ParsedUnitResult {
|
|
/// Convenience method to package [path] and [uri] into a [PreviewPath]
|
|
PreviewPath toPreviewPath() => (path: path, uri: uri);
|
|
}
|
|
|
|
extension SourceExtension on Source {
|
|
/// Convenience method to package [fullName] and [uri] into a [PreviewPath]
|
|
PreviewPath toPreviewPath() => (path: fullName, uri: uri);
|
|
}
|
|
|
|
/// Used to protect global state accessed in blocks containing calls to
|
|
/// asynchronous methods.
|
|
///
|
|
/// Originally from DDS:
|
|
/// https://github.com/dart-lang/sdk/blob/3fe58da3cfe2c03fb9ee691a7a4709082fad3e73/pkg/dds/lib/src/utils/mutex.dart
|
|
class PreviewDetectorMutex {
|
|
/// Executes a block of code containing asynchronous calls atomically.
|
|
///
|
|
/// If no other asynchronous context is currently executing within
|
|
/// [criticalSection], it will immediately be called. Otherwise, the
|
|
/// caller will be suspended and entered into a queue to be resumed once the
|
|
/// lock is released.
|
|
Future<T> runGuarded<T>(FutureOr<T> Function() criticalSection) async {
|
|
try {
|
|
await _acquireLock();
|
|
return await criticalSection();
|
|
} finally {
|
|
_releaseLock();
|
|
}
|
|
}
|
|
|
|
Future<void> _acquireLock() async {
|
|
if (!_locked) {
|
|
_locked = true;
|
|
return;
|
|
}
|
|
|
|
final request = Completer<void>();
|
|
_outstandingRequests.add(request);
|
|
await request.future;
|
|
}
|
|
|
|
void _releaseLock() {
|
|
if (_outstandingRequests.isNotEmpty) {
|
|
final Completer<void> request = _outstandingRequests.removeFirst();
|
|
request.complete();
|
|
return;
|
|
}
|
|
// Only release the lock if no other requests are pending to prevent races
|
|
// between the next request from the queue to be handled and incoming
|
|
// requests.
|
|
_locked = false;
|
|
}
|
|
|
|
var _locked = false;
|
|
final _outstandingRequests = Queue<Completer<void>>();
|
|
}
|