memoizer.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // wrapped by build app
  2. define("dojox/lang/aspect/memoizer", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.lang.aspect.memoizer");
  4. (function(){
  5. var aop = dojox.lang.aspect;
  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. var memoizeN = function(/*Function*/keyMaker){
  19. return {
  20. around: function(/*arguments*/){
  21. var ctx = aop.getContext(), self = ctx.joinPoint, that = ctx.instance, t, u, ret,
  22. key = keyMaker.apply(that, arguments);
  23. if((t = that.__memoizerCache) && (t = t[self.targetName]) && (key in t)){
  24. return t[key];
  25. }
  26. var ret = aop.proceed.apply(null, arguments);
  27. if(!(t = that.__memoizerCache)){ t = that.__memoizerCache = {}; }
  28. if(!(u = t[self.targetName])){ u = t[self.targetName] = {}; }
  29. return u[key] = ret;
  30. }
  31. };
  32. };
  33. aop.memoizer = function(/*Function?*/ keyMaker){
  34. // summary:
  35. // Returns an object, which can be used to count calls to methods.
  36. //
  37. // keyMaker:
  38. // the function, which takes method's arguments and returns a key,
  39. // which can be used to index the result.
  40. return arguments.length == 0 ? memoize1 : memoizeN(keyMaker); // Object
  41. };
  42. })();
  43. });