Right now there is a single option, mutate, intended to control the following:
- Whether or not before advice can mutate arguments.
- Whether or not after advice can mutate return values.
For around advice, this option applies to both before and after advice.
A perhaps unintended side effect of the implementation is that after advice receives the original arguments when mutate is false, and the target function return value when mutate is true. This means that after advice that simply wants to observe the return value needs to set mutate to true, which makes the code read like the observer is actually mutating.
var observeResult = function (res) {
console.log('result %d', res);
};
var o = {
target: function (a, b) {
return a + b;
}
};
advisable.sync.call(o);
// `mutate` must be true for after advice to receive result, despite no intent to mutate.
o.afterSync('target', observeResult, { mutate: true });
It would probably be better to create a convention where after advice always receives all arguments as well as the target function return value as the last argument, e.g:
// after advice called with target function arguments followed by result
var observeResult = function (a, b, res) {
console.log('result %d', res);
};
var o = {
target: function (a, b) {
return a + b;
}
};
advisable.sync.call(o);
// The after advice does not intend to mutate, and it does not need the mutate option
o.afterSync('target', observeResult);
Right now there is a single option,
mutate, intended to control the following:For around advice, this option applies to both before and after advice.
A perhaps unintended side effect of the implementation is that after advice receives the original arguments when
mutateis false, and the target function return value whenmutateis true. This means that after advice that simply wants to observe the return value needs to setmutateto true, which makes the code read like the observer is actually mutating.It would probably be better to create a convention where after advice always receives all arguments as well as the target function return value as the last argument, e.g: