@@ -756,10 +756,6 @@ export function createRouter(init: RouterInit): Router {
756
756
// cancel active deferreds for eliminated routes.
757
757
let activeDeferreds = new Map < string , DeferredData > ( ) ;
758
758
759
- // We ony support a single active blocker at the moment since we don't have
760
- // any compelling use cases for multi-blocker yet
761
- let activeBlocker : string | null = null ;
762
-
763
759
// Store blocker functions in a separate Map outside of router state since
764
760
// we don't need to update UI state if they change
765
761
let blockerFunctions = new Map < string , BlockerFunction > ( ) ;
@@ -784,7 +780,7 @@ export function createRouter(init: RouterInit): Router {
784
780
}
785
781
786
782
warning (
787
- activeBlocker != null && delta == = null ,
783
+ blockerFunctions . size === 0 || delta ! = null ,
788
784
"You are trying to use a blocker on a POP navigation to a location " +
789
785
"that was not created by @remix-run/router. This will fail silently in " +
790
786
"production. This can happen if you are navigating outside the router " +
@@ -2129,12 +2125,6 @@ export function createRouter(init: RouterInit): Router {
2129
2125
2130
2126
if ( blockerFunctions . get ( key ) !== fn ) {
2131
2127
blockerFunctions . set ( key , fn ) ;
2132
- if ( activeBlocker == null ) {
2133
- // This is now the active blocker
2134
- activeBlocker = key ;
2135
- } else if ( key !== activeBlocker ) {
2136
- warning ( false , "A router only supports one blocker at a time" ) ;
2137
- }
2138
2128
}
2139
2129
2140
2130
return blocker ;
@@ -2143,9 +2133,6 @@ export function createRouter(init: RouterInit): Router {
2143
2133
function deleteBlocker ( key : string ) {
2144
2134
state . blockers . delete ( key ) ;
2145
2135
blockerFunctions . delete ( key ) ;
2146
- if ( activeBlocker === key ) {
2147
- activeBlocker = null ;
2148
- }
2149
2136
}
2150
2137
2151
2138
// Utility function to update blockers, ensuring valid state transitions
@@ -2176,18 +2163,19 @@ export function createRouter(init: RouterInit): Router {
2176
2163
nextLocation : Location ;
2177
2164
historyAction : HistoryAction ;
2178
2165
} ) : string | undefined {
2179
- if ( activeBlocker == null ) {
2166
+ if ( blockerFunctions . size === 0 ) {
2180
2167
return ;
2181
2168
}
2182
2169
2183
- // We only allow a single blocker at the moment. This will need to be
2184
- // updated if we enhance to support multiple blockers in the future
2185
- let blockerFunction = blockerFunctions . get ( activeBlocker ) ;
2186
- invariant (
2187
- blockerFunction ,
2188
- "Could not find a function for the active blocker"
2189
- ) ;
2190
- let blocker = state . blockers . get ( activeBlocker ) ;
2170
+ // We ony support a single active blocker at the moment since we don't have
2171
+ // any compelling use cases for multi-blocker yet
2172
+ if ( blockerFunctions . size > 1 ) {
2173
+ warning ( false , "A router only supports one blocker at a time" ) ;
2174
+ }
2175
+
2176
+ let entries = Array . from ( blockerFunctions . entries ( ) ) ;
2177
+ let [ blockerKey , blockerFunction ] = entries [ entries . length - 1 ] ;
2178
+ let blocker = state . blockers . get ( blockerKey ) ;
2191
2179
2192
2180
if ( blocker && blocker . state === "proceeding" ) {
2193
2181
// If the blocker is currently proceeding, we don't need to re-check
@@ -2198,7 +2186,7 @@ export function createRouter(init: RouterInit): Router {
2198
2186
// At this point, we know we're unblocked/blocked so we need to check the
2199
2187
// user-provided blocker function
2200
2188
if ( blockerFunction ( { currentLocation, nextLocation, historyAction } ) ) {
2201
- return activeBlocker ;
2189
+ return blockerKey ;
2202
2190
}
2203
2191
}
2204
2192
0 commit comments