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

commonMain.co.touchlab.skie.compilerinject.interceptor.ErasedPhaseInterceptorChain.kt Maven / Gradle / Ivy

The newest version!
package co.touchlab.skie.compilerinject.interceptor

typealias OriginalPhaseBody = (Context, Input) -> Output
typealias ErasedPhaseInterceptor = (Context, Input, OriginalPhaseBody) -> Output

class ErasedPhaseInterceptorChain(
    interceptors: List>,
) : ErasedPhaseInterceptor {

    // We need to get rid of the `PhaseInterceptor` type as it's not available between different class loaders
    private val chainedInterceptors: ErasedPhaseInterceptor by lazy {
        val erasedInterceptors: Sequence> = interceptors.asSequence().map { it::intercept }
        erasedInterceptors.reduce { acc, next ->
            acc then next
        }
    }

    override fun invoke(context: Context, input: Input, original: OriginalPhaseBody): Output {
        return chainedInterceptors(context, input, original)
    }
}

infix fun  ErasedPhaseInterceptor.then(
    next: ErasedPhaseInterceptor,
): ErasedPhaseInterceptor {
    return { outerContext, outerInput, original ->
        this.invoke(outerContext, outerInput) { innerContext, innerInput ->
            next.invoke(innerContext, innerInput, original)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy