Module

Control.Coroutine.Aff

Package
purescript-aff-coroutines
Repository
purescript-contrib/purescript-aff-coroutines

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 Source

newtype Emitter m a r

Constructors

Instances

#Step Source

data Step a b

Constructors

#emit Source

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

#close Source

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

#produce Source

produce :: forall r a. (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' Source

produce' :: forall m r a. 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 Source

produceAff :: forall r a. (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"