From 678897e8de97b9e8410262d2a20f606c4d453ae8 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Thu, 3 Nov 2022 08:04:24 +0000 Subject: [PATCH] extracting reducer scope creation --- .../app/dapk/st/core/page/PageReducer.kt | 37 +++++++++---------- .../src/main/kotlin/app/dapk/state/State.kt | 19 ++++------ 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/domains/android/compose-core/src/main/kotlin/app/dapk/st/core/page/PageReducer.kt b/domains/android/compose-core/src/main/kotlin/app/dapk/st/core/page/PageReducer.kt index aa43a46..c4d935d 100644 --- a/domains/android/compose-core/src/main/kotlin/app/dapk/st/core/page/PageReducer.kt +++ b/domains/android/compose-core/src/main/kotlin/app/dapk/st/core/page/PageReducer.kt @@ -31,29 +31,28 @@ fun

createPageReducer( initialPage: SpiderPage, factory: PageReducerScope

.() -> ReducerFactory, ): ReducerFactory, S>> = shareState { - combineReducers( - createPageReducer(initialPage), - factory(object : PageReducerScope

{ - override fun withPageContent(page: KClass, block: PageDispatchScope.() -> Unit) { - val currentPage = getSharedState().state1.page.state - if (currentPage::class == page) { - val pageDispatchScope = object : PageDispatchScope { - override fun ReducerScope<*>.pageDispatch(action: PageAction) { - val currentPageGuard = getSharedState().state1.page.state - if (currentPageGuard::class == page) { - dispatch(action) - } - } + combineReducers(createPageReducer(initialPage), factory(pageReducerScope())) +} - override fun getPageState() = getSharedState().state1.page.state as? PC +private fun

SharedStateScope, S>>.pageReducerScope() = object : PageReducerScope

{ + override fun withPageContent(page: KClass, block: PageDispatchScope.() -> Unit) { + val currentPage = getSharedState().state1.page.state + if (currentPage::class == page) { + val pageDispatchScope = object : PageDispatchScope { + override fun ReducerScope<*>.pageDispatch(action: PageAction) { + 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") diff --git a/domains/state/src/main/kotlin/app/dapk/state/State.kt b/domains/state/src/main/kotlin/app/dapk/state/State.kt index 3914aaf..252043e 100644 --- a/domains/state/src/main/kotlin/app/dapk/state/State.kt +++ b/domains/state/src/main/kotlin/app/dapk/state/State.kt @@ -83,17 +83,8 @@ fun shareState(block: SharedStateScope.() -> ReducerFactory): ReducerF fun combineReducers(r1: ReducerFactory, r2: ReducerFactory): ReducerFactory> { return object : ReducerFactory> { override fun create(scope: ReducerScope>): Reducer> { - val r1Scope = object : ReducerScope { - override val coroutineScope: CoroutineScope = scope.coroutineScope - override fun dispatch(action: Action) = scope.dispatch(action) - override fun getState() = scope.getState().state1 - } - - val r2Scope = object : ReducerScope { - override val coroutineScope: CoroutineScope = scope.coroutineScope - override fun dispatch(action: Action) = scope.dispatch(action) - override fun getState() = scope.getState().state2 - } + val r1Scope = createReducerScope(scope) { scope.getState().state1 } + val r2Scope = createReducerScope(scope) { scope.getState().state2 } val r1Reducer = r1.create(r1Scope) val r2Reducer = r2.create(r2Scope) @@ -106,6 +97,12 @@ fun combineReducers(r1: ReducerFactory, r2: ReducerFactory): Re } } +private fun createReducerScope(scope: ReducerScope<*>, state: () -> S) = object : ReducerScope { + override val coroutineScope: CoroutineScope = scope.coroutineScope + override fun dispatch(action: Action) = scope.dispatch(action) + override fun getState() = state.invoke() +} + fun createReducer( initialState: S, vararg reducers: (ReducerScope) -> ActionHandler,