summaryrefslogtreecommitdiff
path: root/js/dojo/dojox/lang/oo/aop.js
blob: df2f03bc851288cd422a65f5f79808b9949fd123 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//>>built
// wrapped by build app
define("dojox/lang/oo/aop", ["dijit","dojo","dojox","dojo/require!dojox/lang/oo/Decorator,dojox/lang/oo/general"], function(dijit,dojo,dojox){
dojo.provide("dojox.lang.oo.aop");

dojo.require("dojox.lang.oo.Decorator");
dojo.require("dojox.lang.oo.general");

(function(){
	var oo = dojox.lang.oo, md = oo.makeDecorator, oog = oo.general, ooa = oo.aop,
		isF = dojo.isFunction;

	// five decorators implementing light-weight AOP weaving

	/*=====
	ooa.before = md(function(name, newValue, oldValue){
		// summary: creates a "before" advise, by calling new function
		// before the old one

		// dummy body
	});

	ooa.around = md(function(name, newValue, oldValue){
		// summary: creates an "around" advise,
		// the previous value is passed as a first argument and can be null,
		// arguments are passed as a second argument

		// dummy body
	});
	=====*/

	// reuse existing decorators
	ooa.before = oog.before;
	ooa.around = oog.wrap;

	ooa.afterReturning = md(function(name, newValue, oldValue){
		// summary: creates an "afterReturning" advise,
		// the returned value is passed as the only argument
		return isF(oldValue) ?
			function(){
				var ret = oldValue.apply(this, arguments);
				newValue.call(this, ret);
				return ret;
			} : function(){ newValue.call(this); };
	});

	ooa.afterThrowing = md(function(name, newValue, oldValue){
		// summary: creates an "afterThrowing" advise,
		// the exception is passed as the only argument
		return isF(oldValue) ?
			function(){
				var ret;
				try{
					ret = oldValue.apply(this, arguments);
				}catch(e){
					newValue.call(this, e);
					throw e;
				}
				return ret;
			} : oldValue;
	});

	ooa.after = md(function(name, newValue, oldValue){
		// summary: creates an "after" advise,
		// it takes no arguments
		return isF(oldValue) ?
			function(){
				var ret;
				try{
					ret = oldValue.apply(this, arguments);
				}finally{
					newValue.call(this);
				}
				return ret;
			} : function(){ newValue.call(this); }
	});
})();

});