Skip to content

Commit

Permalink
refactor(ts): splitting up the module functions to a cleaner public api
Browse files Browse the repository at this point in the history
  • Loading branch information
mateoguzmana committed Oct 2, 2024
1 parent eacf1d0 commit 3dae8e9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
decompressFile,
getLz4VersionNumber,
getLz4VersionString,
_global,
type FileOperationResult,
} from '../index';
import { _global } from '../module';

jest.mock('react-native', () => ({
Platform: {
Expand Down
31 changes: 4 additions & 27 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
28 changes: 28 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 3dae8e9

Please sign in to comment.