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

radar.js

v0.1.7

Published

A simple wrapper for objects that emits events *around* specified methods. It effectively sandwiches object methods between two event emitters.

Downloads

12

Readme

Radar.js

A simple wrapper for objects that emits events around specified methods. It effectively sandwiches object methods between two event emitters.

Build Status NPM version

Installation

npm install radar.js

Quick Start

var Radar = require("./lib")
  , radar = new Radar({ methods: ["leave"] });

var bear = radar.wrap({
  name: "Bear",
  leave: function () {
    console.log(this.name + " is leaving...");
  },
  stay: function () {
    console.log(this.name + " is staying...");
  }
}, {
  prefix:  "bear",
  methods: ["stay"]
});

radar.before("bear:leave", function () {
  console.log(this.name + " is thinking about leaving...");
});

radar.after("bear:leave", function () {
  console.log(this.name + " left.");
});

radar.before("bear:stay", function () {
  console.log(this.name + " is thinking about staying...");
});

radar.after("bear:stay", function () {
  console.log(this.name + " stayed.");
});

bear.leave();
bear.stay();

// Bear is thinking about leaving...
// Bear is leaving...
// Bear is thinking about staying...
// Bear is staying...
// Bear left.
// Bear stayed.

Sync vs. Async

This library works with both synchronous and asynchronous methods, but it does require an adherence to a certain asynchronous convention -- callback parameters MUST be the last parameter supplied to a method. For example...

var okay = radar.wrap({
  asyncMethod: function (name, done) {
    done(null, name.toUpperCase());
  }
}, { prefix: "okay" });

var notOkay = radar.wrap({
  asyncMethod: function (done, name) {
    done(null, name.toUpperCase());
  }
}, { prefix: "notOkay" });

API

Radar(options) [constructor]

Create a new Radar object with some optional configuration.

Usage

var radar = new Radar();
var radar = new Radar({ methods: ["leave"] });

Arguments

name|req|type ----|---|---- options|n|obj

Options

name|req|type|default|description ----|---|----|-------|----------- separator|n|str|":"|separates the prefix and method name (prefix:method) methods|n|arr|[]|list of methods to wrap, if they exist

Radar.wrap(object, options)

Wraps designated methods within an object with before and after event emitters.

Usage

var bear = radar.wrap({
  name:  "Bear",
  leave: function () {
    console.log(this.name + " is leaving...");
  }
}, { prefix: "bear" });

var fox = radar.wrap({
  name: "Fox",
  stay: function () {
    console.log(this.name + " is staying...");
  }
}, {
  prefix:    "fox",
  separator: "/"
  methods:   ["stay"]
})

Arguments

name|req|type ----|---|---- object|y|obj options|y|obj

Options

name|req|type|default|description ----|---|----|-------|----------- prefix|y|str|""|prefixes the event names attached to this object separator|n|str|":"|overrides the default separator for the object argument's events only methods|n|arr|[]|list of additional methods to wrap for the object argument only

Radar.before(eventName, handler)

Trigger handler before the wrapped method is called on the object. The context (this) within this method is the object which the event was registered against.

Usage

radar.before("bear:leave", function () {
  console.log(this.name + " is about to leave..."); // Bear is about to leave...
});

Arguments

name|req|type ----|---|---- eventName|y|str handler|y|func

Radar.after(eventName, handler)

Trigger handler after the wrapped method is called on the object. The context (this) within this method is the object which the event was registered against.

Usage

radar.after("bear:leave", function () {
  console.log(this.name + " left."); // Bear left.
});

Arguments

name|req|type ----|---|---- eventName|y|str handler|y|func

License

MIT