Leland Richardson [G]
08/03/2019, 12:02 AM// normal API to create a store based on readme example
val mainStore = Store(
reducer = ::counterReducer,
state = null
)
// effect-based API to select/subscribe to store in composition
fun <TSlice, TState> Store<TState>.select(selector: (TState) -> TSlice) = effectOf<TSlice> {
val result = +state { selector(state) }
+onCommit(selector) {
val observer = StoreSubscriber<TSlice> { result.value = it }
subscribe(observer, selector)
onDispose {
unsubscribe(observer)
}
}
result.value
}
// usage
@Composable fun SomeView() {
val count = +mainStore.select { it.counter }
Text("Count: $count")
}
moetouban
08/03/2019, 4:45 AMPatrick Jackson
08/05/2019, 4:40 PMAntanas A.
08/06/2019, 8:39 AM