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

sprocket-js

v0.1.13

Published

A framework for building real-time server UI components and live views in [Gleam ✨](https://gleam.run/)

Downloads

96

Readme

Sprocket

A framework for building real-time server UI components and live views in Gleam ✨

Package Version Hex Docs

Demo Documentation

Heavily inspired by Phoenix LiveView and React. The name "sprocket" is loosely derived from the metaphor of a bicycle's sprocket, cassette and chain.

An initial static view is rendered as HTML on the "first paint" which then establishes a connection to the server over a WebSocket to facilitate sending browser events and receiving view update diffs. These updates are patched into a client-side in-memory representation of the DOM and efficiently rendered to the browser DOM. Declarative views are built using functional components that accept props and re-render when state changes. Contextual hooks are used to manage state and effects, e.g. state, reducer and effect.

Typed component interfaces snap together and are used to create higher-level views. Data flow is "uni-directional" in that State always flows down into components as props and Events bubble up through event handler functions (which are also passed in as props, e.g. on_some_event("Something happened")).

Key Features

  • Real-time server-side UI component framework
  • Renders initial HTML and efficiently patches updates to the DOM using diffs sent over a persistent WebSocket connection
  • Declarative and composable functional components that re-render when state changes
  • Strong, static type system means fewer runtime crashes and easier maintenance
  • Lightweight OTP processes make for a more efficient and scalable application
  • Built on top of the venerable Erlang BEAM VM, which is renowned for high-concurrency and fault-tolerance

Example

Clock Component

pub type ClockProps {
  ClockProps(label: Option(String), time_unit: Option(erlang.TimeUnit))
}

pub fn clock(ctx: Context, props) {
  let ClockProps(label, time_unit) = props

  // Default time unit is seconds if one is not provided
  let time_unit =
    time_unit
    |> option.unwrap(erlang.Second)

  // Use a state hook to track the current time
  use ctx, time, set_time <- state(ctx, erlang.system_time(time_unit))

  // Example effect that runs once when the component is mounted
  // and has a cleanup function that runs when the component is unmounted
  use ctx <- effect(
    ctx,
    fn() {
      let interval_duration = case time_unit {
        erlang.Millisecond -> 1
        _ -> 1000
      }

      let cancel =
        interval(interval_duration, fn() {
          set_time(erlang.system_time(time_unit))
        })

      Some(fn() { cancel() })
    },
    [],
  )

  let current_time = int.to_string(time)

  render(
    ctx,
    case label {
      Some(label) -> fragment([span([], [text(label)]), span([], [text(current_time)])])
      None -> text(current_time)
    },
  )
}

Parent view

pub type ExampleViewProps {
  ExampleViewProps
}

pub fn example_view(ctx: Context, _props: ExampleViewProps) {
  render(
    ctx,
    html(
      [lang("en")],
      [
        head([], [link([rel("stylesheet"), href("/app.css")])]),
        body(
          [class("bg-white dark:bg-gray-900 dark:text-white p-4")],
          [
            component(
              clock,
              ClockProps(label: Some("The current time is: "), time_unit: None),
            ),
          ],
        ),
      ],
    ),
  )
}

Getting Started

To get started with Sprocket, follow the instructions below:

  1. Clone the Sprocket starter repository:
git clone https://github.com/bitbldr/sprocket_starter.git
  1. Install the required dependencies:
gleam deps download
yarn
  1. Start the development server:
yarn run watch
  1. Open your web browser and visit http://localhost:3000 to see the starter app.

Installation

This package can be added to your Gleam project:

gleam add sprocket

For getting started with Sprocket, refer to the Official Docs. Here you will find detailed examples and tutorials. These docs are build with sprocket, which also make them an excellent reference implementation github.com/bitbldr/sprocket_docs.

API Documentation

API documentation can be found at https://hexdocs.pm/sprocket.

Roadmap

Sprocket is still in its early stages and has a roadmap for future development. Here are some of the planned improvements:

  • [x] Build out full set of base HTML functions for components
  • [x] Expand the available hooks to enable more flexible component behavior
  • [x] Add documentation to modules and simplify API
  • [ ] Add support for additional event types to handle various user interactions
  • [ ] Improve unit test coverage to ensure code quality and reliability
  • [ ] Add support for web-based client components
  • [ ] Investigate extending to support more than just web views, such as native desktop, iOS, and Android applications.

Contributing

Contributions to Sprocket are welcome and encouraged! If you would like to contribute, please follow the guidelines outlined in the CONTRIBUTING.md file.

License

Sprocket is released under the MIT License.