Module

Halogen.Query.EventSource

Package
purescript-halogen
Repository
purescript-halogen/purescript-halogen

#EventSource Source

newtype EventSource m a

An event source definition - an effect in m that when run returns a producer coroutine that emits actions of type a, and runs in the effect monad m.

It's generally unnecessary to build values of this type directly, the affEventSource, effectEventSource, and eventListenerEventSource cover most common event source constructions.

Constructors

Instances

#hoist Source

hoist :: forall n m. Functor n => (m ~> n) -> (EventSource m) ~> (EventSource n)

Changes the effect monad of an event source.

#affEventSource Source

affEventSource :: forall a m. MonadAff m => (Emitter Aff a -> Aff (Finalizer Aff)) -> EventSource m a

Constructs an event source from a setup function that operates in Aff.

  • The Emitter that the passed function receives is used to emit actions that will be received by the current component, or can be closed to shut down the event source and remove the subscription.
  • The Finalizer that the passed function produces is there to allow for some clean-up action to be taken when the event source is unsubscribed from. This also runs if the Emitter is closed. mempty can be used here if there is no clean-up to perform.

#effectEventSource Source

effectEventSource :: forall a m. MonadAff m => (Emitter Effect a -> Effect (Finalizer Effect)) -> EventSource m a

Constructs an event source from a setup function that operates in Eff.

  • The Emitter that the passed function receives is used to emit actions that will be received by the current component, or can be closed to shut down the event source and remove the subscription.
  • The Finalizer that the passed function produces is there to allow for some clean-up action to be taken when the event source is unsubscribed from. This also runs if the Emitter is closed. mempty can be used here if there is no clean-up to perform.

#eventListenerEventSource Source

eventListenerEventSource :: forall a m. MonadAff m => EventType -> EventTarget -> (Event -> Maybe a) -> EventSource m a

Constructs an event source from an event in the DOM. Accepts a function that maps event values to a Maybe-wrapped action, allowing it to filter events if necessary.

#Emitter Source

newtype Emitter m a

Values of this type are created internally by affEventSource and effectEventSource, and then passed into the user-provided setup function.

This type is just a wrapper around a callback, used to simplify the type signatures for setting up event sources.

Constructors

#emit Source

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

Emits an action via the emitter. For example:

data Action = Notify String

myEventSource = EventSource.affEventSource \emitter -> do
  Aff.delay (Milliseconds 1000.0)
  EventSource.emit emitter (Notify "hello")
  pure mempty

#close Source

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

Closes the emitter, shutting down the event source. This allows an event source to stop itself internally, rather than requiring external shutdown by unsubscribing from it.

The event source will automatically be unsubscribed from when this is called, and the finalizer returned during event source setup will be executed.

Any further calls to emit after close will be ignored.

#hoistEmitter Source

hoistEmitter :: forall n m. (m Unit -> n Unit) -> (Emitter m) ~> (Emitter n)

Changes the effect monad of an emitter.

#Finalizer Source

newtype Finalizer m

When setting up an event source, values of this type should be returned to describe any clean-up operations required. This is just a newtype around an effectful operation, but helps with type signature comprehension.

There is a Monoid instance provided for finalizers, so mempty can be used in cases where there are no relevant clean-up actions to take.

Constructors

Instances

#finalize Source

finalize :: forall m. Finalizer m -> m Unit

Runs a finalizer.

#hoistFinalizer Source

hoistFinalizer :: forall n m. (m ~> n) -> Finalizer m -> Finalizer n

Changes the effect monad for a finalizer.