CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-unimodules--core

Deprecated compatibility wrapper that provides backward compatibility for applications migrating from legacy Unimodules infrastructure to Expo Modules API

Pending
Overview
Eval results
Files

native-modules.mddocs/

Native Module Access

Proxy system providing dynamic access to native modules with automatic method binding, error handling, and cross-platform compatibility.

Capabilities

NativeModulesProxy Object

Central proxy object that provides access to all available native modules with automatic method binding and argument validation.

/**
 * Proxy object providing access to native modules
 * Each property corresponds to a registered native module
 */
const NativeModulesProxy: { [moduleName: string]: ProxyNativeModule };

/**
 * Shape of each native module proxy
 */
interface ProxyNativeModule {
  [propertyName: string]: any;
  addListener: (eventName: string) => void;
  removeListeners: (count: number) => void;
}

Usage Examples:

import { NativeModulesProxy } from "@unimodules/core";

// Access a native module
const CameraModule = NativeModulesProxy.ExpoCamera;

if (CameraModule) {
  // Call native methods (returns Promise)
  try {
    const result = await CameraModule.takePictureAsync({
      quality: 0.8,
      base64: false
    });
    console.log('Picture saved to:', result.uri);
  } catch (error) {
    console.error('Failed to take picture:', error);
  }
}

// Check if module is available
const LocationModule = NativeModulesProxy.ExpoLocation;
if (!LocationModule) {
  console.warn('Location module not available on this platform');
}

Module Method Invocation

All native module methods are automatically bound and return Promises with built-in error handling.

Usage Examples:

import { NativeModulesProxy } from "@unimodules/core";

// Native methods with different signatures
const FileSystemModule = NativeModulesProxy.ExpoFileSystem;

if (FileSystemModule) {
  // Method with no arguments
  const documentDirectory = await FileSystemModule.getDocumentDirectoryAsync();
  
  // Method with single argument
  const fileInfo = await FileSystemModule.getInfoAsync('/path/to/file.txt');
  
  // Method with multiple arguments
  await FileSystemModule.writeAsStringAsync(
    '/path/to/file.txt',
    'Hello world',
    { encoding: 'utf8' }
  );
  
  // Method with options object
  const downloadResult = await FileSystemModule.downloadAsync(
    'https://example.com/file.pdf',
    '/local/path/file.pdf',
    {
      headers: { 'Authorization': 'Bearer token' },
      progressCallback: (progress) => {
        console.log(`Downloaded ${progress.totalBytesWritten} bytes`);
      }
    }
  );
}

Error Handling

Native module methods automatically validate arguments and provide detailed error messages.

Usage Examples:

import { NativeModulesProxy, CodedError } from "@unimodules/core";

const SomeModule = NativeModulesProxy.SomeModule;

try {
  // This will throw an error if wrong number of arguments
  await SomeModule.methodExpectingTwoArgs('arg1'); // Missing second argument
} catch (error) {
  console.error(error.message); 
  // "Native method SomeModule.methodExpectingTwoArgs expects 2 arguments but received 1"
}

try {
  // Module-specific errors
  await SomeModule.someMethodThatMightFail();
} catch (error) {
  if (error instanceof CodedError) {
    console.error(`Module error ${error.code}: ${error.message}`);
  }
}

Module Constants Access

Access compile-time constants exported by native modules.

Usage Examples:

import { NativeModulesProxy } from "@unimodules/core";

const ConstantsModule = NativeModulesProxy.ExpoConstants;

if (ConstantsModule) {
  // Access module constants
  console.log('App version:', ConstantsModule.appVersion);
  console.log('Device model:', ConstantsModule.deviceModel);
  console.log('System version:', ConstantsModule.systemVersion);
  
  // Platform-specific constants
  if (Platform.OS === 'ios') {
    console.log('iOS bundle ID:', ConstantsModule.iosBundleIdentifier);
  } else if (Platform.OS === 'android') {
    console.log('Android package name:', ConstantsModule.androidPackageName);
  }
}

Event Listener Management

Native modules support event listeners through standardized methods.

Usage Examples:

import { NativeModulesProxy } from "@unimodules/core";

const SensorModule = NativeModulesProxy.AccelerometerModule;

if (SensorModule) {
  // Add event listener (handled by EventEmitter internally)
  SensorModule.addListener('accelerometerData');
  
  // Remove listeners (handled by EventEmitter internally)
  SensorModule.removeListeners(1);
}

Advanced Usage

Dynamic Module Discovery

import { NativeModulesProxy } from "@unimodules/core";

// Get all available module names
const availableModules = Object.keys(NativeModulesProxy);
console.log('Available native modules:', availableModules);

// Check for specific module
function hasModule(moduleName: string): boolean {
  return moduleName in NativeModulesProxy && NativeModulesProxy[moduleName] != null;
}

if (hasModule('ExpoCamera')) {
  console.log('Camera module is available');
}

Conditional Native Calls

import { NativeModulesProxy, Platform } from "@unimodules/core";

// Platform-specific module access
const module = NativeModulesProxy.SomePlatformSpecificModule;

if (module && Platform.OS === 'ios') {
  await module.iosSpecificMethod();
} else if (module && Platform.OS === 'android') {
  await module.androidSpecificMethod();
} else {
  console.warn('Platform-specific functionality not available');
}

Module Method Introspection

import { NativeModulesProxy } from "@unimodules/core";

const module = NativeModulesProxy.SomeModule;

if (module) {
  // Check if specific method exists
  if (typeof module.someMethod === 'function') {
    await module.someMethod();
  } else {
    console.warn('Method someMethod not available in this version');
  }
}

Types

type NativeModuleMethod = (...args: any[]) => Promise<any>;

interface ModuleMethodInfo {
  name: string;
  key: string;
  argumentsCount: number;
}

interface NativeModuleDefinition {
  [exportedMethodsKey]: { [moduleName: string]: ModuleMethodInfo[] };
  [modulesConstantsKey]: { [moduleName: string]: any };
}

Install with Tessl CLI

npx tessl i tessl/npm-unimodules--core

docs

error-handling.md

event-management.md

index.md

native-modules.md

native-views.md

permissions.md

platform-utilities.md

utilities.md

tile.json