Dart包的条件导入/代码 [英] Conditional imports / code for Dart packages

查看:371
本文介绍了Dart包的条件导入/代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以根据环境标志或Dart中的目标平台有条件地导入库/代码吗?我试图根据目标平台在 dart:io 的ZLibDecoder / ZLibEncoder类和zlib.js之间切换。

Is there any way to conditionally import libraries / code based on environment flags or target platforms in Dart? I'm trying to switch out between dart:io's ZLibDecoder / ZLibEncoder classes and zlib.js based on the target platform.

有一篇文章介绍了如何创建一个统一的界面,但是我无法想象这种技术不会创建重复的代码,也不会创建冗余测试来测试该重复的代码。 game_loop 采用此技术 ,但使用似乎没有共享任何内容的单独的类(GameLoopHtml和GameLoopIsolate)。

There is an article that describes how to create a unified interface, but I'm unable to visualize that technique not creating duplicate code and redundant tests to test that duplicate code. game_loop employs this technique, but uses separate classes (GameLoopHtml and GameLoopIsolate) that don't seem to share anything.

我的代码看起来像这样:

My code looks a bit like this:

class Parser {
  Layer parse(String data) {
    List<int> rawBytes = /* ... */;
    /* stuff you don't care about */
    return new Layer(_inflateBytes(rawBytes));
  }
  String _inflateBytes(List<int> bytes) {
    // Uses ZLibEncoder on dartvm, zlib.js in browser
  }
}

我想通过使用两个独立的类ParserHtml和ParserServer来避免重复代码,除了实现相同的所有功能外, _inflateBytes

I'd like to avoid duplicating code by having two separate classes -- ParserHtml and ParserServer -- that implement everything identically except for _inflateBytes.

编辑:此处的具体示例: https://github.com/radicaled/citadel/blob/master/lib/tilemap/parser.dart 。这是一个TMX(磁贴图XML)解析器。

concrete example here: https://github.com/radicaled/citadel/blob/master/lib/tilemap/parser.dart. It's a TMX (Tile Map XML) parser.

推荐答案

您可以使用镜像(反射)解决此问题。发布包 路径 使用反射来访问 dart:io 或在浏览器中> dart:html

You could use mirrors (reflection) to solve this problem. The pub package path is using reflection to access dart:io on the standalone VM or dart:html in the browser.

源位于此处。好消息是,它们使用 @MirrorsUsed ,因此,镜像api仅包含必需的类。我认为该代码的文档记录非常好,应该很容易为您的代码采用该解决方案。

The source is located here. The good thing is, that they use @MirrorsUsed, so only the required classes are included for the mirrors api. In my opinion the code is documented very good, it should be easy to adopt the solution for your code.

从吸气剂 _io开始 _html (在第72行声明),它们表明您可以加载库,而在您的VM类型上不可用。如果该库不可用,则加载只会返回false。

Start at the getters _io and _html (stating at line 72), they show that you can load a library without that they are available on your type of the VM. Loading just returns false if the library it isn't available.

/// 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')];

// TODO(nweiz): when issue 6490 or 6943 are fixed, make this work under dart2js.
/// 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')];

以后,您可以使用镜像来调用方法或吸气剂。有关示例实现,请参见getter current (从第86行开始)。

Later you can use mirrors to invoke methods or getters. See the getter current (starting at line 86) for an example implementation.

/// Gets the path to the current working directory.
///
/// In the browser, this means the current URL. When using dart2js, this
/// currently returns `.` due to technical constraints. In the future, it will
/// return the current URL.
String get current {
  if (_io != null) {
    return _io.classes[#Directory].getField(#current).reflectee.path;
  } else if (_html != null) {
    return _html.getField(#window).reflectee.location.href;
  } else {
    return '.';
  }
}

正如您在评论中看到的那样,这仅适用于目前是Dart VM。发出后 6490 已解决,它也应在Dart2Js中工作。这可能意味着此解决方案暂时不适合您,但以后将成为解决方案。

As you see in the comments, this only works in the Dart VM at the moment. After issue 6490 is solved, it should work in Dart2Js, too. This may means that this solution isn't applicable for you at the moment, but would be a solution later.

问题 6943 也可能会有帮助,但描述了另一个尚未实现的解决方案。

The issue 6943 could also be helpful, but describes another solution that is not implemented yet.

这篇关于Dart包的条件导入/代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆