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

universal-observer

v1.0.1

Published

Observe deep changes in any object, array, map, set, date, ...

Downloads

8

Readme

universal-observer

Build Status Coverage Status License: MIT

universal-observer observes changes in objects, arrays, maps, sets, dates, etc.
The implementation is based on es6 proxies, so it must be used in an environnement supporting them.

Features

  • synchronous delivery
  • deep observation
  • circular references handling
  • different delivery modes (single/bulk)
  • lazy construction of proxies (proxies on nested properties are constructed only when needed, leading to a performance increase in some cases)

Installation

npm install universal-observer

Example

import {observe} from 'universal-observer';

const obj = {a: 1, b: [1, 2]};
const observed = observe(obj, change => console.log(change));

observed.a = 2; // console: {object: obj, type: 'update', name: 'a', oldValue: 1}
observed.b.push(3); // console: {object: obj.b, type: 'add', name: '2', oldValue: undefined}
observed.c = 3; // console: {object: obj, type: 'add', name: 'c', oldValue: undefined}

API

import {observe, unobserve, pause, resume} from 'universal-observer';

observe(object, callback, options); // calls <callback> when a change is observed on <object>
unobserve(object); // removes the observation trap
pause(object); // pauses the observation
resume(object); // resumes a paused observation

The change object

When a change is observed, the specified callback is called with one argument, the change object, which has the following properties:

  • object: the direct object being modified
  • type: can be 'update, 'add', 'delete'
  • name: the name of the property being changed
  • oldValue: the value before the change

For Date objects, name is the name of the mutator method, e. g. : setYear, setDate, ...

Options

Default options are :

const observed = observe(obj, callback, {
  deliveryMode: 'singleUpdate',
  reportLength: false
});

deliveryMode

accepted values: 'singleUpdate'(default), 'bulk', 'singleOperation'

Specifies the delivery mode, i.e. when the callback is called and what argument is passed to the callback. This is useful for mutator functions like sort, reverse, ... which triggers multiple changes at once.

DeliveryMode may be:

  • singleUpdate: each individual change triggers the callback
  • bulk: groups the changes in an array. The array is passed instead of a single change object, even if there is only one change.
  • singleOperation: multiple changes are merged into one change object. The type property is the name of the mutator (sort, reverse, ...). This option is faster than the bulk option.

Example :

const observed = observe([1, 2], callback, options);
observed.reverse();

// deliveryMode = singleUpdate : reverse triggers 2 calls with one change object each time
// deliveryMode = bulk : reverse triggers 1 call with an array of 2 change object
// deliveryMode = singleOperation : reverse triggers 1 call with 1 change object whose type is 'reverse'

reportLength

accepted values: false (default) or true

When a change modifies the length of an array, observation also reports the modification of the length property.

const observed = observe([], callback, {reportLength: true});
observed.push(1);

// callback is called 2 times with the following parameters:
// 1) {object: [1], type: 'add', name: '0', oldValue: undefined}
// 2) {object: [1], type: 'update', name: 'length', oldValue: 0}
});