Class: KeepYourHead::WindowEdit::LazyUpdate

Inherits:
Object
  • Object
show all
Defined in:
lib/Keepyourhead/gui/WindowEdit.rb

Constant Summary collapse

Delay =
0.5
Timeout =
1000 * Delay / 3.0 * 1.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ LazyUpdate

Returns a new instance of LazyUpdate.



283
284
285
286
# File 'lib/Keepyourhead/gui/WindowEdit.rb', line 283

def initialize( view )
	self.running = false
	self.view = view
end

Instance Attribute Details

#runningObject

Returns the value of attribute running.



278
279
280
# File 'lib/Keepyourhead/gui/WindowEdit.rb', line 278

def running
  @running
end

#selectedObject

Returns the value of attribute selected.



278
279
280
# File 'lib/Keepyourhead/gui/WindowEdit.rb', line 278

def selected
  @selected
end

#stateObject

Returns the value of attribute state.



278
279
280
# File 'lib/Keepyourhead/gui/WindowEdit.rb', line 278

def state
  @state
end

#viewObject

Returns the value of attribute view.



278
279
280
# File 'lib/Keepyourhead/gui/WindowEdit.rb', line 278

def view
  @view
end

#whenObject

Returns the value of attribute when.



278
279
280
# File 'lib/Keepyourhead/gui/WindowEdit.rb', line 278

def when
  @when
end

Instance Method Details

#restartObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/Keepyourhead/gui/WindowEdit.rb', line 288

def restart
	if not self.selected == view.selected then
		if view.selected.kind_of? Database::Flashcard then
			self.selected = view.selected
		else
			self.selected = nil # no more updates
		end
	end

	view.updateImages if view.selected == self.selected

	self.state = :waiting
	self.when = Time.now + Delay

	if not self.running then
		self.running = true
		Gtk.timeout_add( Timeout ) {
			if self.state == :waiting and self.when < Time.now then
				self.state = :running
			
				Thread.new {
					begin
						$cache.getCacheItem( self.selected, Database::FRONT ) if self.selected
						$cache.getCacheItem( self.selected, Database::BACK ) if self.selected
					rescue
						puts $!.backtrace, $!
					end
				}
			end

			if self.state == :running then
				ret = true
				$cache.hasCacheItem( self.selected, Database::FRONT ) {|cacheItem| ret &&= cacheItem } if self.selected
				$cache.hasCacheItem( self.selected, Database::BACK ) { |cacheItem| ret &&= cacheItem } if self.selected
				if ret then
					self.state = :done
				end
				
				view.updateImages if view.selected == self.selected
			end
			self.running = (self.state != :done)
			
			self.running
		}
	end
end