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

grafn

v1.0.0

Published

Graph + Fn: Execute functions as a graph

Downloads

2

Readme

Grafn

Graph + Fn: Execute functions as a graph.

What it's for?

Grafn lets you execute sequences of async functions by defining them as vertices in a graph.

This allows you to easily define and manipulate complex dependencies in asynchronous code.

How can I use it?

In order to use it you must first create a graph.

const Grafn = require('grafn');

const graph = new Grafn();

Next you need to add some vertexes.

graph.vertex({
  name: 'root',
  action() {

    return 5;
  }
});

This is a vertex without dependencies, it means it can be executed without waiting for anything else. The return value of this function will be stored so it can be used by its dependents.

You can add dependencies by listing the names as an array in the vertex definition.

graph.vertex({
  name: 'addition',
  dependencies: ['root'],
  action(state) {

    return state.root + state.root;
  }
});

Notice how the result of the root vertex is available in this action.

Vertexes will be executed as soon as all of their dependencies are met. For example, consider the following graph

const Grafn = require('grafn');

const graph = new Grafn();

graph.vertex({
  name: 'root',
  action() {

    return 5;
  }
});

graph.vertex({
  name: 'addition',
  dependencies: ['root'],
  action(state) {

    return state.root + state.root;
  }
});

graph.vertex({
  name: 'subtraction',
  dependencies: ['root'],
  action(state) {

    return state.root - state.root;
  }
});

graph.vertex({
  name: 'multiplication',
  dependencies: ['root'],
  action(state) {

    return state.root * state.root;
  }
});

graph.vertex({
  name: 'division',
  dependencies: ['root'],
  action(state) {

    return state.root / state.root;
  }
});

graph.vertex({
  name: 'tally',
  dependencies: ['addition', 'subtraction', 'multiplication', 'division'],
  action(state) {

    console.log('+', state.addition);
    console.log('-', state.subtraction);
    console.log('*', state.multiplication);
    console.log('/', state.division);
  }
});

The addition, subtraction, multiplication, and division vertices will all run as soon as roott is executed. However, the final tally vertex won't run until all other operations are complete.

You can visualize the graph by using the graph.toString() method. This will output a graphviz digraph:

digraph {
    root
    addition
    root -> addition
    subtraction
    root -> subtraction
    multiplication
    root -> multiplication
    division
    root -> division
    tally
    addition -> tally
    subtraction -> tally
    multiplication -> tally
    division -> tally
}

execution graph, before execution

You can easily change the shape of the graph by changing the dependencies array.

In order to start execution of the graph you should select which node to start from.

graph.run('root');

After an execution, you can use graph.toString() to check the result of an execution. It will highlight which nodes were executed successfully (green), and which nodes threw an error (red).

execution graph, after execution

Acknowledgements

This project implements the same ideas behind fluorine, a similar graph based library. Grafn wouldn't exist without it.