Skip to content

Commit

Permalink
Merge pull request #196 from bitmovin/feature/document-race-condition
Browse files Browse the repository at this point in the history
Document race condition and produce meaningful error
  • Loading branch information
hawk23 authored Nov 26, 2024
2 parents 079424c + 1cf497c commit 593feab
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/src/platform/player_view_platform_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PlayerViewPlatformMethodChannel extends PlayerViewPlatformInterface {
final void Function() _handleExitFullscreen;
final void Function()? _onViewCreated;
final EventDeserializer _eventDeserializer = EventDeserializer();
late final MethodChannel _methodChannel;
MethodChannel? _methodChannel;
late final EventChannel _eventChannel;

@override
Expand Down Expand Up @@ -77,14 +77,24 @@ class PlayerViewPlatformMethodChannel extends PlayerViewPlatformInterface {
String methodName, [
dynamic data,
]) async {
final result = await _methodChannel.invokeMethod<T>(methodName, data);
if (_methodChannel == null) {
return Future.error(
'Method channel for player view not initialized yet.',
);
}

final result = await _methodChannel?.invokeMethod<T>(methodName, data);
if (result is! T) {
// result is T?, if it `is` not T => T is not nullable and result is null.
throw Exception('Native $methodName returned null.');
}
return result;
}

// TODO(mario): Method channels are created only once `_onPlatformViewCreated`
// is called. Calls to `_invokeMethod` that happen before that lead to an
// error. This race condition can be fixed the same way as for
// `PlayerPlatformMethodChannel` by using an `_initializationResult`.
void _onPlatformViewCreated(int id) {
_methodChannel = ChannelManager.registerMethodChannel(
name: '${Channels.playerView}-$id',
Expand Down

0 comments on commit 593feab

Please sign in to comment.