chan: Some extra kit for Chans

[ bsd3, concurrency, library ] [ Propose Tags ]
Versions [RSS] 0.0.0, 0.0.1, 0.0.2, 0.0.3, 0.0.4, 0.0.4.1
Dependencies async, base (>=4.7 && <5), stm [details]
License BSD-3-Clause
Copyright 2017 Athan Clark
Author Athan Clark
Maintainer athan.clark@gmail.com
Category Web
Home page https://github.com/athanclark/chan#readme
Source repo head: git clone https://github.com/athanclark/chan
Uploaded by athanclark at 2018-01-28T19:02:05Z
Distributions LTSHaskell:0.0.4.1, NixOS:0.0.4.1, Stackage:0.0.4.1
Reverse Dependencies 4 direct, 3 indirect [details]
Downloads 3856 total (31 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-01-28 [all 1 reports]

Readme for chan-0.0.3

[back to package description]

chan

This is just some extra Chan and TChan kit that might help the average user. It relies on spawning threads with async and either canceling (debouncing) or waiting (throttling) messages.

Unfortunately, the current design is untyped in the sense that the channel which you supply is the output channel, and the returned channel is the one you would write to. I'm not sure how this should be fixed.

An example might be the following:

import Control.Concurrent.Chan (readChan)
import Control.Concurrent.Chan.Extra (throttleStatic, intersperseStatic)



-- For example, some websockets:

data SomeMessage
  = Ping
  -- | ...

throttleLayer :: Chan SomeMessage -> IO (Chan SomeMessage)
throttleLayer output = do
  (x,_) <- throttleStatic output 1000000 -- nanoseconds, = 1 second
  pure x

pingLayer :: Chan SomeMessage -> IO (Chan SomeMessage)
pingLayer output = do
  (x,_,_) <- intersperseStatic output (pure Ping) 1000000
  pure x

performWebsocket :: Chan SomeMessage -> IO ()
performWebsocket output = do
  output' <- pingLayer =<< throttleLayer output
  _ <- async $ forever $ do
    msg <- readChan output'
    send msg -- something like that - it'll include Ping messages for us,
             -- and throttle the outgoing messages at the same time.