Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Created May 14, 2015 19:41
Show Gist options
  • Save TheSeamau5/e1677a84eefd843362f4 to your computer and use it in GitHub Desktop.
Save TheSeamau5/e1677a84eefd843362f4 to your computer and use it in GitHub Desktop.
type alias Component state action view
= { initial : (state, action)
, view : Address action -> state -> view
, address : Address action
, history : Signal (List (state, action))
}
makeComponent
: state
-> action
-> (action -> state -> state)
-> (Address action -> state -> view)
-> Component state action view
makeComponent initialState initialAction update view =
let
componentMailbox =
mailbox initialAction
address =
componentMailbox.address
actions =
componentMailbox.signal
state = Signal.foldp update initialState actions
history =
Signal.foldp (\a b -> a :: b) []
(Signal.map2 (,) state actions)
in
{ initial = (initialState, initialAction)
, view = view
, address = address
, history = history
}
view : Component state action view -> Signal view
view component =
Signal.map (component.view component.address) (getCurrentState component)
getCurrentState : Component state action view -> Signal state
getCurrentState component =
let
getState initial history =
case history of
[] -> initial
x :: xs -> fst x
in
Signal.map (getState (fst component.initial)) component.history
send : action -> Component state action view -> Task error ()
send action component =
Signal.send component.address action
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment