platformCallBackMethodHandlerSetup method

  1. @override
Future platformCallBackMethodHandlerSetup(
  1. MethodCall call
)
override

Implementation

@override
Future<dynamic> platformCallBackMethodHandlerSetup(MethodCall call) async {
  try {
    if (_callbacks.isEmpty) {
      // throw Exception('No callback registered.');
      // TODO(v): maybe there is a delay between hosts callbacks invoke
      log('SUPOSE TO: \nthrow Exception(\'No callback registered.\');',
          level: 2000);
      return Future.value('[Platform info]: No callback registered.');
    }

    switch (call.method) {
      case PlatformEntrypoint.setupMethodHandler:
      case PlatformEntrypoint.platformCallBackMethodHandler:
        Message message = Message.fromBuffer(call.arguments);

        if (message.header.intent == Header_CommunicationType.CANCELATION) {
          disposeCallBack(message.header.callBackReferenceId);
          break;
        }

        int id = message.header.callBackReferenceId;

        if (id == 0) {
          throw Exception('Invalid [callback id] value.');
        }

        /// On this case we can call a global dispose on host.
        /// Means that there are more instances inside our native channel.
        if (!_callbacks.any((e) => e.id == id)) {
          // TODO(v): maybe there is a delay between hosts callbacks invoke
          log('SUPOSE TO: \nthrow Exception(\'Invalid [callback id]. Is this callback disposed?\');',
              level: 2000);
          return Future.value('[Platform info]: callback id not found');
          // throw Exception(
          //     'Invalid [callback id]. Is this callback disposed?');
        }

        final dynamicMethod =
            _callbacks.firstWhere((element) => element.id == id);

        // TODO(v): function references doesn't throw explicit runtime exceptions on message.apply
        // final arguments = await message.apply();

        dynamicMethod.call(
          message.methodCall,
          message.error,
        );

        break;
      default:
        log('CallBacksController, platformCallBackMethodHandlerSetup -> IGNORING: ${call.method}');
        break;
    }
  } catch (e, s) {
    log('CallBacksController, platformCallBackMethodHandlerSetup\n$call',
        name: _tag, stackTrace: s, error: e);
    rethrow;
  }
}