Created
October 30, 2019 18:03
-
-
Save BenHenning/bf3e1aeb57bfb93a8497a52d8552e3f2 to your computer and use it in GitHub Desktop.
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
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