-
Notifications
You must be signed in to change notification settings - Fork 13
Actor extensions
diklimchuk edited this page Jan 26, 2021
·
1 revision
When mapping events it's recommened to use extentions from MappingActor.kt. This is required to handle command result logging.
Examples:
fun execute(command) = when (command) {
is CommandWithoutResult -> repository.doSomething()
.ignoreEvents()
is CommandWithImportantResult -> repository.doSomethingElse()
.mapEvents(Internal::OnCommandSuccess, Internal::OnCommandError)
is CommandWithImporantSuccess -> repository.doAnotherThing()
.mapSuccessEvent(Internal:OnAnotherCommandSuccess)
is CommandWithImportantError -> repository.doOneMoreThing()
.mapErrorEvent(Interrnal::OnOneMoreCommandError)
}
To implement debounce and switchMap inside Actor you can use Switcher
Capabilities:
- Usubscribe from previous command execution when the next one starts
private val switcher = Switcher()
fun execute(command: SomeCommand) = when (command) {
is Command -> switchOn(switcher) {
repository.action().ignoreEvents()
}
}
- Debounce + switchMap
private val switcher = Switcher()
fun execute(command: SomeCommand) = when (command) {
is Command -> switchOn(switcher, delayMillis = 300) {
repository.action().ignoreEvents()
}
}
- Execute only one command of two type at each moment in time and cancel all the other onces.
// Note: the same swithcer instance for both commands
private val switcher = Switcher()
fun execute(command: SomeCommand) = when (command) {
is FirstCommand -> switchOn(switcher) {
repository.action().ignoreEvents()
}
is SecondCommand -> switchOn(switcher) {
repository.action().ignoreEvents()
}
}
- switchMap for two different commands
// Note: different swithcer instances
private val firstCommandSwitcher = Switcher()
private val secondCommandSwitcher
fun execute(command: SomeCommand) = when (command) {
is FirstCommand -> switchOn(firstCommandSwitcher) {
repository.action().ignoreEvents()
}
is SecondCommand -> switchOn(secondCommandSwitcher) {
repository.action().ignoreEvents()
}
}