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

panoptic

v0.0.9

Published

see all, do all. simple object keypath observers.

Downloads

36

Readme

#panoptic simple object keypath observers.

Build Status Coverage Status

##Installation ###node Add to your dependencies in package.json:

  ...
  "dependencies": {
    "panoptic": "~0.0.9",
    ...
  },
  ...

or install directly:

npm install --save panoptic

###browser Include either of the bundles in the dist/ folder of the repo, depending on whether or not you'd like it minified.

##Usage Import the panoptic module:

var panoptic = require('panoptic');

then call with an object you want to observe:

var data = {
  name: 'David Rekow',
  age: 25
};

var observable = panoptic(data);

###getting

value = observable.get('key');
value = observable.key;

Retrieve the value for a particular key via getter or object property access. If you're not sure whether the key exists before accessing, use get() to avoid a TypeError when retrieving deeply nested values:

value = observable.get('a.b.c');  // if a or b doesn't exist, returns null
value = observable.a.b.c;         // if a or b doesn't exist, throws TypeError

###setting

observable.set('key', value);
observable.key = value;

Set the value of a key in the same way. If setting a deeply nested key and you don't know whether intermediate objects exist, use set() and they will be created (think mkdir -p):

observable.set('a.b.c', value);   // if a or b doesn't exist they will be created
observable.a.b.c = value;         // if a or b doesn't exist, throws TypeError

To set multiple keys at once, simply pass an object to set():

observable.set({  // as a flat namespaced object
  'a': true,
  'b.c': 12
});
observable.set({  // as a fully-structured object - will be set as diff
  a: true,
  b: {
    c: 12
  }
});

Setting via object property syntax only works if the key has already been seen - if you're adding a new key, use set() to ensure the observation chain is set up. ###removing

observable.set('a.b', null);
observable.a.b = null;

To remove a key from an observable object simply set it to null. If the key currently points to a nested object, watchers for any existing nested properties will be invoked before removing the key:

observable = panoptic({
  a: {
    b: {
      c: 1
    }
  }
});

observable.watch('a.b.c', function (value) {
  console.log('"a.b.c" value: ' + value);
});
observable.watch('a.b.d', function (value) {
  console.log('"a.b.d" value: ' + value);
});
observable.watch('a.b', function (value) {
  console.log('"a.b" value: ' + value);
});

observable.a.b = null;

outputs

"a.b.c" value: null
"a.b" value: null

Because the key a.b.d had not yet been set, its watcher did not fire. ###replacing

observable.replace({key: 'newValue'});

Calling replace() replaces the current observed data entirely with the passed data, triggering watchers for removed, modified and added keys. This method uses remove() behind the scenes, so only watchers for existing properties will fire upon removal or modification. Any already-registered watchers for properties being added will be invoked. ###watching

observable.watch('a.b.c', function (newValue) {
  var value = this.get('c');  // 'this' is the object actually holding the value
  value === newValue;         // watcher receives the new value after it's set
});

Nested watchers will trigger parent watchers after triggering their own:

observable.watch('a', function () {
  console.log('reached "a" watcher');
});

observable.watch('a.b', function () {
  console.log('reached "a.b" watcher');
});

observable.set('a.b', 5);

outputs

reached "a.b" watcher
reached "a" watcher

###unwatching

var watcher = function (newValue) { ... };

observable.watch('key', watcher);
observable.unwatch('key', watcher);  // if watcher is passed, only it gets removed
observable.unwatch('key');           // if no watcher is passed, all get removed

That's it!

##FAQ ###why panoptic? it's super lightweight, matches whatever data access syntax you're currently using, and uses a simple but powerful sparse-tree data structure to avoid the overhead of propagation and digest cycles when dispatching change events.

###why panoptic? pan (all) + optic (seeing), derived from panopticon (the non-terrible connotations!)

Find a bug? Please file an issue!