counter() returns an instance of an object, which can be used as an AOP advice to track how many times a method (or methods) was called, and how many times it produced an error (thrown an exception based on Error).
This advice is used mainly to profile and to debug methods.
It is defined in dcl/advices/counter.js.
Description
The value of this module is a factory function that returns an object used to provide an AOP advice to profile/debug methods. This is an API of that object:
// our object:varA=dcl(null,{declaredClass:"A",showThis:function(){...},showThat:function(){...},hide:function(){...}});// our countersvarcountShows=counter();varcountHides=counter();// instrumented version:varInstrumentedA=dcl(A,{declaredClass:"InstrumentedA",showThis:dcl.advise(countShows.advice()),showThat:dcl.advise(countShows.advice()),hide:dcl.advise(countHides.advice())});// as you can see we count showThis() and showThat() together,// while hide() is counted separately// our instrumented instances:varx=InstrumentedA();vary=InstrumentedA();// working with x and y// now we are ready for results:console.log("Shows: "+countShows.calls+" (with "+countShows.errors+" errors)");console.log("Hides: "+countHides.calls+" (with "+countHides.errors+" errors)");// let's reset results:countShows.reset();countHides.reset();// now we are ready to take another measurement
Or we can work with them on per-instance basis:
counter() object-level example
1234567891011121314151617181920212223242526272829
// our instances:varx=A();vary=A();// our countersvarcountShows=counter();varcountHides=counter();// we want to instrument only x instance:advise(x,"showThis",countShows.advice());advise(x,"showThat",countShows.advice());advise(x,"hide",countHides.advice());// as you can see we count showThis() and showThat() together,// while hide() is counted separately// working with x and y, only x is tracked// now we are ready for results:console.log("Shows: "+countShows.calls+" (with "+countShows.errors+" errors)");console.log("Hides: "+countHides.calls+" (with "+countHides.errors+" errors)");// let's reset results:countShows.reset();countHides.reset();// now we are ready to take another measurement