JSPM

  • Created
  • Published
  • Downloads 2326
  • Score
    100M100P100Q115928F
  • License ISC

Library for aspect-oriented programming with JavaScript using ES6 Proxy

Package Exports

  • to-aop

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (to-aop) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

to-aop

Build Status dependencies Status Coverage Status NPM package version code style: prettier

The to-aop module help you with applying Aspect Oriented Programming to JavaScript. It use under the hood ES Proxy for object same as other similar modules. It allow you hook class without creating new instance as well. It use javascript prototype for that.

More articles about AOP:

  1. https://blog.mgechev.com/2015/07/29/aspect-oriented-programming-javascript-aop-js/
  2. https://hackernoon.com/aspect-oriented-programming-in-javascript-es5-typescript-d751dda576d0
  3. https://kyu.io/sneak-peek-to-javascript-aop/

Installation

npm i to-aop --save

Usage

You have some common class. For example:

// a.js
export default class A {
  constructor(variable) {
    this.variable = variable;
  }

  method() {
    return this.variable;
  }

  notHookedMethod() {
    return 'not hook';
  }    
}

Applying AOP to class

import { aop, hookName, createHook } from 'to-aop';
import A from './a';

const classHook = createHook(
  hookName.afterMethod,
  /^(method)$/,
  ({ target, object, property, context, args, payload }) => {
    console.log(
      `Instance of ${target.name} call "${property}"
with arguments ${args && args.length ? args : '[]'}
and return value is "${payload}".`
    );
  }
);

aop(A, classHook); // bind hook to class
const a = new A('my hook');

a.method(); // Instance of A call "method" with arguments [] and return value is "my hook".
a.notHookedClassMethod(); // not hook

Applying AOP to instance or object

import { aop, hookName, createHook } from 'to-aop';
import A from './a';

const instanceHook = createHook(
  hookName.afterMethod,
  /^(method)$/,
  ({ target, object, property, context, args, payload }) => {
    console.log(
      `Instance of ${object.constructor.name} call "${property}"
with arguments ${args && args.length ? args : '[]'}
and return value is "${payload}".`
    );
  }
);

const a = new A('my hook');
const hookedInstance = aop(a, instanceHook); // bind hook to instance

hookedInstance.method(); // "Instance of A call "method" with arguments [] and return value is "my hook".
hookedInstance.notHookedClassMethod(); // not hook

Hooks API

We implemented base set of hooks which you can use for AOP.

  1. beforeMethod
  2. afterMethod
  3. aroundMethod
  4. beforeGetter
  5. afterGetter
  6. aroundGetter
  7. beforeSetter
  8. afterSetter
  9. aroundSetter