Module

Control.Coroutine.Aff

This module defines functions for creating coroutines on top of the Aff monad.

The Aff monad only supports actions which return a single value, asynchronously, so this module provides a principled way to deal with asynchronous streams of values, and asynchronous consumers of streamed data.

#Emitter

newtype Emitter m a r

Constructors

Instances

#Step

data Step a b

Constructors

#emit

emit :: forall m a r. Emitter m a r -> a -> m Unit

#close

close :: forall m a r. Emitter m a r -> r -> m Unit

#produce

produce :: forall a r. (Emitter Effect a r -> Effect Unit) -> Producer a Aff r

Create a Producer using an asynchronous callback.

The callback should provide zero or more values of type a, which will be emitted by the Producer, terminated by an optional value of type r. No values should be provided after a value of type r has been provided.

For example:

produce \emitter -> do
  log "Working..."
  emit emitter "progress"
  log "Done!"
  close emitter "finished"

#produce'

produce' :: forall a r m. MonadAff m => (Emitter Effect a r -> Effect Unit) -> Producer a m r

A version of produce that creates a Producer with an underlying MonadAff, rather than Aff specifically.

#produceAff

produceAff :: forall a r. (Emitter Aff a r -> Aff Unit) -> Producer a Aff r

A variant of produce where the setup and callback functions use the Aff monad. This can be helpful in certain cases.

For example:

produceAff \emitter -> do
  delay $ Milliseconds 1000
  emit emitter "progress"
  delay $ Milliseconds 1000
  close emitter "finished"

Modules