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

instigator

v0.6.1

Published

A minimal memoization / observer pattern library

Downloads

45

Readme

instigator

A minimal memoization / observer pattern library

Instigator lets you define networks of sources, transformers, and consumers.

Sources are values that can change over time.

Transformers are functions that calculate new values, taking sources and other transformers as inputs. Transformers are automatically memoized (i.e. they return the previous result if their inputs have not changed) and they are lazy (i.e. they are only calculated when a downstream consumer is created or triggered).

Consumers are functions that perform side-effects and have no return value. They also take sources and transformers as inputs. They are triggered automatically when any of their inputs change.

Usage

import { activeSource, transformer, consumer } from 'instigator';

// Sources can be any value
const number = activeSource(1);

// Transformers take sources (or other transformers) as inputs, and return outputs
const double = transformer([number], (n) => n * 2);
const triple = transformer([number], (n) => n * 3);
const sumOfAllThree = transformer([number, double, triple], (n, d, t) => n + d + t);

// Consumers take transformers/sources as inputs, and produce side-effects (no outputs)
const logSum = consumer([sumOfAllThree], (sum) => console.log(sum));

// Consumers do not trigger when created, but can be invoked manually if you like
logSum(); // prints 6 (because number is set to 1, so 1 + 2 + 3 = 6)

// Consumers are invoked automatically when an input they (or their inputs,
// or their inputs' inputs, etc) rely on changes
number(2); // Triggers logSum, which prints 12 (from 2 + 4 + 6).

// The current value of sources and transformers can be retrieved at any time
console.log(number(), double(), triple()); // prints 2, 4, 6

// Setting a source to the same value has no effect
number(2); // Nothing happens

Tranformers and consumers only execute their functions if their inputs have changed. Sources also store their values and only trigger downstream consumers if their value changes.

Input changes are determined by shallow equality checks by default, but can be overridden by providing a custom comparitor:

import { activeSource, transformer, consumer } from 'instigator';

// Using the default equality check, all simple functions are considered equal
const fnSource: ActiveSource<() => number> = activeSource(() => 1);
const doubler = transformer([fnSource], (fn) => (() => fn() * 2));
consumer([doubler], (fn) => console.log(fn()));
fnSource(() => 1); // Does nothing
fnSource(() => 2); // Does nothing

// We can override the default shallow equality check and use a simple reference equality test
// Note the source, transformer and consumer all receive functions as inputs, so all need a custom
// comparitor
const refEquals: Comparator<() => number> = (a, b) => a === b;
const fnSourceCustom = activeSource(() => 1, refEquals);
const doublerCustom = transformer([fnSourceCustom], (fn) => (() => fn() * 2), refEquals);
consumer([doublerCustom], (fn) => console.log(fn()), refEquals);
fnSourceCustom(() => 1); // Prints 2
fnSourceCustom(() => 2); // Prints 4

If you want to update multiple sources but only trigger downstream consumers once, you can use batch:

import { batch, activeSource, consumer } from 'instigator';

const source1 = activeSource('1a');
const source2 = activeSource('2a');
const source3 = activeSource('3a');
consumer([source1, source2, source3], console.log);

// Without batch, an update to any source will trigger the consumer
source1('1b'); // Prints 1b, 2a, 3a
source2('2b'); // Prints 1b, 2b, 3a
source3('3b'); // Prints 1b, 2b, 3b

// With batch, updates are held until the end
batch(() => { // The function passed to batch is executed immediately and synchronously
    source1('1c'); // Nothing is printed
    source2('2c'); // Nothing is printed
    source3('3c'); // Nothing is printed
}); // Once the batch function completes consumers trigger, so logger prints 1c, 2c, 3c

You may wish to 'disable' a consumer, such that changes to its inputs no longer trigger it. You can do this by deregistering it:

import { activeSource, consumer } from 'instigator';

const source1 = activeSource('1a');
const source2 = activeSource('2a');
const source3 = activeSource('3a');
const logConsumer = consumer([source1, source2, source3], console.log);

// After deregistering a consumer, it is not triggered by changes to its sources
logConsumer.deregister();
source1('1d'); // Nothing is printed
source2('2d'); // Nothing is printed
source3('3d'); // Nothing is printed

(Note that sources retain references to consumers, so you may wish to deregister consumers to avoid memory leaks and unexpected actions occuring once your consumer has gone out of scope)

You may also wish with 'merge' a number of sources / transformers into a single object. You can do this with mergeTranformer:

import { activeSource, transformer, mergeTransformer } from 'instigator';

const source = activeSource(1);
const double = transformer([source], (i) => i * 2);

const merged = mergeTransformer({ source, double });

console.log(JSON.stringify(merged())); // Prints { source: 1, double: 2 }