@@ -932,59 +932,59 @@ public Task AddInitScriptAsync(string script, string scriptPath)
932932 } ) ;
933933
934934 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
935- public Task RouteAsync ( string url , Func < IRoute , Task > handler , PageRouteOptions options = null )
936- => RouteAsync ( new Regex ( Context . CombineUrlWithBase ( url ) . GlobToRegex ( ) ) , null , handler , options ) ;
935+ public Task RouteAsync ( string globMatch , Func < IRoute , Task > handler , PageRouteOptions options = null )
936+ => RouteAsync ( globMatch , null , null , handler , options ) ;
937937
938938 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
939- public Task RouteAsync ( string url , Action < IRoute > handler , PageRouteOptions options = null )
940- => RouteAsync ( new Regex ( Context . CombineUrlWithBase ( url ) . GlobToRegex ( ) ) , null , handler , options ) ;
939+ public Task RouteAsync ( string globMatch , Action < IRoute > handler , PageRouteOptions options = null )
940+ => RouteAsync ( globMatch , null , null , handler , options ) ;
941941
942942 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
943- public Task RouteAsync ( Regex url , Action < IRoute > handler , PageRouteOptions options = null )
944- => RouteAsync ( url , null , handler , options ) ;
943+ public Task RouteAsync ( Regex reMatch , Action < IRoute > handler , PageRouteOptions options = null )
944+ => RouteAsync ( null , reMatch , null , handler , options ) ;
945945
946946 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
947- public Task RouteAsync ( Regex url , Func < IRoute , Task > handler , PageRouteOptions options = null )
948- => RouteAsync ( url , null , handler , options ) ;
947+ public Task RouteAsync ( Regex reMatch , Func < IRoute , Task > handler , PageRouteOptions options = null )
948+ => RouteAsync ( null , reMatch , null , handler , options ) ;
949949
950950 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
951- public Task RouteAsync ( Func < string , bool > url , Action < IRoute > handler , PageRouteOptions options = null )
952- => RouteAsync ( null , url , handler , options ) ;
951+ public Task RouteAsync ( Func < string , bool > funcMatch , Action < IRoute > handler , PageRouteOptions options = null )
952+ => RouteAsync ( null , null , funcMatch , handler , options ) ;
953953
954954 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
955- public Task RouteAsync ( Func < string , bool > url , Func < IRoute , Task > handler , PageRouteOptions options = null )
956- => RouteAsync ( null , url , handler , options ) ;
955+ public Task RouteAsync ( Func < string , bool > funcMatch , Func < IRoute , Task > handler , PageRouteOptions options = null )
956+ => RouteAsync ( null , null , funcMatch , handler , options ) ;
957957
958958 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
959959 public async Task UnrouteAllAsync ( PageUnrouteAllOptions options = default )
960960 {
961- await UnrouteInternalAsync ( _routes , new ( ) , options ? . Behavior ) . ConfigureAwait ( false ) ;
961+ await UnrouteInternalAsync ( _routes , [ ] , options ? . Behavior ) . ConfigureAwait ( false ) ;
962962 DisposeHarRouters ( ) ;
963963 }
964964
965965 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
966- public Task UnrouteAsync ( string urlString , Action < IRoute > handler )
967- => UnrouteAsync ( new Regex ( Context . CombineUrlWithBase ( urlString ) . GlobToRegex ( ) ) , null , handler ) ;
966+ public Task UnrouteAsync ( string globMatch , Action < IRoute > handler )
967+ => UnrouteAsync ( globMatch , null , null , handler ) ;
968968
969969 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
970- public Task UnrouteAsync ( string urlString , Func < IRoute , Task > handler )
971- => UnrouteAsync ( new Regex ( Context . CombineUrlWithBase ( urlString ) . GlobToRegex ( ) ) , null , handler ) ;
970+ public Task UnrouteAsync ( string globMatch , Func < IRoute , Task > handler )
971+ => UnrouteAsync ( globMatch , null , null , handler ) ;
972972
973973 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
974- public Task UnrouteAsync ( Regex urlString , Action < IRoute > handler )
975- => UnrouteAsync ( urlString , null , handler ) ;
974+ public Task UnrouteAsync ( Regex reMatch , Action < IRoute > handler )
975+ => UnrouteAsync ( null , reMatch , null , handler ) ;
976976
977977 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
978- public Task UnrouteAsync ( Regex urlString , Func < IRoute , Task > handler )
979- => UnrouteAsync ( urlString , null , handler ) ;
978+ public Task UnrouteAsync ( Regex reMatch , Func < IRoute , Task > handler )
979+ => UnrouteAsync ( null , reMatch , null , handler ) ;
980980
981981 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
982- public Task UnrouteAsync ( Func < string , bool > urlFunc , Action < IRoute > handler )
983- => UnrouteAsync ( null , urlFunc , handler ) ;
982+ public Task UnrouteAsync ( Func < string , bool > funcMatch , Action < IRoute > handler )
983+ => UnrouteAsync ( null , null , funcMatch , handler ) ;
984984
985985 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
986- public Task UnrouteAsync ( Func < string , bool > urlFunc , Func < IRoute , Task > handler )
987- => UnrouteAsync ( null , urlFunc , handler ) ;
986+ public Task UnrouteAsync ( Func < string , bool > funcMatch , Func < IRoute , Task > handler )
987+ => UnrouteAsync ( null , null , funcMatch , handler ) ;
988988
989989 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
990990 public Task WaitForLoadStateAsync ( LoadState ? state = default , PageWaitForLoadStateOptions options = default )
@@ -1235,11 +1235,16 @@ internal void OnFrameNavigated(Frame frame)
12351235
12361236 internal void FirePageError ( string error ) => PageError ? . Invoke ( this , error ) ;
12371237
1238- private Task RouteAsync ( Regex urlRegex , Func < string , bool > urlFunc , Delegate handler , PageRouteOptions options )
1238+ private Task RouteAsync ( string globMatch , Regex reMatch , Func < string , bool > funcMatch , Delegate handler , PageRouteOptions options )
12391239 => RouteAsync ( new ( )
12401240 {
1241- Regex = urlRegex ,
1242- Function = urlFunc ,
1241+ urlMatcher = new URLMatch ( )
1242+ {
1243+ glob = globMatch ,
1244+ re = reMatch ,
1245+ func = funcMatch ,
1246+ baseURL = Context . Options . BaseURL ,
1247+ } ,
12431248 Handler = handler ,
12441249 Times = options ? . Times ,
12451250 } ) ;
@@ -1250,23 +1255,22 @@ private Task RouteAsync(RouteHandler setting)
12501255 return UpdateInterceptionAsync ( ) ;
12511256 }
12521257
1253- private Task UnrouteAsync ( Regex urlRegex , Func < string , bool > urlFunc , Delegate handler = null )
1254- => UnrouteAsync ( new ( )
1255- {
1256- Function = urlFunc ,
1257- Regex = urlRegex ,
1258- Handler = handler ,
1259- } ) ;
1260-
1261- private Task UnrouteAsync ( RouteHandler setting )
1258+ private async Task UnrouteAsync ( string globMatch , Regex reMatch , Func < string , bool > funcMatch , Delegate handler )
12621259 {
1263- var newRoutes = new List < RouteHandler > ( ) ;
1264- newRoutes . AddRange ( _routes . Where ( r =>
1265- ( setting . Regex != null && ! ( r . Regex == setting . Regex || ( r . Regex . ToString ( ) == setting . Regex . ToString ( ) && r . Regex . Options == setting . Regex . Options ) ) ) ||
1266- ( setting . Function != null && r . Function != setting . Function ) ||
1267- ( setting . Handler != null && r . Handler != setting . Handler ) ) ) ;
1268- _routes = newRoutes ;
1269- return UpdateInterceptionAsync ( ) ;
1260+ var removed = new List < RouteHandler > ( ) ;
1261+ var remaining = new List < RouteHandler > ( ) ;
1262+ foreach ( var routeHandler in _routes )
1263+ {
1264+ if ( routeHandler . urlMatcher . Equals ( globMatch , reMatch , funcMatch , Context . Options . BaseURL ) && ( handler == null || routeHandler . Handler == handler ) )
1265+ {
1266+ removed . Add ( routeHandler ) ;
1267+ }
1268+ else
1269+ {
1270+ remaining . Add ( routeHandler ) ;
1271+ }
1272+ }
1273+ await UnrouteInternalAsync ( removed , remaining , UnrouteBehavior . Default ) . ConfigureAwait ( false ) ;
12701274 }
12711275
12721276 private async Task UnrouteInternalAsync ( List < RouteHandler > removed , List < RouteHandler > remaining , UnrouteBehavior ? behavior )
@@ -1277,7 +1281,7 @@ private async Task UnrouteInternalAsync(List<RouteHandler> removed, List<RouteHa
12771281 {
12781282 return ;
12791283 }
1280- var tasks = removed . Select ( routeHandler => routeHandler . StopAsync ( ( UnrouteBehavior ) behavior ) ) ;
1284+ var tasks = removed . Select ( routeHandler => routeHandler . StopAsync ( behavior . Value ) ) ;
12811285 await Task . WhenAll ( tasks ) . ConfigureAwait ( false ) ;
12821286 }
12831287
@@ -1317,17 +1321,14 @@ private void Channel_BindingCall(object sender, BindingCall bindingCall)
13171321 private async Task OnRouteAsync ( Route route )
13181322 {
13191323 route . _context = Context ;
1320- var routeHandlers = _routes . ToArray ( ) ;
1321- foreach ( var routeHandler in routeHandlers )
1324+ foreach ( var routeHandler in _routes . ToArray ( ) )
13221325 {
13231326 // If the page was closed we stall all requests right away.
13241327 if ( CloseWasCalled || Context . CloseWasCalled )
13251328 {
13261329 return ;
13271330 }
1328- var matches = ( routeHandler . Regex ? . IsMatch ( route . Request . Url ) == true ) ||
1329- ( routeHandler . Function ? . Invoke ( route . Request . Url ) == true ) ;
1330- if ( ! matches )
1331+ if ( ! routeHandler . Matches ( route . Request . Url ) )
13311332 {
13321333 continue ;
13331334 }
@@ -1578,7 +1579,7 @@ public async Task RemoveLocatorHandlerAsync(ILocator locator)
15781579 [ "uid" ] = uid ,
15791580 } ) . ConfigureAwait ( false ) ;
15801581 }
1581- catch ( System . Exception )
1582+ catch ( Exception )
15821583 {
15831584 // Ignore
15841585 }
@@ -1602,12 +1603,12 @@ private Task RouteWebSocketAsync(string globMatch, Regex urlRegex, Func<string,
16021603 {
16031604 _webSocketRoutes . Insert ( 0 , new WebSocketRouteHandler ( )
16041605 {
1605- URL = new URLMatch ( )
1606+ urlMatcher = new URLMatch ( )
16061607 {
1607- BaseURL = Context . Options . BaseURL ,
1608- globMatch = globMatch ,
1609- reMatch = urlRegex ,
1610- funcMatch = urlFunc ,
1608+ baseURL = Context . Options . BaseURL ,
1609+ glob = globMatch ,
1610+ re = urlRegex ,
1611+ func = urlFunc ,
16111612 } ,
16121613 Handler = handler ,
16131614 } ) ;
0 commit comments