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

deep-proxy-observe

v1.0.6

Published

Zero dependency implementation of deep observable objects

Downloads

14

Readme

deep-proxy-observe

Zero dependency implementation of deep observable objects

This module implements the Observable pattern on deeply nested objects using the Proxy API.

The module exposes a single method that creates the deeply observable object.

Disclaimer

I created this module for a project at work. It is a solution created to get around a particular problem in a large codebase full of legacy code and outdated technologies. Some might find it useful, but I cannot guarantee proper maintenance of this project, or that this module will work in any given environment. The intended purpose of this module is highly specific, so it might break your code if you use it on some edge case I didn't take into account.

I may introduce breaking changes to this module without any prior announcement.

I may close issues and reject pull requests without an objective reason.

Use at your own risk.

deepObserve function

Creates a deep/nested Proxy (observable) from a given object. A deep Proxy listens to any changes within a nested object by turning every object in the nested tree into a Proxy. Any changes to objects created within the tree during its lifetime will also trigger the callback.

The Proxy created by this module only listens to changes (set and delete) and executes the callback after the action, therefore it becomes an observable. Modifying the object in the callback function can be implemented, but it is not the intended use case.

Parameters

  • object: object the object to create an observable from

  • callback: function the function to call when the object is changed

    Parameters

    • target: object the object in the tree, a property of which is being modified
    • path: string[] the path of the modified property (for example, the property accessed by observableObject.a.b.c will have the path of ["a", "b", "c"])
    • value: any the new value of the property. In case the property is being deleted, this argument will not be passed, so it is possible to check for it.

    Returns

    any the result of the callback function does not matter.

Returns

Proxy the observable Proxy with the callback attached. Otherwise behaves like a normal object.

Usage

Log any modification of the object to the console:

import { deepObserve } from 'deep-proxy-observe'

const observable = deepObserve(
  {},
  (t, p, v) =>
    console.log(
      `property ${p.join('.')} modified to ${JSON.stringify(v)}`
    )
)

observable.p = { a: { b: { c: 'd' } } }
// => property p modified to {"a":{"b":{"c":"d"}}}

observable.p.a.b.c = 'f'
// => property p.a.b.c modified to "f"