-
Notifications
You must be signed in to change notification settings - Fork 196
/
store.kt
51 lines (46 loc) · 2.13 KB
/
store.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package org.mobilenativefoundation.store.store5.impl.extensions
import kotlinx.coroutines.flow.filterNot
import kotlinx.coroutines.flow.first
import org.mobilenativefoundation.store.store5.Bookkeeper
import org.mobilenativefoundation.store.store5.MutableStore
import org.mobilenativefoundation.store.store5.Store
import org.mobilenativefoundation.store.store5.StoreReadRequest
import org.mobilenativefoundation.store.store5.StoreReadResponse
import org.mobilenativefoundation.store.store5.Updater
import org.mobilenativefoundation.store.store5.impl.RealMutableStore
import org.mobilenativefoundation.store.store5.impl.RealStore
/**
* Helper factory that will return data for [key] if it is cached otherwise will return
* fresh/network data (updating your caches)
*/
suspend fun <Key : Any, Output : Any> Store<Key, Output>.get(key: Key) =
stream(StoreReadRequest.cached(key, refresh = false))
.filterNot { it is StoreReadResponse.Loading || it is StoreReadResponse.NoNewData }
.first()
.requireData()
/**
* Helper factory that will return fresh data for [key] while updating your caches
*
* Note: If the [Fetcher] does not return any data (i.e the returned
* [kotlinx.coroutines.Flow], when collected, is empty). Then store will fall back to local
* data **even** if you explicitly requested fresh data.
* See https://github.com/dropbox/Store/pull/194 for context
*/
suspend fun <Key : Any, Output : Any> Store<Key, Output>.fresh(key: Key) =
stream(StoreReadRequest.fresh(key))
.filterNot { it is StoreReadResponse.Loading || it is StoreReadResponse.NoNewData }
.first()
.requireData()
@Suppress("UNCHECKED_CAST")
fun <Key : Any, Network : Any, Output : Any, Local : Any, Response : Any> Store<Key, Output>.asMutableStore(
updater: Updater<Key, Output, Response>,
bookkeeper: Bookkeeper<Key>?
): MutableStore<Key, Output> {
val delegate = this as? RealStore<Key, Network, Output, Local>
?: throw Exception("MutableStore requires Store to be built using StoreBuilder")
return RealMutableStore(
delegate = delegate,
updater = updater,
bookkeeper = bookkeeper
)
}