Skip to content

Commit

Permalink
shared: Move next tick callbacks into IResource
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMrBonnie committed Oct 17, 2023
1 parent 832aaa1 commit fcbbe2a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
20 changes: 1 addition & 19 deletions shared/src/interfaces/IAltResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include "IResource.h"

#include <queue>

namespace js
{
class IAltResource : public alt::IResource::Impl, public IResource
Expand All @@ -22,21 +20,16 @@ namespace js
static void ExternalFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info);
};

using NextTickCallback = std::function<void()>;

protected:
alt::IResource* resource = nullptr;

std::unordered_map<alt::IResource*, Persistent<v8::Object>> resourceObjects;

std::queue<NextTickCallback> nextTickCallbacks;

void Reset() override
{
IResource::Reset();
resource = nullptr;
resourceObjects.clear();
nextTickCallbacks = {};
}

public:
Expand Down Expand Up @@ -84,13 +77,7 @@ namespace js

void OnTick() override
{
while(!nextTickCallbacks.empty())
{
NextTickCallback& callback = nextTickCallbacks.front();
callback();
nextTickCallbacks.pop();
}

IResource::ProcessNextTickCallbacks();
js::Function onTick = GetBindingExport<v8::Function>("timers:tick");
if(onTick.IsValid()) onTick.Call();
}
Expand All @@ -102,10 +89,5 @@ namespace js
resourceObjects.at(resource).Get(isolate)->SetAlignedPointerInInternalField(1, nullptr);
resourceObjects.erase(resource);
}

void PushNextTickCallback(NextTickCallback&& callback)
{
nextTickCallbacks.push(std::move(callback));
}
};
} // namespace js
25 changes: 22 additions & 3 deletions shared/src/interfaces/IResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <array>
#include <type_traits>
#include <queue>

#include "v8.h"
#include "cpp-sdk/SDK.h"
Expand All @@ -20,19 +21,20 @@ namespace js
{
class IResource : public IScriptObjectHandler, public ICompatibilityHandler
{
public:
using NextTickCallback = std::function<void()>;

protected:
static constexpr int ContextInternalFieldIdx = 1;

static void RequireBindingNamespaceWrapper(FunctionContext& ctx);

v8::Isolate* isolate;
Persistent<v8::Context> context;

std::unordered_map<std::string, Persistent<v8::Value>> bindingExports;

std::unordered_map<Buffer*, Persistent<v8::Object>> ownedBuffers;

bool rawEmitEnabled = false;
std::queue<NextTickCallback> nextTickCallbacks;

void Initialize()
{
Expand All @@ -53,10 +55,22 @@ namespace js
context.Reset();
bindingExports.clear();
ownedBuffers.clear();
rawEmitEnabled = false;
nextTickCallbacks = {};
}

void InitializeBinding(Binding* binding);

void ProcessNextTickCallbacks()
{
while(!nextTickCallbacks.empty())
{
NextTickCallback& callback = nextTickCallbacks.front();
callback();
nextTickCallbacks.pop();
}
}

public:
class Scope
{
Expand Down Expand Up @@ -197,6 +211,11 @@ namespace js
rawEmitEnabled = toggle;
}

void PushNextTickCallback(NextTickCallback&& callback)
{
nextTickCallbacks.push(std::move(callback));
}

template<class ResourceType = js::IResource>
static ResourceType* GetFromContext(v8::Local<v8::Context> context)
{
Expand Down

0 comments on commit fcbbe2a

Please sign in to comment.