Skip to content

Instantly share code, notes, and snippets.

@stevenroose
Last active August 29, 2015 14:15
Show Gist options
  • Save stevenroose/dfb29e0ac851f99ab541 to your computer and use it in GitHub Desktop.
Save stevenroose/dfb29e0ac851f99ab541 to your computer and use it in GitHub Desktop.
library environment;
import "dart:async";
import "dart:io" deferred as io;
import "dart:html" deferred as html;
Future inEnvironment({Future onIo(), Future onHtml()}) {
Completer c = new Completer();
io.loadLibrary().then((_) {
c.complete(onIo());
}, onError: (_) {});
html.loadLibrary().then((_) {
c.complete(onHtml());
}, onError: (_) {});
return c.future;
}
// usage
void main() {
inEnvironment(onIo: () {
io.WebSocket.connect("").then((ws) {
methodThatNeedsWebsocket(ws, ws);
});
}, onHtml: () {
html.WebSocket ws = new html.WebSocket("");
methodThatNeedsWebsocket(ws, ws);
});
}
void methodThatNeedsWebsocket(Stream responses, StreamSink requests) {
//[...]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment