A new Router API, modeled as a Stream of Routes.
Find this at dartpad.dartlang.org/?source=e64508eb-f437-43c8-b3dd-9c84936a0216.
Created with <3 with dartpad.dartlang.org.
A new Router API, modeled as a Stream of Routes.
Find this at dartpad.dartlang.org/?source=e64508eb-f437-43c8-b3dd-9c84936a0216.
Created with <3 with dartpad.dartlang.org.
| 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}'; | |
| } | |
| /// A change from a [previous] route to a new ([next]) one. | |
| class Change { | |
| final Route previous; | |
| final Route next; | |
| const Change(this.previous, this.next); | |
| @override | |
| String toString() => '$Change {$previous --> $next}'; | |
| } |