GithubHelp home page GithubHelp logo

purescript-halogen / purescript-halogen-subscriptions Goto Github PK

View Code? Open in Web Editor NEW
13.0 4.0 1.0 23 KB

Utilities for creating and transforming push-based subscriptions.

License: Apache License 2.0

Dhall 8.79% PureScript 91.21%
purescript

purescript-halogen-subscriptions's Introduction

Halogen Subscriptions

CI Release Pursuit Maintainer: garyb Maintainer: thomashoneyman

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.

Installation

Install halogen-subscriptions with Spago:

spago install halogen-subscriptions

Quick start

The halogen-subscriptions library helps you create and transform push-based subscriptions. Most subscriptions follow this pattern:

  1. Use the create function to produce a paired Emitter and Listener. 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.
  2. Use the subscribe function to subscribe to outputs from the emitter by providing a callback function to run each time a value is emitted.
  3. Use the notify function to push values to the emitter via the listener.
  4. 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.

Documentation

Module documentation is published on Pursuit.

purescript-halogen-subscriptions's People

Contributors

thomashoneyman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

glocq

purescript-halogen-subscriptions's Issues

Add at least some documentation to makeEmitter

Is your change request related to a problem? Please describe.

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

This is a very confusing signature and definition for someone of novice-intermediate language experience. There's no examples, no test case, and what even is ((a -> Effect Unit) -> Effect (Effect Unit))? I think even adding a type alias like the type Canceler = Effect (Effect Unit) could help clarify what's going on. Not sure on the best solution, but currently I find it hard to understand.

Describe the solution you'd like
I started a thread on Discourse about requestAnimationFrame that has a useful, and probably popular use-case for makeEmitter in getting ticks for each frame. It could perhaps be common enough to have an Halogen.Subscriptions.{Common,Extra,Solutions,HTML,DOM,etc.} so the community can have a standardized solution for common problems -- especially for people looking for batteries-included, zephyr-will-clean-up-the-excess fix to "get sh*t done". Obviously you don't want to add DOM/HTML dependencies here, but it'd be a common dep in an application setting.

Additional context
Control.Plus.empty

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.