summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2005-04-21 18:50:12 +0000
committerLuke Kanies <luke@madstop.com>2005-04-21 18:50:12 +0000
commit89d23818abca673b0b7b29f0792afb8ec990ebb4 (patch)
tree2dbab9ad42ad2040f5cdc084038aecef1c4fd26d
parent3c6c0f7956baddbe0033a43b654915de06dcdaf6 (diff)
downloadpuppet-89d23818abca673b0b7b29f0792afb8ec990ebb4.tar.gz
puppet-89d23818abca673b0b7b29f0792afb8ec990ebb4.tar.xz
puppet-89d23818abca673b0b7b29f0792afb8ec990ebb4.zip
moving event info out of type.rb
git-svn-id: https://reductivelabs.com/svn/puppet/library/trunk@204 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--lib/blink/events.rb103
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/blink/events.rb b/lib/blink/events.rb
new file mode 100644
index 000000000..0185b3876
--- /dev/null
+++ b/lib/blink/events.rb
@@ -0,0 +1,103 @@
+#!/usr/local/bin/ruby -w
+
+# $Id$
+
+# included so we can test object types
+require 'blink'
+require 'blink/type'
+
+
+#---------------------------------------------------------------
+# here i'm separating out the methods dealing with handling events
+# currently not in use, so...
+
+class Blink::Type
+ #---------------------------------------------------------------
+ # return action array
+ # these are actions to use for responding to events
+ # no, this probably isn't the best way, because we're providing
+ # access to the actual hash, which is silly
+ def action
+ if not defined? @actions
+ puts "defining action hash"
+ @actions = Hash.new
+ end
+ @actions
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # call an event
+ # this is called on subscribers by the trigger method from the obj
+ # which sent the event
+ # event handling should probably be taking place in a central process,
+ # but....
+ def event(event,obj)
+ Blink.debug "#{self} got event #{event} from #{obj}"
+ if @actions.key?(event)
+ Blink.debug "calling it"
+ @actions[event].call(self,obj,event)
+ else
+ p @actions
+ end
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def newevent(args)
+ if args[:event].nil?
+ raise "newevent called wrong on #{self}"
+ end
+
+ return Blink::Event.new(
+ :event => args[:event],
+ :object => self
+ )
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # subscribe to an event or all events
+ # this entire event system is a hack job and needs to
+ # be replaced with a central event handler
+ def subscribe(args,&block)
+ obj = args[:object]
+ event = args[:event] || '*'.intern
+ if obj.nil? or event.nil?
+ raise "subscribe was called wrongly; #{obj} #{event}"
+ end
+ obj.action[event] = block
+ #events.each { |event|
+ unless @notify.key?(event)
+ @notify[event] = Array.new
+ end
+ unless @notify[event].include?(obj)
+ Blink.debug "pushing event '%s' for object '%s'" % [event,obj]
+ @notify[event].push(obj)
+ end
+ # }
+ #else
+ # @notify['*'.intern].push(obj)
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # initiate a response to an event
+ def trigger(event)
+ subscribers = Array.new
+ if @notify.include?('*') and @notify['*'].length > 0
+ @notify['*'].each { |obj| subscribers.push(obj) }
+ end
+ if (@notify.include?(event) and (! @notify[event].empty?) )
+ @notify[event].each { |obj| subscribers.push(obj) }
+ end
+ Blink.debug "triggering #{event}"
+ subscribers.each { |obj|
+ Blink.debug "calling #{event} on #{obj}"
+ obj.event(event,self)
+ }
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+end # Blink::Type