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 🙏

© 2026 – Pkg Stats / Ryan Hefner

observedvar

v1.2.0

Published

Create a variable to which you can attach listeners

Downloads

30

Readme

Package ObservedVar

What is it?

A package which simply allows you to attach listeners to your variables.

How to install ?

$ npm i observedvar

How it works?

Includes package

const { ObservedVar } = require('observedvar');
// or (shorthand)
const { Ov } = require('observedvar');

Create an observed variable

const foo = new ObservedVar('defaultValue');
// or (shorthand with the right import)
const foo = new Ov('defaultValue');

Set value

foo.value = 'new value'
// or
foo.set('new value')

Get value

foo.value
// or 
foo.get()

Add a listener, for everytime or once

It return a listener object, use his id to unsubscribe.

const callback = (listener, newValue)=>{
    console.log(`(listener ${listener.id}) Foo value changed, his value is now ${newValue}`)
    if(conditionToDestroy) {
        listener.destroy();
    }
};

const fooListener = foo.subscribe(
    callback,
    isDirectlyDestroyedAfterBeingCalled // default is false
);
// shorthands
foo.sub(...)
foo.once(...) // equals to foo.sub(callback, true)

Remove a listener

With the Ov variable

foo.unsubscribe(fooListener.id);
// shorthand
foo.unsub(...);

Or directly from the listener

fooListener.destroy();

The method once has another use

You could use a listener to handle a fetch result. To prevent the following case

const fetchedData = new ObservedVar(null);
fetch(...).then(data=>{
    fetchedData.value = data;
});

...

if(fetchedData===null) {
    // supposing the data is fetching.
    // so we need to subscribe a listener
    fetchedData.subscribe(()=>handleData());
} else {
    // supposing the data is already fetched.
    // we don't need a listener
    handleData();
}

Use once() like that:

const fetchedData = new ObservedVar(null);
fetch(...).then(data=>{
    fetchedData.value = data;
});

...

fetchedData.once(
    handleData,
    null // The value expected to subscribe a listener
);

Listener object

It is returned when the method .subscribe(...) is called, or within the callback

const fooListener = foo.sub(listener=>{
    console.log(listener)
}, once);

console.log(fooListener)

// listener = fooListener = {
//      once: false,
//      id: 0,
//      destroy: [Function: destroy],
//      callback: [Function: callback]
// }

Shorthands

|Full|Shorthand| |---|---| |new ObservedVar(defaultValue)|new Ov(defaultValue)| |.subscribe(listener)|.sub(listener)| |.unsubscribe(listenerId)|.unsub(listenerId)| |.subscribe(listener, true)|.once(listener)|