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

compute

v1.0.2

Published

A simple reactive programming library.

Downloads

189

Readme

Compute Run Tests

An extremely simple reactive programming library using Observables.

Wait, what is an Observable?

An observable variable is a function, which holds a value. You can get the value by calling the function without any arguments, and you update the value by calling the function with the desired new value.

The useful thing about an observable is that you can choose to be notified every time the value of this observable changes.

import { observable, onChange } from "compute";

// Create a new observable.
const o1 = observable(42);

// Access the value
console.log(o1()); // Logs 42

// Subscribe to it
const mySubscription = onChange(newValue => console.log(newValue), o1);

// Update the value
o1(84);  // Console shows 84

// You won't get notified until the value changes (strict equality).
o1(84);  // Nothing happens.

// Stop getting notifications
mySubscription.unsubscribe();

onChange : React to observable value changes.

As demonstrated above, onChange let's you subscribe to observables. In fact, you can also subscribe to multiple variables.

import {observable, onChange} from "compute";
const a = observable(1);
const b = observable(2);
const c = observable(3);
const sum = (x, y, z) => console.log(`The sum is ${x + y + z}`);
onChange(sum, a, b, c); // Nothing happens so far
a(2); // Console shows "The sum is 7"
b(3); // Console shows "The sum is 8"

from() : Define an observable based on the value of other observables.

import {observable, onChange, from} from "compute";
const a = observable(1);
const b = observable(2);
const c = observable(3);
const getSum = (...values) => values?.reduce(val, x => x + val, 0);
const sum = from(
  getSum,
  a,
  b,
  c,
);
console.log(sum()); // Prints 6
a(2);
console.log(sum()); // Prints 7

// And because sum is an observable, you can subscribe to it.
when(sum, x => console.log(x));
a(3); // Console shows 8
b(3); // Console shows 9
c(0); // Console shows 6

Installation

npm install compute

Or if you are using yarn,

yarn add compute

And then in your code, simply import

import { compute, onChange, from } from "compute";

Docs

You can see the API at https://akshat1.github.io/compute/