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

x-vars

v0.1.0

Published

A small JavaScript library for programming in a reactive style

Downloads

1

Readme

x-vars

x-vars is a small library for programming in a reactive style. It allows you to create reactive variables that act like cells in a spreadsheet - when you update any variable, all dependent variables will be updated automatically. Here is the example showing the difference between reactive and non-reactive variables (pseudocode):

// Non reactive variables
var a = 10;
var b = 5;
var c = a + b; // 15
var b = 10;
console.log(c) // still 15

// Reactive variables
var a = 10;
var b = 5;
var c <= a + b; // <= means 'c always equals a + b'
var b = 10;
console.log(c) // 20

Basic usage

Here is the code demonstrating how to implement the example above using x-vars

import $ from 'x-vars';

const a = new $(10);
const b = new $(5);
const c = $.calc((a, b) => a + b, [a, b]);
console.log(c.$); // 15
b.$ = 10;
console.log(c.$); // 20

Each reactive variable represents a data stream, its $ property (read/write) contains the current value. The $.calc method can take 3 arguments:

  • The function to calculate the resulting value. The number of arguments can vary and corresponds the values of argument variables (see below). The very last argument contains the last value of resulting variable
  • Array of reactive variables that affect the resulting value (argument variables)
  • (optional) The reactive variable where to put the result. If not specified the new variable will be created.

If you don't need a separate reactive variable, but still want to react to changes, you can call $.calc as a procedure:

$.calc((a) => {
	console.log(`Variable "a" has changed. New value is ${a}`);
}, [a]);

Every calculated variable created by $.calc makes subscriptions to the variables it depends on (argument variables). If the calculated variable lives less than it's argument variables it must be released manually to clear its subscriptions and prevent performance loss and memory leaks:

$.release(a, b, c, d, ...);

Calling $.calc as a procedure also creates subscriptions and to clear them you can pass a third argument to the method:

const $$ = new $(null); // Dummy variable to hold subscriptions
...
$.calc((a) => {/* Do something */}, [a], $$);
$.calc((b) => {/* Do something else */}, [b], $$);
...
$.release($$);

The same variable can be used multiple times as a result of the $.calc method to merge different logics to the same data stream:

const loadedData = new $([]);
const removedItem = new $(null);
const list = $.calc((data) => data, [loadedData]);
$.calc((item, currentValue) => currentValue.filter(x => x !== item), [removedItem], list);
loadedData.$ = [1, 2, 3, 4, 5];
console.log(list.$); // [1, 2, 3, 4, 5]
removedItem.$ = 3;
console.log(list.$); // [1, 2, 4, 5]

Creating reactive variables from asynchronous sources

You can create reactive variable from asynchronous sources using $.gen method:

// From setTimeout
const fromTimer = $.gen(0, (emit) => setTimeout(
    () => emit(10)
    , 1000));

// From promise
const promise = ...
const fromPromise = $.gen(null, (emit) => promise.then(
    (result) => emit(result));

// From rxjs observables
const observable = ...
const fromObservable = $.gen(null, (emit) => observable.subscribe(
    (result) => emit(result));

// From user input
const input = document.querySelector(...);
const fromInput = $.gen(input.value, (emit) => {
    input.onchange = (e) => {
        emit(e.target.value);
    }
});

The first argument accepts the initial value, the second is a function to produce new values when needed. The method can also take an optional third argument where to put the result, similar to $.calc. The new value will be assigned to the variable when emit function is called.

Authors

  • Yuri Spektor

License

MIT