memoizer.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.lang.aspect.memoizer"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.aspect.memoizer"] = true;
  8. dojo.provide("dojox.lang.aspect.memoizer");
  9. (function(){
  10. var aop = dojox.lang.aspect;
  11. var memoize1 = {
  12. around: function(key){
  13. var ctx = aop.getContext(), self = ctx.joinPoint, that = ctx.instance, t, u, ret;
  14. if((t = that.__memoizerCache) && (t = t[self.targetName]) && (key in t)){
  15. return t[key];
  16. }
  17. var ret = aop.proceed.apply(null, arguments);
  18. if(!(t = that.__memoizerCache)){ t = that.__memoizerCache = {}; }
  19. if(!(u = t[self.targetName])){ u = t[self.targetName] = {}; }
  20. return u[key] = ret;
  21. }
  22. };
  23. var memoizeN = function(/*Function*/keyMaker){
  24. return {
  25. around: function(/*arguments*/){
  26. var ctx = aop.getContext(), self = ctx.joinPoint, that = ctx.instance, t, u, ret,
  27. key = keyMaker.apply(that, arguments);
  28. if((t = that.__memoizerCache) && (t = t[self.targetName]) && (key in t)){
  29. return t[key];
  30. }
  31. var ret = aop.proceed.apply(null, arguments);
  32. if(!(t = that.__memoizerCache)){ t = that.__memoizerCache = {}; }
  33. if(!(u = t[self.targetName])){ u = t[self.targetName] = {}; }
  34. return u[key] = ret;
  35. }
  36. };
  37. };
  38. aop.memoizer = function(/*Function?*/ keyMaker){
  39. // summary:
  40. // Returns an object, which can be used to count calls to methods.
  41. //
  42. // keyMaker:
  43. // the function, which takes method's arguments and returns a key,
  44. // which can be used to index the result.
  45. return arguments.length == 0 ? memoize1 : memoizeN(keyMaker); // Object
  46. };
  47. })();
  48. }