Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Use LongLivedObjects for TurboModule callbacks (#10436) * Fix build break
- Loading branch information
Showing
17 changed files
with
803 additions
and
235 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/react-native-windows-74efbb30-e443-4b2f-b4d4-482b9fa56fe5.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "Use LongLivedObjects for TurboModule callbacks", | ||
"packageName": "react-native-windows", | ||
"email": "vmorozov@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
#pragma once | ||
#ifndef MICROSOFT_REACTNATIVE_JSI_LONGLIVEDJSIVALUE_ | ||
#define MICROSOFT_REACTNATIVE_JSI_LONGLIVEDJSIVALUE_ | ||
|
||
#include <ReactCommon/LongLivedObject.h> | ||
#include <jsi/jsi.h> | ||
|
||
namespace winrt::Microsoft::ReactNative { | ||
|
||
// Wrap up JSI Runtime into a LongLivedObject | ||
struct LongLivedJsiRuntime : facebook::react::LongLivedObject { | ||
static std::weak_ptr<LongLivedJsiRuntime> CreateWeak( | ||
std::shared_ptr<facebook::react::LongLivedObjectCollection> const &longLivedObjectCollection, | ||
facebook::jsi::Runtime &runtime) noexcept { | ||
auto value = std::shared_ptr<LongLivedJsiRuntime>(new LongLivedJsiRuntime(longLivedObjectCollection, runtime)); | ||
longLivedObjectCollection->add(value); | ||
return value; | ||
} | ||
|
||
facebook::jsi::Runtime &Runtime() { | ||
return runtime_; | ||
} | ||
|
||
public: // LongLivedObject overrides | ||
void allowRelease() override { | ||
if (auto longLivedObjectCollection = longLivedObjectCollection_.lock()) { | ||
if (longLivedObjectCollection != nullptr) { | ||
longLivedObjectCollection->remove(this); | ||
return; | ||
} | ||
} | ||
LongLivedObject::allowRelease(); | ||
} | ||
|
||
protected: | ||
LongLivedJsiRuntime( | ||
std::shared_ptr<facebook::react::LongLivedObjectCollection> const &longLivedObjectCollection, | ||
facebook::jsi::Runtime &runtime) | ||
: longLivedObjectCollection_(longLivedObjectCollection), runtime_(runtime) {} | ||
|
||
LongLivedJsiRuntime(LongLivedJsiRuntime const &) = delete; | ||
|
||
private: | ||
// Use a weak reference to the collection to avoid reference loops | ||
std::weak_ptr<facebook::react::LongLivedObjectCollection> longLivedObjectCollection_; | ||
facebook::jsi::Runtime &runtime_; | ||
}; | ||
|
||
// Wrap up a JSI Value into a LongLivedObject. | ||
template <typename TValue> | ||
struct LongLivedJsiValue : LongLivedJsiRuntime { | ||
static std::weak_ptr<LongLivedJsiValue<TValue>> CreateWeak( | ||
std::shared_ptr<facebook::react::LongLivedObjectCollection> const &longLivedObjectCollection, | ||
facebook::jsi::Runtime &runtime, | ||
TValue &&value) noexcept { | ||
auto valueWrapper = std::shared_ptr<LongLivedJsiValue<TValue>>( | ||
new LongLivedJsiValue<TValue>(longLivedObjectCollection, runtime, std::forward<TValue>(value))); | ||
longLivedObjectCollection->add(valueWrapper); | ||
return valueWrapper; | ||
} | ||
|
||
TValue &Value() { | ||
return value_; | ||
} | ||
|
||
protected: | ||
template <typename TValue2> | ||
LongLivedJsiValue( | ||
std::shared_ptr<facebook::react::LongLivedObjectCollection> const &longLivedObjectCollection, | ||
facebook::jsi::Runtime &runtime, | ||
TValue2 &&value) | ||
: LongLivedJsiRuntime(longLivedObjectCollection, runtime), value_(std::forward<TValue2>(value)) {} | ||
|
||
private: | ||
TValue value_; | ||
}; | ||
|
||
using LongLivedJsiFunction = LongLivedJsiValue<facebook::jsi::Function>; | ||
|
||
} // namespace winrt::Microsoft::ReactNative | ||
|
||
#endif // MICROSOFT_REACTNATIVE_JSI_LONGLIVEDJSIVALUE_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.