Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[web] Freeze window.defaultRouteName #15565

Merged
merged 3 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/web_ui/lib/src/engine/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ class EngineWindow extends ui.Window {
/// Simulates clicking the browser's back button.
Future<void> webOnlyBack() => _browserHistory.back();

String _defaultRouteName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a while to realize (found the ??= in the code) that this is lazy-initialized and cached route name. I think a comment would be useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


@override
String get defaultRouteName => _browserHistory.currentPath;
String get defaultRouteName => _defaultRouteName ??= _browserHistory.currentPath;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be frozen on construction, or when the browser window opens rather than on first access?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to delay the initialization of defaultRouteName a bit so that the app has time to initialize the engine and set locationStrategy which is used by _browserHistory to get the current path.

But once the app gets the value of the default route name, it shouldn't change after that.


/// Change the strategy to use for handling browser history location.
/// Setting this member will automatically update [_browserHistory].
Expand Down
22 changes: 22 additions & 0 deletions lib/web_ui/test/window_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:test/test.dart';
import 'package:ui/src/engine.dart';

TestLocationStrategy _strategy;
TestLocationStrategy get strategy => _strategy;
set strategy(TestLocationStrategy newStrategy) {
window.locationStrategy = _strategy = newStrategy;
}

void main() {
test('window.defaultRouteName should not change', () {
strategy = TestLocationStrategy.fromEntry(TestHistoryEntry('initial state', null, '/initial'));
expect(window.defaultRouteName, '/initial');

strategy.replaceState(null, null, '/newpath');
expect(window.defaultRouteName, '/initial');
});
}