DCL

An elegant OOP with mixins + AOP for JavaScript.

counter()

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 can be included with following commands:

Include counter()
1
2
3
4
5
6
7
8
9
10
11
12
13
// node.js
var counter = require("dcl/advices/counter");
...

// AMD (code)
require(["dcl/advices/counter"], function(counter){
  ...
});

// AMD (definition)
define(["dcl/advices/counter"], function(counter){
  ...
});

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:

Counter
1
2
3
4
5
6
7
8
var Counter = new dcl(null, {
  declaredClass: "dcl/advices/counter/Counter",
  calls: 0,
  errors: 0,
  constructor: function(){...},
  reset: function(){...},
  advice: function(){...}
});

calls

calls is a numeric parameter that counts how many times the method was called.

errors

errors is a numeric parameter that counts how many times the method returned an error (threw an exception based on Error).

Constructor

The constructor initializes an internal state by setting calls and errors` to 0.

reset()

Sets calls and errors` to 0.

advice()

Returns an advice object that can be weaved with any given method. It can be used directly with dcl.advise() or advise().

Examples

We can use a counter to track calls for all instances at the same time:

counter() class-level example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// our object:
var A = dcl(null, {
  declaredClass: "A",
  showThis: function(){...},
  showThat: function(){...},
  hide:     function(){...}
});

// our counters
var countShows = counter();
var countHides = counter();

// instrumented version:
var InstrumentedA = 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:
var x = InstrumentedA();
var y = 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// our object:
var A = dcl(null, {
  declaredClass: "A",
  showThis: function(){...},
  showThat: function(){...},
  hide:     function(){...}
});

// our instances:
var x = A();
var y = A();

// our counters
var countShows = counter();
var countHides = 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

Fork me on GitHub