Last active
March 7, 2016 05:56
-
-
Save otakustay/ec05b002c3127af861a1 to your computer and use it in GitHub Desktop.
decorator as advice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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