Skip to content

Commit

Permalink
Refactor navigate method
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Gilder authored and Tom Gilder committed May 31, 2021
1 parent 5d804d5 commit 6d13bca
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
55 changes: 30 additions & 25 deletions lib/routemaster.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
library routemaster;

export 'src/parser.dart';
export 'src/route_data.dart';
export 'src/pages/guard.dart';

import 'dart:async';
Expand All @@ -15,12 +14,12 @@ import 'src/pages/guard.dart';
import 'src/path_parser.dart';
import 'src/system_nav.dart';
import 'src/trie_router/trie_router.dart';
import 'src/route_data.dart';

part 'src/pages/page_stack.dart';
part 'src/pages/tab_pages.dart';
part 'src/pages/basic_pages.dart';
part 'src/observers.dart';
part 'src/route_data.dart';

/// A function that builds a [Page] from given [RouteData].
typedef PageBuilder = Page Function(RouteData route);
Expand Down Expand Up @@ -313,7 +312,11 @@ class RoutemasterDelegate extends RouterDelegate<RouteData>
// Otherwise we do a convoluted dance which uses a custom UrlStrategy that
// supports replacing the URL.
_navigate(
path,
uri: PathParser.getAbsolutePath(
basePath: currentConfiguration!.fullPath,
path: path,
queryParameters: queryParameters,
),
queryParameters: queryParameters,
isReplacement: true,
);
Expand All @@ -333,10 +336,14 @@ class RoutemasterDelegate extends RouterDelegate<RouteData>

final result = NavigationResult<T>._();
_navigate(
path,
uri: PathParser.getAbsolutePath(
basePath: currentConfiguration!.fullPath,
path: path,
queryParameters: queryParameters,
),
queryParameters: queryParameters,
isReplacement: false,
result: result,
navigationResult: result,
);
return result;
}
Expand Down Expand Up @@ -445,17 +452,17 @@ class RoutemasterDelegate extends RouterDelegate<RouteData>
_state.routeMap = _buildRoutes(context);

final pending = _state.pendingNavigation;

if (pending != null) {
// Process pending navigation after rebuild
_navigate(
pending.uri.toString(),
uri: pending.uri,
isReplacement: pending.isReplacement,
result: pending.result,
navigationResult: pending.result,
useCurrentState: false,
);
} else {
_navigate(
currentConfiguration?.fullPath ?? '/',
uri: currentConfiguration?._uri ?? Uri(path: '/'),
isReplacement: false,
useCurrentState: false,
);
Expand All @@ -471,26 +478,19 @@ class RoutemasterDelegate extends RouterDelegate<RouteData>
_isBuilding = false;
}

void _navigate(
String path, {
void _navigate({
required Uri uri,
required bool isReplacement,
NavigationResult? result,
NavigationResult? navigationResult,
Map<String, String>? queryParameters,
bool useCurrentState = true,
bool isRetry = false,
}) {
_state.pendingNavigation = null;

final uri = PathParser.getAbsolutePath(
basePath: currentConfiguration!.fullPath,
path: path,
queryParameters: queryParameters,
);

final request = _RouteRequest(
uri: uri,
isReplacement: isReplacement,
result: result,
result: navigationResult,
);

var pages = _createAllPageWrappers(
Expand All @@ -500,25 +500,30 @@ class RoutemasterDelegate extends RouterDelegate<RouteData>
);

if (pages == null) {
// No page found from router
if (isRetry) {
// This is a retry after giving the routing map a chance to rebuild
pages = _onUnknownRoute(request);
} else {
// No page has been found, but we don't call onUnknownRoute immediately.
// Instead we schedule a new navigation for after this frame. This is
// for cases where the user has updated the route map (e.g. by changing
// the app state) and called .push() within the same frame.
_state.pendingNavigation = request;

if (!_isBuilding) {
// Schedule rebuild if we're not in build phase
notifyListeners();
}

WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
// If navigate has been called, do nothing

// Check if any page has been found in the meantime
if (_state.pendingNavigation != null) {
// Retry navigation
_navigate(
path,
uri: uri,
isReplacement: isReplacement,
useCurrentState: useCurrentState,
result: result,
navigationResult: navigationResult,
queryParameters: queryParameters,
isRetry: true,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/parser.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'route_data.dart';
import 'package:routemaster/routemaster.dart';

/// A delegate that is used by the [Router] widget to parse URLs.
///
Expand Down
4 changes: 1 addition & 3 deletions lib/src/route_data.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'package:flutter/widgets.dart';
import 'package:routemaster/routemaster.dart';
import 'trie_router/trie_router.dart';
part of '../../routemaster.dart';

/// Information generated from a specific path (URL).
///
Expand Down
4 changes: 2 additions & 2 deletions lib/src/system_nav_web.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:routemaster/routemaster.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'fake_html.dart' if (dart.library.js) 'dart:html';
import 'package:flutter/foundation.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'route_data.dart';
import 'system_nav.dart';

class SystemNav {
Expand Down

0 comments on commit 6d13bca

Please sign in to comment.