New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

radar.js

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

radar.js

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

  • 0.1.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

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
namereqtype
optionsnobj
Options
namereqtypedefaultdescription
separatornstr":"separates the prefix and method name (prefix:method)
methodsnarr[]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
namereqtype
objectyobj
optionsyobj
Options
namereqtypedefaultdescription
prefixystr""prefixes the event names attached to this object
separatornstr":"overrides the default separator for the object argument's events only
methodsnarr[]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
namereqtype
eventNameystr
handleryfunc

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
namereqtype
eventNameystr
handleryfunc

License

MIT

FAQs

Package last updated on 18 Aug 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc