Last active
May 12, 2020 23:41
-
-
Save fkorotkov/6054a5e7a11df2939940094ed5904404 to your computer and use it in GitHub Desktop.
Code sample for a blog post
This file contains hidden or 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
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