Mtproto constructor

Mtproto({
  1. String? pathTdl,
  2. Map? clientOption,
  3. bool is_cli = false,
  4. String event_invoke = "invoke",
  5. String event_update = "update",
  6. EventEmitter? eventEmitter,
  7. Duration? delayUpdate,
  8. double timeOutUpdate = 1.0,
  9. Duration? delayInvoke,
  10. Duration? invokeTimeOut,
  11. bool isAutoGetChat = false,
  12. FutureOr<String> on_generate_extra_invoke(
    1. int client_id,
    2. Mtproto Mtproto
    )?,
  13. FutureOr<Map> on_get_invoke_data(
    1. String extra,
    2. int client_id,
    3. Mtproto Mtproto
    )?,
  14. FutureOr<void> on_receive_update(
    1. dynamic update,
    2. Mtproto Mtproto
    )?,
})

Implementation

Mtproto({
  String? pathTdl,
  Map? clientOption,
  this.is_cli = false,
  this.event_invoke = "invoke",
  this.event_update = "update",
  EventEmitter? eventEmitter,
  Duration? delayUpdate,
  this.timeOutUpdate = 1.0,
  Duration? delayInvoke,
  Duration? invokeTimeOut,
  bool isAutoGetChat = false,
  this.on_generate_extra_invoke,
  this.on_get_invoke_data,
  this.on_receive_update,
}) {
  pathTdl ??= "Mtproto.${getFormatLibrary}";
  path_tdlib = pathTdl;
  is_auto_get_chat = isAutoGetChat;
  invokeTimeOut ??= Duration(minutes: 5);
  invoke_time_out = invokeTimeOut;
  if (eventEmitter != null) {
    event_emitter = eventEmitter;
  }

  if (clientOption != null) {
    client_option.addAll(clientOption);
    if (clientOption["is_android"] == true) {
      is_android = true;
    }
  }

  receivePort.listen((update) async {
    if (on_receive_update != null) {
      await on_receive_update!(update, this);
    } else if (update is MtprotoIsolateReceiveData) {
      MtprotoIsolateReceiveData tdlibIsolateReceiveData = update;
      try {
        if (tdlibIsolateReceiveData.updateData["@extra"] is String) {
          event_emitter.emit(event_invoke, null, tdlibIsolateReceiveData);
        } else {
          event_emitter.emit(event_update, null, tdlibIsolateReceiveData);
        }
      } catch (e) {
        event_emitter.emit(event_update, null, tdlibIsolateReceiveData);
      }
    } else if (update is MtprotoIsolateReceiveDataError) {
      MtprotoIsolateReceiveDataError tdlibIsolateReceiveDataError = update;
      try {
        MtprotoClient? tdlibClient =
            clients.getClientById(tdlibIsolateReceiveDataError.clientId);
        if (tdlibClient != null) {
          tdlibClient.close();
        }
      } catch (e) {}
    }
  });
}