Skip to content

Instantly share code, notes, and snippets.

@fredcy
Created December 10, 2015 16:06
Show Gist options
  • Save fredcy/4a23e182ca0113256648 to your computer and use it in GitHub Desktop.
Save fredcy/4a23e182ca0113256648 to your computer and use it in GitHub Desktop.
Example getting initial window.dimensions value in Elm app using StartApp
import Effects exposing (Effects)
import Html exposing (..)
import StartApp
import Task exposing (Task)
import Window
type alias Model = (Int, Int)
type Action = Update (Int, Int) | NoOp
app : StartApp.App Model
app =
StartApp.start
{ init = ((0,0), sendInitial)
, view = \addr model -> div [] [ toString model |> text ]
, update = update
, inputs = [firstResize, resizes]
}
update : Action -> Model -> (Model, Effects Action)
update action model =
case action of
Update dim -> (dim, Effects.none)
NoOp -> (model, Effects.none)
resizes : Signal Action
resizes =
Signal.map Update Window.dimensions
main : Signal Html
main =
app.html
port tasks : Signal (Task Effects.Never ())
port tasks =
app.tasks
-- All of the following is needed just to get the initial window dimensions.
appStartMailbox : Signal.Mailbox ()
appStartMailbox =
Signal.mailbox ()
{- Get the initial value of the `resizes` signal by sampling it on the signal of a
mailbox that we use just for this purpose.
-}
firstResize: Signal Action
firstResize = Signal.sampleOn appStartMailbox.signal resizes
{- An Effect that sends a message to a special mailbox just so we have a signal
that triggers the sampleOn above.
-}
sendInitial : Effects Action
sendInitial =
Signal.send appStartMailbox.address () -- Task a ()
|> Task.map (always NoOp)
|> Effects.task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment