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

@jointly/object-loudifier

v2.1.2

Published

This is a library allowing to create reactive objects. Given an object, it will return a new object with the same properties but with the ability to react to changes in the original object.

Downloads

4

Readme

What is it?

This is a library allowing to create reactive objects. Given an object, it will return a new object with the same properties but with the ability to react to changes in the original object.

How do I install it?

You can install it by using the following command:

npm install @jointly/object-loudifier

How to use it?

Just pass any object to the loudify function and it will return a loud object.

import { loudify } from '@jointly/object-loudifier';
const obj = loudify({
  a: {
    b: {
      c: {
        d: 1
      }
    }
  }
});
obj.$on('*', (v) => {
  console.log('* -->', v);
  return;
});
obj.$on('a.*', (v) => {
  console.log('a.* -->', v);
  return;
});
obj.a.b = 1; // This will cause both console.logs to execute.

Tests

You can run the tests by using the following command:

npm test

How does it work?

Just wrap the object in the loudify function.
The function expects just a single parameter, the object you want to make reactive.
You can then watch for changes in the object by using the $on method.
The function expects three parameters:

  • The name of the property you want to watch
  • A callback function.
  • An options parameter for additional configuration. It is explained later in the document as Listener options.

The callback function will be called every time the property changes.
The callback function will be called with the new value of the property as its first parameter.
You can also watch for changes for nested properties by using the dot notation and the wildcards (Explained in the Wildcards section).

Other Info

Wildcards

You can use wildcards to watch for changes in multiple properties.
For example, if you want to watch for changes in the foo and bar properties, you can use the following code:

const obj = loudify({ foo: 1, bar: 2 });
obj.$on('*', (newValue) => {
  console.log(newValue);
});

You can also use wildcards for nested properties.

const obj = loudify({ foo: { bar: 1 } }, { allowNesting: true });
obj.$on('foo.*', (newValue) => {
  console.log(newValue);
});

Listener options

You can pass a third parameter to the $on method, which is an object with the following properties:

  • preventBubbling - A boolean indicating if bubbling should be prevented. Default is false.
  • once - A boolean indicating if the listener should be called only once. Default is false.
obj.$on('foo.bar', (newValue) => {
  console.log(newValue);
}, { preventBubbling: false, once: true });

Bubbling

By default, the $on method will bubble the changes to the parent object.
For example, if you have the following object:

const obj = loudify({ foo: { bar: 1 } }, { allowNesting: true });

And you watch for changes in the foo.bar property and in the foo property, you will get notified in both cases.

obj.$on('foo.bar', (newValue) => {
  console.log(newValue);
});

obj.$on('foo', (newValue) => {
  console.log(newValue);
});

Bubbling priority

The event emission order is the following:

  1. The property listeners are called.
  2. The parent listeners are called.
  3. The wildcard listeners are called using the following sub-order:
    1. The property listeners are called.
    2. The parent listeners are called.

Considering the following example:

const obj = loudify({ foo: { bar: 1 } }, { allowNesting: true });
obj.foo.$on('bar', (newValue) => {
  console.log('foo->bar');
});
obj.$on('*', (newValue) => {
  console.log('*');
});
obj.$on('foo.*', (newValue) => {
  console.log('foo.*');
});
obj.$on('foo.bar', (newValue) => {
  console.log('foo.bar');
});

The following output will be printed:

foo->bar
foo.bar
foo.*
*

Once

The $once method is a shortcut for the $on method with the once option set to true.

Benchmarks

You can run the benchmarks by using the following command:

npm run benchmark

Tested on a MacBook Pro M1 Max (Retina, 16-inch, 2021) with 32GB of RAM.
The results are in milliseconds.
The results are the average of 100000 runs.

| Benchmark | Without loudify | With loudify | Notes | | --------- | --------------- | ------------ | -------------------------------------------------------------- | | 1 | 1.73 | 55.72 | Simple object assignment | | 2 | 1.38 | 144.11 | Object nested assignment | | 3 | 1.51 | 155.05 | Object nested assignment with wildcard | | 4 | 1.57 | 244.63 | Object nested assignment with wildcard and multiple listeners | | 5 | 1.54 | 721.68 | Object assignment with multiple nested properties | | 6 | 1.82 | 997.00 | Object assignment with multiple nested properties and wildcard |

Even if the benchmarks show a big difference with a native object, yet the library is capable of easily handling tens of thousands of changes per second.

In a real-case scenario, reaching milions of changes per second is possible (As in Benchmark #1).