commonMain.co.touchlab.skie.compilerinject.interceptor.ErasedPhaseInterceptorChain.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-linker-plugin-kgp_1.9.20 Show documentation
Show all versions of kotlin-compiler-linker-plugin-kgp_1.9.20 Show documentation
Kotlin compiler plugin that improves Swift interface of a Kotlin Multiplatform framework.
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)
}
}
}