Skip to content

Feature Request: Sugar for running simple workers and actions #75

@digitalbuddha

Description

@digitalbuddha

Hi folks, I wrote an extension function run which tries to reduce some of the boilerplate associated with running workers. I find myself writing a bit more boilerplate that the minimum when creating a worker that changes state. I'd like the ability to pass a lambda to runningWorker that is an Action.Updater while still receiving the result of my worker.

As an example:

context.runningWorker(
  Worker.from {calendarStore.get(Unit)}) { action { State.ShowingCalendars(it) } 
}

Ideally I wanted to collapse the following to something that asks for a suspended function to execute and a state to show with the response

//does not compile
context.run({calendarStore.get(Unit)}, { State.ShowingCalendars(it) })

I got pretty close but cannot get a working solution that allows me to pass an updater to my run function without having to also pass an action:

context.run({calendarStore.get(Unit)},{ action { State.ShowingCalendars(it) } })

From what I gather (apologies this is all new to me) the issue is with action being unable to pass a param to updater the only reason the above example compiles is that it is being captured from the "handler" without ever getting passed between the action and the action.updater.
For context here is my current solution

inline fun <reified T,StateT, reified OutputT : Any> RenderContext<StateT, OutputT>.run(
        noinline block: suspend () -> T,
        noinline handler: (T) -> WorkflowAction<StateT, OutputT>
    ) {
        runningWorker(Worker.from { block.invoke() }, handler =  handler)
    }

I'm coming from mvrx which has a similar api as I describe above where a single execute function both acts as a mutator and an action. It would be helpful to have the same here.

//mvrx psuedocode
observable.execute{ result:Result
  return NewState(this as State++)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions