diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 8b638f2..a58e069 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -3,9 +3,9 @@ import { decompressFile, getLz4VersionNumber, getLz4VersionString, - _global, type FileOperationResult, } from '../index'; +import { _global } from '../module'; jest.mock('react-native', () => ({ Platform: { diff --git a/src/index.tsx b/src/index.tsx index 1213859..f9d4459 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,32 +1,9 @@ -import { NativeModules, Platform } from 'react-native'; import { formatFilePath } from './utils/formatFilePath'; -import type { FileOperationResult, Lz4Type } from './types'; +import type { FileOperationResult } from './types'; +import { _global, Lz4 } from './module'; -const LINKING_ERROR = - `The package 'react-native-lz4' doesn't seem to be linked. Make sure: \n\n` + - Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + - '- You rebuilt the app after installing the package\n' + - '- You are not using Expo Go\n'; - -// @ts-expect-error -const isTurboModuleEnabled = global.__turboModuleProxy != null; - -const Lz4Module = isTurboModuleEnabled - ? require('./NativeLz4').default - : NativeModules.Lz4; - -const Lz4 = Lz4Module - ? Lz4Module - : new Proxy( - {}, - { - get() { - throw new Error(LINKING_ERROR); - }, - } - ); - -export const _global = global as unknown as Lz4Type; +// @TODO: it might not be needed to initialize the module in the index file +// at all as it is being initialized in the Android and iOS layers. Could be removed in the future. /** * Initializes the Lz4 module. diff --git a/src/module.ts b/src/module.ts new file mode 100644 index 0000000..5de8652 --- /dev/null +++ b/src/module.ts @@ -0,0 +1,28 @@ +import { NativeModules, Platform } from 'react-native'; +import type { Lz4Type } from './types'; + +const LINKING_ERROR = + `The package 'react-native-lz4' doesn't seem to be linked. Make sure: \n\n` + + Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + + '- You rebuilt the app after installing the package\n' + + '- You are not using Expo Go\n'; + +// @ts-expect-error +const isTurboModuleEnabled = global.__turboModuleProxy != null; + +const Lz4Module = isTurboModuleEnabled + ? require('./NativeLz4').default + : NativeModules.Lz4; + +export const Lz4 = Lz4Module + ? Lz4Module + : new Proxy( + {}, + { + get() { + throw new Error(LINKING_ERROR); + }, + } + ); + +export const _global = global as unknown as Lz4Type;