lib/space/events.rb in space-0.0.5 vs lib/space/events.rb in space-0.0.6
- old
+ new
@@ -1,34 +1,41 @@
module Space
module Events
- autoload :Buffer, 'space/events/buffer'
- autoload :Event, 'space/events/event'
+ autoload :Subscription, 'space/events/subscription'
+ autoload :Sources, 'space/events/sources'
- attr_reader :buffer
+ class << self
+ def sources
+ @sources ||= Sources.new(self)
+ end
- def buffering
- @buffer = Buffer.new
- yield.tap do
- buffer, @buffer = @buffer, nil
- buffer.flush
+ def subscriptions
+ @subscriptions ||= []
end
- end
- def buffering?
- !!@buffer
- end
+ def events
+ @events ||= []
+ end
- def observers
- @observers ||= []
- end
+ def subscribe(observer, *types)
+ subscriptions << Subscription.new(observer, types)
+ end
- def subscribe(observer)
- observers << observer
+ def flush
+ event = events.first
+ events.clear
+ notify(event, false) if event
+ end
+
+ def notify(event)
+ # log event
+ subscriptions.each do |subscription|
+ subscription.notify(event)
+ end
+ end
end
def notify(*args)
- event = args.first.is_a?(Event) ? args.first : Event.new(self, *args)
- App.logger.debug "Event on #{self.class.name.split('::').last}: #{event.source.class.name.split('::').last} #{event.event.inspect}"
- buffering? ? buffer.push(event) : observers.each { |observer| observer.notify(event) }
+ Events.notify(*args)
end
end
end