Repository: electron/electron
File: shell/common/gin_helper/reply_channel.cc (C++)

Code before:
             ReplyChannel>::GetObjectTemplateBuilder(isolate)
      .SetMethod("sendReply", &ReplyChannel::SendReply);
}

const char* ReplyChannel::GetTypeName() {
  return "ReplyChannel";
}

ReplyChannel::ReplyChannel(InvokeCallback callback)
    : callback_(std::move(callback)) {}

ReplyChannel::~ReplyChannel() {
  if (callback_)
    SendError(electron::JavascriptEnvironment::GetIsolate(),
              std::move(callback_), "reply was never sent");
}

// static
void ReplyChannel::SendError(v8::Isolate* isolate,
                             InvokeCallback callback,
                             std::string_view const errmsg) {
  if (!callback)
    return;

  // If we're shutting down, we don't need to send an event
  if (!isolate->IsExecutionTerminating())
    return;

  v8::HandleScope scope{isolate};
  SendReplyImpl(isolate, std::move(callback),
                gin::DataObjectBuilder(isolate).Set("error", errmsg).Build());
}

// static
bool ReplyChannel::SendReplyImpl(v8::Isolate* isolate,
                                 InvokeCallback callback,
                                 v8::Local<v8::Value> arg) {
  if (!callback)
    return false;

  blink::CloneableMessage msg;
  if (!gin::ConvertFromV8(isolate, arg, &msg))
    return false;

  std::move(callback).Run(std::move(msg));
  return true;
}

bool ReplyChannel::SendReply(v8::Isolate* isolate, v8::Local<v8::Value> arg) {
  return SendReplyImpl(isolate, std::move(callback_), std::move(arg));
}

Review comment (suggestion):
Condition needs to be reversed.

```suggestion
  if (isolate->IsExecutionTerminating())
```



Code after:
             ReplyChannel>::GetObjectTemplateBuilder(isolate)
      .SetMethod("sendReply", &ReplyChannel::SendReply);
}

const char* ReplyChannel::GetTypeName() {
  return "ReplyChannel";
}

ReplyChannel::ReplyChannel(InvokeCallback callback)
    : callback_(std::move(callback)) {}

ReplyChannel::~ReplyChannel() {
  if (callback_)
    SendError(electron::JavascriptEnvironment::GetIsolate(),
              std::move(callback_), "reply was never sent");
}

// static
void ReplyChannel::SendError(v8::Isolate* isolate,
                             InvokeCallback callback,
                             std::string_view const errmsg) {
  if (!callback)
    return;

  // If there's no current context, it means we're shutting down,
  // so we don't need to send an event.
  v8::HandleScope scope{isolate};
  if (isolate->GetCurrentContext().IsEmpty())
    return;

  SendReplyImpl(isolate, std::move(callback),
                gin::DataObjectBuilder(isolate).Set("error", errmsg).Build());
}

// static
bool ReplyChannel::SendReplyImpl(v8::Isolate* isolate,
                                 InvokeCallback callback,
                                 v8::Local<v8::Value> arg) {
  if (!callback)
    return false;

  blink::CloneableMessage msg;
  if (!gin::ConvertFromV8(isolate, arg, &msg))
    return false;

  std::move(callback).Run(std::move(msg));
  return true;
}

bool ReplyChannel::SendReply(v8::Isolate* isolate, v8::Local<v8::Value> arg) {
  return SendReplyImpl(isolate, std::move(callback_), std::move(arg));