Skip to content

Commit

Permalink
send browser events on new orders
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed Jan 19, 2012
1 parent f16c601 commit 422cb20
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 2 deletions.
10 changes: 9 additions & 1 deletion app/assets/javascripts/home/donations.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,13 @@ $.fn.donations = ()->
new Donations(this, $('.pagination', this))

$(document).ready ->
$('#donations').donations() if $('#donations').length > 0
if $('#donations').length > 0
$('#donations').donations()

if EventSource?
src = new EventSource('/orders.es')
src.onmessage = (e) ->
payload = JSON.parse e.data
order = payload.order
user = order.user
Notifier.notify "We've got another one!", "#{user.name} just donated $#{order.total}!", user.gravatar_url, 2000
105 changes: 105 additions & 0 deletions app/assets/javascripts/vendor/notifier.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions app/models/order_observer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class OrderObserver < ActiveRecord::Observer
def after_create(order)
OrderStream.send_json(order: order.as_json)
end
end
5 changes: 4 additions & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment', __FILE__)
run Travis::Application
require 'order_stream'

map('/') { run Travis::Application }
map('/orders.es') { run OrderStream }
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Application < Rails::Application

config.action_mailer.default_url_options = { :host => 'support.travis-ci.org' }

config.active_record.observers = :order_observer
config.threadsafe!
end
end
43 changes: 43 additions & 0 deletions lib/order_stream.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'eventmachine'

class OrderStream
@streams ||= []

def self.call(env)
stream = new
@streams << stream
env['async.close'].callback { @streams.delete(stream) }
EM.next_tick { env['async.callback'].call [200, {'Content-Type' => 'text/event-stream'}, stream] }
throw :async
end

def self.send_json(data)
send_message(data.as_json.to_json)
@streams.each { |s| s << data.as_json.to_json }
end

def self.send_message(data)
data.split(/\n/).each { |l| send_raw("data: #{l}\n") }
send_raw("\n")
end

def self.send_raw(data)
@streams.each { |s| s << data.to_s }
end

include EventMachine::Deferrable

def <<(data)
@body_callback.call(data)
end

def initialize
@body_callback = proc { |_| }
end

def each(&block)
@body_callback = block
end

#EventMachine.schedule { EventMachine::PeriodicTimer.new(10) { send_raw "\0" } }
end
7 changes: 7 additions & 0 deletions test/unit/order_observer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class OrderObserverTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 422cb20

Please sign in to comment.