Files
libp2p-native-bridge/utils/eventHelpers.ts
Chris Daßler 6f1d6ec37b Initial commit: libp2p-native-bridge package
- Extracted libp2p component from main app
- Created modular package structure with interfaces and implementations
- Added dependency injection for NativeModules
- Configured for IOR loading from Gitea
- Added comprehensive README and documentation
2025-08-29 11:18:37 +02:00

104 lines
2.0 KiB
TypeScript

/**
* Helper functions for standardized event emission across platforms
* Import this file in native modules to ensure consistent event structures
*/
import type {
ConnectionStatusEvent,
ErrorEvent,
LogEvent,
PeerDiscoveredEvent,
PeerInfoEvent,
PeerLostEvent,
} from './types';
/**
* Creates a standardized connection status event payload
*/
export function createConnectionStatusEvent(
peerId: string,
status: string,
direction?: string,
multiaddr?: string,
error?: string,
reason?: string,
): ConnectionStatusEvent {
return {
peerId,
status: status as ConnectionStatus,
direction: direction as ConnectionDirection,
multiaddr,
error,
reason,
};
}
/**
* Creates a standardized peer discovered event payload
*/
export function createPeerDiscoveredEvent(
peerId: string,
addresses: string[],
): PeerDiscoveredEvent {
return {
peerId,
addresses,
multiaddrs: addresses, // Use same array for both for compatibility
};
}
/**
* Creates a standardized peer lost event payload
*/
export function createPeerLostEvent(peerId: string): PeerLostEvent {
return {
peerId,
};
}
/**
* Creates a standardized log event payload
*/
export function createLogEvent(
message: string,
level: 'debug' | 'info' | 'warn' | 'error' = 'info',
): LogEvent {
return {
message,
level,
};
}
/**
* Creates a standardized error event payload
*/
export function createErrorEvent(message: string, code?: string): ErrorEvent {
return {
message,
code,
};
}
/**
* Creates a standardized peer info event payload
*/
export function createPeerInfoEvent(peerId: string, multiaddrs: string[]): PeerInfoEvent {
return {
peerId,
multiaddrs,
};
}
/**
* Event names used across platforms
*/
export const EVENT_NAMES = {
PEER_INFO: 'onPeerInfo',
PEER_DISCOVERED: 'onPeerDiscovered',
PEER_LOST: 'onPeerLost',
CONNECTION_STATUS: 'onConnectionStatus',
INCOMING_CONNECTION: 'onIncomingConnection',
LOG: 'onLog',
ERROR: 'onError',
} as const;