Hey all. I have a problem I’m stumped by. I have ...
# arrow
e
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 {
Log output:
Copy code
2023-03-22T21:07:07.106-04:00 DEBUG 38879 --- [-5 @coroutine#7] i.l.common.workflow.WorkflowDispatcher   : BEFORE EFFECT
2023-03-22T21:07:07.107-04:00 DEBUG 38879 --- [-5 @coroutine#7] i.l.common.workflow.WorkflowDispatcher   : START EFFECT
...
2023-03-22T21:07:07.175-04:00 DEBUG 38879 --- [-3 @coroutine#7] i.l.common.workflow.WorkflowDispatcher   : START EFFECT
y
Where is the code that consumes the effect though? I think you might be invoking the effect itself twice
e
Oh interesting. I’ll investigate that. Thx for the insight
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
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.
e
Thx for the assist!