source: Dev/branches/rest-dojo-ui/client/dojox/lang/aspect/memoizer.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 1.4 KB
Line 
1dojo.provide("dojox.lang.aspect.memoizer");
2
3(function(){
4        var aop = dojox.lang.aspect;
5
6        var memoize1 = {
7                around: function(key){
8                        var ctx = aop.getContext(), self = ctx.joinPoint, that = ctx.instance, t, u, ret;
9                        if((t = that.__memoizerCache) && (t = t[self.targetName]) && (key in t)){
10                                return t[key];
11                        }
12                        var ret = aop.proceed.apply(null, arguments);
13                        if(!(t = that.__memoizerCache)){ t = that.__memoizerCache = {}; }
14                        if(!(u = t[self.targetName])){ u = t[self.targetName] = {}; }
15                        return u[key] = ret;
16                }
17        };
18
19        var memoizeN = function(/*Function*/keyMaker){
20                return {
21                        around: function(/*arguments*/){
22                                var ctx = aop.getContext(), self = ctx.joinPoint, that = ctx.instance, t, u, ret,
23                                        key  = keyMaker.apply(that, arguments);
24                                if((t = that.__memoizerCache) && (t = t[self.targetName]) && (key in t)){
25                                        return t[key];
26                                }
27                                var ret = aop.proceed.apply(null, arguments);
28                                if(!(t = that.__memoizerCache)){ t = that.__memoizerCache = {}; }
29                                if(!(u = t[self.targetName])){ u = t[self.targetName] = {}; }
30                                return u[key] = ret;
31                        }
32                };
33        };
34
35        aop.memoizer = function(/*Function?*/ keyMaker){
36                // summary:
37                //              Returns an object, which can be used to count calls to methods.
38                //
39                // keyMaker:
40                //              the function, which takes method's arguments and returns a key,
41                //              which can be used to index the result.
42
43                return arguments.length == 0 ? memoize1 : memoizeN(keyMaker);   // Object
44        };
45})();
Note: See TracBrowser for help on using the repository browser.