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

aspectjs

v2.0.4

Published

A simple AOP implementation for Node.

Downloads

167

Readme

A simple AOP implementation for Node. It works either with standalone functions or with object methods. It can be used on the client-side with Browserify too.

Build Status npm version

Example

aspectjs.after(myGreatObject, "someMethod").add(myGreatAdvice, "someOtherMethod");

// Thereafter whenever myGreatObject.someMethod is called, 
// myGreatAdvice.someOtherMethod will be called automatically afterward.

API Documentation

All of the following methods of aspectjs return an "advice object" that contains an add() method, which applies the advice setup in after(), before(), or around().

Methods of aspectjs

before(joinpoint, [methodname])

Will add advice before the specified join point, once add() is called. The join point can be either a function or an object followed by a method name. Returns the advice object.

after(joinpoint, [methodname])

Will add the advice after the specified join point, once add() is called.
The join point can be either a function or an object followed by a method name.
Returns the he advice object.

around(joinpoint, [methodname])

Adds the advice round the specified join point, once add() is called.
The join point can be either a function or an object followed by a method name.
Returns the advice object.

These method requires an advice function/method that takes an Invocation object. Within the advice body, invocation.proceed() should be called where the joinpoint should occur. The advice is applied only after add() is called on the returned advice object.


// Example advice object for around advice
adviser = {adviseFunction: function(invocation){
    // Some code goes here.
    invocation.proceed();   // Calls the original function.
    // Some more code goes here.
}};

Methods of the advice object

add([thisArg,] function|string)

Returns the new function that wraps the original function passed to before(), after(), or around().

Usage

Standalone functions

Both the advice and joinpoints can be standalone functions:

aspectjs.before(joinpoint).add(advice).

Or the advice can be an object method, while the joinpoint is standalone:

aspectjs..after(joinpoint).add(advice, 'methodname')

Object methods

Both the advice and joinpoint can be object methods:

aspectjs.before(joinpointObj, 'methodname').add(adviceObj, 'methodname')

Examples

Before advice

const before = require('aspectjs').before;
let addAdvice = require("aspectjs").addAdvice;

let advised, adviser, result;
advised = {
   add: function(increment){this.left += increment; }, 
   id: 'test', 
   left: 32, 
   top: 43
};
adviser = {
   override: function(increment){ advised.left = increment; }
};

before(advised, "add").add(adviser, "override");
advised.add(2);  // Should equal 4.        

Around advice

const around = require('aspectjs').around;
let advised, adviser, result;
advised = {
   add: function(increment){
       this.left += increment; 
   }, 
   id: 'test', 
   left: 32, 
   top: 43
};

// The advice function/method should take an Invocation object as input.
// Then invocation.proceed() should be called where the joinpoint occurs.
adviser = {
   override: function(invocation){
       advised.left += 5; // 37
       invocation.proceed(); // 39
       advised.left -= 19;
   }
};

around(advised, "add").add(adviser, "override");

Links

  • NPM: https://www.npmjs.com/package/aspectjs/