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

esmonitor

v0.1.2

Published

Monitor ESM objects for changes

Downloads

2

Readme

esmonitor

Listen for changes on ECMAScript Objects

esmonitor allows you to monitor ECMAScript Objects for changes.

This library uses three different methods for recognizing changes:

  1. Wrapping functions to allow for output interception
  2. Adding setters to objects, which are triggered on updates
  3. Intermittent polling for objects with read-only properties (e.g. ES Modules).

Getting Started

const monitor = new Monitor()

// ---------------- Register Object ----------------
const id = 'actor'
const add = (input) => input + 1
const actor = { nested: { add } }

const storeId = 'store'
const store = { value: 0 }

monitor.set(id, actor, {static: false})
monitor.set(storeId, store, {static: false})


// ---------------- Create Listeners for the Entire Object ----------------
const functionPath = ['nested', 'function']

const testSubs = monitor.on(storeId, (path, ...args) => {
    console.log(`Updated Store (${path}) - ${args}`)
})

// ---------------- Selectively Listen to Object Property ----------------
const fSubs = monitor.on([id, ...functionPath], (path, ...args) => {
    console.log(`Update from Function (${path}) - ${args}`)
    store.value = args[0] // set store value
    store.updated = true
})

reference.nested.function(store.value)

monitor.remove() // Remove all subscriptions

Polling

The polling option allows you to specify a custom sampling rate for changes to your modules:

const monitor = new Monitor({
    polling: {
        sps: 60
    }
})

This may also be set directly on the Poller object:

monitor.poller.sps = 60

Notes

Dynamic vs. Static Objects

Dynamic objects are only necessary if you'd like to have updates from properties that were not originally on the object and that you aren't manually subscribing to.

For example, you may decide to listen to all changes on an objects.

const store = { value: 0 }
monitor.set('store', store, {static: true}) // declare a static object
monitor.on('store', (path) => console.log(`Updated!`, path))

However, if you add a new key later, this will not be registered.

store.test = true // will not trigger the callback

For this to be recognized, you will have to set static: false.

Additionally, top level of dynamic objects will only respond to changes to their original objects if those keys were present on initialization. This is unlike the behavior of setting the Proxy object directly (or objects nested inside it, which have been converted to Proxies).

const store = { value: 0 }
const proxy = monitor.set('store', store, {static: false}) // declare a dynamic object
monitor.on('store', (path) => console.log(`Updated!`, path))

// Update Existing Key
store.value = 1 // callback!
proxy.value = 2 // callback!

// Update New Key
store.test = 'works?' // no response...
proxy.test = 'works?' // callback!

Multiple Listeners on One Object

Setting the same object multiple times can have unexpected behavior. For example, passing a function by reference to two (parts of the same object) will result in only the first of these functions that are set to have the correct behavior.