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

als-object-observer

v0.1.0

Published

Triger callback on js object change

Downloads

3

Readme

ObjectObserver

The ObjectObserver class is a built-in class in JavaScript that can be used to add a proxy to an object and its subtree. When changes are made to the object or any of its properties, the proxy will trigger a callback function that can be used to handle the change. This can be useful in situations where you need to monitor changes to an object and take action based on those changes.

Basics

To use ObjectObserver, you must first create an instance of the class and pass in an object to observe, a callback function to run when changes occur, and an optional params object. The params object is an object that can be used to store additional parameters that are needed by the callback function.

function callback({map,action,args=[],result,error=null},params={}) {
   // ...your code for callback
}

let obj = {a: 1, b: {c: 2, d: 3}};
let dobj = new ObjectObserver(obj, callback, params={});

$vars

The dobj.$vars property is a proxy with a subtree of proxies for every object in the obj object. Changes made using $vars trigger the callback function.

dobj.$vars.a = 2; // Triggers callback

Here are example for $vars usage:

const obj = {
    foo: 'bar',
    baz: null,
    some: null,
    qux: {
       a: 1,
       b: [2, 3, { c: 4 },'df'],
       c: {
          d: 'e'
       }
    }
};

let dobj = new ObjectObserver(obj,function(arguments) {
   let {map,action,args,result,error} = arguments
   console.log(arguments)
})
let {$vars,objMap} = dobj

$vars.test = ['hello',{some:'test'},5] 
// {"map":["test"],"action":"set","args":[],"result":["hello",{"some":"test"},5],"error":null}
$vars.qux.b.splice(0,1)
// {"map":["qux","b"],"action":"splice","args":[0,1],"result":[2],"error":null}
delete dobj.$vars.qux
// {"map":["qux"],"action":"delete","args":[],"result":true,"error":null}
delete dobj.$vars.quxd
// {"map":["quxe"],"action":"delete","args":[],"result":false,"error":"Can't delete \"quxe\" because it's not exists"}
$vars.qux.b.some = 'test'
// {"map":["qux","b","some"],"action":"set","args":[],"result":"test","error":null}

callback params

  • map - map in subtree for value that changed.
    • For example obj.some[2].test is ['some',2,'test']
  • action - set,delete, or array method (like push, reverse,...)
  • args - arguments for array method
  • result - result for new value
    • array methods - splice will reture spliced ellement, push - new element
    • delete will return true (if deleted) or false (if not deleted)
  • error - null or string in case for deleting value which not exists
  • params - is an object which
    • for example calback is method of some class, you can pass self (this for method)

objMap

The dobj.objMap property is a Map object that maps objects in the obj object to their corresponding proxies. The dobj.mapKeys property is an object that maps the keys of objects in the obj object to their corresponding parent, key, and parentProxy.

In other words, objMap is a Map object which has objects and arrays from obj subtree as a key and {key,value,map,parent,proxy,revoke} as value.

dobj.objMap.get(obj.b); // Returns the proxy for obj.b
dobj.mapKeys["b.c"]; // Returns { parent: { c: 2, d: 3 }, key: "c", parentProxy: Proxy }

For example (example from code above) :

objMap.get(obj)
/*{
   key: undefined,
   map: [],
   parent: undefined,
   proxy: Proxy(Object) {foo: 'bar', baz: null, some: null, qux: {…}},
   revoke: ƒ ()
}*/
objMap.get(obj.qux.b)
/*{
   key: "b",
   map: (2) ['qux', 'b'],
   parent: {a: 1, b: Array(4), c: {…}},
   proxy: Proxy(Array) {0: 2, 1: 3, 2: {…}, 3: 'df'},
   revoke: ƒ (),
}*/

mapKeys

mapKeys is an object which has joined map for each item in obj as a key and {key,parent,parentProxy} as a value.

Here is the example for obj above:

'baz': {parent: {…}, key: 'baz', parentProxy: Proxy(Object)}
'foo': {parent: {…}, key: 'foo', parentProxy: Proxy(Object)}
'some': {parent: {…}, key: 'some', parentProxy: Proxy(Object)}
'test': {parent: {…}, key: 'test', parentProxy: Proxy(Object)}
'test.0': {parent: Array(3), key: '0', parentProxy: Proxy(Array)}
'test.1': {parent: Array(3), key: '1', parentProxy: Proxy(Array)}
'test.1.some': {parent: {…}, key: 'some', parentProxy: Proxy(Object)}
'test.2': {parent: Array(3), key: '2', parentProxy: Proxy(Array)}