Skip to content

Instantly share code, notes, and snippets.

@hyamamoto
Last active August 29, 2015 13:56
Show Gist options
  • Save hyamamoto/8968589 to your computer and use it in GitHub Desktop.
Save hyamamoto/8968589 to your computer and use it in GitHub Desktop.
"isClient", "isServer", "isDartClient", "isJsClient" properties to check if the Dart application is running whether on Javascript enabled browser, on Chronium browser, or as a server process.
library vmcheck;
import 'package:path/src/style.dart';
// This snippet is for Dart >= 1.1
bool get isClient => Style.platform == Style.url;
bool get isServer => Style.platform != Style.url;
// Uncomment to check if this application is running on Dart-enabled-browser or not.
//
//import 'dart:html';
//
//bool get _isDart => document.getElementsByTagName("script").where((s) => s.src.endsWith(".dart.js")).isEmpty;
//
//bool get isDartClient => isClient && _isDart;
//
//bool get isJsClient => isClient && !_isDart;
library vmcheck_older;
// This snippet is for Dart =< 1.0
// Checking method is based on the code found in earlier version of Dart 'path library'.
import 'dart:mirrors';
/// If we're running in the server-side Dart VM, this will return a
/// [LibraryMirror] that gives access to the `dart:io` library.
///
/// If `dart:io` is not available, this returns null.
LibraryMirror get _io => currentMirrorSystem().libraries[Uri.parse('dart:io')];
/// If we're running in Dartium, this will return a [LibraryMirror] that gives
/// access to the `dart:html` library.
///
/// If `dart:html` is not available, this returns null.
LibraryMirror get _html =>
currentMirrorSystem().libraries[Uri.parse('dart:html')];
bool get isClient => _io == null;
bool get isServer => _io != null;
bool get isDartClient => isClient && _html == null;
bool get isJsClient => isClient && _html != null;
@hyamamoto
Copy link
Author

This snippet has become a single package available from "pub" repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment