Git Product home page Git Product logo

purescript-wire's Introduction

purescript-wire

Wire aims to provide useful reactive tools: currently Event and Signal.

Wire.Event

Event models asynchronous, discreet events over time, and are roughly analogous to Observables in RxJS.

Note: this implementation was largely inspired by FRP.Event from the purescript-event library. My implementation has a more restricted set of functionality, has immediate unsubscription (which works better when combined with React), and a monad instance.

Creating an event

create :: forall a. Effect { event :: Event a, push :: a -> Effect Unit }

Example:

periodically :: Milliseconds -> Effect (Event Unit)
periodically (Milliseconds ms) = do
  { event, push } <- Event.create
  setInterval (Math.floor ms) do
    push unit
  pure event

Subscribing to an event

subscribe :: forall a b. Event a -> (a -> Effect b) -> Effect (Effect Unit)

Example:

logToConsole :: forall a. Show a => Event a -> Effect (Effect Unit)
logToConsole event = Event.subscribe event Console.logShow

The Effect Unit being returned from Event.subscribe is the subscription canceller.

Extra functionality

  • fold :: forall a b. (b -> a -> b) -> b -> Event a -> Event b
  • share :: forall a. Event a -> Effect (Event a) (see #12)
  • distinct :: forall a. Eq a => Event a -> Event a
  • bufferUntil :: forall b a. Event a -> Event b -> Event (Array a)
  • fromFoldable :: forall a f. Foldable f => f a -> Event a
  • range :: Int -> Int -> Event Int
  • times :: Int -> Event Int

Wire.Signal

Signal is like Event but it models a continuous value over time, with change events. As such you can always read the current value of a signal, as well as subscribe to future changes. Signals are build on top of Event, and also have a monad instance.

This was originally created for modelling application-level reactive state in React, for things like routing and user authentication.

Creating a signal

create :: forall a. a -> Effect { signal :: Signal a, modify :: (a -> a) -> Effect Unit }

Example:

createAuthSignal :: Effect { auth :: Signal (Maybe AuthData), login :: AuthData -> Effect Unit, logout :: Effect Unit }
createAuthSignal = do
  { signal, modify } <- Signal.create Nothing
  pure
    { auth: signal
    , login: \authData -> modify (const (Just authData))
    , logout: modify (const Nothing)
    }

Sampling a signal

read :: forall a. Signal a -> Effect a

Subscribing to a signal

Subscribing to a signal is identical to events, with one exception: in the current implementation a signal will push it's current value to a new subscriber immediately (kind of like a ReplaySubject(1)).

subscribe :: forall b a. Signal a -> (a -> Effect b) -> Effect (Effect Unit)

Extra functionality

  • distinct :: forall a. Eq a => Signal a -> Signal a
  • event :: forall a. Signal a -> Event a
  • share :: forall a. Signal a -> Effect (Signal a)

purescript-wire's People

Contributors

dependabot[bot] avatar robertdp avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

Forkers

miuirussia

purescript-wire's Issues

Project objectives

Hi,

This library looks really interesting as it provides ready to use react-basic integration.

Would you be so kind and shortly describe what are the project objectives? Could you please tell me how the event implementation part differs from bodil/purescript-signal for example?

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.