Last active
August 29, 2015 14:15
-
-
Save stevenroose/dfb29e0ac851f99ab541 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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