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-subscriptionsThe halogen-subscriptions library helps you create and transform push-based subscriptions. Most subscriptions follow this pattern:
- Use the createfunction to produce a pairedEmitterandListener. 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 subscribefunction to subscribe to outputs from the emitter by providing a callback function to run each time a value is emitted.
- Use the notifyfunction to push values to the emitter via the listener.
- Use the unsubscribefunction 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 subscriptionEmitters can be combined and transformed to make more sophisticated subscriptions.
Module documentation is published on Pursuit.