]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/AdapterRegistry.js.uncompressed.js
1 define("dojo/AdapterRegistry", ["./_base/kernel", "./_base/lang"], function(dojo
, lang
) {
3 // dojo/AdapterRegistry
7 var AdapterRegistry
= dojo
.AdapterRegistry = function(/*Boolean?*/ returnWrappers
){
9 // A registry to make contextual calling/searching easier.
11 // Objects of this class keep list of arrays in the form [name, check,
12 // wrap, directReturn] that are used to determine what the contextual
13 // result of a set of checked arguments is. All check/wrap functions
14 // in this registry should be of the same arity.
16 // | // create a new registry
17 // | var reg = new dojo.AdapterRegistry();
18 // | reg.register("handleString",
21 // | // do something with the string here
24 // | reg.register("handleArr",
27 // | // do something with the array here
31 // | // now we can pass reg.match() *either* an array or a string and
32 // | // the value we pass will get handled by the right function
33 // | reg.match("someValue"); // will call the first function
34 // | reg.match(["someValue"]); // will call the second
37 this.returnWrappers
= returnWrappers
|| false; // Boolean
42 AdapterRegistry = dojo.AdapterRegistry;
45 lang
.extend(AdapterRegistry
, {
46 register: function(/*String*/ name
, /*Function*/ check
, /*Function*/ wrap
, /*Boolean?*/ directReturn
, /*Boolean?*/ override
){
48 // register a check function to determine if the wrap function or
49 // object gets selected
51 // a way to identify this matcher.
53 // a function that arguments are passed to from the adapter's
54 // match() function. The check function should return true if the
55 // given arguments are appropriate for the wrap function.
57 // If directReturn is true, the value passed in for wrap will be
58 // returned instead of being called. Alternately, the
59 // AdapterRegistry can be set globally to "return not call" using
60 // the returnWrappers property. Either way, this behavior allows
61 // the registry to act as a "search" function instead of a
62 // function interception library.
64 // If override is given and true, the check function will be given
65 // highest priority. Otherwise, it will be the lowest priority
67 this.pairs
[((override
) ? "unshift" : "push")]([name
, check
, wrap
, directReturn
]);
70 match: function(/* ... */){
72 // Find an adapter for the given arguments. If no suitable adapter
73 // is found, throws an exception. match() accepts any number of
74 // arguments, all of which are passed to all matching functions
75 // from the registered pairs.
76 for(var i
= 0; i
< this.pairs
.length
; i
++){
77 var pair
= this.pairs
[i
];
78 if(pair
[1].apply(this, arguments
)){
79 if((pair
[3])||(this.returnWrappers
)){
82 return pair
[2].apply(this, arguments
);
86 throw new Error("No match found");
89 unregister: function(name
){
91 // Remove a named adapter from the registry
93 // The name of the adapter.
95 // Returns true if operation is successful.
96 // Returns false if operation fails.
98 // FIXME: this is kind of a dumb way to handle this. On a large
99 // registry this will be slow-ish and we can use the name as a lookup
100 // should we choose to trade memory for speed.
101 for(var i
= 0; i
< this.pairs
.length
; i
++){
102 var pair
= this.pairs
[i
];
104 this.pairs
.splice(i
, 1);
112 return AdapterRegistry
;