Created
October 20, 2015 21:06
-
-
Save seanhess/c56647d309b4cb57d2b6 to your computer and use it in GitHub Desktop.
Start-app working with initial value of inputs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Effects exposing (Effects, Never) | |
import Html exposing (Html) | |
import Signal exposing (Signal, Mailbox, Address) | |
import Signal.Extra exposing (foldp', combine, mapMany) | |
import Task | |
import StartApp exposing (Config, App) | |
import Debug | |
import History | |
import Html exposing (..) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (..) | |
main : Signal Html | |
main = Signal.map (testView address) states | |
singleton action = [ action ] | |
messages : Mailbox (List Action) | |
messages = | |
Signal.mailbox [] | |
address : Address Action | |
address = | |
Signal.forwardTo messages.address singleton | |
type Action = Hash String | Message String | Path String | |
type alias State = | |
{ hash : String | |
, path : String | |
, message : String | |
} | |
update : Action -> State -> State | |
update action state = | |
case action of | |
Hash hash -> | |
{ state | hash <- hash } | |
Path path -> | |
{ state | path <- path } | |
Message msg -> | |
{ state | message <- msg } | |
updateAll : List Action -> State -> State | |
updateAll actions state = | |
List.foldl update state actions | |
init : List Action -> State | |
init actions = | |
let d = Debug.log "INIT!" actions in | |
updateAll actions { hash = "", message = "", path = "" } | |
states : Signal State | |
states = foldp' updateAll init inputs | |
hashes : Signal Action | |
hashes = Signal.map Hash History.hash | |
paths : Signal Action | |
paths = Signal.map Path History.path | |
-- port messages : Mailbox Action | |
-- port messages = Signal.mailbox (Test "") | |
inputs : Signal (List Action) | |
inputs = | |
-- Signal.mergeMany (messages.signal :: List.map (Signal.map singleton) config.inputs) | |
mapMany List.concat [Signal.map singleton paths, Signal.map singleton hashes, messages.signal] | |
-- address = Signal.forwardTo messages.address singleton | |
testView : Address Action -> State -> Html | |
testView address state = | |
div [] | |
[ div [] [ text ("hello? " ++ state.message) ] | |
, div [] [ text ("State: " ++ (toString state)) ] | |
, div [] | |
[ a [ href "#link" ] [ text "Link" ] ] | |
, div [] | |
[ button [ onClick address (Message "new message") ] [ text "Change Message" ] ] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment