Skip to content

Commit 879bf9f

Browse files
authored
Document extension functions in store.kt (#669)
Signed-off-by: Blake Oliveira <[email protected]>
1 parent f9072fc commit 879bf9f

File tree

1 file changed

+90
-2
lines changed
  • store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions

1 file changed

+90
-2
lines changed

store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/extensions/store.kt

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ import org.mobilenativefoundation.store.store5.impl.RealStore
1515
/**
1616
* Helper factory that will return data for [key] if it is cached otherwise will return
1717
* fresh/network data (updating your caches)
18+
*
19+
* Note: Exceptions will not be handled within this function.
20+
*
21+
* ```
22+
* try {
23+
* store.get<Key, Common>(Key.Read.All)
24+
* } catch (e: Exception) {
25+
* // handle exception
26+
* }
27+
* ```
28+
*
29+
* @param Key The key to get cached data for.
30+
* @param Output The common representation of the data.
1831
*/
1932
suspend fun <Key : Any, Output : Any> Store<Key, Output>.get(key: Key) =
2033
stream(StoreReadRequest.cached(key, refresh = false))
@@ -25,17 +38,51 @@ suspend fun <Key : Any, Output : Any> Store<Key, Output>.get(key: Key) =
2538
/**
2639
* Helper factory that will return fresh data for [key] while updating your caches
2740
*
28-
* Note: If the [Fetcher] does not return any data (i.e the returned
29-
* [kotlinx.coroutines.Flow], when collected, is empty). Then store will fall back to local
41+
* If the [Fetcher] does not return any data (i.e the returned
42+
* [kotlinx.coroutines.flow.Flow], when collected, is empty). Then store will fall back to local
3043
* data **even** if you explicitly requested fresh data.
3144
* See https://github.com/dropbox/Store/pull/194 for context
45+
*
46+
* Note: Exceptions will not be handled within this function.
47+
*
48+
* ```
49+
* try {
50+
* store.fresh<Key, Common>(Key.Read.All)
51+
* } catch (e: Exception) {
52+
* // handle exception
53+
* }
54+
* ```
55+
*
56+
* @param Key The key to fetch fresh data for.
57+
* @param Output The common representation of the data.
58+
* @return The fresh data associated with the key.
3259
*/
3360
suspend fun <Key : Any, Output : Any> Store<Key, Output>.fresh(key: Key) =
3461
stream(StoreReadRequest.fresh(key))
3562
.filterNot { it is StoreReadResponse.Loading || it is StoreReadResponse.NoNewData }
3663
.first()
3764
.requireData()
3865

66+
/**
67+
* Extension function to convert a [Store] into a [MutableStore].
68+
*
69+
* This function allows a [Store] to be used as a [MutableStore] by providing an [Updater] and an
70+
* optional [Bookkeeper].
71+
*
72+
* ```
73+
* store.asMutableStore<Key, Dto, Common, Entity, WriteResponse>(updater, bookkeeper)
74+
* ```
75+
*
76+
* @param Key The type of the key used to get data.
77+
* @param Network The type of data returned by the fetcher
78+
* @param Output The common representation of the data.
79+
* @param Local The type of the data used by the source of truth.
80+
* @param Response The updater result write response type.
81+
* @param updater Posts data to remote data source.
82+
* @param bookkeeper Optionally used to track when local changes fail to sync with network.
83+
* @return A [MutableStore] instance.
84+
* @throws Exception if the [Store] is not built using [StoreBuilder].
85+
*/
3986
@OptIn(ExperimentalStoreApi::class)
4087
@Suppress("UNCHECKED_CAST")
4188
fun <Key : Any, Network : Any, Output : Any, Local : Any, Response : Any> Store<Key, Output>.asMutableStore(
@@ -53,13 +100,54 @@ fun <Key : Any, Network : Any, Output : Any, Local : Any, Response : Any> Store<
53100
)
54101
}
55102

103+
/**
104+
* Helper function that returns data for the given [key] if it is cached, otherwise it will return
105+
* fresh/network data (updating your caches).
106+
*
107+
* Note: Exceptions will not be handled within this function.
108+
*
109+
* ```
110+
* try {
111+
* store.get<Key, Common, WriteResponse>(Key.Read.All)
112+
* } catch (e: Exception) {
113+
* // handle exception
114+
* }
115+
* ```
116+
*
117+
* @param Key The key to get cached data for.
118+
* @param Output The common representation of the data.
119+
* @param Response The updater result write response type.
120+
* @return The data associated with the key.
121+
*/
56122
@OptIn(ExperimentalStoreApi::class)
57123
suspend fun <Key : Any, Output : Any, Response : Any> MutableStore<Key, Output>.get(key: Key) =
58124
stream<Response>(StoreReadRequest.cached(key, refresh = false))
59125
.filterNot { it is StoreReadResponse.Loading || it is StoreReadResponse.NoNewData }
60126
.first()
61127
.requireData()
62128

129+
/**
130+
* Helper function that returns fresh data for the given [key] while updating your caches.
131+
*
132+
* If the [Fetcher] does not return any data (i.e., the returned [kotlinx.coroutines.flow.Flow],
133+
* when collected, is empty), then the store will fall back to local data even if you explicitly
134+
* requested fresh data.
135+
*
136+
* Note: Exceptions will not be handled within this function.
137+
*
138+
* ```
139+
* try {
140+
* store.fresh<Key, Common, WriteResponse>(Key.Read.All)
141+
* } catch (e: Exception) {
142+
* // handle exception
143+
* }
144+
* ```
145+
*
146+
* @param Key The key to fetch fresh data for.
147+
* @param Output The common representation of the data.
148+
* @param Response The updater result write response type.
149+
* @return The fresh data associated with the key.
150+
*/
63151
@OptIn(ExperimentalStoreApi::class)
64152
suspend fun <Key : Any, Output : Any, Response : Any> MutableStore<Key, Output>.fresh(key: Key) =
65153
stream<Response>(StoreReadRequest.fresh(key))

0 commit comments

Comments
 (0)