https://github.com/athanclark/elm-threading
A simple threading patch for elm ports
https://github.com/athanclark/elm-threading
elm threading
Last synced: 7 months ago
JSON representation
A simple threading patch for elm ports
- Host: GitHub
- URL: https://github.com/athanclark/elm-threading
- Owner: athanclark
- Created: 2016-06-18T08:07:29.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-06-25T03:13:14.000Z (almost 10 years ago)
- Last Synced: 2024-12-28T16:38:04.255Z (over 1 year ago)
- Topics: elm, threading
- Language: Elm
- Size: 3.91 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# elm-threading
A simple method for managing asynchronous ports that may be flooded
with invocations:
```elm
-- a simple hashing module
module Hash exposing (..)
import Threading
import Task
type alias Hashed = String
port makeHash : String -> Cmd a
port madeHash : (Hashed -> a) -> Sub a
type Msg a
= ThreadingMsg (Threading.Msg String Hashed Msg)
| HashThis String (Hashed -> Cmd a)
type alias Model a =
{ threading : Threading.Model Hashed (Msg a)
}
init : (Model a, Cmd (Msg a))
init = ({ threading = Threading.init }, Cmd.none )
update : Msg a -> Model a -> (Model a, Cmd a)
update action model =
case action of
ThreadingMsg a ->
let (newThreading, threadingCmd) =
Threading.update makeHash a model.threading
in ( { model | threading = newThreading }
, Cmd.map (\r -> case r of
Err a -> ThreadingMsg a
Ok a -> a) threadingCmd
)
HashThis toHash whenComplete ->
( model
, Task.perform Debug.crash ThreadingMsg
<| Task.succeed <| Threading.Call toHash whenComplete
)
subscriptions : Sub (Msg a)
subscriptions = Sub.map ThreadingMsg
<| Threading.subscriptions madeHash
```
Bam! Continuation-style passing for javascript ports. Note that
we leave `a` here - we still need the user of this module to pass
their "handler" for when the port finishes, but that's okay by
my book. If you have serious complaints, you should use sticks
and rocks instead of your computer.
> It may be easier to use
> [Shmookey's Cmd.Extra](http://package.elm-lang.org/packages/shmookey/cmd-extra/1.0.0/Cmd-Extra)
> instead of constantly doing `Task.perform Debug.crash identity << Task.succeed`
> all over teh place
```js
app.ports.makeHash.subscribe(function(threadedInput) {
var threadedOutput = {
threadId = threadedInput.threadId,
payload = hash(threadedInput.payload) // something async or w/e
};
app.ports.madeHash.send(threadedOutput);
});
```