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

Put state in Future instead of using Future<isInit> #65

Open
wants to merge 1 commit into
base: PFL-69/align-nullness-with-other-sdks
Choose a base branch
from

Conversation

krocard
Copy link
Contributor

@krocard krocard commented Oct 9, 2023

The native player was previously initialized and the communication channels set in a separate async context, with a Future<bool> guarding when this was done. This is very similar to how it would be done in C with a condition variable.

The safer, and more common in Dart, way is to embed the async result in the future itself. The channels can be safely accessed by awaiting on the new Future<NativePlayerHandle>.

This PR depends on: #63

@krocard krocard self-assigned this Oct 9, 2023
Comment on lines +135 to 141
if (tNull is T) { // T is nullable
final jsonStringOrNull = await _invokeMethod<String?>(methodName, data);
if (jsonStringOrNull == null) return tNull;
jsonString = jsonStringOrNull;
} else {
jsonString = await _invokeMethod<String>(methodName, data);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated to the async stuff. Made the function generic over nullness.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not super fond of that change. Kind of hard to understand and mentally decode IMO. Why do we need it in the first place? In which case was the old implementation not working as expeced?


_completer.complete(true);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Completer is used to bridge a callback based API to Future. Given that we are chaining Futures, it is not needed.

},
),
)
.then((value) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Migrated to async method rather than Future chaining.

return EventChannel(name);
static EventChannel registerEventChannel({
required String name,
void Function(dynamic event)? onEvent,
Copy link
Contributor Author

@krocard krocard Oct 9, 2023

Choose a reason for hiding this comment

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

Align with registerMethodChannel: the callback is passed to the register* function rather than registered in a following call.

@krocard krocard marked this pull request as ready for review October 9, 2023 18:22
Copy link
Contributor

@testcenter testcenter left a comment

Choose a reason for hiding this comment

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

lgtm 👍

throw Exception('Error initializing player on native platform side.');
final String jsonString;
const T? tNull = null;
if (tNull is T) { // T is nullable
Copy link
Contributor

Choose a reason for hiding this comment

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

this if condition is always false, or do I misunderstand something here?
(you create tNull = null and in the next line you check for tNull is T 🤔

Copy link
Collaborator

@hawk23 hawk23 left a comment

Choose a reason for hiding this comment

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

Nice improvement!

/// Private method channel for this player instance
MethodChannel methodChannel;

/// Private method channel for this player instance to receive events
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit:

Suggested change
/// Private method channel for this player instance to receive events
/// Private event channel for this player instance

if (value == null || value == false) {
_completer.complete(false);
return;
final success = await mainChannel.invokeMethod<bool>(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Starting from here, the indentation is off. My VSCode is auto-fixing this when I edit this file. Is your IDE maybe configured to indent using 4 spaces?

Comment on lines +135 to 141
if (tNull is T) { // T is nullable
final jsonStringOrNull = await _invokeMethod<String?>(methodName, data);
if (jsonStringOrNull == null) return tNull;
jsonString = jsonStringOrNull;
} else {
jsonString = await _invokeMethod<String>(methodName, data);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not super fond of that change. Kind of hard to understand and mentally decode IMO. Why do we need it in the first place? In which case was the old implementation not working as expeced?

static EventChannel registerEventChannel({
required String name,
void Function(dynamic event)? onEvent,
}) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Incorrect indentation. VSCode auto-fixed that when I touched the file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants