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

@dhruvio/pyramid

v1.0.2

Published

Composable web UI framework.

Downloads

7

Readme

Pyramid

Pyramid is a composable web UI framework. It is based on the Elm architecture, and is implemented in JavaScript, utilising the virtual-dom library for rendering, and ImmutableJS for state management.

The main goal of this framework is to provide a minimal API to build applications with unidirectional data flow. The struggle of implementing such a framework in JavaScript is that the language is dynamic, and its data structures are mutable. By utilising the Elm architecture and ImmutableJS, Pyramid components are have four parts: state initialization (init), state mutation (actions), rendering (render), and side-effects (effects). If you are coming from building applications with React, Mercury, or Elm, most, if not all, of this will be familiar to you. The most foreign concept may be the notion of side-effects. The idea behind side-effects is to organise code that interacts with the browser's API into one section of your component, as opposed to making pure parts of your application impure. As the documentation for this framework improves over time, the role of effects will become more apparent.

Over time, this repository aims to also provide a set of effects and utility functions that will assist developers in building composable applications with ease. For example, an asynchronous router using the browser's history API is currently under development as an effect. Other developers are welcome to contribute their own effects and utility functions to this repository, however please be friendly and humble :-).

Example usage

// dependencies
const pyramid                    = require("@dhruvio/pyramid");
const h                          = require("@dhruvio/pyramid/helpers/h");
const createAnimationFrameEffect = require("@dhruvio/pyramid/effects/animation-frame");

// set up the root component
const app = {

  // the app is going to track the last animation frame
  // and the number of frames elapsed
  init () {
    return {
      lastFrame: 0,
      numFrames: 0
    };
  },

  // the animation frame effect triggers an action
  // named "animationFrame" with the current frame time
  // every time the callback registered with requestAnimationFrame
  // is called
  actions: {
    animationFrame: function (state, { frame }) {
      return state.merge({
        lastFrame: frame,
        numFrames: state.get("numFrames") + 1
      });
    }
  },

  // we use the pyramid-supplied animation frame effect
  // it takes one argument, which is the action name
  // that should be triggered every time an animation frame
  // comes through; and it returns the effect function
  effects: [
    createAnimationFrameEffect("animationFrame")
  ],

  // we render a simple DOM tree that displays the state
  // in a list
  render (state, update) {
    return h("ul#main", [
      h("li", [ "lastFrame: " + state.get("lastFrame") ]),
      h("li", [ "numFrames: " + state.get("numFrames") ])
    ]);
  }

};

// append the component to the <body>; and
// kick off the run loop
pyramid(document.body, app);

API

Functions

This section describes core functions exported from Pyramid.

pyramid (element: HTMLElement, rootComponent: Component): Undefined

This is the main function exported by Pyramid when you run require("@dhruvio/pyramid"). It takes two arguments, the HTML element you want your application to be appended to (e.g. document.body), and the root Component of your application.

Types

This section describes common types used in Pyramid applications.

Component

A plain object defining a single component. Component's can be reasoned as if they have their own state life cycle. Each component has four properties:

init (): Object

The state initialization function. It accepts no arguments and returns a plain JavaScript describing the starting state for your component. Pyramid converts this to an ImmutableJS object internally.

actions: Object

A plain object. Each key must be a string (i.e. an action's name), and each value must be of action (state: ImmutableState, data: Any): ImmutableState. Pyramid creates an update function that, when called, triggers an action specified in this object. The action is called with the current state and any data specified during the update function call. The value returned from the action must be the new state to replace the entire component's state.

effects: [effect (state: ImmutableState, update: UpdateFunction): Undefined]

A list of functions that receive the current state and an update function to trigger state mutations. Each effect function is called after a cycle of state mutations.

On a related note, the entire render cycle is implemented as an effect internal to Pyramid!

render (state: ImmutableState, update: UpdateFunction): VTree

A function that receive the current state and an update function to trigger state mutations. The function is expected to return a virtual-dom VTree, typically created with the library's h function.

ImmutableState

An ImmutableJS object that represents a component's state.

UpdateFunction

A function constructed internally in Pyramid to dispatch state mutations. It is of the following type: update (actionName: String, data: Any): Undefined. Each actionName must correspond to one of the component's actions.

VTree

The object returned from virtual-dom's h function.

Links

Author

Dhruv Dang
[email protected]
dhruv.io