]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
plugins.rb: use fast delegation hash
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 2 Sep 2007 09:30:13 +0000 (09:30 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 2 Sep 2007 09:30:13 +0000 (09:30 +0000)
We delegate common hooks through the fast-delegation hash, and revert to
brute-force querying for everything else. Beware that this may breaks plugins
which add/remove delegatable methods at runtime, but no such plugin is
currently used (AFAIK).

lib/rbot/plugins.rb

index 9880d720f1fbbdf81a05c860b0e0c226ebe01cb9..7bec55aec5233b385c2f9da7b3c0bbc693f6ea81 100644 (file)
@@ -646,18 +646,35 @@ module Plugins
     def delegate(method, *args)
       # debug "Delegating #{method.inspect}"
       ret = Array.new
-      (core_modules + plugins).each { |p|
-        if(p.respond_to? method)
+      if method.match(DEFAULT_DELEGATE_PATTERNS)
+        debug "fast-delegating #{method}"
+        m = method.to_sym
+        debug "no-one to delegate to" unless @delegate_list.has_key?(m)
+        return [] unless @delegate_list.has_key?(m)
+        @delegate_list[m].each { |p|
           begin
-            # debug "#{p.botmodule_class} #{p.name} responds"
             ret.push p.send(method, *args)
           rescue Exception => err
             raise if err.kind_of?(SystemExit)
             error report_error("#{p.botmodule_class} #{p.name} #{method}() failed:", err)
             raise if err.kind_of?(BDB::Fatal)
           end
-        end
-      }
+        }
+      else
+        debug "slow-delegating #{method}"
+        (core_modules + plugins).each { |p|
+          if(p.respond_to? method)
+            begin
+              # debug "#{p.botmodule_class} #{p.name} responds"
+              ret.push p.send(method, *args)
+            rescue Exception => err
+              raise if err.kind_of?(SystemExit)
+              error report_error("#{p.botmodule_class} #{p.name} #{method}() failed:", err)
+              raise if err.kind_of?(BDB::Fatal)
+            end
+          end
+        }
+      end
       return ret
       # debug "Finished delegating #{method.inspect}"
     end