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-observer

v1.1.7

Published

Tiny Object Observe library, to deep watch and track changes in Objects and Arrays.

Downloads

142

Readme

logo

Deep Observer

NoDependencies Browser Node

Tiny Object Observe library ( < 100bytes gziped), to deep watch and track changes in Objects and Arrays. The provided callback to the Constuctor is executed each time a change is deteced, recieving a complete set of data relative to the detected change.

  • Deep Observation ( configurable Depth )
  • Optional construction observe
  • Compatible with all JS primitive types
  • No dependencies
  • Wide platform support :
    • Node
    • Chrome 49
    • Firefox 34
    • Edge 14
    • Opera 36
    • Safari 10

Syntax

1. Constructor

When at least two arguments are passed to Observer() , it behaves as a Constructor :

new Observer( object , callback [, config] )

  • object: Object to observe
  • callback : Function to be invoked on object changes
  • config : (optional) Object
    • id : Unique string to use as identifier to the Observable. (if not provided is generated automatically)
    • observeConstruction : Boolean. If true callback will be executed also in construction stage. (default: false)
    • depth : Integer. Sets the observing depth limit. When set to 0, no limit is applied (default : 0 )
    • ignoreSameValueReassign : Boolean. By default callback is only executed when the assigned value differs from the previous one. When this option is set to false, callback function is executed always ( default : true )

Returns : an Observable (Proxy)

Note : deep-observer DOES NOT perform changes in the original provided Object. The observation is performed in the Observable returned Object.

2. Getter

When only a String is provided to Observer() it behaves as a getter :

Observer( id )

  • ìd: String provided previously in the constructor config object

Returns : the matching Observable (Proxy) or undefined

3. Callback function

The function called each time changes are made, will receive an event object containng the following properties :

  • action : String containing one of the following values : add|update|delete
  • object: Affected property parent's Object
  • name: Name of the modified property (or array index)
  • oldValue : Value of the property before the change
  • keypath : String representing the internal path to the affected property.

Basic usage example

Provide to the constructor an object and a callback, and perform a change on the object, to see how the callback is triggered:

   // create an observable object
   const myObserved = new Observer( { a : 12 } , e=>console.log('changed!' , e) );
   // perform a modification
   myObserved.a = 14; 
   // console outputs : 'changed!' { action:'update', oldValue:12, object:{a:14}, name:'a' }

Advanced usage example

An example of new Observer using all the configuration parameters

   // create an observable object
   const myObserved = new Observer( 
       { a : 12 } ,   // object to abserve
       e=>console.log('changed!' , e) ,  // callback
       {
           id : 'observed_1', // observable internal id  
           depth : 5, // observe maximum 6 levels of depth
           observeConstruction : true , // execute callback on construction
           ignoreSameValueReassign : false // call callback always
       }
   );
   // because observeConstruction=true, callback fuction is executed...
   // console outputs : 'changed!' { action:'add', keypath : 'observed_1.a.' , oldValue:undefined, object:{a:12}, name:'a' }
   // perform a modification...
   myObserved.a = 14; 
   // console outputs : 'changed!' { action:'update', keypath : 'observed_1.a.' , oldValue:12, object:{a:14}, name:'a' }
   // retrieve the observable...
   const sameObserved = Observer('observed_1');
   console.log( myObserved === sameObserved );
   // console outputs : true

Package distribution :

In browser enviroment you can include this library using the jsdelivr CDN ...

<script src='https://cdn.jsdelivr.net/gh/colxi/deep-observer@latest/src/deep-observer.min.js'></script>

If you are in the NodeJs enviroment, can install the package via:

$ npm install deep-observer --save

Licence

GPL 3