mirror of
https://github.com/zoriya/nitro.git
synced 2026-10-13 00:05:23 +00:00
The v0.37.0 View props rewrite changed `CachedProp<T>`'s API (immutable shared entry, `get()`/`isProvided()`/`hasSameValue()`), which broke modules whose specs were generated by nitrogen <= 0.36.x - their generated code uses the old mutable API (`.value`, `.isDirty`, 3-arg `fromRawValue(runtime, value, oldProp)`) and no longer compiled against the new core. - Rename the new implementation to `ReactProp<T>` (cpp/views/ReactProp.hpp), and fold its now-single-use private helpers into `fromRawValue` - Re-add `CachedProp<T>` exactly as it shipped in v0.36.5, marked [[deprecated]], so old generated specs compile again (with a warning) - Update nitrogen to emit `ReactProp` and re-generate nitro-test specs
68 lines
2.0 KiB
C++
68 lines
2.0 KiB
C++
//
|
|
// Created by Marc Rousavy on 30.07.24.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include "BorrowingReference.hpp"
|
|
#include "JSIConverter.hpp"
|
|
#include "NitroDefines.hpp"
|
|
#include <jsi/jsi.h>
|
|
|
|
namespace margelo::nitro {
|
|
|
|
using namespace facebook;
|
|
|
|
/**
|
|
* A React prop that caches its converted value against the original
|
|
* JS value (via `jsi::Value::strictEquals(...)`).
|
|
*
|
|
* @deprecated This is the legacy mutable prop type - it only exists so specs
|
|
* generated by older versions of nitrogen still compile.
|
|
* Update nitrogen and re-generate your specs to use `ReactProp<T>` instead.
|
|
*/
|
|
template <typename T>
|
|
struct [[deprecated("CachedProp<T> is deprecated. Update nitrogen and re-generate your specs to use ReactProp<T> instead.")]] CachedProp {
|
|
public:
|
|
T value;
|
|
bool isDirty = false;
|
|
|
|
// Default constructor
|
|
CachedProp() = default;
|
|
// Constructor with value
|
|
CachedProp(T&& value, BorrowingReference<jsi::Value>&& jsiValue)
|
|
: value(std::move(value)), isDirty(true), jsiValue(std::move(jsiValue)) {}
|
|
// Copy/Move/Destruct
|
|
CachedProp(const CachedProp&) = default;
|
|
CachedProp(CachedProp&&) = default;
|
|
~CachedProp() = default;
|
|
|
|
private:
|
|
BorrowingReference<jsi::Value> jsiValue;
|
|
|
|
public:
|
|
bool equals(jsi::Runtime& runtime, const jsi::Value& other) const {
|
|
if (jsiValue == nullptr) {
|
|
return false;
|
|
}
|
|
return jsi::Value::strictEquals(runtime, *jsiValue, other);
|
|
}
|
|
|
|
public:
|
|
static CachedProp<T> fromRawValue(jsi::Runtime& runtime, const jsi::Value& value, const CachedProp<T>& oldProp) {
|
|
if (oldProp.equals(runtime, value)) {
|
|
// jsi::Value hasn't changed - no need to convert it again!
|
|
return oldProp;
|
|
}
|
|
T converted = JSIConverter<T>::fromJSI(runtime, value);
|
|
BorrowingReference<jsi::Value> cached;
|
|
{
|
|
JSICacheReference cache = JSICache::getOrCreateCache(runtime);
|
|
cached = cache.makeShared(jsi::Value(runtime, value));
|
|
}
|
|
return CachedProp<T>(std::move(converted), std::move(cached));
|
|
}
|
|
};
|
|
|
|
} // namespace margelo::nitro
|