Skip to content

Commit

Permalink
JavascriptAsyncMethodHandler - Obtain Promise from context on Execute
Browse files Browse the repository at this point in the history
Previously the promise reference was passed into the handler, now we obtain the reference on demand
  • Loading branch information
amaitland committed May 15, 2018
1 parent ddb2fdc commit 5f3dca4
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../CefSharp.Core/Internals/Messaging/Messages.h"
#include "../CefSharp.Core/Internals/Serialization/Primitives.h"
#include "Serialization/V8Serialization.h"
#include "CefAppUnmanagedWrapper.h"

using namespace CefSharp::Internals::Messaging;
using namespace CefSharp::Internals::Serialization;
Expand All @@ -21,8 +22,11 @@ namespace CefSharp
{
auto context = CefV8Context::GetCurrentContext();
auto browser = context->GetBrowser();

auto promiseCreator = context->GetGlobal()->GetValue(CefAppUnmanagedWrapper::kPromiseCreatorFunction);

//this will create a promise and give us the reject/resolve functions {p: Promise, res: resolve(), rej: reject()}
auto promiseData = _promiseCreator->ExecuteFunctionWithContext(context, nullptr, CefV8ValueList());
auto promiseData = promiseCreator->ExecuteFunctionWithContext(context, nullptr, CefV8ValueList());
retval = promiseData->GetValue("p");

auto resolve = promiseData->GetValue("res");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ namespace CefSharp
private:
gcroot<JavascriptCallbackRegistry^> _callbackRegistry;
gcroot<Func<JavascriptAsyncMethodCallback^, int64>^> _methodCallbackSave;
CefRefPtr<CefV8Value> _promiseCreator;
int64 _objectId;

public:
JavascriptAsyncMethodHandler(int64 objectId, JavascriptCallbackRegistry^ callbackRegistry, CefRefPtr<CefV8Value> promiseCreator, Func<JavascriptAsyncMethodCallback^, int64>^ methodCallbackSave)
:_callbackRegistry(callbackRegistry), _objectId(objectId), _promiseCreator(promiseCreator), _methodCallbackSave(methodCallbackSave)
JavascriptAsyncMethodHandler(int64 objectId, JavascriptCallbackRegistry^ callbackRegistry, Func<JavascriptAsyncMethodCallback^, int64>^ methodCallbackSave)
:_callbackRegistry(callbackRegistry), _objectId(objectId), _methodCallbackSave(methodCallbackSave)
{

}
Expand All @@ -37,7 +36,6 @@ namespace CefSharp
// It's lifecycle is managed in the JavascriptRootObjectWrapper.
_callbackRegistry = nullptr;
_methodCallbackSave = nullptr;
_promiseCreator = NULL;
}

IMPLEMENT_REFCOUNTING(JavascriptAsyncMethodHandler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ namespace CefSharp
MCefRefPtr<JavascriptAsyncMethodHandler> _javascriptMethodHandler;

public:
JavascriptAsyncMethodWrapper(int64 ownerId, JavascriptCallbackRegistry^ callbackRegistry, CefRefPtr<CefV8Value> promiseCreator, Func<JavascriptAsyncMethodCallback^, int64>^ methodCallbackSave)
: _javascriptMethodHandler(new JavascriptAsyncMethodHandler(ownerId, callbackRegistry, promiseCreator, methodCallbackSave))
JavascriptAsyncMethodWrapper(int64 ownerId, JavascriptCallbackRegistry^ callbackRegistry, Func<JavascriptAsyncMethodCallback^, int64>^ methodCallbackSave)
: _javascriptMethodHandler(new JavascriptAsyncMethodHandler(ownerId, callbackRegistry, methodCallbackSave))
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace CefSharp
{
namespace Async
{
void JavascriptAsyncObjectWrapper::Bind(JavascriptObject^ object, const CefRefPtr<CefV8Value> &value, const CefRefPtr<CefV8Value> &promiseCreator)
void JavascriptAsyncObjectWrapper::Bind(JavascriptObject^ object, const CefRefPtr<CefV8Value> &value)
{
//V8Value that represents this javascript object - only one per complex type, no accessor
auto javascriptObject = CefV8Value::CreateObject(nullptr, nullptr);
Expand All @@ -24,7 +24,7 @@ namespace CefSharp

for each (JavascriptMethod^ method in Enumerable::OfType<JavascriptMethod^>(object->Methods))
{
auto wrappedMethod = gcnew JavascriptAsyncMethodWrapper(object->Id, _callbackRegistry, promiseCreator, _methodCallbackSave);
auto wrappedMethod = gcnew JavascriptAsyncMethodWrapper(object->Id, _callbackRegistry, _methodCallbackSave);
wrappedMethod->Bind(method, javascriptObject);

_wrappedMethods->Add(wrappedMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace CefSharp
}
}

void Bind(JavascriptObject^ object, const CefRefPtr<CefV8Value> &value, const CefRefPtr<CefV8Value> &promiseCreator);
void Bind(JavascriptObject^ object, const CefRefPtr<CefV8Value> &value);
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ namespace CefSharp
if (objects->Count > 0)
{
auto saveMethod = gcnew Func<JavascriptAsyncMethodCallback^, int64>(this, &JavascriptRootObjectWrapper::SaveMethodCallback);
auto promiseCreator = v8Value->GetValue(CefAppUnmanagedWrapper::kPromiseCreatorFunction);

for each (JavascriptObject^ obj in Enumerable::OfType<JavascriptObject^>(objects))
{
if (obj->IsAsync)
{
auto wrapperObject = gcnew JavascriptAsyncObjectWrapper(_callbackRegistry, saveMethod);
wrapperObject->Bind(obj, v8Value, promiseCreator);
wrapperObject->Bind(obj, v8Value);

_wrappedAsyncObjects->Add(wrapperObject);
}
Expand Down

0 comments on commit 5f3dca4

Please sign in to comment.