AdapterRegistry.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. define(["./_base/kernel", "./_base/lang"], function(dojo, lang){
  2. // module:
  3. // dojo/AdapterRegistry
  4. var AdapterRegistry = dojo.AdapterRegistry = function(/*Boolean?*/ returnWrappers){
  5. // summary:
  6. // A registry to make contextual calling/searching easier.
  7. // description:
  8. // Objects of this class keep list of arrays in the form [name, check,
  9. // wrap, directReturn] that are used to determine what the contextual
  10. // result of a set of checked arguments is. All check/wrap functions
  11. // in this registry should be of the same arity.
  12. // example:
  13. // | // create a new registry
  14. // | require(["dojo/AdapterRegistry"],
  15. // | function(AdapterRegistry){
  16. // | var reg = new AdapterRegistry();
  17. // | reg.register("handleString",
  18. // | function(str){
  19. // | return typeof val == "string"
  20. // | },
  21. // | function(str){
  22. // | // do something with the string here
  23. // | }
  24. // | );
  25. // | reg.register("handleArr",
  26. // | dojo.isArray,
  27. // | function(arr){
  28. // | // do something with the array here
  29. // | }
  30. // | );
  31. // |
  32. // | // now we can pass reg.match() *either* an array or a string and
  33. // | // the value we pass will get handled by the right function
  34. // | reg.match("someValue"); // will call the first function
  35. // | reg.match(["someValue"]); // will call the second
  36. // | });
  37. this.pairs = [];
  38. this.returnWrappers = returnWrappers || false; // Boolean
  39. };
  40. lang.extend(AdapterRegistry, {
  41. register: function(/*String*/ name, /*Function*/ check, /*Function*/ wrap, /*Boolean?*/ directReturn, /*Boolean?*/ override){
  42. // summary:
  43. // register a check function to determine if the wrap function or
  44. // object gets selected
  45. // name:
  46. // a way to identify this matcher.
  47. // check:
  48. // a function that arguments are passed to from the adapter's
  49. // match() function. The check function should return true if the
  50. // given arguments are appropriate for the wrap function.
  51. // directReturn:
  52. // If directReturn is true, the value passed in for wrap will be
  53. // returned instead of being called. Alternately, the
  54. // AdapterRegistry can be set globally to "return not call" using
  55. // the returnWrappers property. Either way, this behavior allows
  56. // the registry to act as a "search" function instead of a
  57. // function interception library.
  58. // override:
  59. // If override is given and true, the check function will be given
  60. // highest priority. Otherwise, it will be the lowest priority
  61. // adapter.
  62. this.pairs[((override) ? "unshift" : "push")]([name, check, wrap, directReturn]);
  63. },
  64. match: function(/* ... */){
  65. // summary:
  66. // Find an adapter for the given arguments. If no suitable adapter
  67. // is found, throws an exception. match() accepts any number of
  68. // arguments, all of which are passed to all matching functions
  69. // from the registered pairs.
  70. for(var i = 0; i < this.pairs.length; i++){
  71. var pair = this.pairs[i];
  72. if(pair[1].apply(this, arguments)){
  73. if((pair[3])||(this.returnWrappers)){
  74. return pair[2];
  75. }else{
  76. return pair[2].apply(this, arguments);
  77. }
  78. }
  79. }
  80. throw new Error("No match found");
  81. },
  82. unregister: function(name){
  83. // summary:
  84. // Remove a named adapter from the registry
  85. // name: String
  86. // The name of the adapter.
  87. // returns: Boolean
  88. // Returns true if operation is successful.
  89. // Returns false if operation fails.
  90. // FIXME: this is kind of a dumb way to handle this. On a large
  91. // registry this will be slow-ish and we can use the name as a lookup
  92. // should we choose to trade memory for speed.
  93. for(var i = 0; i < this.pairs.length; i++){
  94. var pair = this.pairs[i];
  95. if(pair[0] == name){
  96. this.pairs.splice(i, 1);
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. });
  103. return AdapterRegistry;
  104. });