This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[connectivity_for_web] Fix JS Interop in release mode. #2869
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2cb3a21
Remove auto-generator script, since we don't use almost anything of i…
ditman 6bfadf4
Switch to getProperty instead of accessing properties directly when i…
ditman 43c8d71
Migrate to dart:html NetworkInformation API.
ditman 6429ff5
Bump version
ditman 3e38a71
Make package publishable again.
ditman dbb76d5
Make version check happy
ditman 3069f31
Overwrite onchange until we can free resources on hot restart.
ditman 5dde17d
Make analyzer happy
ditman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 0 additions & 78 deletions
78
packages/connectivity/connectivity_for_web/lib/src/generated/network_information_types.dart
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 6 additions & 11 deletions
17
packages/connectivity/connectivity_for_web/lib/src/utils/connectivity_result.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,22 @@ | ||
| import 'dart:html' as html show NetworkInformation; | ||
| import 'package:connectivity_platform_interface/connectivity_platform_interface.dart'; | ||
|
|
||
| /// Converts an incoming NetworkInformation object into the correct ConnectivityResult. | ||
| // | ||
| // We can't be more specific on the signature of this method because the API is odd, | ||
| // data can come from a static value in the DOM, or as the 'target' of a DOM Event. | ||
| // | ||
| // If we type info as `NetworkInformation`, Dart will complain with: | ||
| // "Uncaught Error: Expected a value of type 'NetworkInformation', | ||
| // but got one of type 'NetworkInformation'" | ||
|
Comment on lines
-5
to
-10
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was probably Dart telling me that there already existed a native |
||
| ConnectivityResult networkInformationToConnectivityResult( | ||
| dynamic /* NetworkInformation */ info) { | ||
| html.NetworkInformation info, | ||
| ) { | ||
| if (info == null) { | ||
| return ConnectivityResult.none; | ||
| } | ||
| if (info.downlink == 0 && info.rtt == 0) { | ||
| return ConnectivityResult.none; | ||
| } | ||
| if (info.type != null) { | ||
| return _typeToConnectivityResult(info.type); | ||
| } | ||
| if (info.effectiveType != null) { | ||
| return _effectiveTypeToConnectivityResult(info.effectiveType); | ||
| } | ||
| if (info.type != null) { | ||
| return _typeToConnectivityResult(info.type); | ||
| } | ||
| return ConnectivityResult.none; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 11 additions & 43 deletions
54
packages/connectivity/connectivity_for_web/test/lib/src/connectivity_mocks.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,60 +1,28 @@ | ||
| import 'dart:async'; | ||
| import 'dart:html'; | ||
|
|
||
| import 'package:connectivity_for_web/src/generated/network_information_types.dart' | ||
| as dom; | ||
| import 'package:mockito/mockito.dart'; | ||
|
|
||
| /// A Mock implementation of the NetworkInformation API that allows | ||
| /// for external modification of its values. | ||
| class MockNetworkInformation extends dom.NetworkInformation { | ||
| @override | ||
| String type; | ||
|
|
||
| @override | ||
| String effectiveType; | ||
|
|
||
| @override | ||
| num downlink; | ||
| class MockNetworkInformation extends Mock implements NetworkInformation { | ||
| StreamController<Event> _onChangeController = StreamController<Event>(); | ||
|
|
||
| @override | ||
| num rtt; | ||
|
|
||
| @override | ||
| EventListener onchange; | ||
|
|
||
| /// Constructor of mocked instances... | ||
| MockNetworkInformation({ | ||
| this.type, | ||
| this.effectiveType, | ||
| this.downlink, | ||
| this.rtt, | ||
| }); | ||
| Stream<Event> get onChange => _onChangeController.stream; | ||
|
|
||
| /// Changes the desired values, and triggers the change event listener. | ||
| void mockChangeValue({ | ||
| String type, | ||
| String effectiveType, | ||
| num downlink, | ||
| num rtt, | ||
| }) { | ||
| this.type = type ?? this.type; | ||
| this.effectiveType = effectiveType ?? this.effectiveType; | ||
| this.downlink = downlink ?? this.downlink; | ||
| this.rtt = rtt ?? this.rtt; | ||
| }) async { | ||
| when(this.type).thenAnswer((_) => type); | ||
| when(this.effectiveType).thenAnswer((_) => effectiveType); | ||
| when(this.downlink).thenAnswer((_) => downlink); | ||
| when(this.rtt).thenAnswer((_) => rtt); | ||
|
|
||
| onchange(Event('change')); | ||
| _onChangeController.add(Event('change')); | ||
| } | ||
|
|
||
| @override | ||
| void addEventListener(String type, listener, [bool useCapture]) {} | ||
|
|
||
| @override | ||
| bool dispatchEvent(Event event) { | ||
| return true; | ||
| } | ||
|
|
||
| @override | ||
| Events get on => null; | ||
|
|
||
| @override | ||
| void removeEventListener(String type, listener, [bool useCapture]) {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is unrelated to your PR, but I noticed it and wanted to point it out. The listener being attached here is never cleaned up. This will potentially cause problems with hot restart since the old listener will remain intact while a new listener is created.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, before this PR there used to be only one listener that you keep overriding. Now you're using a stream that could have multiple listeners which makes it possible to leak listeners.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! I don't know of any way to detect the app is hot-restarting to clean-up the plugin!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how we do it in the web engine. But that's only one example that requires clean up, there may be others.
One thing you can do is make the listening lazy. So you only attach a listener when someone subscribes to
_connectivityResult.stream. And as soon as there are no more subscribers to_connectivityResult.streamyou can release your listener. Doing it this way ensures that your code won't leak anything as long as the user code isn't leaking the stream subscription.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean, I'm going to create an issue, because this is not exclusive of this plugin!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are indeed listeners leaking. Two problems:
I'm going to fix the current asymmetry: subscribe to the inner Stream only when there's a new listener, and unsubscribe from it when there are none left.
Thanks for bringing this up!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've created a Dart issue regarding the first issue: dart-lang/sdk#42679
Without knowing when to call "dispose", we can't clear up the Stream.
I've reverted that part of the code to write directly to the "onchange" DOM property of the object, until we get a clear way of cleaning up after ourselves (from the plugin)