Skip to content

Instantly share code, notes, and snippets.

@fkorotkov
Last active May 12, 2020 23:41
Show Gist options
  • Save fkorotkov/6054a5e7a11df2939940094ed5904404 to your computer and use it in GitHub Desktop.
Save fkorotkov/6054a5e7a11df2939940094ed5904404 to your computer and use it in GitHub Desktop.
Code sample for a blog post
class BatchLoader<ID, T>(
/* ... */
) : Loader<ID, T> {
private val requests = Channel<LoadRequest<ID, T>>(Channel.UNLIMITED) // UNLIMITED allows to queue requests
override suspend fun loadByIds(ids: Set<ID>): Map<ID, T> {
val request = LoadRequest<ID, T>(ids) // create a request
requests.send(request) // queue the request
return request.result.await() // await for the request to be fulfilled
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment