SmartAccountEventEmitter

Event emitter for Smart Account lifecycle events.

This class manages event subscriptions and dispatches events to all registered listeners. It provides thread-safe subscription management and error handling.

All synchronization uses platformSynchronized on the internal listeners map as the single lock. Event emission snapshots the listener list under the lock, then invokes listeners outside the lock to avoid holding the lock during callbacks.

Features:

  • Thread-safe listener management with platformSynchronized

  • Multiple listeners per event type

  • Error isolation (one failing listener does not affect others)

  • Optional error handler for debugging listener failures

  • Public addListener() for Java/Swift/non-Kotlin callers

Example:

val emitter = SmartAccountEventEmitter()

// Type-safe subscription (Kotlin only, uses reified generics)
val unsubscribe = emitter.on<SmartAccountEvent.WalletConnected> { event ->
println("Connected to ${event.contractId}")
}

// Non-generic subscription (works from Java, Swift, etc.)
val unsub = emitter.addListener { event ->
when (event) {
is SmartAccountEvent.WalletConnected ->
println("Connected to ${event.contractId}")
else -> {}
}
}

// One-time listener
emitter.once<SmartAccountEvent.TransactionSubmitted> { event ->
println("First transaction: ${event.hash}")
}

// Remove listener
unsubscribe()

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard

Subscribes a listener to receive all Smart Account events.

Link copied to clipboard
fun listenerCount(eventType: String): Int

Returns the number of listeners for a specific event type.

Link copied to clipboard
inline fun <T : SmartAccountEvent> on(crossinline listener: (T) -> Unit): () -> Unit

Subscribes to events of a specific type.

Link copied to clipboard
inline fun <T : SmartAccountEvent> once(crossinline listener: (T) -> Unit): () -> Unit

Subscribes to an event, but only triggers once.

Link copied to clipboard
fun removeAllListeners(eventType: String? = null)

Removes all listeners for a specific event type, or all listeners if no type is specified.

Link copied to clipboard
fun setErrorHandler(handler: (event: SmartAccountEvent, error: Throwable) -> Unit?)

Sets the error handler for listener errors.