Module

Halogen.Subscription

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

#SubscribeIO Source

type SubscribeIO a = { emitter :: Emitter a, listener :: Listener a }

A paired Listener and Emitter produced with the create function.

#create Source

create :: forall a. Effect (SubscribeIO a)

Create a paired Listener and Emitter, where you can push values to the listener and subscribe to values from the emitter.

{ emitter, listener } <- create

-- Push values into the listener:
notify listener "hello"

-- Subscribe to outputs from the emitter with a callback:
subscription <- subscribe emitter \value ->
  Console.log value

-- Unsubscribe at any time:
unsubscribe subscription

#Listener Source

newtype Listener a

Conceptually, a Listener represents an input source to an Emitter. You can push a value to its paired emitter with the notify function.

Instances

#notify Source

notify :: forall a. Listener a -> a -> Effect Unit

Push a value to the Emitter paired with the provided Listener argument.

-- Create an emitter and listener with `create`:
{ emitter, listener } <- create

-- Then, push values to the emitter via the listener with `notify`:
notify listener "hello"

#Emitter Source

newtype Emitter a

An Emitter represents a collection of discrete occurrences of an event; conceptually, an emitter is a possibly-infinite list of values.

Emitters are created from real events like timers or mouse clicks and can be combined or transformed with the functions and instances in this module.

Emitters are consumed by providing a callback via the subscribe function.

Instances

#makeEmitter Source

makeEmitter :: forall a. ((a -> Effect Unit) -> Effect (Effect Unit)) -> Emitter a

Make an Emitter from a function which accepts a callback and returns an unsubscription function.

Note: You should use create unless you need explicit control over unsubscription.

#Subscription Source

newtype Subscription

A Subscription results from subscribing to an Emitter with subscribe; the subscription can be ended at any time with unsubscribe.

Instances

#subscribe Source

subscribe :: forall r a. Emitter a -> (a -> Effect r) -> Effect Subscription

Subscribe to an Emitter by providing a callback to run on values produced by the emitter:

-- Produce an emitter / listener pair with `create`:
{ emitter, listener } <- create

-- Then, subscribe to the emitter by providing a callback:
subscription <- subscribe emitter \emitted ->
  doSomethingWith emitted

-- End the subscription at any time with `unsubscribe`:
unsubscribe subscription

#unsubscribe Source

unsubscribe :: Subscription -> Effect Unit

End a subscription to an Emitter.

#fold Source

fold :: forall a b. (a -> b -> b) -> Emitter a -> b -> Emitter b

Fold over values received from some Emitter, creating a new Emitter.

#filter Source

filter :: forall a. (a -> Boolean) -> Emitter a -> Emitter a

Create an Emitter which only fires when a predicate holds.

#fix Source

fix :: forall i o. (Emitter i -> { input :: Emitter i, output :: Emitter o }) -> Emitter o

Compute a fixed point.