-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[modules][ios] Defer the creation of module's JS object until its fir…
…st usage (#18863)
- Loading branch information
Showing
11 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2022-present 650 Industries. All rights reserved. | ||
|
||
#include "JSIUtils.h" | ||
|
||
namespace expo { | ||
|
||
std::vector<jsi::PropNameID> jsiArrayToPropNameIdsVector(jsi::Runtime &runtime, const jsi::Array &array) { | ||
size_t size = array.size(runtime); | ||
std::vector<jsi::PropNameID> vector; | ||
|
||
vector.reserve(size); | ||
|
||
for (size_t i = 0; i < size; i++) { | ||
jsi::String name = array.getValueAtIndex(runtime, i).getString(runtime); | ||
vector.push_back(jsi::PropNameID::forString(runtime, name)); | ||
} | ||
return vector; | ||
} | ||
|
||
} // namespace expo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2022-present 650 Industries. All rights reserved. | ||
|
||
#pragma once | ||
#ifdef __cplusplus | ||
|
||
#include <jsi/jsi.h> | ||
|
||
namespace jsi = facebook::jsi; | ||
|
||
namespace expo { | ||
|
||
/** | ||
Converts `jsi::Array` to a vector with prop name ids (`std::vector<jsi::PropNameID>`). | ||
*/ | ||
std::vector<jsi::PropNameID> jsiArrayToPropNameIdsVector(jsi::Runtime &runtime, const jsi::Array &array); | ||
|
||
} // namespace expo | ||
|
||
#endif // __cplusplus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2022-present 650 Industries. All rights reserved. | ||
|
||
#include "JSIUtils.h" | ||
#include "LazyObject.h" | ||
|
||
namespace expo { | ||
|
||
LazyObject::LazyObject(const LazyObjectInitializer initializer) : initializer(initializer) {} | ||
|
||
LazyObject::~LazyObject() { | ||
backedObject = nullptr; | ||
} | ||
|
||
jsi::Value LazyObject::get(jsi::Runtime &runtime, const jsi::PropNameID &name) { | ||
if (!backedObject) { | ||
if (name.utf8(runtime) == "$$typeof") { | ||
// React Native asks for this property for some reason, we can just ignore it. | ||
return jsi::Value::undefined(); | ||
} | ||
backedObject = initializer(runtime); | ||
} | ||
return backedObject ? backedObject->getProperty(runtime, name) : jsi::Value::undefined(); | ||
} | ||
|
||
void LazyObject::set(jsi::Runtime &runtime, const jsi::PropNameID &name, const jsi::Value &value) { | ||
if (!backedObject) { | ||
backedObject = initializer(runtime); | ||
} | ||
if (backedObject) { | ||
backedObject->setProperty(runtime, name, value); | ||
} | ||
} | ||
|
||
std::vector<jsi::PropNameID> LazyObject::getPropertyNames(jsi::Runtime &runtime) { | ||
if (!backedObject) { | ||
backedObject = initializer(runtime); | ||
} | ||
if (backedObject) { | ||
jsi::Array propertyNames = backedObject->getPropertyNames(runtime); | ||
return jsiArrayToPropNameIdsVector(runtime, propertyNames); | ||
} | ||
return {}; | ||
} | ||
|
||
} // namespace expo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2022-present 650 Industries. All rights reserved. | ||
|
||
#pragma once | ||
|
||
#ifdef __cplusplus | ||
|
||
#include <jsi/jsi.h> | ||
|
||
namespace jsi = facebook::jsi; | ||
|
||
namespace expo { | ||
|
||
/** | ||
A function that is responsible for initializing the backed object. | ||
*/ | ||
typedef std::function<std::shared_ptr<jsi::Object>(jsi::Runtime &)> LazyObjectInitializer; | ||
|
||
/** | ||
A host object that defers the creating of the raw object until any property is accessed for the first time. | ||
*/ | ||
class JSI_EXPORT LazyObject : public jsi::HostObject { | ||
public: | ||
using Shared = std::shared_ptr<LazyObject>; | ||
|
||
LazyObject(const LazyObjectInitializer initializer); | ||
|
||
virtual ~LazyObject(); | ||
|
||
jsi::Value get(jsi::Runtime &, const jsi::PropNameID &name) override; | ||
|
||
void set(jsi::Runtime &, const jsi::PropNameID &name, const jsi::Value &value) override; | ||
|
||
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override; | ||
|
||
private: | ||
const LazyObjectInitializer initializer; | ||
std::shared_ptr<jsi::Object> backedObject; | ||
|
||
}; // class LazyObject | ||
|
||
} // namespace expo | ||
|
||
#endif // __cplusplus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters