Permalink
Browse files

Create a generic events interface backed by AS::Notifications.

Webmachine::Events implements the AS::Notifications API and uses
AS::Notifications as the default backend. The backend is exchangeable
with any class that implements the AS::Notifications API.
  • Loading branch information...
bernd committed Jan 6, 2013
1 parent 7ac4b2a commit f1522af301d94386b20791a1231edd45304b7822
View
@@ -10,7 +10,7 @@ def to_html
class LogListener
def call(*args)
- handle_event(ActiveSupport::Notifications::Event.new(*args))
+ handle_event(Webmachine::Events::InstrumentedEvent.new(*args))
end
def handle_event(event)
@@ -25,7 +25,7 @@ def handle_event(event)
end
end
-ActiveSupport::Notifications.subscribe('wm.dispatch', LogListener.new)
+Webmachine::Events.subscribe('wm.dispatch', LogListener.new)
App = Webmachine::Application.new do |app|
app.routes do
@@ -1,8 +1,7 @@
require 'forwardable'
require 'webmachine/configuration'
require 'webmachine/dispatcher'
-require 'securerandom' # For AS::Notifications
-require 'active_support/notifications'
+require 'webmachine/events'
module Webmachine
# How to get your Webmachine app running:
@@ -40,7 +40,7 @@ def add_route(*args, &block)
# @param [Response] response the response object
def dispatch(request, response)
if resource = find_resource(request, response)
- ActiveSupport::Notifications.instrument('wm.dispatch') do |payload|
+ Webmachine::Events.instrument('wm.dispatch') do |payload|
Webmachine::Decision::FSM.new(resource, request, response).run
payload[:resource] = resource.class.name
View
@@ -0,0 +1,33 @@
+require 'securerandom' # For AS::Notifications
+require 'active_support/notifications'
+require 'webmachine/events/instrumented_event'
+
+module Webmachine
+ module Events
+ class << self
+ attr_accessor :backend
+
+ def publish(name, *args)
+ backend.publish(name, *args)
+ end
+
+ def instrument(name, payload = {}, &block)
+ backend.instrument(name, payload, &block)
+ end
+
+ def subscribe(*args, &block)
+ backend.subscribe(*args, &block)
+ end
+
+ def subscribed(callback, *args, &block)
+ backend.subscribed(callback, *args, &block)
+ end
+
+ def unsubscribe(subscriber)
+ backend.unsubscribe(subscriber)
+ end
+ end
+
+ self.backend = ActiveSupport::Notifications
+ end
+end
@@ -0,0 +1,12 @@
+require 'delegate'
+require 'active_support/notifications/instrumenter'
+
+module Webmachine
+ module Events
+ class InstrumentedEvent < SimpleDelegator
+ def initialize(*args)
+ super(ActiveSupport::Notifications::Event.new(*args))
+ end
+ end
+ end
+end
View
@@ -14,7 +14,7 @@ module Trace
:pstore => PStoreTraceStore
}
- DEFAULT_TRACE_SUBSCRIBER = ActiveSupport::Notifications.subscribe(
+ DEFAULT_TRACE_SUBSCRIBER = Webmachine::Events.subscribe(
/wm\.trace\..+/,
Webmachine::Trace::Listener.new
)
@@ -80,12 +80,12 @@ def trace_store
# Sets the trace listener objects.
# Defaults to Webmachine::Trace::Listener.new.
# @param [Array<Object>] listeners a list of event listeners
- # @return [Array<ActiveSupport::Notifications::Subscriber] a list of event subscribers
+ # @return [Array<Object>] a list of event subscribers
def trace_listener=(listeners)
- ActiveSupport::Notifications.unsubscribe(DEFAULT_TRACE_SUBSCRIBER)
+ Webmachine::Events.unsubscribe(DEFAULT_TRACE_SUBSCRIBER)
Array(listeners).map do |listener|
- ActiveSupport::Notifications.subscribe(/wm\.trace\..+/, listener)
+ Webmachine::Events.subscribe(/wm\.trace\..+/, listener)
end
end
end
@@ -42,7 +42,7 @@ def finish_request(*args)
proxy_callback :finish_request, *args
ensure
resource.response.headers['X-Webmachine-Trace-Id'] = object_id.to_s
- ActiveSupport::Notifications.publish('wm.trace.record', {
+ Webmachine::Events.publish('wm.trace.record', {
:trace_id => object_id.to_s,
:trace => resource.response.trace
})

0 comments on commit f1522af

Please sign in to comment.