Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move everything Cramp::Controller::* to Cramp::*.
Before : class Home < Cramp::Controller::Action end After : class Home < Cramp::Action end Similarly, Cramp::Controller::Websocket now becomes Cramp::Websocket.
- Loading branch information
Showing
37 changed files
with
420 additions
and
443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'active_support/core_ext/hash/keys' | ||
|
||
module Cramp | ||
class Abstract | ||
|
||
include Callbacks | ||
|
||
ASYNC_RESPONSE = [-1, {}, []].freeze | ||
|
||
class << self | ||
def call(env) | ||
controller = new(env).process | ||
end | ||
end | ||
|
||
def initialize(env) | ||
@env = env | ||
end | ||
|
||
def process | ||
EM.next_tick { before_start } | ||
ASYNC_RESPONSE | ||
end | ||
|
||
def continue | ||
init_async_body | ||
|
||
status, headers = respond_with | ||
send_initial_response(status, headers, @body) | ||
|
||
EM.next_tick { start } if respond_to?(:start) | ||
EM.next_tick { on_start } | ||
end | ||
|
||
def respond_with | ||
[200, {'Content-Type' => 'text/html'}] | ||
end | ||
|
||
def init_async_body | ||
@body = Body.new | ||
|
||
if self.class.on_finish_callbacks.any? | ||
@body.callback { on_finish } | ||
@body.errback { on_finish } | ||
end | ||
end | ||
|
||
def finish | ||
@body.succeed | ||
end | ||
|
||
def send_initial_response(response_status, response_headers, response_body) | ||
EM.next_tick { @env['async.callback'].call [response_status, response_headers, response_body] } | ||
end | ||
|
||
def halt(status, headers = {}, halt_body = '') | ||
send_initial_response(status, headers, halt_body) | ||
end | ||
|
||
def request | ||
@request ||= Rack::Request.new(@env) | ||
end | ||
|
||
def params | ||
@params ||= request.params.update(@env['usher.params']).symbolize_keys | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Cramp | ||
class Action < Abstract | ||
|
||
include PeriodicTimer | ||
include KeepConnectionAlive | ||
|
||
def render(body) | ||
@body.call(body) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright 2008 James Tucker <raggi@rubyforge.org>. | ||
|
||
module Cramp | ||
class Body | ||
include EventMachine::Deferrable | ||
|
||
def initialize | ||
@queue = [] | ||
|
||
# Make sure to flush out the queue before closing the connection | ||
callback { flush } | ||
end | ||
|
||
def call(body) | ||
@queue << body | ||
schedule_dequeue | ||
end | ||
|
||
def each &blk | ||
@body_callback = blk | ||
schedule_dequeue | ||
end | ||
|
||
def closed? | ||
@deferred_status != :unknown | ||
end | ||
|
||
def flush | ||
return unless @body_callback | ||
|
||
until @queue.empty? | ||
Array(@queue.shift).each {|chunk| @body_callback.call(chunk) } | ||
end | ||
end | ||
|
||
def schedule_dequeue | ||
return unless @body_callback | ||
|
||
EventMachine.next_tick do | ||
next unless body = @queue.shift | ||
|
||
Array(body).each {|chunk| @body_callback.call(chunk) } | ||
schedule_dequeue unless @queue.empty? | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Cramp | ||
module Callbacks | ||
|
||
extend ActiveSupport::Concern | ||
|
||
included do | ||
class_inheritable_accessor :before_start_callbacks, :on_finish_callbacks, :on_start_callback, :instance_reader => false | ||
self.before_start_callbacks = [] | ||
self.on_finish_callbacks = [] | ||
self.on_start_callback = [] | ||
end | ||
|
||
module ClassMethods | ||
def before_start(*methods) | ||
self.before_start_callbacks += methods | ||
end | ||
|
||
def on_finish(*methods) | ||
self.on_finish_callbacks += methods | ||
end | ||
|
||
def on_start(*methods) | ||
self.on_start_callback += methods | ||
end | ||
end | ||
|
||
def before_start(n = 0) | ||
if callback = self.class.before_start_callbacks[n] | ||
EM.next_tick { send(callback) { before_start(n+1) } } | ||
else | ||
continue | ||
end | ||
end | ||
|
||
def on_start | ||
self.class.on_start_callback.each do |callback| | ||
EM.next_tick { send(callback) } | ||
end | ||
end | ||
|
||
def on_finish | ||
self.class.on_finish_callbacks.each do |callback| | ||
EM.next_tick { send(callback) } | ||
end | ||
end | ||
|
||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.