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

regl-combine

v1.0.4

Published

Combine hundreds or thousands of regl commands without worrying about "too much recursion" or "maximum call stack size exceeded" errors.

Downloads

18

Readme

regl-combine

Compbine hundreds or thousands of regl commands without worrying about "too much recursion" or "Maximum call stack size exceeded" errors. This module will group and segment multiple sequential regl calls into a single regl function while also allowing for intermediate "middleware" functions.

Install

$ npm install regl-combine

Why?

Javascript recursion (or stack depth) limits vary from browser to browser.

They can cause errors like

InternalError: too much recursion

or

RangeError: Maximum call stack size exceeded.

which can happen quite easily if you build many small regl components that inject contexts, provide shaders, and more. This module allows you to create small components and compose them together without the overhead or worry of recursion depth and stack overflows.

How?

This module composes regl commands together by capturing the constructor options used to create the commands and merging them into a single object that is used to create the new returned command. Commands are grouped together and are only segmented when a non-regl command is reached.

The following will segment the regl commands and create 4 function groups where group 1 contains command1, command2, command3, group 2 contains middleware1, group 3 contains middleware2, and group 4 contains command4, and command5.

combine(
    regl,
    command1,
    command2,
    command3,
    middleware1,
    middleware2,
    command4,
    command5)

Caveats

Parent Context Dependencies

In order to mitigate parent context dependencies, such as a command2 dynamic context property depending on the result of a command1 dynamic context property, you must ensure that command2 is not "combined" directly with command1 because the order in which their context properties cannot be guaranteed. You can achieve this by doing the following:

combine(regl, command1, (...args) => command2(...args))

which will produce two distinct groups.

Context Property Conflict

If a dynamic or static context property in command2 exists as a dynamic or static context property in command1, command1 will overwrite the context property in command1 and void it from creation.

API

wrap(regl) -> regl

This function accepts a regl constructor function and returns a new regl function that when invoked returns a regl command that injects the options used to create it as an injected context propetry reglOptions. This is needed for combine() to work properly if you provide it already created regl commands. Otherwise, you will have to expose regl command options to combine() only.

const { wrap } = require('regl-combine')
const regl = wrap(require('regl')())

note: If regl isn't wrapped with this function then the only input that will work with combine() are regl constructor options objects.

combine(regl, commands) -> Function

This function accepts a regl constructor function and an array of commands. An element in the commands array may be a constructed regl command if regl was previously wrapped with wrap(). An element in the comamnds array may also be a regl constructor options object, which is required if regl wasn't wrapped with the wrap() function call.

const command = combine(regl, [
  regl({ vert: ` ... ` }),
  { frag: ` ... `},
  { attributes: {position [ ... ]} }
])

note: If regl isn't wrapped with wrap() then the only input that will work with this function are regl constructor options objects.

Example

The following example composes several regl command functions into a single function.

const { combine, wrap } = require('regl-combine')
const regl = wrap(require('regl')()) // required

const drawTriangle = combine(regl, [
  regl({
    frag: `
      precision mediump float;
      uniform vec4 color;
      void main() {
        gl_FragColor = color;
      }
    `
  }),

  regl({
    vert: `
      precision mediump float;
      attribute vec2 position;
      void main() {
        gl_Position = vec4(position, 0, 1);
      }
    `
  }),

  regl({
		 attributes: {
       position: regl.buffer([
         [-2, -2],
         [4, -2],
         [4,  4]
       ])
     }
  }),

  regl({
    uniforms: {
      color: regl.prop('color')
    }
  }),

  regl({
    count: 3
	})
])

regl.frame(({time}) => {
  drawTriangle({
    color: [
      Math.cos(time * 0.001),
      Math.sin(time * 0.0008),
      Math.cos(time * 0.003),
      1
    ]
  })
})

License

MIT