#pragma once #include #include #include "AsyncQueue.h" #include "JSScheduler.h" #include "Shareables.h" #include #include #include #include #include using namespace facebook; using namespace react; namespace reanimated { class WorkletRuntime : public jsi::HostObject, public std::enable_shared_from_this { public: explicit WorkletRuntime( jsi::Runtime &rnRuntime, const std::shared_ptr &jsQueue, const std::shared_ptr &jsScheduler, const std::string &name, const bool supportsLocking, const std::string &valueUnpackerCode); jsi::Runtime &getJSIRuntime() const { return *runtime_; } template inline jsi::Value runGuarded( const std::shared_ptr &shareableWorklet, Args &&...args) const { jsi::Runtime &rt = *runtime_; return runOnRuntimeGuarded( rt, shareableWorklet->getJSValue(rt), std::forward(args)...); } void runAsyncGuarded( const std::shared_ptr &shareableWorklet) { if (queue_ == nullptr) { queue_ = std::make_shared(name_); } queue_->push( [=, self = shared_from_this()] { self->runGuarded(shareableWorklet); }); } jsi::Value executeSync(jsi::Runtime &rt, const jsi::Value &worklet) const; std::string toString() const { return "[WorkletRuntime \"" + name_ + "\"]"; } jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propName) override; std::vector getPropertyNames(jsi::Runtime &rt) override; private: const std::shared_ptr runtimeMutex_; const std::shared_ptr runtime_; #ifndef NDEBUG const bool supportsLocking_; #endif const std::string name_; std::shared_ptr queue_; }; // This function needs to be non-inline to avoid problems with dynamic_cast on // Android std::shared_ptr extractWorkletRuntime( jsi::Runtime &rt, const jsi::Value &value); void scheduleOnRuntime( jsi::Runtime &rt, const jsi::Value &workletRuntimeValue, const jsi::Value &shareableWorkletValue); } // namespace reanimated