Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:altmp/altv-js-module into dev
Browse files Browse the repository at this point in the history
Former-commit-id: 57544d3
  • Loading branch information
LeonMrBonnie committed Nov 24, 2021
2 parents 64b5262 + bfc3b22 commit 5cfd293
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
25 changes: 25 additions & 0 deletions client/src/CV8Resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ bool CV8ResourceImpl::OnEvent(const alt::CEvent* e)
}
}

{
if(e->GetType() == alt::CEvent::Type::WEB_VIEW_EVENT)
{
auto ev = static_cast<const alt::CWebViewEvent*>(e);
if (ev->GetName() == "load")
HandleWebViewEventQueue(ev->GetTarget());
}
}

return true;
}

Expand Down Expand Up @@ -402,3 +411,19 @@ void CV8ResourceImpl::RemoveWorker(CWorker* worker)
{
workers.erase(worker);
}

void CV8ResourceImpl::HandleWebViewEventQueue(const alt::Ref<alt::IWebView> view)
{
auto& eventQueuesMap = this->GetWebviewsEventQueue();
if (!eventQueuesMap.count(view)) return;

auto& eventQueue = eventQueuesMap.at(view);
if (eventQueue.empty()) return;

for (auto& [evName, mvArgs] : eventQueue)
{
view->Trigger(evName, mvArgs);
}

eventQueuesMap.erase(view);
}
12 changes: 12 additions & 0 deletions client/src/CV8Resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,25 @@ class CV8ResourceImpl : public V8ResourceImpl, public IImportHandler
return workers.size();
}

void AddWebViewEventToQueue(const alt::Ref<alt::IWebView> view, const alt::String& evName, const alt::MValueArgs& mvArgs)
{
webViewsEventsQueue[view].push_back(std::pair(evName, mvArgs));
}
private:
using WebViewEvents = std::unordered_multimap<std::string, V8::EventCallback>;
using WebViewsEventsQueue = std::unordered_map<alt::Ref<alt::IWebView>, std::vector<std::pair<alt::String, alt::MValueArgs>>>;

std::unordered_map<alt::Ref<alt::IWebView>, WebViewEvents> webViewHandlers;
std::unordered_map<alt::Ref<alt::IWebSocketClient>, WebViewEvents> webSocketClientHandlers;
std::unordered_map<alt::Ref<alt::IAudio>, WebViewEvents> audioHandlers;

WebViewsEventsQueue webViewsEventsQueue;
WebViewsEventsQueue& GetWebviewsEventQueue()
{
return webViewsEventsQueue;
}
void HandleWebViewEventQueue(const alt::Ref<alt::IWebView> view);

std::unordered_set<alt::Ref<alt::IBaseObject>> ownedObjects;

std::unordered_set<CWorker*> workers;
Expand Down
5 changes: 4 additions & 1 deletion client/src/bindings/WebView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ static void Emit(const v8::FunctionCallbackInfo<v8::Value>& info)

for(int i = 1; i < info.Length(); ++i) mvArgs.Push(V8Helpers::V8ToMValue(info[i], false));

view->Trigger(evName, mvArgs);
if (!view->IsReady())
static_cast<CV8ResourceImpl*>(resource)->AddWebViewEventToQueue(view, evName, mvArgs);
else
view->Trigger(evName, mvArgs);
}

static void GetEventListeners(const v8::FunctionCallbackInfo<v8::Value>& info)
Expand Down

0 comments on commit 5cfd293

Please sign in to comment.