Last active
December 7, 2018 17:48
-
-
Save anttih/94897e668914751391022d22654b9850 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Control.Coroutine.Aff.Utils where | |
import Prelude | |
import Control.Coroutine (Producer, consumer, runProcess, ($$)) | |
import Control.Coroutine.Aff (close, emit, produceAff) | |
import Control.Monad.Trans.Class (lift) | |
import Data.Maybe (Maybe(..)) | |
import Effect.Aff (Aff, forkAff, parallel, sequential) | |
import Effect.Aff.AVar as AV | |
import Effect.Exception (error) | |
mergeProducers :: forall o a. Producer o Aff a -> Producer o Aff a -> Producer o Aff Unit | |
mergeProducers l r = do | |
var <- lift AV.empty | |
let c = consumer \i -> AV.put i var *> pure Nothing | |
void $ lift $ forkAff do | |
void $ sequential $ parallel (runProcess (l $$ c)) *> parallel (runProcess (r $$ c)) | |
AV.kill (error "Both ended") var | |
produceAff \emitter -> | |
let | |
go = do | |
status <- AV.status var | |
if AV.isKilled status | |
then close emitter unit | |
else do | |
a <- AV.take var | |
emit emitter a | |
go | |
in go |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment