-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MemoryView implementation -- Renaming and Arrow
- TODO: missing arrow tests - TODO: Actor?
- Loading branch information
1 parent
4e49933
commit e9f081e
Showing
12 changed files
with
150 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...row/src/commonMain/kotlin/com/fraktalio/fmodel/application/EphemeralViewArrowExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.fraktalio.fmodel.application | ||
|
||
import arrow.core.Either | ||
import arrow.core.raise.catch | ||
import arrow.core.raise.either | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.fold | ||
|
||
|
||
suspend fun <S, E, Q, MV> MV.handleWithEffect(query: Q): Either<Error, S> | ||
where MV : ViewStateComputation<S, E>, MV : EphemeralViewRepository<E, Q> { | ||
|
||
fun Q.fetchEventsWithEffect(): Either<Error, Flow<E>> = | ||
either { | ||
catch({ | ||
fetchEvents() | ||
}) { | ||
raise(Error.FetchingEventsFailed(query, it)) | ||
} | ||
} | ||
|
||
suspend fun Flow<E>.computeStateWithEffect(): Either<Error, S> = | ||
either { | ||
catch({ | ||
fold(initialState) { s, e -> evolve(s, e) } | ||
}) { | ||
raise(Error.CalculatingNewViewStateFailed(this@computeStateWithEffect, it)) | ||
} | ||
} | ||
|
||
return either { | ||
query.fetchEventsWithEffect().bind() | ||
.computeStateWithEffect().bind() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...-vanilla/src/commonMain/kotlin/com/fraktalio/fmodel/application/EphemeralViewExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.fraktalio.fmodel.application | ||
|
||
import kotlinx.coroutines.flow.fold | ||
|
||
/** | ||
* Extension function - Handles the query of type [Q] | ||
* | ||
* @param query Query of type [Q] to be handled | ||
* @return State of type [S] | ||
* | ||
* @author Domenic Cassisi | ||
*/ | ||
suspend fun <S, E, Q, MV> MV.handle(query: Q): S where MV : ViewStateComputation<S, E>, MV : EphemeralViewRepository<E, Q> = | ||
query.fetchEvents().fold(initialState) { s, e -> evolve(s, e) } |
14 changes: 0 additions & 14 deletions
14
...ion-vanilla/src/commonMain/kotlin/com/fraktalio/fmodel/application/MemoryViewExtension.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
application/src/commonMain/kotlin/com/fraktalio/fmodel/application/EphemeralView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.fraktalio.fmodel.application | ||
|
||
import com.fraktalio.fmodel.domain.IView | ||
|
||
/** | ||
* EphemeralView is using/delegating a `view` / [ViewStateComputation]<[S], [E]> to handle events of type [E] without maintaining a state of projection(s). | ||
* | ||
* [EphemeralView] extends [ViewStateComputation] and [EphemeralViewRepository] interfaces, | ||
* clearly communicating that it is composed out of these two behaviours. | ||
* | ||
* @param S Ephemeral View state of type [S] | ||
* @param E Events of type [E] that are handled by this Ephemeral View | ||
* @param Q Query of type [Q] | ||
* | ||
* @author Domenic Cassisi | ||
*/ | ||
interface EphemeralView<S, E, Q> : ViewStateComputation<S, E>, EphemeralViewRepository<E, Q> | ||
|
||
/** | ||
* Ephemeral View constructor-like function. | ||
* | ||
* The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it natively requiring zero boilerplate code. | ||
* | ||
* @param S Ephemeral View state of type [S] | ||
* @param E Events of type [E] that are used internally to build/fold new state | ||
* @param Q Identifier of type [Q] | ||
* @property view A view component of type [IView]<[S], [E]> | ||
* @property ephemeralViewRepository Interface for fetching events for [Q] - dependencies by delegation | ||
* @return An object/instance of type [EphemeralView]<[S], [E], [Q]> | ||
* | ||
* @author Domenic Cassisi | ||
*/ | ||
fun <S, E, Q> EphemeralView( | ||
view: IView<S, E>, | ||
ephemeralViewRepository: EphemeralViewRepository<E, Q> | ||
): EphemeralView<S, E, Q> = | ||
object : EphemeralView<S, E, Q>, | ||
EphemeralViewRepository<E, Q> by ephemeralViewRepository, | ||
IView<S, E> by view {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
application/src/commonMain/kotlin/com/fraktalio/fmodel/application/MemoryView.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters