Skip to content

Instantly share code, notes, and snippets.

Created April 26, 2017 17:37
Show Gist options
  • Save anonymous/ef8caa5da6c1144977023852914fcbd2 to your computer and use it in GitHub Desktop.
Save anonymous/ef8caa5da6c1144977023852914fcbd2 to your computer and use it in GitHub Desktop.
little-sunset-1278
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}';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment