This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.0
README

State

This library provides a different approach to handling of app state. According to this idea the application state changing in time by stream of state-mutating functions (updates).

import Html
import Time
import State

main = State.start state view updates

state = 0

view address state =
  Html.text <| toString state

updates =
  Signal.map (always <| (+) 1)
  <| Time.every Time.second

Types

type alias Update state = state -> state

State update (function from the old state to the new one).

type alias State = Int

updates : Signal (Update State)
updates = Signal.constant ((*) 2)
type alias View state view = Address (Update state) -> state -> view

View. Receives an address for the updates, and the current state to display.

type alias State = { value : Int }

view : View State Html
view address state =
  button
    [ onClick address (\s -> { s | value = s.value * 2 }) ]
    [ text <| toString state ]

Common helpers

start --| initial state : state --| displaying function -> View state a --| stream of updates -> Signal (Update state) --| resulting signal -> Signal a

Starts the application. Receives an initial state, the view function and the stream of updates.

forward --| address to send updates to : Address (Update state) --| current state -> state --| focus to sub-state -> (Focus state subState) --| sub-view -> View subState view --| result of sub-view call -> view

Calls the sub-view function to display sub-state, extracting it from the "root" state using the Focus. Sub-view can send updates only for sub-state (inner components are isolated from the container).

Infix operators

module State
  ( View, Update
  , start, forward
  , (^.), (%~)
  ) where

{-| This library provides a different approach to handling of app state.
According to this idea the application state changing in time
by stream of state-mutating functions (updates).

    import Html
    import Time
    import State

    main = State.start state view updates

    state = 0

    view address state =
      Html.text <| toString state

    updates =
      Signal.map (always <| (+) 1)
      <| Time.every Time.second

# Types
@docs Update, View

# Common helpers
@docs start, forward

# Infix operators
@docs (^.), (%~)
-}

import Signal exposing (Signal, Address)

import Focus exposing (Focus)
import Signal.Extra as Signal

{-| State update (function from the old state to the new one).

    type alias State = Int

    updates : Signal (Update State)
    updates = Signal.constant ((*) 2)
-}
type alias Update state = state -> state

{-| View. Receives an address for the updates, and the current
state to display.

    type alias State = { value : Int }

    view : View State Html
    view address state =
      button
        [ onClick address (\s -> { s | value = s.value * 2 }) ]
        [ text <| toString state ]
-}
type alias View state view =
  Address (Update state) -> state -> view

{-| Starts the application. Receives an initial state, the view function
and the stream of updates.
-}
start
  --| initial state
  : state
  --| displaying function
  -> View state a
  --| stream of updates
  -> Signal (Update state)
  --| resulting signal
  -> Signal a
start state view updates =
  let
    mailbox = Signal.mailbox identity
  in
    Signal.map (view mailbox.address)
    <| Signal.foldp (<|) state
    <| Signal.fairMerge (<<) updates mailbox.signal

{-| Calls the sub-view function to display sub-state, extracting it
from the "root" state using the Focus. Sub-view can send updates
only for sub-state (inner components are isolated from the container).
-}
forward
  --| address to send updates to
  : Address (Update state)
  --| current state
  -> state
  --| focus to sub-state
  -> (Focus state subState)
  --| sub-view
  -> View subState view
  --| result of sub-view call
  -> view
forward address state focus subView =
  let
    visit = Focus.update focus
  in
    subView (Signal.forwardTo address visit) <| state ^. focus

{-| Infix operator for constructing of updater.

    type alias Counter = { value_ : Int }

    value : Focus Counter Int
    value = ...

    increment : Counter -> Counter
    increment = value %~ (+) 1
-}
(%~)
  : Focus state subState
  -> (subState -> subState)
  -> state -> state
(%~) = Focus.update

{-| Sub-state extracting infix operator.

    type alias User = { name_ : String }

    name : Focus User String
    name = ...

    -- somewhere in view
      Html.text <| user ^. name
-}
(^.)
  : state
  -> Focus state subState
  -> subState
(^.) = flip Focus.get