Hey all.
I have a problem I’m stumped by. I have a block of code that I invoke once, but it runs twice.
If anyone has some guidance/intuition/experience with a similar problem, I’d love the assist.
Code is here:
Copy code
suspend fun <E: Event> dispatch(command: Command): Effect<WorkflowError, E> {
log.debug("BEFORE EFFECT")
return effect {
log.debug("START EFFECT")
ensureNotNull(commandHandlers[command::class]) {
MissingHandler("No handler for command $command")
}.let {
it.invoke(command) as E
}
}
}
The signature of the
invoke
method, if this helps:
Copy code
context(EffectScope<WorkflowError>)
final override suspend fun invoke(request: R): E {
Where is the code that consumes the effect though? I think you might be invoking the effect itself twice
e
Erik Dreyer
03/23/2023, 1:47 PM
Oh interesting. I’ll investigate that. Thx for the insight
Erik Dreyer
03/23/2023, 2:13 PM
OK, so I refactored a return type from
Either
to
Effect
and a side effect (no pun intented) of that is that
fold
was being called twice. For
Either
this is fine, but for
Effect
it runs the process twice.
y
Youssef Shoaib [MOD]
03/23/2023, 2:15 PM
Oh yeah, that makes sense. Effect "basically" returns an Either, and so what happened is that you were recomputing that either twice, thus producing side effects.