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

async-watch

v1.1.29

Published

[![Build Status](https://travis-ci.org/wiresjs/async-watch.svg?branch=master)](https://travis-ci.org/wiresjs/async-watch) [![IHateReact](https://badges.gitter.im/owner/repo.png)](https://gitter.im/I-Hate-React/Lobby)

Downloads

62

Readme

async-watch

Build Status IHateReact

AsyncWatch is a small library for watching javascript/node.js objects. It uses Object.defineProperty which makes it compatible with most browsers. Any changes happening within present tick will be called on the next available one.

Diagramm

Features

  • Asynchronous execution (Changes are triggered on requestAnimationFrame in browser and on process.nextTick in Node)
  • Being Asynchronous means we don’t have to worry about the execution flow, details below
  • Works in Node and Browser
  • No Dirty Checking or Object.observe
  • Using Object.defineProperty() makes it compatible with all modern browser down to IE 9
  • Order of watchers is preserved
  • Deep watch properties
  • Ability to inspect change type for Arrays (init,splice, push)
  • Computed Properties
  • Get old and new values in the change callback
  • Can watch any type of Object for example Watching DOM Node properties and attributes
  • Restoring watchers after objects are destroyed
  • Can be used as the back-bone to create a custom efficient two-way binding system
  • Comprehensive test coverage

Install

npm install async-watch --save

Examples

var AsyncWatch = require('async-watch').AsyncWatch;
var obj = {}; // creating an empty object
AsyncWatch(obj, 'a.b.c', function(value){
    console.log('set', value);
});
// You can pass an array as well
//AsyncWatch(obj, ['a', 'b', 'c'])

As you can see here, we start with an empty object. AsyncWatch will set a watcher on property "a", which knows about its descendands

 obj.a = {
  b : {
   c : 1
  }
 };
 obj.a.b.c = 2;
 obj.a.b.c = 3;
 setTimeout(function(){
    obj.a.b.c = 4;
 },0)

The output will look like this:

set 3
set 4

Callback is called on "transaction commit". Each transaction is a requestAnimationFrame tick. Surely, initial transaction loop happens when first value is changed.

Worth mentioning: Transactions happen on demand, without "perpetual" loop or/and any other dirty checkers.

Destroying a watcher

Destroys a watcher (does not destroy its descendants or similar watchers)

var watcher = AsyncWatch(obj, 'a', function(value) {
});
watcher.destroy();

Watching many objects

watchAll is not implemented yet, however subscriptions are introduced. Each watcher returns a "transaction" / watcher.

var obj = {
  a : 1,
  b : 2
}
var watcher1 = AsyncWatch(obj, 'a', function(value) {
});
var watcher2 = AsyncWatch(obj, 'b', function(value) {
});
var subscription = AsyncWatch.subscribe([watcher1, watcher2], function(changes){
   console.log(changes)
})

Subscribers' callback guarantees all watchers to be in sync.

Outputs:

{a : 1, b: 2 }

Unfortunately, subscriptions won't clean up themselves, you need to do it manually.

subscription.unsubscribe();

If you want to unsubscribe and destroy corresponding watchers:

subscription.destroy();

Computed properties

You can define a computed property.

var obj = {
   firstName : "Bob",
   lastName : "Marley"
}

AsyncComputed(obj, 'fullName', ['firstName', 'lastName'],
	self =>`Name is ${self.firstName} ${self.lastName}`);


obj.lastName = "Foo";
obj.lastName = "Foo1";
AsyncWatch(obj, 'fullName', (value) => {
   // Name is Bob Foo1
});

Watching arrays

var obj = {
   users : [{name : "John"}, {name : "Bob"}]
}


AsyncWatchArray(obj, 'users', function(array,events){
   console.log(events);
});

Triggers events: "init" "push" "splice"

To have better understanding check these test/sync_test.js

Contribution

Contribution is greatly appreciated! Please, run tests before submitting a pull request.