Created
July 26, 2017 15:22
-
-
Save pTalanov/f8818b3cdb28f3e365fb8e0a69fdd7c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kotlin.script.dependencies.experimental | |
import kotlinx.coroutines.experimental.Unconfined | |
import kotlinx.coroutines.experimental.channels.ReceiveChannel | |
import kotlinx.coroutines.experimental.channels.produce | |
import kotlin.script.dependencies.Environment | |
import kotlin.script.dependencies.ScriptContents | |
interface PipelineDependenciesResolver : AsyncDependenciesResolver { | |
/** | |
* Starts resolution pipeline and returns output channel that client can read responses from. | |
* | |
* The resolver must take requests from the [input] channel and place matching responses in the output channel. | |
* | |
* Only the most recent request for a particular script (as identified by `script.file`) is guaranteed to get | |
* a matching response to allow for debouncing. | |
* | |
* A single request can yield multiple responses so as to allow the resolver to react to changes in the external environment. | |
* | |
* The client *must* close the given [input] channel when no more requests are to be expected to allow for the disposal of resources. | |
* | |
* The coroutine completes when the [input] channel is closed or when a fatal error occurs. | |
*/ | |
suspend fun startPipeline(input: ReceiveChannel<Request>): ReceiveChannel<Response> | |
data class Request( | |
/** | |
* The contents of the script. `script.file` is *not guaranteed* to be in sync with `script.text`. | |
*/ | |
val script: ScriptContents, | |
val environment: ScriptEnvironment | |
) | |
data class Response( | |
/** | |
* The request that caused this response. | |
*/ | |
val request: Request, | |
/** | |
* Contains ScriptDependencies and diagnostics | |
*/ | |
val result: ResolutionResult | |
) | |
suspend override fun resolveAsync(scriptContents: ScriptContents, environment: Environment): ResolutionResult { | |
val request = Request(scriptContents, environment) | |
val input = produce(Unconfined) { | |
send(request) | |
close() | |
} | |
val output = startPipeline(input) | |
val response = output.receive() | |
assert(response.request == request) | |
return response.result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Right.
Yes, that's what triggered my comment about
startPipeline
.Multiple calls wouldn't be a problem as long as the protocol is fully respected, termination semantics and all.