package io.vertx.kotlin.core.internal

import io.vertx.core.AsyncResult
import io.vertx.core.Future
import io.vertx.core.Handler

open class KotlinAsyncHandler<K, J>(val handler: (AsyncResult<K>) -> Unit, val converter: (J) -> K) :
        Handler<AsyncResult<J>> {

    override fun handle(event: AsyncResult<J>) {
        if (event.succeeded()) {
            handler(Future.succeededFuture(converter(event.result())))
        } else {
            handler(Future.failedFuture(event.cause()))
        }
    }
}

open class KotlinHandler<K, J>(val handler: (K) -> Unit, val converter: (J) -> K) : Handler<J> {
    override fun handle(event: J) {
        handler(converter(event))
    }
}
