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

com.swmansion.starknet.extensions.Request.kt Maven / Gradle / Ivy

The newest version!
package com.swmansion.starknet.extensions

import com.swmansion.starknet.provider.Request
import java.util.concurrent.CompletableFuture

internal fun  Request.compose(mapping: (value: T) -> Request): Request {
    return DependentRequest(this, mapping)
}

internal fun  Request.map(mapping: (value: T) -> N): Request {
    return MappedRequest(this, mapping)
}

internal class DependentRequest(
    private val dependsOn: Request,
    private val mapper: (D) -> Request,
) : Request {

    override fun send(): T {
        return mapper(dependsOn.send()).send()
    }

    override fun sendAsync(): CompletableFuture {
        return dependsOn.sendAsync()
            .thenApplyAsync(mapper)
            .thenComposeAsync(Request::sendAsync)
    }
}

internal class MappedRequest(
    private val originalRequest: Request,
    private val mapper: (D) -> T,
) : Request {
    override fun send(): T {
        return mapper(originalRequest.send())
    }

    override fun sendAsync(): CompletableFuture {
        return originalRequest.sendAsync()
            .thenApplyAsync(mapper)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy