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

propem

v1.0.2

Published

An event emitter that emit events whenever an object property is accessed

Downloads

1

Readme

PropertyEmitter

An event emitter that emit events whenever an object property is accessed

npm version Build Status Coverage Status Dependency Status

About

This library includes an EventEmitter implementation that you can extend in your own code if you want to react to property changes or function calls on an object.

The library allows you to register listeners that will be called with information about what was called with which parameters and what the previous value was.

Example

This is an example of how to react to a property being set on your own object.

const {PropertyEmitter} = require('propem');

class Example extends PropertyEmitter {
  constructor() {
    super();
  }
}

let example = new Example();
example.emitter.on('set', console.log);

emitter.myprop = "a value"
// => emitter => console.log()
//    {object: !ref, name: "myprop", current: "a value" }

Installation

In your npm project directory run

npm i --save propem

How does it work?

This tool makes use of the new Proxy class introduced with ES2015 by wrapping the target object and returning the proxy. All calls and access are forwarded to the original object while triggering events if any listeners are present.

This shouldn't interfere in most cases and the object should just behave as usual. However, due to the custom code running around getting, setting and deleting properties there's always a chance that special cases exist where behavior might change. Understanding that you're really dealing with 2 objects, one wrapping the other might help solve some of those issues.

And as always if you run into any such issues, I'd appreciate some feedback ideally as a Bug Report. It might be valuable information for improving the quality of this library.

API

The PropertyEmitter extends all existing EventEmitter methods and properties. As such be careful not to overwrite any existing structures.

PropertyEmitter.constructor(EventEmitter=null, EmitterName='emitter')

Allows you to set a custom event emitter library. Defaults to the built in EventEmitter. In any case don't forget to call the super() method in your own constructor, otherwise you're missing initialization even if you go with default values.

The constructor also allows you to specify the property under which to find the emitter. Defaults to 'emitter'.

watchProperties(object, EventEmitter=null)

In case you need to construct an emitter but can't add an extra property and want the original object to stay clean you can use this function to get a wrapped object back that will now emit events on the passed in emitter, without having a reference on the object itself.

const {watchProperties} = require('propem');
let emitter = new require('events').EventEmitter();
let watched = watchProperties(obj, emitter);

emitter.on('set', console.log);

watched.myprop = "a value"
// => emitter => console.log()
//    {object: !ref, name: "myprop", current: "a value" }

PropertyEmitter.emitter.on(event, listener)

The emitter.on() method has a few built in events that get fired whenever a property is accessed, other than that it is an instance of the passed in EventEmitter class from the constructor so you can make use of all methods including "on()".

This is the list of built in events:

  • set: Is fired whenever a property is assigned
  • get: Is fired whenever a property is accessed
  • delete: Is fired when a property is removed
  • in: Fires if an in operator has been used on the object
  • any: Is fired when either a get, set or function is triggered

You can register listeners to these events that react whenever the event get fired. The arguments passed to the listeners are encapsulated in an object:

{
  type: "property",
  name: "myprop",
  event: "function",
  current: "current value",
  previous: "previous value",
  result: "function response"
}

Tests

To run the test you must check out the project code, install all dependencies via npm install. Once the project is set up you can get coverage reports and test results simply by running:

npm test

Bugs and Feature Requests

I try to find all the bugs and have tests to cover all cases, but since I'm working on this project alone, it's easy to miss something. Also I'm trying to think of new features to implement, but most of the time I add new features because someone asked me for it. So please report any bugs or feature request to [email protected] or file an issue directly on Github. Thanks!