1 // Copyright 2022-present 650 Industries. All rights reserved.
2 
3 #pragma once
4 
5 #ifdef __cplusplus
6 
7 #include <jsi/jsi.h>
8 
9 namespace jsi = facebook::jsi;
10 
11 namespace expo {
12 
13 /**
14  A function that is responsible for initializing the backed object.
15  */
16 typedef std::function<std::shared_ptr<jsi::Object>(jsi::Runtime &)> LazyObjectInitializer;
17 
18 /**
19  A host object that defers the creating of the raw object until any property is accessed for the first time.
20  */
21 class JSI_EXPORT LazyObject : public jsi::HostObject {
22 public:
23   using Shared = std::shared_ptr<LazyObject>;
24 
25   LazyObject(const LazyObjectInitializer initializer);
26 
27   virtual ~LazyObject();
28 
29   jsi::Value get(jsi::Runtime &, const jsi::PropNameID &name) override;
30 
31   void set(jsi::Runtime &, const jsi::PropNameID &name, const jsi::Value &value) override;
32 
33   std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
34 
35 private:
36   const LazyObjectInitializer initializer;
37   std::shared_ptr<jsi::Object> backedObject;
38 
39 }; // class LazyObject
40 
41 } // namespace expo
42 
43 #endif // __cplusplus
44