xilnocas/step/4.0.0

NOTE: the latest version is 4.1.4

Renamed to nicklawls/step. That is where the repository lives now. It has not been published as a package under the new name.

Versions published as xilnocas/step still install under that name. 11 versions of 15 come from the new location (see their notes).

Rerouted (renamed, verified). The GitHub user or repository was renamed. The tag is still there, at the new name. The Lamdera package server serves a copy recovered from https://github.com/nicklawls/step.

What we checked: The repo moved to github.com/nicklawls/step. Rebuilt with git archive from commit 3a4a0cf7af19 of https://github.com/nicklawls/step (zip prefix xilnocas-step-3a4a0cf/, America/Los_Angeles times): the zip is byte-identical to the one package.elm-lang.org recorded, SHA-1 ac8ae4cb990e377a08b25dd7db0caf191ad6a77e.

The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.

Details · package.elm-lang.org's zip: https://github.com/xilnocas/step/zipball/4.0.0/

Step

elm-state-transition

Steps, and how to make them

type Step model msg a

Step

A Step model msg a describes one state transition of an application, and is inteneded to be what gets returned from an update function.

It's helpful to look at how a Step is (roughly) represented under the hood

type Step model msg a
    = To model (Cmd msg)
    | Exit a
    | Stay

We provide a constructor for each of these options, but we hide the internal representation to make sure you're combining steps in principled ways.

That being said, if you find something that makes you want the data structure fully exposed, let me know what the use case is!

to : model -> Step model msg a

Transition to a new state, without executing any commands

stay : Step model msg a

Keep the interaction in the state it was in.

NOTE: This will prevent any commands from being returned

Step.stay == (Step.stay |> Step.withCmd myHttpCall)

If you want to stay in the same state, but run some commands, use Step.to explicitly

Step.to MySameState |> Step.withCmd myHttpCall
exit : a -> Step model msg a

End the interaction by returning a value of type a

Executing commands

withCmd : Cmd msg -> Step model msg a -> Step model msg a

If we're stepping to a new state, add an command to fire off

This can be called on a step multiple times, and all the commands will fire.

No commands are fired if the Step turns out to be a stay or an exit

withAttempt : (Result x a -> msg) -> Task x a -> Step model msg b -> Step model msg b

A helper for building steps out of tasks

Composing Steps

All of these functions help you build functions that return steps out of other functions that return steps

map : (model1 -> model2) -> Step model1 msg a -> Step model2 msg a

Apply a function to the state inside a step, if it's there

Most useful in building a bigger step out of a sub-step you happen to have lying around

mapMsg : (msg1 -> msg2) -> Step model msg1 a -> Step model msg2 a

Apply a function to any msgs conteined in the step

Also used for building larger interaction steps out of smaller ones

mapExit : (a -> b) -> Step model msg a -> Step model msg b

Map over the output of an interaction, if we've reached the end

orElse : Step model msg a -> Step model msg a -> Step model msg a

Run the first suceeding step, with priority given to the second argument

Intended to be used pipeline style

Step.to { loading = True } |> Step.orElse (Step.to { loading = False }) == Step.to { loading = True }


Step.noop |> Step.orElse Step.to { loading = True } == Step.to { loading = True }
onExit : (a -> Step model msg b) -> Step model msg a -> Step model msg b

Choose a step based on the result of another interaction

You can use this in combination with map and mapMsg to glue the end of one interaction to the beginning of another.

Notice that it looks a lot like Maybe.andThen and Maybe.result, but operating on the last type variable.

Getting back to TEA land

run : Step model msg Never -> Maybe ( model, Cmd msg )

Turn a Step into the usual TEA update tuple

It must be a step in an interaction that continues forever. We know it is if the type variable o isn't a specifc type, and can thus be chosen to be Never

(msg -> model -> Step model msg Never)
-> msg
-> model
-> ( model, Cmd msg )

turn an update function that returns a Step to a normal Elm Architecture update function

uses run internally to default with the provided model in case of a stay

Misc

fromUpdate : ( model, Cmd msg ) -> Step model msg o

Build a step from a normal elm update tuple

fromMaybe : Maybe model -> Step model msg a

Step to the state denoted by the model in the Just case, and stay otherwise

(msg -> model -> Step model msg a)
-> ( model, Cmd msg )
-> List msg
-> Step model msg a

starting from an initial state, fold an update function over a list of messages