Skip to content

Instantly share code, notes, and snippets.

@pTalanov
Created July 26, 2017 15:22
Show Gist options
  • Save pTalanov/f8818b3cdb28f3e365fb8e0a69fdd7c2 to your computer and use it in GitHub Desktop.
Save pTalanov/f8818b3cdb28f3e365fb8e0a69fdd7c2 to your computer and use it in GitHub Desktop.
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
}
}
@bamboo
Copy link

bamboo commented Jul 27, 2017

We have environment in case we need to share additional information specific to an implementation. Is it not enough?

Right.

Is this regarding resolveAsync implementation?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment