Skip to content

Commit

Permalink
chore: Aggregate commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tjzel committed Sep 3, 2024
1 parent 5a7d3b6 commit d87a9e1
Show file tree
Hide file tree
Showing 62 changed files with 1,205 additions and 577 deletions.
2 changes: 1 addition & 1 deletion apps/fabric-example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2191,7 +2191,7 @@ SPEC CHECKSUMS:
RNCPicker: 7973f617de8809ab9e7577b93ce23d3449fb1ec7
RNFlashList: 2381687e1305c20b7e173e9a980e2b9a7a3973f5
RNGestureHandler: 8781e2529230a1bc3ea8d75e5c3cd071b6c6aed7
RNReanimated: 1d37407d7cd4bd430cd7fed170be53fa219b6528
RNReanimated: 37c88511191e491ef9788e75899315b69d11cb0a
RNScreens: de6e57426ba0e6cbc3fb5b4f496e7f08cb2773c2
RNSVG: 6b65086b51556fd9723d5570a3455e865e1304a3
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "Shareables.h"

#include <jsi/jsi.h>
#include <stdio.h>
#include <functional>
#include <memory>
#include <mutex>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,16 @@
#endif

#include <functional>
#include <iomanip>
#include <sstream>
#include <thread>
#include <unordered_map>

#ifdef RCT_NEW_ARCH_ENABLED
#include <react/renderer/scheduler/Scheduler.h>
#include "ReanimatedCommitShadowNode.h"
#include "ShadowTreeCloner.h"
#endif

#include "AsyncQueue.h"
#include "CollectionUtils.h"
#include "EventHandlerRegistry.h"
#include "FeaturesConfig.h"
#include "ReanimatedWorkletRuntimeDecorator.h"
#include "Shareables.h"
#include "UIRuntimeDecorator.h"
#include "WorkletEventHandler.h"

#ifdef __ANDROID__
#include <fbjni/fbjni.h>
Expand All @@ -43,39 +34,22 @@ bool CoreFeatures::useNativeState;
namespace reanimated {

NativeReanimatedModule::NativeReanimatedModule(
jsi::Runtime &rnRuntime,
const std::shared_ptr<JSScheduler> &jsScheduler,
const std::shared_ptr<MessageQueueThread> &jsQueue,
const std::shared_ptr<UIScheduler> &uiScheduler,
const std::shared_ptr<NativeWorkletsModule> &NativeWorkletsModule,
const PlatformDepMethodsHolder &platformDepMethodsHolder,
const std::string &valueUnpackerCode,
const bool isBridgeless,
const bool isReducedMotion)
: NativeReanimatedModuleSpec(
isBridgeless ? nullptr : jsScheduler->getJSCallInvoker()),
isBridgeless_(isBridgeless),
: NativeReanimatedModuleSpec(NativeWorkletsModule->getJSCallInvoker()),
isReducedMotion_(isReducedMotion),
jsQueue_(jsQueue),
jsScheduler_(jsScheduler),
uiScheduler_(uiScheduler),
uiWorkletRuntime_(std::make_shared<WorkletRuntime>(
rnRuntime,
jsQueue,
jsScheduler_,
"Reanimated UI runtime",
true /* supportsLocking */,
valueUnpackerCode)),
valueUnpackerCode_(valueUnpackerCode),
NativeWorkletsModule_(NativeWorkletsModule),
eventHandlerRegistry_(std::make_unique<EventHandlerRegistry>()),
requestRender_(platformDepMethodsHolder.requestRender),
onRenderCallback_([this](const double timestampMs) {
renderRequested_ = false;
onRender(timestampMs);
}),
animatedSensorModule_(platformDepMethodsHolder),
jsLogger_(std::make_shared<JSLogger>(jsScheduler_)),
layoutAnimationsManager_(
std::make_shared<LayoutAnimationsManager>(jsLogger_)),

layoutAnimationsManager_(std::make_shared<LayoutAnimationsManager>(
NativeWorkletsModule_->getJSLogger())),
#ifdef RCT_NEW_ARCH_ENABLED
synchronouslyUpdateUIPropsFunction_(
platformDepMethodsHolder.synchronouslyUpdateUIPropsFunction),
Expand Down Expand Up @@ -156,7 +130,7 @@ void NativeReanimatedModule::commonInit(
};
#endif

jsi::Runtime &uiRuntime = uiWorkletRuntime_->getJSIRuntime();
jsi::Runtime &uiRuntime = NativeWorkletsModule_->getUIRuntime();
UIRuntimeDecorator::decorate(
uiRuntime,
#ifdef RCT_NEW_ARCH_ENABLED
Expand Down Expand Up @@ -190,66 +164,6 @@ NativeReanimatedModule::~NativeReanimatedModule() {
// runtime, so they have to go away before we tear down the runtime
eventHandlerRegistry_.reset();
frameCallbacks_.clear();
uiWorkletRuntime_.reset();
}

void NativeReanimatedModule::scheduleOnUI(
jsi::Runtime &rt,
const jsi::Value &worklet) {
auto shareableWorklet = extractShareableOrThrow<ShareableWorklet>(
rt, worklet, "[Reanimated] Only worklets can be scheduled to run on UI.");
uiScheduler_->scheduleOnUI([=] {
#if JS_RUNTIME_HERMES
// JSI's scope defined here allows for JSI-objects to be cleared up after
// each runtime loop. Within these loops we typically create some temporary
// JSI objects and hence it allows for such objects to be garbage collected
// much sooner.
// Apparently the scope API is only supported on Hermes at the moment.
const auto scope = jsi::Scope(uiWorkletRuntime_->getJSIRuntime());
#endif
uiWorkletRuntime_->runGuarded(shareableWorklet);
});
}

jsi::Value NativeReanimatedModule::executeOnUIRuntimeSync(
jsi::Runtime &rt,
const jsi::Value &worklet) {
return uiWorkletRuntime_->executeSync(rt, worklet);
}

jsi::Value NativeReanimatedModule::createWorkletRuntime(
jsi::Runtime &rt,
const jsi::Value &name,
const jsi::Value &initializer) {
auto workletRuntime = std::make_shared<WorkletRuntime>(
rt,
jsQueue_,
jsScheduler_,
name.asString(rt).utf8(rt),
false /* supportsLocking */,
valueUnpackerCode_);
auto initializerShareable = extractShareableOrThrow<ShareableWorklet>(
rt, initializer, "[Reanimated] Initializer must be a worklet.");
workletRuntime->runGuarded(initializerShareable);
ReanimatedWorkletRuntimeDecorator::decorate(workletRuntime->getJSIRuntime());
return jsi::Object::createFromHostObject(rt, workletRuntime);
}

jsi::Value NativeReanimatedModule::scheduleOnRuntime(
jsi::Runtime &rt,
const jsi::Value &workletRuntimeValue,
const jsi::Value &shareableWorkletValue) {
reanimated::scheduleOnRuntime(rt, workletRuntimeValue, shareableWorkletValue);
return jsi::Value::undefined();
}

jsi::Value NativeReanimatedModule::makeShareableClone(
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote,
const jsi::Value &nativeStateSource) {
return reanimated::makeShareableClone(
rt, value, shouldRetainRemote, nativeStateSource);
}

jsi::Value NativeReanimatedModule::registerEventHandler(
Expand All @@ -265,10 +179,10 @@ jsi::Value NativeReanimatedModule::registerEventHandler(
rt, worklet, "[Reanimated] Event handler must be a worklet.");
int emitterReactTagInt = emitterReactTag.asNumber();

uiScheduler_->scheduleOnUI([=] {
NativeWorkletsModule_->getUIScheduler()->scheduleOnUI([=, this] {
auto handler = std::make_shared<WorkletEventHandler>(
newRegistrationId, eventNameStr, emitterReactTagInt, handlerShareable);
eventHandlerRegistry_->registerEventHandler(std::move(handler));
eventHandlerRegistry_->registerEventHandler(handler);
});

return jsi::Value(static_cast<double>(newRegistrationId));
Expand All @@ -278,8 +192,8 @@ void NativeReanimatedModule::unregisterEventHandler(
jsi::Runtime &,
const jsi::Value &registrationId) {
uint64_t id = registrationId.asNumber();
uiScheduler_->scheduleOnUI(
[=] { eventHandlerRegistry_->unregisterEventHandler(id); });
NativeWorkletsModule_->getUIScheduler()->scheduleOnUI(
[=, this] { eventHandlerRegistry_->unregisterEventHandler(id); });
}

#ifdef RCT_NEW_ARCH_ENABLED
Expand Down Expand Up @@ -348,8 +262,8 @@ jsi::Value NativeReanimatedModule::getViewProp(
const auto funPtr = std::make_shared<jsi::Function>(
callback.getObject(rnRuntime).asFunction(rnRuntime));
const auto shadowNode = shadowNodeFromValue(rnRuntime, shadowNodeWrapper);
uiScheduler_->scheduleOnUI([=]() {
jsi::Runtime &uiRuntime = uiWorkletRuntime_->getJSIRuntime();
NativeWorkletsModule_->getUIScheduler()->scheduleOnUI([=, this]() {
jsi::Runtime &uiRuntime = NativeWorkletsModule_->getUIRuntime();
const auto resultStr =
obtainPropFromShadowNode(uiRuntime, propNameStr, shadowNode);

Expand All @@ -375,19 +289,20 @@ jsi::Value NativeReanimatedModule::getViewProp(

const int viewTagInt = viewTag.asNumber();

uiScheduler_->scheduleOnUI([=]() {
jsi::Runtime &uiRuntime = uiWorkletRuntime_->getJSIRuntime();
NativeWorkletsModule_->getUIScheduler()->scheduleOnUI([=, this]() {
jsi::Runtime &uiRuntime = NativeWorkletsModule_->getUIRuntime();
const jsi::Value propNameValue =
jsi::String::createFromUtf8(uiRuntime, propNameStr);
const auto resultValue =
obtainPropFunction_(uiRuntime, viewTagInt, propNameValue);
const auto resultStr = resultValue.asString(uiRuntime).utf8(uiRuntime);

jsScheduler_->scheduleOnJS([=](jsi::Runtime &rnRuntime) {
const auto resultValue =
jsi::String::createFromUtf8(rnRuntime, resultStr);
funPtr->call(rnRuntime, resultValue);
});
NativeWorkletsModule_->getJSScheduler()->scheduleOnJS(
[=](jsi::Runtime &rnRuntime) {
const auto resultValue =
jsi::String::createFromUtf8(rnRuntime, resultStr);
funPtr->call(rnRuntime, resultValue);
});
});
return jsi::Value::undefined();
}
Expand Down Expand Up @@ -485,15 +400,15 @@ void NativeReanimatedModule::requestAnimationFrame(
void NativeReanimatedModule::maybeRequestRender() {
if (!renderRequested_) {
renderRequested_ = true;
jsi::Runtime &uiRuntime = uiWorkletRuntime_->getJSIRuntime();
jsi::Runtime &uiRuntime = NativeWorkletsModule_->getUIRuntime();
requestRender_(onRenderCallback_, uiRuntime);
}
}

void NativeReanimatedModule::onRender(double timestampMs) {
auto callbacks = std::move(frameCallbacks_);
frameCallbacks_.clear();
jsi::Runtime &uiRuntime = uiWorkletRuntime_->getJSIRuntime();
jsi::Runtime &uiRuntime = NativeWorkletsModule_->getUIRuntime();
jsi::Value timestamp{timestampMs};
for (const auto &callback : callbacks) {
runOnRuntimeGuarded(uiRuntime, *callback, timestamp);
Expand All @@ -508,7 +423,7 @@ jsi::Value NativeReanimatedModule::registerSensor(
const jsi::Value &sensorDataHandler) {
return animatedSensorModule_.registerSensor(
rt,
uiWorkletRuntime_,
NativeWorkletsModule_->getUIWorkletRuntime(),
sensorType,
interval,
iosReferenceFrame,
Expand Down Expand Up @@ -572,7 +487,11 @@ bool NativeReanimatedModule::handleEvent(
const jsi::Value &payload,
double currentTime) {
eventHandlerRegistry_->processEvent(
uiWorkletRuntime_, currentTime, eventName, emitterReactTag, payload);
NativeWorkletsModule_->getUIWorkletRuntime(),
currentTime,
eventName,
emitterReactTag,
payload);

// TODO: return true if Reanimated successfully handled the event
// to avoid sending it to JavaScript
Expand All @@ -598,7 +517,7 @@ bool NativeReanimatedModule::handleRawEvent(
if (eventType.rfind("top", 0) == 0) {
eventType = "on" + eventType.substr(3);
}
jsi::Runtime &rt = uiWorkletRuntime_->getJSIRuntime();
jsi::Runtime &rt = NativeWorkletsModule_->getUIRuntime();
#if REACT_NATIVE_MINOR_VERSION >= 73
const auto &eventPayload = rawEvent.eventPayload;
jsi::Value payload = eventPayload->asJSIValue(rt);
Expand Down Expand Up @@ -643,7 +562,7 @@ void NativeReanimatedModule::performOperations() {
auto copiedOperationsQueue = std::move(operationsInBatch_);
operationsInBatch_.clear();

jsi::Runtime &rt = uiWorkletRuntime_->getJSIRuntime();
jsi::Runtime &rt = NativeWorkletsModule_->getUIRuntime();

{
auto lock = propsRegistry_->createLock();
Expand Down Expand Up @@ -786,7 +705,7 @@ jsi::String NativeReanimatedModule::obtainProp(
jsi::Runtime &rt,
const jsi::Value &shadowNodeWrapper,
const jsi::Value &propName) {
jsi::Runtime &uiRuntime = uiWorkletRuntime_->getJSIRuntime();
jsi::Runtime &uiRuntime = NativeWorkletsModule_->getUIRuntime();
const auto propNameStr = propName.asString(rt).utf8(rt);
const auto shadowNode = shadowNodeFromValue(rt, shadowNodeWrapper);
const auto resultStr =
Expand Down Expand Up @@ -862,8 +781,8 @@ void NativeReanimatedModule::initializeLayoutAnimations() {
layoutAnimationsManager_,
componentDescriptorRegistry,
scheduler->getContextContainer(),
uiWorkletRuntime_->getJSIRuntime(),
uiScheduler_);
NativeWorkletsModule_->getUIRuntime(),
NativeWorkletsModule_->getUIScheduler());
uiManager_->getShadowTreeRegistry().enumerate(
[this](const ShadowTree &shadowTree, bool &stop) {
shadowTree.getMountingCoordinator()->setMountingOverrideDelegate(
Expand All @@ -882,8 +801,8 @@ jsi::Value NativeReanimatedModule::subscribeForKeyboardEvents(
handlerWorklet,
"[Reanimated] Keyboard event handler must be a worklet.");
return subscribeForKeyboardEventsFunction_(
[=](int keyboardState, int height) {
uiWorkletRuntime_->runGuarded(
[=, this](int keyboardState, int height) {
NativeWorkletsModule_->getUIWorkletRuntime()->runGuarded(
shareableHandler, jsi::Value(keyboardState), jsi::Value(height));
},
isStatusBarTranslucent.getBool());
Expand Down
Loading

0 comments on commit d87a9e1

Please sign in to comment.