package com.brickmodule import com.facebook.react.bridge.ReactContext /** * Minimal interface that all Brick modules must implement Contains only essential properties for * module identification */ interface BrickModuleBase { /** The name of the module (required for registration) */ val moduleName: String /** * Returns the constants exposed by this module * Override this method to provide module-specific constants * @return Map of constant name to value */ fun getConstants(): Map = emptyMap() /** * Emits an event to JavaScript using React Native's auto-generated event emitters * This method should be implemented by the bridge layer to call the appropriate * auto-generated emitModuleName_onEventName method from React Native Codegen * @param eventName The name of the event (e.g., "onCalculationCompleted") * @param payload The event data as a Map */ fun emitEvent(eventName: String, payload: Map) } /** * Abstract base class for Brick modules that provides internal implementation details Module * implementations should extend this class instead of implementing BrickModuleBase directly */ abstract class BrickModuleSpec(private val reactContext: ReactContext) : BrickModuleBase { /** * Get the ReactContext instance * @return ReactContext for this module */ protected fun getReactContext(): ReactContext = reactContext /** * Event emitter callback set by the bridge layer * This allows the bridge to provide access to auto-generated event emitter methods */ var eventEmitterCallback: ((String, Map) -> Unit)? = null /** * Emits an event to JavaScript using React Native's auto-generated event emitters * Delegates to the bridge layer which has access to the generated emitModuleName_onEventName methods */ override fun emitEvent(eventName: String, payload: Map) { eventEmitterCallback?.invoke(eventName, payload) ?: throw IllegalStateException( "Event emitter callback not set for module $moduleName. " + "Events cannot be emitted until the module is registered with the bridge." ) } } // MARK: - BrickError Interface /** * Error interface for Brick modules. * Implement this interface to propagate all error properties to JavaScript. * Follows the same pattern as iOS BrickError protocol. * * Note: message is inherited from Throwable, so it's not defined in this interface. * Classes implementing BrickError must extend Exception/Throwable. */ interface BrickError { /** Error code (accessed as error.name in JS) */ val code: String /** Additional properties (accessed as error.userInfo.xxx in JS) */ val userInfo: Map? get() = null } /** * Default implementation of BrickError. * Extend this class to create custom errors. */ open class BrickException( override val code: String, message: String, override val userInfo: Map? = null, cause: Throwable? = null ) : Exception(message, cause), BrickError /** Error types for Brick modules - implements BrickError interface */ open class BrickModuleError( message: String, override val code: String ) : Exception(message), BrickError { override val userInfo: Map? get() = null class TypeMismatch(message: String) : BrickModuleError("Type mismatch: $message", "TYPE_ERROR") class ExecutionError(message: String) : BrickModuleError("Execution error: $message", "EXECUTION_ERROR") class InvalidDefinition(message: String) : BrickModuleError("Invalid definition: $message", "DEFINITION_ERROR") class MethodNotFound(message: String) : BrickModuleError("Method not found: $message", "METHOD_NOT_FOUND") class ModuleNotFound(message: String) : BrickModuleError("Module not found: $message", "MODULE_NOT_FOUND") }