on method

EventEmitterListener on({
  1. required String event_name,
  2. required FutureOr onUpdate(
    1. UpdateTelegramClient updateTelegramClient
    ),
  3. required FutureOr onError(
    1. Object error,
    2. StackTrace stackTrace
    ),
})

return original data json

Implementation

EventEmitterListener on({
  required String event_name,
  required FutureOr<dynamic> Function(
          UpdateTelegramClient updateTelegramClient)
      onUpdate,
  required FutureOr<dynamic> Function(Object error, StackTrace stackTrace)
      onError,
}) {
  return event_emitter.on(
      eventName: event_name,
      onCallback: (listener, update) async {
        try {
          if (update is UpdateBot) {
            await onUpdate(
              UpdateTelegramClient(
                uri: update.uri,
                rawData: update.body,
                query: update.query,
                client_option: {},
                tg: this,
                telegramClientData: TelegramClientData(
                  tdlib_client_id: 0,
                  telegram_bot_api_token_bot: "",
                  telegramClientType: TelegramClientType.telegam_bot_api,
                ),
              ),
            );
          }

          if (update is TdlibIsolateReceiveData) {
            final TdlibClient? tdlibClient = tdlib.clients[update.clientId];

            final UpdateTd updateTd = UpdateTd(
              update: update.updateData,
              client_id: update.clientId,
              client_option: () {
                try {
                  if (tdlibClient != null) {
                    return tdlibClient.client_option;
                  }
                } catch (e) {}
                return {};
              }(),
            );
            await onUpdate(
              UpdateTelegramClient(
                rawData: updateTd.raw,
                uri: Uri.base,
                query: {},
                tg: this,
                client_option: updateTd.client_option,
                telegramClientData: TelegramClientData(
                  tdlib_client_id: updateTd.client_id,
                  telegram_bot_api_token_bot: "",
                  telegramClientType: TelegramClientType.tdlib,
                  client_user_name: () {
                    try {
                      if (tdlibClient != null) {
                        if (tdlibClient.client_option["client_username"]
                            is String) {
                          return tdlibClient.client_option["client_username"];
                        }
                      }
                    } catch (e) {}

                    return "";
                  }(),
                  client_tg_user_id: () {
                    try {
                      if (tdlibClient != null) {
                        return tdlibClient.client_tg_user_id;
                      }
                    } catch (e) {}
                    return 0;
                  }(),
                  is_bot: () {
                    try {
                      if (tdlibClient != null) {
                        return tdlibClient.is_bot;
                      }
                    } catch (e) {}
                    return false;
                  }(),
                ),
              ),
            );
          }
        } catch (e, stack) {
          try {
            await onError(e, stack);
          } catch (e) {}
        }
      });
}