Is it a common pattern to use something like seale...
# store
a
Is it a common pattern to use something like sealed interfaces as keys? i want to be able to fetch all items using a key like Operation.GetAll which will then write all objects to the cache, but also be able to get a single object from the cache afterwards if i need to. Or is this not supported?
@Matthew Ramotar
m
Hey, sorry to be slow. With Store5, sealed interfaces as keys can definitely work. Just make sure to handle the relationship between collection operations and individual item operations in your fetcher, SOT, and memory cache
For example
Copy code
sealed interface Operation<out T : Any> {
    data class GetOne<T : Any>(val id: String) : Operation<T>
    data object GetAll<T : Any> : Operation<List<T>>
}
Copy code
val sourceOfTruth = SourceOfTruth.of<Operation<*>, Any, Any>(
    reader = { key -> 
        when (key) {
            is Operation.GetAll<*> -> database.getAllItemsFlow()
            is Operation.GetOne<*> -> database.getItemByIdFlow(key.id)
        }
    },
    writer = { key, value ->
        when (key) {
            is Operation.GetAll<*> -> {
                val items = value as List<Item>
                items.forEach { item ->
                    database.insertItem(item)
                }
                database.insertItems(items)

            }
            is Operation.GetOne<*> -> {
                val item = value as Item
                database.insertItem(item)
            }
        }
    }
)
Copy code
val fetcher = Fetcher.of { key ->
  when (key) {
    is Operation.GetAll<*> -> api.getAllItems()
    is Operation.GetOne<*> -> api.getItem(key.id)
  }
}
You could alternatively use a repository pattern with multiple specialized stores
👍 1