-
-
Save matanlurey/ef4d939121c5ed3d2e942272e1bbdf98 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
import 'dart:async'; | |
main() { | |
final router = new Router(); | |
router.listen(print); | |
router.route(const Route('/', query: const {'referredId': '1234'})); | |
router.route(const Route('/contact')); | |
} | |
/// A simple example of a 'Router' that is just a stream of [Route]. | |
class Router extends Stream<Route> { | |
final StreamController<Route> _onRoute = new StreamController<Route>(); | |
@override | |
StreamSubscription<Route> listen( | |
void onData(Route route), { | |
Function onError, | |
void onDone(), | |
bool cancelOnError, | |
}) { | |
return _onRoute.stream.listen( | |
onData, | |
onError: onError, | |
onDone: onDone, | |
cancelOnError: cancelOnError, | |
); | |
} | |
/// Route to a new [route]. | |
void route(Route route) { | |
_onRoute.add(route); | |
} | |
} | |
/// A simple route representation. | |
class Route { | |
final String path; | |
final Map<String, String> query; | |
const Route(this.path, {this.query: const {}}); | |
@override | |
String toString() => '$Route {$path $query}'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment