Class: Aldebaran::Helpers::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/aldebaran/base.rb

Overview

Class of the response body in case you use #stream.

Three things really matter: The front and back block (back being the blog generating content, front the one sending it to the client) and the scheduler, integrating with whatever concurrency feature the Rack handler is using.

Scheduler has to respond to defer and schedule.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scheduler = self.class, close = true, &back) ⇒ Stream

Returns a new instance of Stream.



249
250
251
# File 'lib/aldebaran/base.rb', line 249

def initialize(scheduler = self.class, close = true, &back)
  @back, @scheduler, @callback, @close = back.to_proc, scheduler, nil, close
end

Class Method Details

.deferObject



247
# File 'lib/aldebaran/base.rb', line 247

def self.defer(*)    yield end

.scheduleObject



246
# File 'lib/aldebaran/base.rb', line 246

def self.schedule(*) yield end

Instance Method Details

#<<(data) ⇒ Object



269
270
271
272
# File 'lib/aldebaran/base.rb', line 269

def <<(data)
  @scheduler.schedule { @front.call(data.to_s) }
  self
end

#callback(&block) ⇒ Object Also known as: errback



274
275
276
# File 'lib/aldebaran/base.rb', line 274

def callback(&block)
  @callback = block
end

#closeObject



253
254
255
# File 'lib/aldebaran/base.rb', line 253

def close
  @scheduler.schedule { @callback.call if @callback }
end

#each(&front) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
# File 'lib/aldebaran/base.rb', line 257

def each(&front)
  @front = front
  @scheduler.defer do
    begin
      @back.call(self)
    rescue Exception => e
      @scheduler.schedule { raise e }
    end
    close if @close
  end
end