krisajenkins/elm-exts/20.0.0

NOTE: this package is not compatible with Elm 0.19.1

Exts.State

Extensions for managing State.

(subaction -> submodel -> ( submodel, Effects subaction ))
-> Focus model submodel
-> (subaction -> action)
-> subaction
-> model
-> ( model, Effects action )

Quite often in Elm we want to say:

  • This action wraps a sub-module's subaction.
  • Take that subaction, apply the sub-module's update function to get (submodel,subeffects)
  • Merge the submodel our model.
  • Merge the subeffects into our effects, using a wrapper.

That plumbing's pretty repetitive, and we can abstract it out if we create a Focus to package up the getting & setting.

For example, if you want to pass actions down the chain to a DatePicker:

-- Create a Focus. This may well be useful in other parts of your code.
datePickerFocus : Focus Model DatePicker.Model
datePickerFocus =
  Focus.create
    .datePicker
    (\f model -> { model | datePicker = f model.datePicker })


-- Handle the sub-update in one sweep.
update action model =
  case action of
    ...
    DatePickerAction subaction ->
      subUpdate
        DatePicker.update
        datePickerFocus
        DatePickerAction
        subaction
        model
    ...