This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.0
Simple method for managing asynchronous ports
version 2.0.0
license BSD3
native-modules False
elm-version 0.17.0 <= v < 0.18.0
Tag 2.0.0
Committed At 2016-06-25 03:12:50 UTC
sgraf812/elm-intdict 1.4.3 <= v < 2.0.0 1.4.3
elm-lang/core 4.0.0 <= v < 5.0.0 4.0.5

Modules

README

elm-threading

A simple method for managing asynchronous ports that may be flooded with invocations:

-- 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 instead of constantly doing Task.perform Debug.crash identity << Task.succeed all over teh place

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);
});