Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1658: Removed inclusion of TurboModuleUtils.h #1670

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ add_library(
"${PROJECT_SOURCE_DIR}/cpp/jsi/JsiValue.cpp"
"${PROJECT_SOURCE_DIR}/cpp/jsi/RuntimeLifecycleMonitor.cpp"
"${PROJECT_SOURCE_DIR}/cpp/jsi/RuntimeAwareCache.cpp"
"${PROJECT_SOURCE_DIR}/cpp/jsi/JsiPromises.cpp"

"${PROJECT_SOURCE_DIR}/cpp/rnskia/RNSkManager.cpp"
"${PROJECT_SOURCE_DIR}/cpp/rnskia/RNSkJsView.cpp"
Expand Down
10 changes: 4 additions & 6 deletions package/cpp/api/JsiSkDataFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@
#include <memory>
#include <utility>

#include <ReactCommon/TurboModuleUtils.h>
#include <jsi/jsi.h>

#include "SkBase64.h"

#include "JsiPromises.h"
#include "JsiSkData.h"
#include "SkBase64.h"

namespace RNSkia {

namespace jsi = facebook::jsi;
namespace react = facebook::react;

class JsiSkDataFactory : public JsiSkHostObject {
public:
JSI_HOST_FUNCTION(fromURI) {
auto jsiLocalUri = arguments[0].asString(runtime);
auto localUri = jsiLocalUri.utf8(runtime);
auto context = getContext();
return react::createPromiseAsJSIValue(
return RNJsi::JsiPromises::createPromiseAsJSIValue(
runtime,
[context = std::move(context), localUri = std::move(localUri)](
jsi::Runtime &runtime,
std::shared_ptr<react::Promise> promise) -> void {
std::shared_ptr<RNJsi::JsiPromises::Promise> promise) -> void {
// Create a stream operation - this will be run in a
// separate thread
context->performStreamOperation(
Expand Down
9 changes: 5 additions & 4 deletions package/cpp/api/JsiSkImageFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <jsi/jsi.h>

#include "JsiPromises.h"
#include "JsiSkData.h"
#include "JsiSkHostObjects.h"
#include "JsiSkImage.h"
Expand Down Expand Up @@ -41,11 +42,11 @@ class JsiSkImageFactory : public JsiSkHostObject {
JSI_HOST_FUNCTION(MakeImageFromViewTag) {
auto viewTag = arguments[0].asNumber();
auto context = getContext();
return react::createPromiseAsJSIValue(
return RNJsi::JsiPromises::createPromiseAsJSIValue(
runtime,
[context = std::move(context),
viewTag](jsi::Runtime &runtime,
std::shared_ptr<react::Promise> promise) -> void {
[context = std::move(context), viewTag](
jsi::Runtime &runtime,
std::shared_ptr<RNJsi::JsiPromises::Promise> promise) -> void {
// Create a stream operation - this will be run on the main thread
context->makeViewScreenshot(
viewTag, [&runtime, context = std::move(context),
Expand Down
39 changes: 39 additions & 0 deletions package/cpp/jsi/JsiPromises.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "JsiPromises.h"

namespace RNJsi {

JsiPromises::Promise::Promise(jsi::Runtime &rt, jsi::Function resolve,
jsi::Function reject)
: runtime_(rt), resolve_(std::move(resolve)), reject_(std::move(reject)) {}

void JsiPromises::Promise::resolve(const jsi::Value &result) {
resolve_.call(runtime_, result);
}

void JsiPromises::Promise::reject(const std::string &message) {
jsi::Object error(runtime_);
error.setProperty(runtime_, "message",
jsi::String::createFromUtf8(runtime_, message));
reject_.call(runtime_, error);
}

jsi::Value
JsiPromises::createPromiseAsJSIValue(jsi::Runtime &rt,
PromiseSetupFunctionType &&func) {
jsi::Function JSPromise = rt.global().getPropertyAsFunction(rt, "Promise");
jsi::Function fn = jsi::Function::createFromHostFunction(
rt, jsi::PropNameID::forAscii(rt, "fn"), 2,
[func = std::move(func)](jsi::Runtime &rt2, const jsi::Value &thisVal,
const jsi::Value *args, size_t count) {
jsi::Function resolve = args[0].getObject(rt2).getFunction(rt2);
jsi::Function reject = args[1].getObject(rt2).getFunction(rt2);
auto wrapper = std::make_shared<Promise>(rt2, std::move(resolve),
std::move(reject));
func(rt2, wrapper);
return jsi::Value::undefined();
});

return JSPromise.callAsConstructor(rt, fn);
}

} // namespace RNJsi
50 changes: 50 additions & 0 deletions package/cpp/jsi/JsiPromises.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <memory>
#include <string>
#include <utility>

#include <jsi/jsi.h>

#include "SkBase64.h"

namespace RNJsi {
namespace jsi = facebook::jsi;

/**
These classes are taken from ReactCommon TurboModuleUtils. It is no longer (RN
0.72) possible to include and uses TurboModulesUtils without a lot of trouble
when use_frameworks are true in POD file. Instead we're now just including the
implementations ourselves.
*/

class LongLivedObject {
public:
void allowRelease();

protected:
LongLivedObject() = default;
virtual ~LongLivedObject() = default;
};

class JsiPromises {
public:
struct Promise : public LongLivedObject {
Promise(jsi::Runtime &rt, jsi::Function resolve, jsi::Function reject);

void resolve(const jsi::Value &result);
void reject(const std::string &error);

jsi::Runtime &runtime_;
jsi::Function resolve_;
jsi::Function reject_;
};

using PromiseSetupFunctionType =
std::function<void(jsi::Runtime &rt, std::shared_ptr<Promise>)>;

static jsi::Value createPromiseAsJSIValue(jsi::Runtime &rt,
PromiseSetupFunctionType &&func);
};

} // namespace RNJsi