npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reason-tea

v0.16.2

Published

The Elm Architecture (TEA) for OCaml, Reason, and Rescript

Downloads

3

Readme

Reason-TEA

Description

This is a library that enables The Elm Architecture for OCaml, Reason, and Rescript.

The Elm Architecture is an MVU pattern for organizing frontend applications and components. Another example a TEA-influenced project is React/Redux.

In TEA, each component has single model. The model is updated by receiving messages (typically named msg) - all relevent browser and user events, including keyboard, mouse, fetch, clipboard, etc, are converted into messages. An update method receives a model and message, and returns a new model. It can also return commands, which affect the outside world, for example by making API calls. And, that's it. That's the whole idea.

The model is used to render HTML via a built-in virtual DOM. The entire application is just a single component with a single model and update function.

Advantages

  • Entirely event driven, this is like React/Redux but type-safe and significantly faster.
  • Amazingly fast compile-times, especially with Rescript's built-in watcher
  • Open license.

You can read more about it here.

Project design

  • [X] Elm API: Following the Elm API as closely as Rescript allows. Converting code back and forth between Elm and OCaml should be made as easy as possible and there exists both a converter, as well as documentation for that process.

Installation

NPM

First verify you have rescript installed, whether globally or just in your project.

Then install via npm by:

npm install --save-dev rescript-tea

Then in your current Rescript project just use this as a dependency add this to your bsconfig.json file:

  "bs-dependencies" : ["rescript-tea"]

Usage

Example project

Once you have your Rescript project set up and the dependencies configured as above then lets make a new TEA module, the Counter, as is traditional in Elm tutorials, this file will be named counter.res in your src directory for this example. Code is described via inline comments:

// This line opens the Tea.App modules into the current scope for Program access functions and types
open Tea.App

// This opens the Elm-style virtual-dom functions and types into the current scope
open Tea.Html

// Let's create a new type here to be our main message type that is passed around
type msg =
  | Increment // This will be our message to increment the counter
  | Decrement // This will be our message to decrement the counter
  | Reset     // This will be our message to reset the counter to 0
  | Set(int)  // This will be our message to set the counter to a specific value

// the model for Counter is just an integer
type model = int

// This is optional for such a simple example, but it is good to have an `init` function to define your initial model default values
let init = () => 4

// This is the central message handler, it takes the model as the first argument
let update = (model: model, msg: msg) : model =>
  switch msg {
  | Increment => model + 1
  | Decrement => model - 1
  | Reset => 0
  | Set(v) => v
  }

// This is just a helper function for the view, a simple function that returns a button based on some argument
let viewButton = (title: string, msg: msg) =>
  button(list{Events.onClick(msg)}, list{text(title)})

// This is the main callback to generate the virtual-dom.
// This returns a virtual-dom node that becomes the view, only changes from call-to-call are set on the real DOM for efficiency, this is also only called once per frame even with many messages sent in within that frame, otherwise does nothing
let view = (model: model): Vdom.t<msg> =>
  div(
    list{},
    list{
      span(list{Attributes.style("text-weight", "bold")}, list{text(string_of_int(model))}),
      br(list{}),
      viewButton("Increment", Increment),
      br(list{}),
      viewButton("Decrement", Decrement),
      br(list{}),
      viewButton("Set to 42", Set(42)),
      br(list{}),
      model != 42 ? viewButton("Reset", Reset) : noNode
    })

// This is the main function, it can be named anything you want but `main` is
// traditional.  The Program returned here has a set of callbacks that can easily be
// called from Rescript or from javascript for running this main attached to an
// element, or even to pass a message into the event loop.  You can even expose the
// constructors to the messages to javascript via the above [@@bs.deriving
// {accessors}] attribute on the `msg` type or manually, that way even javascript can
// use it safely.
let main = beginnerProgram({
    model: init (),
    update: update,
    view: view
  })

If anything is typed wrong then the Rescript type checker will catch it and advise.

To use this from javascript (with your bundler of choice) you can just do:

  var app = require("src/counter.res").main(document.getElementById("my-element"));

And if you need to shut it down or pass it a message or so then you can do so via the app variable, or feel free to not assign it to a variable as well.

For further examples see the test directory, which has many examples.

For a large-scale example, see the Darklang client/editor.

Starter-Kits

A list of starter-kits that get you up and running.

Feel free to extend this list!

celsobonutti/vite-template-rescript-tea

Outdated started kits

tcoopman/bucklescript-tea-starter-kit

feluxe/bs-tea-starter-kit

darklang/philip2

This one is not so much a starter kit as it is a porting kit, it can actually take in Elm files, parse them, and output Rescript-tea code (though in OCaml, which can be converted to Rescript via rescript convert) with only minor'ish tweaks there-after needed to get it working.

See its announcement article at: https://medium.com/@paulbiggar/philip2-an-elm-to-reasonml-compiler-a210aaa6cd04

And its porting guide at: https://github.com/darklang/philip2#how-to-port-your-project

History

Rescript-tea is a fork of bucklescript-tea, aimed to modernize it base on how Rescript has developed since bucklescript-tea was created. We greatly appreciate the work that OvermindDL1 put into it.