Skip to content

Commit

Permalink
Scope LongLivedObjectCollection per runtime [2/n] (#43409)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #43409

Changelog:
[General] [Breaking] - Make `LongLivedObject` constructor accept a `Runtime` reference.

# Context

Approach 1 as described in [RFC post](https://fb.workplace.com/groups/615693552291894/permalink/1693347124526526/).

# This diff

* Embed `Runtime` reference in `LongLivedObject` to keep supporting `allowRelease` method (and update extending classes accordingly)
* Update MSFT fork accordingly

Reviewed By: RSNara

Differential Revision: D54638813

fbshipit-source-id: 0d58d1c32b3689bdd1223c13f5d5ee2f2e15f223
  • Loading branch information
fabriziocucci authored and facebook-github-bot committed Mar 11, 2024
1 parent c6076bc commit 3706bf0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ class CallbackWrapper : public LongLivedObject {
jsi::Function&& callback,
jsi::Runtime& runtime,
std::shared_ptr<CallInvoker> jsInvoker)
: callback_(std::move(callback)),
runtime_(runtime),
: LongLivedObject(runtime),
callback_(std::move(callback)),
jsInvoker_(std::move(jsInvoker)) {}

jsi::Function callback_;
jsi::Runtime& runtime_;
std::shared_ptr<CallInvoker> jsInvoker_;

public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <jsi/jsi.h>
#include <memory>
#include <mutex>
#include <unordered_set>
Expand All @@ -31,8 +32,9 @@ class LongLivedObject {
virtual void allowRelease();

protected:
LongLivedObject() = default;
explicit LongLivedObject(jsi::Runtime& runtime) : runtime_(runtime) {}
virtual ~LongLivedObject() = default;
jsi::Runtime& runtime_;
};

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/react-native/ReactCommon/react/bridging/Promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class AsyncPromise {
},
jsInvoker));

auto promiseHolder = std::make_shared<PromiseHolder>(promise.asObject(rt));
auto promiseHolder =
std::make_shared<PromiseHolder>(rt, promise.asObject(rt));
LongLivedObjectCollection::get().add(promiseHolder);

// The shared state can retain the promise holder weakly now.
Expand Down Expand Up @@ -71,7 +72,8 @@ class AsyncPromise {

private:
struct PromiseHolder : LongLivedObject {
PromiseHolder(jsi::Object p) : promise(std::move(p)) {}
PromiseHolder(jsi::Runtime& runtime, jsi::Object p)
: LongLivedObject(runtime), promise(std::move(p)) {}

jsi::Object promise;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ jsi::Array deepCopyJSIArray(jsi::Runtime& rt, const jsi::Array& arr) {
}

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

void Promise::resolve(const jsi::Value& result) {
resolve_.call(runtime_, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ struct Promise : public LongLivedObject {
void resolve(const jsi::Value& result);
void reject(const std::string& error);

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

0 comments on commit 3706bf0

Please sign in to comment.