Skip to content
This repository has been archived by the owner on Mar 8, 2018. It is now read-only.

Commit

Permalink
remove beginnerWorker, rename to program and programWithFlags, better…
Browse files Browse the repository at this point in the history
… readme example
  • Loading branch information
Luke Westby committed Jul 26, 2016
1 parent 386c28d commit 70da21f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 42 deletions.
68 changes: 60 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,32 @@ 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)


type Msg
= Increment
| NoOp


update : Msg -> Model -> Model
Expand All @@ -28,28 +38,70 @@ 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
}
```


**app.js**
```javascript
var app = Elm.Main.worker()
app.ports.modelOut.subscribe(function (model) {
document.getElementById('seconds').innerHTML = model.toString()
})
```


**index.html**
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="count"></div>
<div>
<button id="incrementButton">+ 1</button>
</div>
<script src="elm-compiler-output.js"></script>
<script src="app.js"></script>
</body>
</html>
```
2 changes: 1 addition & 1 deletion elm-package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
40 changes: 7 additions & 33 deletions src/Worker.elm
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit 70da21f

Please sign in to comment.