Skip to content

Commit

Permalink
shared: properly handle rpc promise callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Sep 30, 2023
1 parent dd75221 commit 2d097da
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
31 changes: 26 additions & 5 deletions client/src/CV8Resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,33 @@ void CV8ResourceImpl::HandleServerRPC(alt::CScriptRPCEvent* ev)
v8::TryCatch tryCatch(isolate);
auto result = V8Helpers::CallFunctionWithTimeout(handler->second.Get(isolate), context, args);

alt::MValue returnValue;

v8::Local<v8::Value> returnValue;
if (!result.IsEmpty())
returnValue = V8Helpers::V8ToMValue(result.ToLocalChecked());
returnValue = result.ToLocalChecked();
else
returnValue = V8Helpers::V8ToMValue(v8::Undefined(isolate));
returnValue = v8::Undefined(isolate);

if(returnValue->IsPromise())
{
v8::Local<v8::Promise> promise = returnValue.As<v8::Promise>();
while(true)
{
v8::Promise::PromiseState state = promise->State();
if(state == v8::Promise::PromiseState::kPending)
{
CV8ScriptRuntime::Instance().OnTick();
// Run event loop
OnTick();
}
else if(state == v8::Promise::PromiseState::kFulfilled)
{
returnValue = promise->Result();
break;
}
else if(state == v8::Promise::PromiseState::kRejected)
break;
}
}

ev->WillAnswer();

Expand All @@ -374,7 +395,7 @@ void CV8ResourceImpl::HandleServerRPC(alt::CScriptRPCEvent* ev)
errorMessage = *v8::String::Utf8Value(isolate, tryCatch.Message()->Get());
}

alt::ICore::Instance().TriggerServerRPCAnswer(ev->GetAnswerID(), returnValue, errorMessage);
alt::ICore::Instance().TriggerServerRPCAnswer(ev->GetAnswerID(), V8Helpers::V8ToMValue(returnValue), errorMessage);
}

std::vector<V8Helpers::EventCallback*> CV8ResourceImpl::GetWebViewHandlers(alt::IWebView* view, const std::string& name)
Expand Down
31 changes: 26 additions & 5 deletions server/src/CNodeResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,33 @@ void CNodeResourceImpl::HandleClientRpcEvent(alt::CScriptRPCEvent* ev)
v8::TryCatch tryCatch(isolate);
auto result = V8Helpers::CallFunctionWithTimeout(handler->second.Get(isolate), context, args);

alt::MValue returnValue;

v8::Local<v8::Value> returnValue;
if (!result.IsEmpty())
returnValue = V8Helpers::V8ToMValue(result.ToLocalChecked());
returnValue = result.ToLocalChecked();
else
returnValue = V8Helpers::V8ToMValue(v8::Undefined(isolate));
returnValue = v8::Undefined(isolate);

if(returnValue->IsPromise())
{
v8::Local<v8::Promise> promise = returnValue.As<v8::Promise>();
while(true)
{
v8::Promise::PromiseState state = promise->State();
if(state == v8::Promise::PromiseState::kPending)
{
CNodeScriptRuntime::Instance().OnTick();
// Run event loop
OnTick();
}
else if(state == v8::Promise::PromiseState::kFulfilled)
{
returnValue = promise->Result();
break;
}
else if(state == v8::Promise::PromiseState::kRejected)
break;
}
}

ev->WillAnswer();

Expand All @@ -301,7 +322,7 @@ void CNodeResourceImpl::HandleClientRpcEvent(alt::CScriptRPCEvent* ev)
errorMessage = *v8::String::Utf8Value(isolate, tryCatch.Message()->Get());
}

alt::ICore::Instance().TriggerClientRPCAnswer(ev->GetTarget(), ev->GetAnswerID(), returnValue, errorMessage);
alt::ICore::Instance().TriggerClientRPCAnswer(ev->GetTarget(), ev->GetAnswerID(), V8Helpers::V8ToMValue(returnValue), errorMessage);
}

void CNodeResourceImpl::HandleClientRpcAnswerEvent(const alt::CScriptRPCAnswerEvent* ev)
Expand Down

0 comments on commit 2d097da

Please sign in to comment.