From 70da21fc39fd97cad047e46f3c6d3d16416a57f4 Mon Sep 17 00:00:00 2001 From: Luke Westby Date: Tue, 26 Jul 2016 09:15:51 -0500 Subject: [PATCH] remove beginnerWorker, rename to program and programWithFlags, better readme example --- README.md | 68 ++++++++++++++++++++++++++++++++++++++++++------ elm-package.json | 2 +- src/Worker.elm | 40 +++++----------------------- 3 files changed, 68 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index aa6c8f7..06d36bb 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,24 @@ Start Elm apps without views ### Example + +**Main.elm** ```elm port module Main exposing (..) import Worker -import Time + +{-| We'll receive messages from the world in the form of strings here -} +port messagesIn : (String -> msg) -> Sub msg + +{-| This port will send our counter back out to the world -} +port modelOut : Model -> Cmd msg + type alias Model = Int + init : (Model, Cmd Msg) init = (0, Cmd.none) @@ -20,6 +29,7 @@ init = type Msg = Increment + | NoOp update : Msg -> Model -> Model @@ -28,18 +38,40 @@ update msg model = Increment -> model + 1 + NoOp -> + model + +{-| In this function we define `parse` in order to go from +the strings that the outside world sends us to the messages our +program knows about. We then pass `parse` to `messagesIn` to get +a subscription that can update our program from things that happen +in JavaScript-land +-} subscriptions : Model -> Sub Msg subscriptions _ = - Time.every (1 * Time.second) (\_ -> Increment) - - -port modelOut : Model -> Cmd msg - - + let + parse value = + case value of + "Increment" -> + Increment + + _ -> + NoOp + in + messagesIn parse + + +{-| The first argument to Worker.worker lets us wrap our update +function with additional Cmds to execute on every change. In this +case we want to send our model out to JS on every update so we +pass it our `modelOut` port. We are already receiving messages from +our `messagesIn` port via `subscriptions` so now we're fully connected +to the JavaScript side of the application! +-} main : Program Never main = - Worker.worker modelOut + Worker.program modelOut { init = init , update = update , subscriptions = subscriptions @@ -47,9 +79,29 @@ main = ``` +**app.js** ```javascript var app = Elm.Main.worker() app.ports.modelOut.subscribe(function (model) { document.getElementById('seconds').innerHTML = model.toString() }) ``` + + +**index.html** +```html + + + + + + +
+
+ +
+ + + + +``` diff --git a/elm-package.json b/elm-package.json index e1f5389..2b36feb 100644 --- a/elm-package.json +++ b/elm-package.json @@ -1,5 +1,5 @@ { - "version": "1.0.2", + "version": "2.0.0", "summary": "Start Elm applications without a view", "repository": "https://github.com/lukewestby/worker.git", "license": "MIT", diff --git a/src/Worker.elm b/src/Worker.elm index 1a0c109..3a4c3f5 100644 --- a/src/Worker.elm +++ b/src/Worker.elm @@ -1,8 +1,8 @@ -module Worker exposing (workerWithFlags, worker, beginnerWorker) +module Worker exposing (programWithFlags, program) {-| Start Elm applications without a view -@docs workerWithFlags, worker, beginnerWorker +@docs programWithFlags, program -} import VirtualDom @@ -61,14 +61,14 @@ In JavaScript token: '12345' }); -} -workerWithFlags : +programWithFlags : (model -> Cmd msg) -> { init : flags -> ( model, Cmd msg ) , update : msg -> model -> ( model, Cmd msg ) , subscriptions : model -> Sub msg } -> Program flags -workerWithFlags extraCmds { init, update, subscriptions } = +programWithFlags extraCmds { init, update, subscriptions } = VirtualDom.programWithFlags { init = wrapInit extraCmds init , update = wrapUpdate extraCmds update @@ -90,42 +90,16 @@ you want to include port calls on every Msg. , subscriptions = subscriptions } -} -worker : +program : (model -> Cmd msg) -> { init : ( model, Cmd msg ) , update : msg -> model -> ( model, Cmd msg ) , subscriptions : model -> Sub msg } -> Program Never -worker extraCmds { init, update, subscriptions } = - workerWithFlags extraCmds +program extraCmds { init, update, subscriptions } = + programWithFlags extraCmds { init = \_ -> init , update = update , subscriptions = subscriptions } - - -{-| Start a worker program with just a model and a simpler update function - - port modelOut : Model -> Cmd msg - - main : Program Never - main = - Worker.beginnerWorker modelOut - { model = model - , update = update - } - --} -beginnerWorker : - (model -> Cmd msg) - -> { model : model - , update : msg -> model -> model - } - -> Program Never -beginnerWorker extraCmds { model, update } = - worker extraCmds - { init = ( model, Cmd.none ) - , update = \msg model -> ( update msg model, Cmd.none ) - , subscriptions = \_ -> Sub.none - }