GithubHelp home page GithubHelp logo

purescript-choco-pie's Introduction

Purescript-Choco-Pie

Build Status

Docs on Pursuit

A Cycle.js-like utility for working with Purescript-Event. Aptly named for a circular Korean snack food.

Usage

program = runChocoPie main' drivers
  where
    main' ::
      { a :: Event Int
      , b :: Unit
      } ->
      { a :: Event Unit
      , b :: Event Int
      }
    main' sources =
      { a: mempty
      , b: sources.a
      }

    drivers ::
      { a :: Event Unit -> Effect (Event Int)
      , b :: Event Int -> Effect Unit
      }
    drivers =
      { a: const $ pure (pure 1)
      , b: \events -> subscribe events logShow
      }

With appropriate context and individual annotations, the type signature annotations are no longer needed.

Usage Exmples

I rewrote some code in my simple-rpc-telegram-bot repo, where I have drivers for running a child process, receiving and sending messages from Telegram, and a timer that ticks for every hour.

type Main
   = { torscraper :: Event Result
     , bot :: Event Request
     , timer :: Event Request
     }
  -> { torscraper :: Event Request
     , bot :: Event Result
     , timer :: Event Unit
     }
main' :: Main
main' sources =
  { torscraper: sources.timer <|> sources.bot
  , bot: sources.torscraper
  , timer: mempty
  }

drivers
  :: Config
  -> { torscraper :: Event Request -> Effect (Event Result)
     , bot :: Event Result -> Effect (Event Request)
     , timer :: Event Unit -> Effect (Event Request)
     }
drivers
  { token
  , torscraperPath
  , master
  } =
  { torscraper
  , bot
  , timer
  }
  where
    torscraper requests = do
      { event, push } <- create
      _ <- subscribe requests $ handleTorscraper torscraperPath master push
      pure event

    bot results = do
      connection <- connect $ unwrap token
      _ <- subscribe results $ sendMessage' connection
      messages <- getMessages connection
      pure $ { origin: FromUser, id: master } <$ messages

    timer _
      | tick <- pure unit <|> unit <$ interval (60 * 60 * 1000)
      , reqs <- { origin: FromTimer, id: master } <$ tick
      = pure reqs

-- runChocoPie main' (drivers config)

I also have a simpler example in atarime-purescript for similar usage of this library.

purescript-choco-pie's People

Contributors

justinwoo avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

purescript-choco-pie's Issues

Needs upgrade to newest behaviors or any PureScript library

Essentially, needs to replace these two lines:

bundle <- create

subscribe sink bundle.push

The first part is where a proxy event gets made, and the second part where events are pushed into it. If you're familiar with Cycle.js, you'll know how all this works anyway, but in short:

Essentially there are two parts here, the "main" function and the "driver" function(s).

For all sink and source and known symbol key, we have something like this:

main :: { key :: source } -> { key :: Event sink }

drivers :: { key :: Event sink -> IO source }

So to start this thing up, the runChocoPie works by creating a proxy Event for every key, so that this can be passed into the driver function to obtain source. This source is then passed into our "main" function to obtain our Event of sink. But of course, this Event cannot be used to call the drivers a second time (and nor do we want to). But because we know that this is a concrete Event/"subject" (and not just some unconstrained f), we can subscribe to it and push items into it. So when we subscribe to this "real" Event and push items into our proxy Event, we have the complete cycle here. Hence, Cycle.js, or Choco-Pie.


Since the new behaviors doesn't give you a subject by default, you would probably have to provide your own subject implementation for this change. But if you think using coroutines or something else would be a better fit, you should make a PR with it. My only conditions are that whatever the library used should allow for both effectful and pure transformation of the streaming structure and provide some basic utilities (Functor, Applicative, Alternative instances, mapMaybe or similar operations for filtering -- note that I don't need a Bind/Monad instance) AND it must not use an npm library (because I do not have enough time on this earth to deal with them, and I already also have purescript-cycle-run).

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.