Last active
April 27, 2017 22:06
-
-
Save matanlurey/a914eef0d08f2312944cc6723a4e38af to your computer and use it in GitHub Desktop.
An example of adding previous/next mechanics to the Router
This file contains hidden or 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>(); | |
Route _previous; | |
@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) { | |
_previous = route = new Route( | |
route.path, | |
query: new Map.from(_previous?.query ?? const {})..addAll(route.query), | |
); | |
_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