All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ch.softappeal.yass2.Interceptor.kt Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
package ch.softappeal.yass2

import kotlin.reflect.*

typealias Invocation = suspend () -> Any?

typealias Interceptor = suspend (functionName: String, parameters: Array, invocation: Invocation) -> Any?

interface ProxyFactory {
    fun  create(service: KClass, implementation: S, interceptor: Interceptor): S
}

val EmptyInterceptor: Interceptor = { _, _, invocation -> invocation() }

inline operator fun  ProxyFactory.invoke(implementation: S, noinline interceptor: Interceptor): S =
    if (interceptor === EmptyInterceptor) implementation else create(S::class, implementation, interceptor)

operator fun Interceptor.plus(second: Interceptor): Interceptor = when {
    this === EmptyInterceptor -> second
    second === EmptyInterceptor -> this
    else -> { functionName, parameters, invocation ->
        this(functionName, parameters) { second(functionName, parameters, invocation) }
    }
}