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

object-subscriptions

v1.0.4

Published

Nested object with mutations and subscribers

Readme

object-subscriptions

Nested object with mutations and subscribers

Examples

Calculate and Subscribe

import {NestedObjectWithSubscriptions} from "object-subscriptions";

const obj = new NestedObjectWithSubscriptions();

obj.set("aa.bb", 123);
obj.set("cc.dd", 123);

obj.calculate(["aa.bb", "cc.dd"], (a, b) => {
    return a + b;
}, "ee.ff")

obj.subscribe("ee.ff", (value) => {
    console.log("VAL!", value);
});

Custom Separator and Traversal

import {NestedObjectWithSubscriptions} from "object-subscriptions";

const obj = new NestedObjectWithSubscriptions({}, {separator: '/'});

obj.set("aa/bb", 123);
obj.set("cc/dd", 123);

obj.calculate(["aa/bb", "cc/dd"], (a, b) => {
    return a + b;
}, "ee/ff")

obj.subscribe("ee/ff", (value) => {
    console.log("VAL!", value);
});


for(let [pathParts, value] of obj.entries({pathParts: true})) {
    console.log(pathParts, value);
}

Defer execution

import {NestedObjectWithSubscriptions} from "object-subscriptions";

const obj = new NestedObjectWithSubscriptions({}, {separator: '/'});

obj.set("aa/bb", 123);
obj.set("cc/dd", 123);

obj.calculate(["aa/bb", "cc/dd"], (a, b) => {
    return a + b;
}, "ee/ff", {defer: true})

obj.subscribe("ee/ff", (value) => {
    console.log("VAL!", value);
});

for(let [pathParts, value] of obj.entries({pathParts: true})) {
    console.log(pathParts, value);
}

Debounced calculation

import {NestedObjectWithSubscriptions} from "object-subscriptions";

const obj = new NestedObjectWithSubscriptions({}, {separator: '/'});

obj.set("aa/bb", 123);
obj.set("cc/dd", 123);

obj.calculate(["aa/bb", "cc/dd"], (a, b) => {
    return a + b;
}, "ee/ff", {debounce: 100, immediate: false})

obj.subscribe("ee/ff", (value) => {
    console.log("VAL!", value);
});

for(let [pathParts, value] of obj.entries({pathParts: true})) {
    console.log(pathParts, value);
}

Take a slice

import {NestedObjectWithSubscriptions} from "object-subscriptions";

const obj = new NestedObjectWithSubscriptions({}, {separator: '/'});

obj.set("aa/bb", 123);

const child = obj.child("aa");

console.log("VAL!", child.get([]));

Two-way sync

const obj1 = new NestedObjectWithSubscriptions({}, {separator: '/'});
const obj2 = new NestedObjectWithSubscriptions({}, {separator: '/'});

const obj1Tag = Symbol("obj1");
const obj2Tag = Symbol("obj2");

const obj1PathParts = ['sync'];
const obj2PathParts = ['aa', 'bb'];

// Sync from obj1 to obj2
obj1.subscribe(obj1PathParts, (value, metadata) => {
// Only sync if the change didn't originate from obj2
if (metadata.syncTag !== obj2Tag) {
    const targetPathParts = [...obj2PathParts, ...metadata.mutatedPathParts.slice(obj1PathParts.length)];

        if (metadata.mutation === NestedObjectWithSubscriptions.MUTATIONS.SET) {
            // For SET operations, use the specific path that was mutated
            obj2.set(targetPathParts, metadata.mutatedValue, {
                syncTag: obj1Tag,
                tag: metadata.options?.tag
            });
        }
        else if (metadata.mutation === NestedObjectWithSubscriptions.MUTATIONS.DELETE) {
            // For DELETE operations, delete the specific path
            obj2.delete(targetPathParts, {
                syncTag: obj1Tag,
                tag: metadata.options?.tag
            });
        }
    }
}, {fetch: false});

// Sync from obj2 to obj1
obj2.subscribe(obj2PathParts, (value, metadata) => {
// Only sync if the change didn't originate from obj1
if (metadata.syncTag !== obj1Tag) {
const targetPathParts = [...obj1PathParts, ...metadata.mutatedPathParts.slice(obj2PathParts.length)];

        if (metadata.mutation === NestedObjectWithSubscriptions.MUTATIONS.SET) {
            // For SET operations, use the specific path that was mutated
            obj1.set(targetPathParts, metadata.mutatedValue, {
                syncTag: obj2Tag,
                tag: metadata.options?.tag
            });
        }
        else if (metadata.mutation === NestedObjectWithSubscriptions.MUTATIONS.DELETE) {
            // For DELETE operations, delete the specific path
            obj1.delete(targetPathParts, {
                syncTag: obj2Tag,
                tag: metadata.options?.tag
            });
        }
    }
}, {fetch: false});



// Test the sync
obj1.set(['sync', 'xx'], 123); // This will sync to obj2
obj2.set(['aa', 'bb', 'cc'], 456); // This will sync to obj1

obj2.delete(['aa', 'bb', 'cc']); // This will sync to obj1