Utilities for creating and managing push-based subscriptions, inspired by the event library. This library is used to implement subscriptions in Halogen, but it can be used independently of Halogen.
Install halogen-subscriptions
with Spago:
spago install halogen-subscriptions
The halogen-subscriptions
library helps you create and transform push-based subscriptions. Most subscriptions follow this pattern:
- Use the
create
function to produce a pairedEmitter
andListener
. An emitter is a possibly-infinite list of values that you can subscribe to, and a listener is a mechanism for pushing values to the emitter. - Use the
subscribe
function to subscribe to outputs from the emitter by providing a callback function to run each time a value is emitted. - Use the
notify
function to push values to the emitter via the listener. - Use the
unsubscribe
function to end a subscription to an emitter.
Here's a simple example that logs "Hello" and then "Goodbye":
module Main where
import Prelude
import Effect (Effect)
import Effect.Console as Console
import Halogen.Subscription as HS
main :: Effect Unit
main = do
{ emitter, listener } <- HS.create
subscription <- HS.subscribe emitter \str -> Console.log str
HS.notify listener "Hello"
HS.notify listener "Goodbye!"
HS.unsubscribe subscription
Emitters can be combined and transformed to make more sophisticated subscriptions.
Module documentation is published on Pursuit.