Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Improve tree-shaking:
Browse files Browse the repository at this point in the history
* Use `dart2js:prefer-inline` everywhere where we use `vm:prefer-inline`.
* Make `platformOS` const/platform-const by wrapping version into a
thunk. This means you don't have to lazy initialize the final field.
  • Loading branch information
mraleph committed Jun 12, 2024
1 parent 6307589 commit 0dd6dfd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
7 changes: 7 additions & 0 deletions lib/os_detect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ String get operatingSystemVersion => OperatingSystem.current.version;
/// for example Android (see [isAndroid]),
/// or if the code is running inside a browser (see [isBrowser]).
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isLinux => OperatingSystem.current.isLinux;

/// Whether the current operating system is a version of
Expand All @@ -45,6 +46,7 @@ bool get isLinux => OperatingSystem.current.isLinux;
/// The value is `false` if the code is running inside a browser,
/// even if that browser is running on MacOS (see [isBrowser]).
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isMacOS => OperatingSystem.current.isMacOS;

/// Whether the current operating system is a version of
Expand All @@ -55,6 +57,7 @@ bool get isMacOS => OperatingSystem.current.isMacOS;
/// The value is `false` if the code is running inside a browser,
/// even if that browser is running on Windows (see [isBrowser]).
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isWindows => OperatingSystem.current.isWindows;

/// Whether the current operating system is a version of
Expand All @@ -65,6 +68,7 @@ bool get isWindows => OperatingSystem.current.isWindows;
/// The value is `false` if the code is running inside a browser,
/// even if that browser is running on Android (see [isBrowser]).
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isAndroid => OperatingSystem.current.isAndroid;

/// Whether the current operating system is a version of
Expand All @@ -75,6 +79,7 @@ bool get isAndroid => OperatingSystem.current.isAndroid;
/// The value is `false` if the code is running inside a browser,
/// even if that browser is running on iOS (see [isBrowser]).
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isIOS => OperatingSystem.current.isIOS;

/// Whether the current operating system is a version of
Expand All @@ -85,6 +90,7 @@ bool get isIOS => OperatingSystem.current.isIOS;
/// The value is `false` if the code is running inside a browser,
/// even if that browser is running on Fuchsia (see [isBrowser]).
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isFuchsia => OperatingSystem.current.isFuchsia;

/// Whether running in a web browser.
Expand All @@ -101,4 +107,5 @@ bool get isFuchsia => OperatingSystem.current.isFuchsia;
/// but browsers are able to lie in the app-version/user-agent
/// string.
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
bool get isBrowser => OperatingSystem.current.isBrowser;
16 changes: 10 additions & 6 deletions lib/src/os_override.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ final class OperatingSystem {
/// from known platform specific libraries,
/// but can be overridden using functionality from the
/// `osid_override.dart` library.
@pragma('vm:try-inline')
@pragma('vm:prefer-inline')
@pragma('dart2js:prefer-inline')
static OperatingSystem get current =>
Zone.current[#_os] as OperatingSystem? ?? platformOS;

Expand All @@ -73,10 +74,12 @@ final class OperatingSystem {
// Operating system ID object.
final RecognizedOS _osId;

final String Function() _computeVersion;

/// A string representing the version of the operating system or platform.
///
/// May be empty if no version is known or available.
final String version;
String get version => _computeVersion();

/// Creates a new operating system object for testing.
///
Expand All @@ -91,7 +94,8 @@ final class OperatingSystem {
// That can avoid retaining *all* the subclasses of `OS`.
@visibleForTesting
@pragma('vm:prefer-inline')
OperatingSystem(String id, String version)
@pragma('dart2js:prefer-inline')
OperatingSystem(String id, String Function() computeVersion)
: this._(
id == linuxId
? const LinuxOS()
Expand All @@ -108,10 +112,10 @@ final class OperatingSystem {
: id == browserId
? const BrowserOS()
: UnknownOS(id),
version);
computeVersion);

/// Used by platforms which know the ID object.
const OperatingSystem._(this._osId, this.version);
const OperatingSystem._(this._osId, this._computeVersion);

/// Whether the operating system is a version of
/// [Linux](https://en.wikipedia.org/wiki/Linux).
Expand Down Expand Up @@ -176,5 +180,5 @@ R overrideOperatingSystem<R>(
// Exposes the `OperatingSystem._` constructor to the conditionally imported
// libraries. Not exported by `../override.dart'.
final class OperatingSystemInternal extends OperatingSystem {
const OperatingSystemInternal(super.id, super.version) : super._();
const OperatingSystemInternal(super.id, super.computeVersion) : super._();
}
6 changes: 3 additions & 3 deletions lib/src/osid_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:html';
import 'os_kind.dart' show BrowserOS;
import 'os_override.dart';

String get _osVersion => window.navigator.appVersion;
String _osVersion() => window.navigator.appVersion;

final OperatingSystem platformOS =
OperatingSystemInternal(const BrowserOS(), _osVersion);
const OperatingSystem platformOS =
OperatingSystemInternal(BrowserOS(), _osVersion);
6 changes: 4 additions & 2 deletions lib/src/osid_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final RecognizedOS? _osType = Platform.operatingSystem == RecognizedOS.linuxId
? const BrowserOS()
: null;

String _osVersion() => Platform.operatingSystemVersion;

@pragma('vm:platform-const')
final OperatingSystem platformOS = OperatingSystemInternal(
_osType ?? UnknownOS(Platform.operatingSystem),
Platform.operatingSystemVersion);
_osType ?? UnknownOS(Platform.operatingSystem), _osVersion);
2 changes: 1 addition & 1 deletion lib/src/osid_unknown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'os_override.dart';
@pragma('vm:platform-const')
const String _os =
String.fromEnvironment('dart.os.name', defaultValue: 'unknown');
const String _osVersion = String.fromEnvironment('dart.os.version');
String _osVersion() => const String.fromEnvironment('dart.os.version');

const OperatingSystem platformOS = OperatingSystemInternal(
_os == RecognizedOS.linuxId
Expand Down

0 comments on commit 0dd6dfd

Please sign in to comment.