Skip to content

Instantly share code, notes, and snippets.

@BenHenning
Created October 30, 2019 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenHenning/bf3e1aeb57bfb93a8497a52d8552e3f2 to your computer and use it in GitHub Desktop.
Save BenHenning/bf3e1aeb57bfb93a8497a52d8552e3f2 to your computer and use it in GitHub Desktop.
fun <V> storeDataWithCustomChannelAsync(updateInMemoryCache: Boolean = true, update: (T) -> T, V): Deferred<V> {
return cache.updateIfPresentAsync { cachedPayload ->
// Although it's odd to notify before the change is made, the single threaded nature of the blocking cache ensures
// nothing can read from it until this update completes.
asyncDataSubscriptionManager.notifyChange(providerId)
val updatedPayload, channelValue = storeFileCache(cachedPayload, update)
if (updateInMemoryCache) (updatedPayload, channelValue) else (cachedPayload, channelValue)
}
}
fun <V> updateWithCustomChannelIfPresentAsync(update: suspend (T) -> T, V): Deferred<V> {
return blockingScope.async {
val updatedValue, channelValue = update(checkNotNull(value) { "Expected to update the cache only after it's been created" })
value = updatedValue
channelValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment