Deprecated compatibility wrapper that provides backward compatibility for applications migrating from legacy Unimodules infrastructure to Expo Modules API
—
Core event handling system for native module events with automatic listener lifecycle management and cross-platform compatibility.
Provides event emitter functionality for native modules with automatic listener management and native module lifecycle handling.
/**
* Event emitter for native module events with automatic listener management
*/
class EventEmitter {
/**
* Create an event emitter for a native module
* @param nativeModule - The native module to create events for
*/
constructor(nativeModule: NativeModule);
/**
* Add a listener for the specified event
* @param eventName - Name of the event to listen for
* @param listener - Function to call when event is emitted
* @returns Subscription object with remove method
*/
addListener<T>(eventName: string, listener: (event: T) => void): Subscription;
/**
* Remove all listeners for the specified event
* @param eventName - Name of the event to remove listeners for
*/
removeAllListeners(eventName: string): void;
/**
* Remove a specific subscription
* @param subscription - The subscription to remove
*/
removeSubscription(subscription: Subscription): void;
/**
* Emit an event to all listeners
* @param eventName - Name of the event to emit
* @param params - Parameters to pass to listeners
*/
emit(eventName: string, ...params: any[]): void;
}Usage Examples:
import { EventEmitter } from "@unimodules/core";
// Create event emitter for a native module
const myModule = NativeModulesProxy.MyModule;
const eventEmitter = new EventEmitter(myModule);
// Add event listener
const subscription = eventEmitter.addListener('dataReceived', (data) => {
console.log('Received data:', data);
});
// Add typed event listener
interface LocationData { latitude: number; longitude: number; }
const locationSubscription = eventEmitter.addListener<LocationData>('locationUpdate', (location) => {
console.log(`Location: ${location.latitude}, ${location.longitude}`);
});
// Remove specific listener
subscription.remove();
// Remove all listeners for an event
eventEmitter.removeAllListeners('dataReceived');
// Emit custom events
eventEmitter.emit('customEvent', { message: 'Hello world' });Represents an active event listener subscription with cleanup functionality.
/**
* Represents an active event listener subscription
*/
interface Subscription {
/**
* Remove this subscription and stop receiving events
*/
remove: () => void;
}React Native's device event emitter, re-exported for convenience.
/**
* React Native's device event emitter for system-level events
*/
const DeviceEventEmitter: DeviceEventEmitterStatic;Usage Examples:
import { DeviceEventEmitter } from "@unimodules/core";
// Listen to system events
const keyboardSubscription = DeviceEventEmitter.addListener('keyboardDidShow', (event) => {
console.log('Keyboard height:', event.endCoordinates.height);
});
// Clean up
keyboardSubscription.remove();For backward compatibility with older code.
/**
* @deprecated Use DeviceEventEmitter instead
*/
const RCTDeviceEventEmitter: DeviceEventEmitterStatic;The EventEmitter automatically manages native module observation lifecycle:
import { EventEmitter } from "@unimodules/core";
const sensorModule = NativeModulesProxy.Accelerometer;
const eventEmitter = new EventEmitter(sensorModule);
// First listener automatically calls startObserving() on Android
const subscription1 = eventEmitter.addListener('accelerometerData', handleData);
// Additional listeners don't trigger additional startObserving calls
const subscription2 = eventEmitter.addListener('accelerometerData', handleOtherData);
// Removing last listener automatically calls stopObserving() on Android
subscription1.remove();
subscription2.remove(); // This triggers stopObserving()import { EventEmitter, CodedError } from "@unimodules/core";
const eventEmitter = new EventEmitter(someModule);
try {
const subscription = eventEmitter.addListener('eventName', (data) => {
if (data.error) {
throw new CodedError('EVENT_ERROR', `Event processing failed: ${data.error}`);
}
// Process data
});
} catch (error) {
if (error instanceof CodedError) {
console.error(`Error ${error.code}: ${error.message}`);
}
}interface NativeModule {
startObserving?: () => void;
stopObserving?: () => void;
addListener: (eventName: string) => void;
removeListeners: (count: number) => void;
}
type EventListener<T> = (event: T) => void;Install with Tessl CLI
npx tessl i tessl/npm-unimodules--core