Skip to content

Commit

Permalink
ReactContext should have a weak ref on the PropertyBag (#12340)
Browse files Browse the repository at this point in the history
* ReactContext should have a weak ref on the PropertyBag

* Change files
  • Loading branch information
acoates-ms authored Nov 1, 2023
1 parent 1f5c70b commit 325195c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "ReactContext should have a weak ref on the PropertyBag",
"packageName": "react-native-windows",
"email": "30809111+acoates-ms@users.noreply.github.com",
"dependentChangeType": "patch"
}
38 changes: 37 additions & 1 deletion vnext/Microsoft.ReactNative/ReactHost/MsoReactContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,42 @@ bool ReactSettingsSnapshot::UseDeveloperSupport() const noexcept {
return false;
}

struct WeakRefPropertyBag : winrt::implements<WeakRefPropertyBag, winrt::Microsoft::ReactNative::IReactPropertyBag> {
WeakRefPropertyBag(winrt::Microsoft::ReactNative::IReactPropertyBag propertyBag) : m_wkPropBag(propertyBag) {}

IInspectable Get(winrt::Microsoft::ReactNative::IReactPropertyName const &name) noexcept {
if (auto propBag = m_wkPropBag.get()) {
return propBag.Get(name);
}
return nullptr;
}

IInspectable GetOrCreate(
winrt::Microsoft::ReactNative::IReactPropertyName const &name,
winrt::Microsoft::ReactNative::ReactCreatePropertyValue const &createValue) noexcept {
if (auto propBag = m_wkPropBag.get()) {
return propBag.GetOrCreate(name, createValue);
}
return nullptr;
}

IInspectable Set(winrt::Microsoft::ReactNative::IReactPropertyName const &name, IInspectable const &value) noexcept {
if (auto propBag = m_wkPropBag.get()) {
return propBag.Set(name, value);
}
return nullptr;
}

void CopyFrom(winrt::Microsoft::ReactNative::IReactPropertyBag const &value) noexcept {
if (auto propBag = m_wkPropBag.get()) {
return propBag.CopyFrom(value);
}
}

private:
winrt::weak_ref<winrt::Microsoft::ReactNative::IReactPropertyBag> m_wkPropBag;
};

//=============================================================================================
// ReactContext implementation
//=============================================================================================
Expand All @@ -131,7 +167,7 @@ ReactContext::ReactContext(
winrt::Microsoft::ReactNative::IReactNotificationService const &notifications) noexcept
: m_reactInstance{std::move(reactInstance)},
m_settings{Mso::Make<ReactSettingsSnapshot>(Mso::Copy(m_reactInstance))},
m_properties{properties},
m_properties{winrt::make<WeakRefPropertyBag>(properties)},
m_notifications{notifications} {}

void ReactContext::Destroy() noexcept {
Expand Down

0 comments on commit 325195c

Please # to comment.