1*af2ec015STomasz Sapeta // Copyright 2022-present 650 Industries. All rights reserved.
2*af2ec015STomasz Sapeta 
3*af2ec015STomasz Sapeta #include "JSIUtils.h"
4*af2ec015STomasz Sapeta #include "LazyObject.h"
5*af2ec015STomasz Sapeta 
6*af2ec015STomasz Sapeta namespace ABI49_0_0expo {
7*af2ec015STomasz Sapeta 
LazyObject(const LazyObjectInitializer initializer)8*af2ec015STomasz Sapeta LazyObject::LazyObject(const LazyObjectInitializer initializer) : initializer(initializer) {}
9*af2ec015STomasz Sapeta 
~LazyObject()10*af2ec015STomasz Sapeta LazyObject::~LazyObject() {
11*af2ec015STomasz Sapeta   backedObject = nullptr;
12*af2ec015STomasz Sapeta }
13*af2ec015STomasz Sapeta 
get(jsi::Runtime & runtime,const jsi::PropNameID & name)14*af2ec015STomasz Sapeta jsi::Value LazyObject::get(jsi::Runtime &runtime, const jsi::PropNameID &name) {
15*af2ec015STomasz Sapeta   if (!backedObject) {
16*af2ec015STomasz Sapeta     if (name.utf8(runtime) == "$$typeof") {
17*af2ec015STomasz Sapeta       // ABI49_0_0React Native asks for this property for some reason, we can just ignore it.
18*af2ec015STomasz Sapeta       return jsi::Value::undefined();
19*af2ec015STomasz Sapeta     }
20*af2ec015STomasz Sapeta     backedObject = initializer(runtime);
21*af2ec015STomasz Sapeta   }
22*af2ec015STomasz Sapeta   return backedObject ? backedObject->getProperty(runtime, name) : jsi::Value::undefined();
23*af2ec015STomasz Sapeta }
24*af2ec015STomasz Sapeta 
set(jsi::Runtime & runtime,const jsi::PropNameID & name,const jsi::Value & value)25*af2ec015STomasz Sapeta void LazyObject::set(jsi::Runtime &runtime, const jsi::PropNameID &name, const jsi::Value &value) {
26*af2ec015STomasz Sapeta   if (!backedObject) {
27*af2ec015STomasz Sapeta     backedObject = initializer(runtime);
28*af2ec015STomasz Sapeta   }
29*af2ec015STomasz Sapeta   if (backedObject) {
30*af2ec015STomasz Sapeta     backedObject->setProperty(runtime, name, value);
31*af2ec015STomasz Sapeta   }
32*af2ec015STomasz Sapeta }
33*af2ec015STomasz Sapeta 
getPropertyNames(jsi::Runtime & runtime)34*af2ec015STomasz Sapeta std::vector<jsi::PropNameID> LazyObject::getPropertyNames(jsi::Runtime &runtime) {
35*af2ec015STomasz Sapeta   if (!backedObject) {
36*af2ec015STomasz Sapeta     backedObject = initializer(runtime);
37*af2ec015STomasz Sapeta   }
38*af2ec015STomasz Sapeta   if (backedObject) {
39*af2ec015STomasz Sapeta     jsi::Array propertyNames = backedObject->getPropertyNames(runtime);
40*af2ec015STomasz Sapeta     return jsiArrayToPropNameIdsVector(runtime, propertyNames);
41*af2ec015STomasz Sapeta   }
42*af2ec015STomasz Sapeta   return {};
43*af2ec015STomasz Sapeta }
44*af2ec015STomasz Sapeta 
45*af2ec015STomasz Sapeta } // namespace ABI49_0_0expo
46