Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion packages/skia/cpp/jsi/JsiPromises.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,64 @@
#include "JsiPromises.h"

#ifndef NDEBUG
#include "utils/RNSkLog.h"
#endif

namespace RNJsi {

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

JsiPromises::Promise::~Promise() {
bool shouldRemove = false;
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's look elegant to me. And it think this is safe if we set runtimeDestroyed_ = true; if the runtime ref match the one Promise hold.

std::lock_guard<std::mutex> lock(mutex_);
shouldRemove = !runtimeDestroyed_;
}
// Call removeListener outside the lock to avoid holding mutex_ across
// an external call. This keeps the locking hierarchy simple and avoids
// potential issues if RuntimeLifecycleMonitor's internals change.
if (shouldRemove) {
RuntimeLifecycleMonitor::removeListener(runtime_, this);
}
}

void JsiPromises::Promise::onRuntimeDestroyed(jsi::Runtime *rt) {
if (rt != &runtime_) {
return;
}
std::lock_guard<std::mutex> lock(mutex_);
runtimeDestroyed_ = true;
{
jsi::Function r(std::move(resolve_));
jsi::Function j(std::move(reject_));
}
}

void JsiPromises::Promise::resolve(const jsi::Value &result) {
std::lock_guard<std::mutex> lock(mutex_);
if (runtimeDestroyed_) {
#ifndef NDEBUG
RNSkia::RNSkLogger::logToConsole(
"Promise::resolve() dropped — runtime already torn down");
#endif
return;
}
resolve_.call(runtime_, result);
}

void JsiPromises::Promise::reject(const std::string &message) {
std::lock_guard<std::mutex> lock(mutex_);
if (runtimeDestroyed_) {
#ifndef NDEBUG
RNSkia::RNSkLogger::logToConsole(
"Promise::reject() dropped — runtime already torn down");
#endif
return;
}
jsi::Object error(runtime_);
error.setProperty(runtime_, "message",
jsi::String::createFromUtf8(runtime_, message));
Expand Down
9 changes: 8 additions & 1 deletion packages/skia/cpp/jsi/JsiPromises.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once

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

#include <jsi/jsi.h>

#include "RuntimeLifecycleMonitor.h"
#include "third_party/base64.h"

namespace RNJsi {
Expand All @@ -29,15 +31,20 @@ class LongLivedObject {

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

void onRuntimeDestroyed(jsi::Runtime *) override;

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

jsi::Runtime &runtime_;
std::mutex mutex_;
jsi::Function resolve_;
jsi::Function reject_;
bool runtimeDestroyed_{false};
};

using PromiseSetupFunctionType =
Expand Down
Loading