extracting reducer scope creation
This commit is contained in:
parent
f0e5ae4502
commit
678897e8de
|
@ -31,29 +31,28 @@ fun <P : Any, S : Any> createPageReducer(
|
||||||
initialPage: SpiderPage<out P>,
|
initialPage: SpiderPage<out P>,
|
||||||
factory: PageReducerScope<P>.() -> ReducerFactory<S>,
|
factory: PageReducerScope<P>.() -> ReducerFactory<S>,
|
||||||
): ReducerFactory<Combined2<PageContainer<P>, S>> = shareState {
|
): ReducerFactory<Combined2<PageContainer<P>, S>> = shareState {
|
||||||
combineReducers(
|
combineReducers(createPageReducer(initialPage), factory(pageReducerScope()))
|
||||||
createPageReducer(initialPage),
|
}
|
||||||
factory(object : PageReducerScope<P> {
|
|
||||||
override fun <PC : Any> withPageContent(page: KClass<PC>, block: PageDispatchScope<PC>.() -> Unit) {
|
|
||||||
val currentPage = getSharedState().state1.page.state
|
|
||||||
if (currentPage::class == page) {
|
|
||||||
val pageDispatchScope = object : PageDispatchScope<PC> {
|
|
||||||
override fun ReducerScope<*>.pageDispatch(action: PageAction<PC>) {
|
|
||||||
val currentPageGuard = getSharedState().state1.page.state
|
|
||||||
if (currentPageGuard::class == page) {
|
|
||||||
dispatch(action)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getPageState() = getSharedState().state1.page.state as? PC
|
private fun <P : Any, S : Any> SharedStateScope<Combined2<PageContainer<P>, S>>.pageReducerScope() = object : PageReducerScope<P> {
|
||||||
|
override fun <PC : Any> withPageContent(page: KClass<PC>, block: PageDispatchScope<PC>.() -> Unit) {
|
||||||
|
val currentPage = getSharedState().state1.page.state
|
||||||
|
if (currentPage::class == page) {
|
||||||
|
val pageDispatchScope = object : PageDispatchScope<PC> {
|
||||||
|
override fun ReducerScope<*>.pageDispatch(action: PageAction<PC>) {
|
||||||
|
val currentPageGuard = getSharedState().state1.page.state
|
||||||
|
if (currentPageGuard::class == page) {
|
||||||
|
dispatch(action)
|
||||||
}
|
}
|
||||||
block(pageDispatchScope)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
override fun rawPage() = getSharedState().state1.page
|
override fun getPageState() = getSharedState().state1.page.state as? PC
|
||||||
})
|
}
|
||||||
)
|
block(pageDispatchScope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun rawPage() = getSharedState().state1.page
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
|
|
@ -83,17 +83,8 @@ fun <S> shareState(block: SharedStateScope<S>.() -> ReducerFactory<S>): ReducerF
|
||||||
fun <S1, S2> combineReducers(r1: ReducerFactory<S1>, r2: ReducerFactory<S2>): ReducerFactory<Combined2<S1, S2>> {
|
fun <S1, S2> combineReducers(r1: ReducerFactory<S1>, r2: ReducerFactory<S2>): ReducerFactory<Combined2<S1, S2>> {
|
||||||
return object : ReducerFactory<Combined2<S1, S2>> {
|
return object : ReducerFactory<Combined2<S1, S2>> {
|
||||||
override fun create(scope: ReducerScope<Combined2<S1, S2>>): Reducer<Combined2<S1, S2>> {
|
override fun create(scope: ReducerScope<Combined2<S1, S2>>): Reducer<Combined2<S1, S2>> {
|
||||||
val r1Scope = object : ReducerScope<S1> {
|
val r1Scope = createReducerScope(scope) { scope.getState().state1 }
|
||||||
override val coroutineScope: CoroutineScope = scope.coroutineScope
|
val r2Scope = createReducerScope(scope) { scope.getState().state2 }
|
||||||
override fun dispatch(action: Action) = scope.dispatch(action)
|
|
||||||
override fun getState() = scope.getState().state1
|
|
||||||
}
|
|
||||||
|
|
||||||
val r2Scope = object : ReducerScope<S2> {
|
|
||||||
override val coroutineScope: CoroutineScope = scope.coroutineScope
|
|
||||||
override fun dispatch(action: Action) = scope.dispatch(action)
|
|
||||||
override fun getState() = scope.getState().state2
|
|
||||||
}
|
|
||||||
|
|
||||||
val r1Reducer = r1.create(r1Scope)
|
val r1Reducer = r1.create(r1Scope)
|
||||||
val r2Reducer = r2.create(r2Scope)
|
val r2Reducer = r2.create(r2Scope)
|
||||||
|
@ -106,6 +97,12 @@ fun <S1, S2> combineReducers(r1: ReducerFactory<S1>, r2: ReducerFactory<S2>): Re
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun <S> createReducerScope(scope: ReducerScope<*>, state: () -> S) = object : ReducerScope<S> {
|
||||||
|
override val coroutineScope: CoroutineScope = scope.coroutineScope
|
||||||
|
override fun dispatch(action: Action) = scope.dispatch(action)
|
||||||
|
override fun getState() = state.invoke()
|
||||||
|
}
|
||||||
|
|
||||||
fun <S> createReducer(
|
fun <S> createReducer(
|
||||||
initialState: S,
|
initialState: S,
|
||||||
vararg reducers: (ReducerScope<S>) -> ActionHandler<S>,
|
vararg reducers: (ReducerScope<S>) -> ActionHandler<S>,
|
||||||
|
|
Loading…
Reference in New Issue