Skip to content

Instantly share code, notes, and snippets.

@acrosa
Created January 25, 2011 00:40
Show Gist options
  • Save acrosa/794295 to your computer and use it in GitHub Desktop.
Save acrosa/794295 to your computer and use it in GitHub Desktop.
early flush
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader'
require 'lib/rack_patch'
# this get's executed for all actions
before do
content_type :html, 'charset' => 'utf-8'
end
class EarlyFlush
attr_accessor :headers, :block
def initialize(headers, &block)
self.headers = headers
self.block = block
end
def each
yield self.headers if self.headers
yield self.block.call() if self.block
end
end
helpers do
def earlyflush(headers, &block)
EarlyFlush.new(headers, &block)
end
end
get '/flush' do
# build the headers js, css, etc.
headers = '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>body { background-color: black; color: white; }</style>
<title>Sample page flushing</title>
</head>
'
# call out earlyflush helper
earlyflush(headers) do
sleep(5) # pretend we are doing slow things, like calling a service
'<body>
And....we are done
</body>
'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment