chan: Some extra kit for Chans

[ bsd3, library, web ] [ Propose Tags ]

[Skip to Readme]
Versions 0.0.0, 0.0.1, 0.0.2, 0.0.3
Dependencies async, base (>=4.7 && <5), stm [details]
License BSD-3-Clause
Copyright 2017 Athan Clark
Author Athan Clark
Category Web
Home page
Source repo head: git clone
Uploaded by athanclark at Sun Jan 28 19:02:05 UTC 2018
Distributions NixOS:0.0.3, Stackage:0.0.3
Downloads 560 total (16 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-01-28 [all 1 reports]
Hackage Matrix CI




Maintainer's Corner

For package maintainers and hackage trustees

Readme for chan-0.0.3

[back to package description]


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.