Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add TurboModuleRegistry.getLazy<T>(..) #43675

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/eslint-plugin-specs/react-native-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ function isModuleRequire(node) {
!(
memberExpression.property.type === 'Identifier' &&
(memberExpression.property.name === 'get' ||
memberExpression.property.name === 'getEnforcing')
memberExpression.property.name === 'getEnforcing' ||
memberExpression.property.name === 'getLazy')
)
) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native-codegen/src/parsers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ function isModuleRegistryCall(node: $FlowFixMe): boolean {
!(
memberExpression.property.type === 'Identifier' &&
(memberExpression.property.name === 'get' ||
memberExpression.property.name === 'getEnforcing')
memberExpression.property.name === 'getEnforcing' ||
memberExpression.property.name === 'getLazy')
)
) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ import {TurboModule} from './RCTExport';

export function get<T extends TurboModule>(name: string): T | null;
export function getEnforcing<T extends TurboModule>(name: string): T;
export function getLazy<T extends TurboModule>(name: string): T | null;
16 changes: 16 additions & 0 deletions packages/react-native/Libraries/TurboModule/TurboModuleRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,19 @@ export function getEnforcing<T: TurboModule>(name: string): T {
invariant(module != null, message);
return module;
}

interface ModuleHolder<T> {
module: T | null
}

export function getLazy<T: TurboModule>(name: string): T {
const proxy = new Proxy<ModuleHolder<T>>({ module: null }, {
get: (target, property) => {
if (target.module == null) {
target.module = getEnforcing(name);
}
return target.module[property];
},
});
return proxy as T;
}
Comment on lines +91 to +106
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrousavy, in general, we want to discourage people from using Proxy. It's extremely slow: I've seen ~20x slowdown quoted internally (for Hermes). And, it may preclude some compile-time optimizations we might want to ship to react native the framework.

So, unfortunately, we cannot ship this addition to the core.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, makes sense. I didn't know about these slowdowns, I'll keep that in mind when working on react-native-mmkv - thanks!