Skip to content

Commit

Permalink
fix: Fix lazy export
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Mar 27, 2024
1 parent c2e610d commit 7018389
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
2 changes: 0 additions & 2 deletions example/ios/.xcode.env.local

This file was deleted.

89 changes: 47 additions & 42 deletions src/LazyTurboModule.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,62 @@
import { NativeModules, Platform, TurboModule } from 'react-native';

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

/**
* Lazily get a TurboModule by wrapping it in a Proxy.
*/
export function getLazyTurboModule<T extends TurboModule>(
initializeTurboModule: () => T | null
): T {
const proxy = new Proxy<T>(null as unknown as T, {
get: (target, property) => {
if (target == null) {
// Target is null, let's initialize it!
const newTarget = initializeTurboModule();
const proxy = new Proxy<ModuleHolder<T>>(
{
module: null,
},
{
get: (target, property) => {
if (target.module == null) {
// Target is null, let's initialize it!
target.module = initializeTurboModule();

if (newTarget != null) {
// successfully initialized TurboModule!
target = newTarget;
} else {
// TurboModule not found, something went wrong!
let message =
'Failed to create a new MMKV instance: The native MMKV Module could not be found.';
message +=
'\n* Make sure react-native-mmkv is correctly autolinked (run `npx react-native config` to verify)';
if (Platform.OS === 'ios' || Platform.OS === 'macos') {
if (target.module == null) {
// TurboModule not found, something went wrong!
let message =
'Failed to create a new MMKV instance: The native MMKV Module could not be found.';
message +=
'\n* Make sure you ran `pod install` in the ios/ directory.';
}
if (Platform.OS === 'android') {
message += '\n* Make sure gradle is synced.';
}
// check if Expo
const ExpoConstants =
NativeModules.NativeUnimoduleProxy?.modulesConstants
?.ExponentConstants;
if (ExpoConstants != null) {
if (ExpoConstants.appOwnership === 'expo') {
// We're running Expo Go
throw new Error(
'react-native-mmkv is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'
);
} else {
// We're running Expo bare / standalone
message += '\n* Make sure you ran `expo prebuild`.';
'\n* Make sure react-native-mmkv is correctly autolinked (run `npx react-native config` to verify)';
if (Platform.OS === 'ios' || Platform.OS === 'macos') {
message +=
'\n* Make sure you ran `pod install` in the ios/ directory.';
}
if (Platform.OS === 'android') {
message += '\n* Make sure gradle is synced.';
}
// check if Expo
const ExpoConstants =
NativeModules.NativeUnimoduleProxy?.modulesConstants
?.ExponentConstants;
if (ExpoConstants != null) {
if (ExpoConstants.appOwnership === 'expo') {
// We're running Expo Go
throw new Error(
'react-native-mmkv is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'
);
} else {
// We're running Expo bare / standalone
message += '\n* Make sure you ran `expo prebuild`.';
}
}
}

message += '\n* Make sure you rebuilt the app.';
throw new Error(message);
message += '\n* Make sure you rebuilt the app.';
throw new Error(message);
}
}
}

// @ts-expect-error property accessors are not typed.
return target[property];
},
});
return proxy;
return target.module[property as keyof T];
},
}
);
return proxy as unknown as T;
}

0 comments on commit 7018389

Please sign in to comment.