New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

observingproxy

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

observingproxy

A proxy for observing object state changes.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

npm npm

Observing proxy

A proxy for observing object state changes.

This module is based on the Object.defineProperty method and serves as an interface to the user provided object, notifying subscribers about the user object changes.

var obj={person:'Eddie',age:22};

_o.onUpdate(obj,{
  age:function(value){
    if(value>this.oldValue)
      console.log('Happy birthday, Peter!')
  },
  person:function(value){
    console.log(this.oldValue+' is now '+value);
  }
});

_o(obj).person='Peter';
//> Eddie is now Peter
_o(obj).age++;
//> Happy birthday, Peter!

Install

npm install observingproxy

or

bower install observingproxy

Advanced usage (implemented with Object.observe in mind)

var obj={person:'Peter',age:22};

_o.observe(obj,function(changes){
  for(var i=0;i<changes.length;i++)
    if(changes[i].name==='age')
      if(changes[i].object.age>changes[i].oldValue)
        console.log('Happy birthday, Peter!')
});

_o(obj).age++;
//> Happy birthday, Peter!

Note: it works only with property update. add or delete operations are not supported.

With arrays

var arr=[1,2,3];

_o.observe(arr,function(changes){
  for(var i=0;i<changes.length;i++)
    if(changes[i].type==='splice')
      console.log('Array changed')
});

_o(arr).push(4);
//> Array changed

Error handling and destructor

var observer=_o.onUpdate(null,'name',function(value){
  //...
});

// Throw observation exception here
try{
  observer.report();
}
catch(e){throw e}
// Observing proxy error: target is null

// Destroy your observer
observer.destroy();

Deferred observer notification

While proxy applies property modification immediately, it sends the observer notification call to the end of execution thread using setTimeout method as a wrapper.

So now, if you do

_o.observe(obj,function(changes){
  // iterate changes
});

_o(obj).p1=1;
_o(obj).p2=2;
_o(obj).p3=3;

observer is notified only once with changes argument containing latest modifications. It makes observer a load sustainable to an observed object being intensively modified.

Keywords

FAQs

Package last updated on 21 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc