Skip to content

Instantly share code, notes, and snippets.

@otakustay
Last active March 7, 2016 05:56
Show Gist options
  • Save otakustay/ec05b002c3127af861a1 to your computer and use it in GitHub Desktop.
Save otakustay/ec05b002c3127af861a1 to your computer and use it in GitHub Desktop.
decorator as advice
import Service from './Service';
import {log} from './decorator';
// May use https://github.com/jayphelps/core-decorators.js
import {deprecate, throttle} from 'core-decorators';
import {forClass} from 'aop';
let MyService = forClass(
Service,
{
aspects: [
{
matches: {regex: /^fetch[A-Z]/},
advices: [throttle(250), log] // Throggle first, then log every invocation
},
// Deprecate all old getXxx methods
{
matches: {regex: /^get[A-Z]/},
advice: [deprecated]
}
]
}
);
let service = new MyService();
service.fetchUserAccount();
// ...
export function log(target, key, descriptor) {
let method = descriptor.value;
descriptor.value = function (...args) {
console.log('calling: ', key, args);
try {
let returnValue = method.apply(this, args);
console.log('returning: ', key, returnValue);
return returnValue;
}
catch (ex) {
console.log('throwing: ', key, ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment