Skip to content

Commit

Permalink
refactor: refactored module installation (#180)
Browse files Browse the repository at this point in the history
* refactor: refactored module installation

* refactor: refactor module installation

---------

Co-authored-by: Maciej Makowski <[email protected]>
  • Loading branch information
maciejmakowski2003 and Maciej Makowski authored Oct 29, 2024
1 parent 1f1c1a5 commit 08202cd
Showing 1 changed file with 10 additions and 50 deletions.
60 changes: 10 additions & 50 deletions packages/react-native-audio-api/src/utils/install.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,25 @@
import { NativeModules, Platform } from 'react-native';
import { TurboModuleRegistry, TurboModule } from 'react-native';

interface AudioAPIModuleSpec extends TurboModule {
install(): boolean;
}

export function installModule() {
const AudioAPIModule = NativeModules.AudioAPIModule;
const AudioAPIModule =
TurboModuleRegistry.getEnforcing<AudioAPIModuleSpec>('AudioAPIModule');

if (AudioAPIModule == null) {
throw new Error(buildErrorMessage());
throw new Error(
`Failed to install react-native-audio-api: The native module could not be found.`
);
}

verifyOnDevice(AudioAPIModule);
runInstall(AudioAPIModule);
verifyInstallation();

return AudioAPIModule;
}

function buildErrorMessage(): string {
let message = `
Failed to install react-native-audio-api: The native 'AudioAPI' Module could not be found.
* Make sure react-native-audio-api is correctly autolinked (run 'npx react-native config' to verify).
`;

message += verifyAppleOS();
message += verifyExpo();

message += '\n* Make sure you rebuilt the app.';

return message.trim();
}

function verifyAppleOS(): string {
if (Platform.OS === 'ios' || Platform.OS === 'macos') {
return '\n* Make sure you ran `pod install` in the ios/ directory.';
}

return '';
}

function verifyExpo(): string {
const ExpoConstants =
NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;

if (!ExpoConstants) {
return '';
}

if (ExpoConstants.appOwnership === 'expo') {
return "\n* 'react-native-audio-api' is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.";
}

return "\n* Make sure you ran 'expo prebuild'.";
}

function verifyOnDevice(Module: any) {
if (global.nativeCallSyncHook == null || Module.install == null) {
throw new Error(
'Failed to install react-native-audio-api: React Native is not running on-device. Audio API can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.'
);
}
}

function runInstall(Module: any) {
const result = Module.install();

Expand Down

0 comments on commit 08202cd

Please sign in to comment.