// Copyright © 2021-present 650 Industries, Inc. (aka Expo) #pragma once #include "JavaReferencesCache.h" #include "Exceptions.h" #include "JavaScriptObject.h" #include "JavaScriptRuntime.h" #include "JSIObjectWrapper.h" #include "WeakRuntimeHolder.h" #include #include #include namespace expo { /** * Registry used to store references to often used JS objects like Promise. * The object lifetime should be bound with the JS runtime. */ class JSReferencesCache { public: enum class JSKeys { PROMISE }; JSReferencesCache() = delete; JSReferencesCache(jsi::Runtime &runtime); /** * Gets a cached object. */ template, int> = 0> T &getObject(JSKeys key) { return static_cast(*jsObjectRegistry.at(key)); } /** * Gets a cached object if present. Otherwise, returns nullptr. */ template, int> = 0> T *getOptionalObject(JSKeys key) { auto result = jsObjectRegistry.find(key); if (result == jsObjectRegistry.end()) { return nullptr; } jsi::Object &object = *result->second; return &static_cast(object); } /** * Gets a cached jsi::PropNameID or creates a new one for the provided string. */ jsi::PropNameID &getPropNameID(jsi::Runtime &runtime, const std::string &name); private: std::unordered_map> jsObjectRegistry; std::unordered_map> propNameIDRegistry; }; } // namespace expo