Permalink
Browse files

feature: Added onError to onEnterCallback

  • Loading branch information...
MikeMitterer committed Nov 21, 2018
1 parent ed4196b commit 44bb91414498208e63b40daece10813ff11be06a
@@ -17,21 +17,21 @@ void _configRouter(final Router router ) {

router
..addRoute(name: "Test I", path: new UrlPattern('/#/test'),
enter: (final RouteEnterEvent event) {
enter: (final RouteEnterEvent event,[ void onError(final Exception exception )]) {
_log(event.route.title);
_logger.info("Path: ${event.path} Params: ${event.params.join(",")}");
_showImage("https://dummyimage.com/400x300/ff0000/000000.png&text=Test");
})

..addRoute(name: 'Kitten', path: new ReactPattern('/kitten'),
enter: (final RouteEnterEvent event) {
enter: (final RouteEnterEvent event,[ void onError(final Exception exception )]) {
_log(event.route.title);
_logger.info("Path: ${event.path} Params: ${event.params.join(",")}");
_showImage("https://raw.githubusercontent.com/MikeMitterer/m4d_router/master/doc/images/cats/IMG-20150820-WA0000.jpg");
})

..addRoute(name: 'Specific cat', path: new ReactPattern(r'/cats/([\w%]+)'),
enter: (final RouteEnterEvent event) {
enter: (final RouteEnterEvent event,[ void onError(final Exception exception )]) {
_log("${event.route.title}: ${event.params.join(",")}");
_logger.info("Path: ${event.path} Params: ${event.params.join(",")}");
if(event.params.first.toLowerCase() == "grumpy cat") {
@@ -42,7 +42,7 @@ void _configRouter(final Router router ) {
})

..addRoute(name: "Google for cats", path: new ReactPattern('/google'),
enter: (final RouteEnterEvent event) {
enter: (final RouteEnterEvent event,[ void onError(final Exception exception )]) {
_log(event.route.title);
_logger.info("Path: ${event.path} Params: ${event.params.join(",")}");
_showImage("https://upload.wikimedia.org/wikipedia/commons/a/a5/Google_Chrome_icon_%28September_2014%29.svg");
@@ -24,7 +24,9 @@ typedef void EventHandler(final Event e);

final _logger = new Logger('m4d_router.browser');

void _defaultEnterCallback(final RouteEnterEvent event) {
void _defaultEnterCallback(final RouteEnterEvent event,
[ void onError(final Exception exception)]) {

_logger.fine(
"Default-Callback for ${event.route.title}. "
"(Path: ${event.path} Params: ${event.params.join(",")})"
@@ -279,7 +281,8 @@ class Router {

void _fire(final RouteEvent event) {
// _logger.info("onChange: ${_onChange}, hasListeners: ${_onChange ?.hasListener}");
print(event);
// print(event);

if(event is RouteErrorEvent) {
if (true == _onError?.hasListener) {
_onError.add(event);
@@ -289,8 +292,11 @@ class Router {
if (true == _onEnter?.hasListener) {
_onEnter.add(event);
}

// Call callback defined with route
event.route.onEnter(event);
event.route.onEnter(event, (final Exception exception) {
_onError?.add(RouteErrorEvent(exception, event.path));
});
}
else {
throw ArgumentError("Undefined RouteEvent! ($event)");
@@ -21,7 +21,9 @@ library route.client;

import 'package:m4d_router/url_pattern.dart';

typedef void RouteEnterCallback(final RouteEnterEvent event);
typedef void RouteEnterCallback(final RouteEnterEvent event,
[ void onError(final Exception exception)]
);

/// Route enter event.
abstract class RouteEvent {
@@ -21,7 +21,7 @@ main() async {

test('go should route to cats', () {

final callback = expectAsync1((final RouteEnterEvent event) {
final RouteEnterCallback callback = expectAsync2((final RouteEnterEvent event,void onError(final Exception exception)) {
expect(event, isNotNull);
expect(event.route.title, "Cats");
});
@@ -21,7 +21,7 @@ main() async {

test('gotoPath should fetch params', () {

final callback = expectAsync1((final RouteEnterEvent event) {
final callback = expectAsync2((final RouteEnterEvent event,void onError(final Exception exception)) {
expect(event, isNotNull);
expect(event.route.title, "Specific cat");
expect(event.params.first, "Grumpy cat");
@@ -20,7 +20,7 @@ main() async {
final router = new Router();
test('gotoUrl should receive params', () {

final callback = expectAsync1((final RouteEnterEvent event) {
final callback = expectAsync2((final RouteEnterEvent event,void onError(final Exception exception)) {
expect(event, isNotNull);
expect(event.route.title, "Specific cat");
expect(event.params.first, "Grumpy cat");
@@ -21,7 +21,7 @@ main() async {

test('onEnter should be called for link', () {

final callback = expectAsync1((final RouteEnterEvent event) {
final callback = expectAsync2((final RouteEnterEvent event,void onError(final Exception exception)) {
expect(event, isNotNull);
expect(event.route.title, "Cats");
});
@@ -21,7 +21,7 @@ main() async {

test('onError should be called for root', () {

final callback = expectAsync1((final RouteEnterEvent event) {
final callback = expectAsync2((final RouteEnterEvent event,void onError(final Exception exception)) {
expect(event, isNotNull);
expect(event.route.title, "Cats");
});

0 comments on commit 44bb914

Please sign in to comment.