@@ -25,29 +25,29 @@ abstract class BrowserHistory {
25
25
late ui.VoidCallback _unsubscribe;
26
26
27
27
/// The strategy to interact with html browser history.
28
- LocationStrategy ? get locationStrategy => _locationStrategy ;
29
- LocationStrategy ? _locationStrategy ;
28
+ JsUrlStrategy ? get urlStrategy => _urlStrategy ;
29
+ JsUrlStrategy ? _urlStrategy ;
30
30
/// Updates the strategy.
31
31
///
32
32
/// This method will also remove any previous modifications to the html
33
33
/// browser history and start anew.
34
- Future <void > setLocationStrategy ( LocationStrategy ? strategy) async {
35
- if (strategy != _locationStrategy ) {
36
- await _tearoffStrategy (_locationStrategy );
37
- _locationStrategy = strategy;
38
- await _setupStrategy (_locationStrategy );
34
+ Future <void > setUrlStrategy ( JsUrlStrategy ? strategy) async {
35
+ if (strategy != _urlStrategy ) {
36
+ await _tearoffStrategy (_urlStrategy );
37
+ _urlStrategy = strategy;
38
+ await _setupStrategy (_urlStrategy );
39
39
}
40
40
}
41
41
42
- Future <void > _setupStrategy (LocationStrategy ? strategy) async {
42
+ Future <void > _setupStrategy (JsUrlStrategy ? strategy) async {
43
43
if (strategy == null ) {
44
44
return ;
45
45
}
46
46
_unsubscribe = strategy.onPopState (onPopState as dynamic Function (html.Event ));
47
47
await setup ();
48
48
}
49
49
50
- Future <void > _tearoffStrategy (LocationStrategy ? strategy) async {
50
+ Future <void > _tearoffStrategy (JsUrlStrategy ? strategy) async {
51
51
if (strategy == null ) {
52
52
return ;
53
53
}
@@ -58,28 +58,28 @@ abstract class BrowserHistory {
58
58
59
59
/// Exit this application and return to the previous page.
60
60
Future <void > exit () async {
61
- if (_locationStrategy != null ) {
62
- await _tearoffStrategy (_locationStrategy );
61
+ if (_urlStrategy != null ) {
62
+ await _tearoffStrategy (_urlStrategy );
63
63
// Now the history should be in the original state, back one more time to
64
64
// exit the application.
65
- await _locationStrategy ! . back ( );
66
- _locationStrategy = null ;
65
+ await _urlStrategy ! . go ( - 1 );
66
+ _urlStrategy = null ;
67
67
}
68
68
}
69
69
70
70
/// This method does the same thing as the browser back button.
71
71
Future <void > back () {
72
- if (locationStrategy != null ) {
73
- return locationStrategy ! . back ( );
72
+ if (_urlStrategy != null ) {
73
+ return _urlStrategy ! . go ( - 1 );
74
74
}
75
75
return Future <void >.value ();
76
76
}
77
77
78
78
/// The path of the current location of the user's browser.
79
- String get currentPath => locationStrategy ? .path ?? '/' ;
79
+ String get currentPath => urlStrategy ? . getPath () ?? '/' ;
80
80
81
81
/// The state of the current location of the user's browser.
82
- dynamic get currentState => locationStrategy ? .state ;
82
+ dynamic get currentState => urlStrategy ? . getState () ;
83
83
84
84
/// Update the url with the given [routeName] and [state] .
85
85
void setRouteName (String ? routeName, {dynamic ? state});
@@ -137,10 +137,10 @@ class MultiEntriesBrowserHistory extends BrowserHistory {
137
137
138
138
@override
139
139
void setRouteName (String ? routeName, {dynamic ? state}) {
140
- if (locationStrategy != null ) {
140
+ if (urlStrategy != null ) {
141
141
assert (routeName != null );
142
142
_lastSeenSerialCount += 1 ;
143
- locationStrategy ! .pushState (
143
+ urlStrategy ! .pushState (
144
144
_tagWithSerialCount (state, _lastSeenSerialCount),
145
145
'flutter' ,
146
146
routeName! ,
@@ -150,13 +150,13 @@ class MultiEntriesBrowserHistory extends BrowserHistory {
150
150
151
151
@override
152
152
void onPopState (covariant html.PopStateEvent event) {
153
- assert (locationStrategy != null );
153
+ assert (urlStrategy != null );
154
154
// May be a result of direct url access while the flutter application is
155
155
// already running.
156
156
if (! _hasSerialCount (event.state)) {
157
157
// In this case we assume this will be the next history entry from the
158
158
// last seen entry.
159
- locationStrategy ! .replaceState (
159
+ urlStrategy ! .replaceState (
160
160
_tagWithSerialCount (event.state, _lastSeenSerialCount + 1 ),
161
161
'flutter' ,
162
162
currentPath);
@@ -179,7 +179,7 @@ class MultiEntriesBrowserHistory extends BrowserHistory {
179
179
@override
180
180
Future <void > setup () {
181
181
if (! _hasSerialCount (currentState)) {
182
- locationStrategy ! .replaceState (
182
+ urlStrategy ! .replaceState (
183
183
_tagWithSerialCount (currentState, 0 ),
184
184
'flutter' ,
185
185
currentPath
@@ -196,11 +196,11 @@ class MultiEntriesBrowserHistory extends BrowserHistory {
196
196
assert (_hasSerialCount (currentState));
197
197
int backCount = _currentSerialCount;
198
198
if (backCount > 0 ) {
199
- await locationStrategy ! . back (count : backCount);
199
+ await urlStrategy ! . go ( - backCount);
200
200
}
201
201
// Unwrap state.
202
202
assert (_hasSerialCount (currentState) && _currentSerialCount == 0 );
203
- locationStrategy ! .replaceState (
203
+ urlStrategy ! .replaceState (
204
204
currentState['state' ],
205
205
'flutter' ,
206
206
currentPath,
@@ -254,16 +254,16 @@ class SingleEntryBrowserHistory extends BrowserHistory {
254
254
255
255
@override
256
256
void setRouteName (String ? routeName, {dynamic ? state}) {
257
- if (locationStrategy != null ) {
258
- _setupFlutterEntry (locationStrategy ! , replace: true , path: routeName);
257
+ if (urlStrategy != null ) {
258
+ _setupFlutterEntry (urlStrategy ! , replace: true , path: routeName);
259
259
}
260
260
}
261
261
262
262
String ? _userProvidedRouteName;
263
263
@override
264
264
void onPopState (covariant html.PopStateEvent event) {
265
265
if (_isOriginEntry (event.state)) {
266
- _setupFlutterEntry (_locationStrategy ! );
266
+ _setupFlutterEntry (_urlStrategy ! );
267
267
268
268
// 2. Send a 'popRoute' platform message so the app can handle it accordingly.
269
269
if (window._onPlatformMessage != null ) {
@@ -305,22 +305,22 @@ class SingleEntryBrowserHistory extends BrowserHistory {
305
305
// 2. Then we remove the new entry.
306
306
// This will take us back to our "flutter" entry and it causes a new
307
307
// popstate event that will be handled in the "else if" section above.
308
- _locationStrategy ! . back ( );
308
+ _urlStrategy ! . go ( - 1 );
309
309
}
310
310
}
311
311
312
312
/// This method should be called when the Origin Entry is active. It just
313
313
/// replaces the state of the entry so that we can recognize it later using
314
314
/// [_isOriginEntry] inside [_popStateListener] .
315
- void _setupOriginEntry (LocationStrategy strategy) {
315
+ void _setupOriginEntry (JsUrlStrategy strategy) {
316
316
assert (strategy != null ); // ignore: unnecessary_null_comparison
317
317
strategy.replaceState (_wrapOriginState (currentState), 'origin' , '' );
318
318
}
319
319
320
320
/// This method is used manipulate the Flutter Entry which is always the
321
321
/// active entry while the Flutter app is running.
322
322
void _setupFlutterEntry (
323
- LocationStrategy strategy, {
323
+ JsUrlStrategy strategy, {
324
324
bool replace = false ,
325
325
String ? path,
326
326
}) {
@@ -342,19 +342,19 @@ class SingleEntryBrowserHistory extends BrowserHistory {
342
342
// the "origin" and "flutter" entries, we can safely assume they are
343
343
// already setup.
344
344
} else {
345
- _setupOriginEntry (locationStrategy ! );
346
- _setupFlutterEntry (locationStrategy ! , replace: false , path: path);
345
+ _setupOriginEntry (urlStrategy ! );
346
+ _setupFlutterEntry (urlStrategy ! , replace: false , path: path);
347
347
}
348
348
return Future <void >.value ();
349
349
}
350
350
351
351
@override
352
352
Future <void > tearDown () async {
353
- if (locationStrategy != null ) {
353
+ if (urlStrategy != null ) {
354
354
// We need to remove the flutter entry that we pushed in setup.
355
- await locationStrategy ! . back ( );
355
+ await urlStrategy ! . go ( - 1 );
356
356
// Restores original state.
357
- locationStrategy ! .replaceState (_unwrapOriginState (currentState), 'flutter' , currentPath);
357
+ urlStrategy ! .replaceState (_unwrapOriginState (currentState), 'flutter' , currentPath);
358
358
}
359
359
}
360
360
}
0 commit comments