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

river-core

v0.1.0

Published

Library for building computational graphs

Downloads

3

Readme

River Core

"River Core" is a minimal set of functions for building computational graphs.

Computational graphs, in essence, are networks of functions. But unlike common functions, River nodes receive their inputs and emit their outputs independently from one another, and instead of parameters, values get passed in and emitted through ports.

Getting started

npm i river-core

API

The River core API consists of three functions:

  • createNode(outFields, createInPorts)
  • connect(outPort, inPort)
  • disconnect(outPort[, inPort])

createNode(outFields, createInPorts)

Creates a node with the specified output and input ports. As output ports have no logic associated with them, only their names are required.

The function createInPorts() takes one argument, outputs' which is a lookup of functions emitting on the node's output ports, and returns functions in a similar structure but for input ports.

Both input and output functions take two arguments:

  • value, which is the value being sent ot / emitted by the node, and
  • tag, which identifies the impulse throughout the graph.

createNode() returns a Node object, with at least two properties:

  • i, a collection of functions that serve as the node's input ports
  • o, a collection of structures that serve as output ports (the specifics of these structures are not relevant to their usage)

Example / TypeScript

import {createNode} from "river-core";
type In = {d_in: number};
type Out = {d_out: number};
const node = createNode<In, Out>(["d_out"], (outputs) => ({
  d_in: (value, tag) => outputs.d_out(value, tag)
}));

Example / ES6

const core = require("river-core");
const node = core.createNode(["d_out"], (outputs) => ({
  d_in: (value, tag) => outputs.d_out(value, tag)
}));

connect(outPort, inPort)

Connects an output port to an input port. After a connection is made, when a node emits a value on one of its connected output ports, that value will be used to invoke all connected input ports.

It's worth noting that input ports are simply functions, and therefore it's possible to 'connect' an output port to a function, eg. console.log.

Example / TypeScript

import {connect} from "river-core";
// initializing node1 & node2 
connect(node1.o.d_out, node2.i.d_in);

Example / ES6

const core = require("river-core");
// initializing node1 & node2 
core.connect(node1.o.d_out, node2.i.d_in);

disconnect(outPort[, inPort])

Disconnects an input port from an output port, or, disconnect all input ports from an output port.

Example / TypeScript

import {disconnect} from "river-core";
// initializing & connecting node1 & node2 
disconnect(node1.o.d_out, node2.i.d_in);
disconnect(node1.o.d_out);

Example / ES6

const core = require("river-core");
// initializing & connecting node1 & node2 
core.disconnect(node1.o.d_out, node2.i.d_in);
core.disconnect(node1.o.d_out);

Types

River Core exports the following types:

  • Any: Lookup with string keys
  • InPort: Describes an input port
  • InPorts: Describes a set of input ports. Usually corresponds to the i property of nodes.
  • Node<In, Out>: Describes a node, where In and Out describe the value type carried by each input and output port, respectively.
  • OutPort: Describes an output port
  • OutPorts: Describes a set of input ports. Usually corresponds to the o property of nodes.
  • Outputs: Describes a set of functions that invoke connected input ports.
  • Tag: Identifies impulses. (In turn, an impulse is an identifiable input that ripples through multiple nodes throughout the graph.)

Custom nodes

In practice, createNode() is not invoked directly, as one would have to supply types (for TypeScript), output port names, and the function createInPorts() on each call. Instead, createNode() usually gets wrapped inside an outer factory function which has these already bundled.

The example below will produce the same node as the ones above, in a reusable way. For those coming from an OOP background, this would be analogous to subclassing.

Example / TypeScript

import {createNode, Node} from "river-core";
type In = {d_in: number};
type Out = {d_out: number};
type Forwarder<V> = Node<In, Out>
function createForwarder<V>(): Node {
    return createNode<In, Out>(["d_out"], (outputs) => ({
      d_in: (value, tag) => outputs.d_out(value, tag)
    }));
}

Example / ES6

const core = require("river-core");
function createForwarder() {
    return createNode(["d_out"], (outputs) => ({
      d_in: (value, tag) => outputs.d_out(value, tag)
    }));
}

Composite nodes

One of the most useful features of computational graphs is their composability. A number of interconnected nodes may act as as a single node themselves, making it easier to compartmentalize functionality, as well as reason about different parts of the application.

Implementing a composite node is quite simple with a factory function, which will create multiple nodes, establish internal connections, and expose specific ports of its components as its own.

Example / TypeScript

import {connect, Node} from "river-core";
type In = {d_in: number};
type Out = {d_out: number};
type Composite = Node<In, Out>;
function createComposite<In, Out>(): Composite {
    const forwarder1 = createForwarder();
    const forwarder2 = createForwarder();
    connect(forwarder1.o.d_out, forwarder2.i.d_in);
    return {
        i: {d_in: forwarder1.i.d_in},
        o: {d_out: forwarder2.o.d_out}
    };
}

Example / ES6

const core = require("river-core");
function createComposite() {
    const forwarder1 = createForwarder();
    const forwarder2 = createForwarder();
    connect(forwarder1.o.d_out, forwarder2.i.d_in);
    return {
        i: {d_in: forwarder1.i.d_in},
        o: {d_out: forwarder2.o.d_out}
    };
}