void MessageListenerProxy()

in src/Consumer.cc [75:107]


void MessageListenerProxy(Napi::Env env, Napi::Function jsCallback, MessageListenerProxyData *data) {
  Napi::Object msg = Message::NewInstance({}, data->cMessage);
  Consumer *consumer = data->consumer;

  // `consumer` might be null in certain cases, segmentation fault might happend without this null check. We
  // need to handle this rare case in future.
  if (consumer) {
    Napi::Value ret;
    try {
      ret = jsCallback.Call({msg, consumer->Value()});
    } catch (std::exception &exception) {
      logMessageListenerError(consumer, exception.what());
    }

    if (ret.IsPromise()) {
      Napi::Promise promise = ret.As<Napi::Promise>();
      Napi::Function catchFunc = promise.Get("catch").As<Napi::Function>();

      ret = catchFunc.Call(promise, {Napi::Function::New(env, [consumer](const Napi::CallbackInfo &info) {
                             Napi::Error error = info[0].As<Napi::Error>();
                             logMessageListenerError(consumer, error.what());
                           })});

      promise = ret.As<Napi::Promise>();
      Napi::Function finallyFunc = promise.Get("finally").As<Napi::Function>();

      finallyFunc.Call(
          promise, {Napi::Function::New(env, [data](const Napi::CallbackInfo &info) { data->callback(); })});
      return;
    }
  }
  data->callback();
}