]> git.cworth.org Git - sup/commitdiff
make load-more callbacks use a queue and be thread-safe
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Thu, 3 Sep 2009 17:49:17 +0000 (13:49 -0400)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Thu, 3 Sep 2009 17:53:30 +0000 (13:53 -0400)
Replaced previous insane implementation with something that actually
makes sense.

lib/sup/modes/line-cursor-mode.rb

index 0b1fd1de5418cf2c533590e4dfcc2836cb705551..51a59134f1ca6a92effba651aec4404f5fb60b5a 100644 (file)
@@ -15,11 +15,24 @@ class LineCursorMode < ScrollMode
   def initialize opts={}
     @cursor_top = @curpos = opts.delete(:skip_top_rows) || 0
     @load_more_callbacks = []
-    @load_more_callbacks_m = Mutex.new
-    @load_more_callbacks_active = false
+    @load_more_q = Queue.new
+    @load_more_thread = ::Thread.new do
+      while true
+        e = @load_more_q.pop
+        debug "calling callbacks on #{e.inspect}"
+        @load_more_callbacks.each { |c| c.call e }
+      end
+    end
+
     super opts
   end
 
+  def cleanup
+    @load_more_thread.kill
+    debug "killing thread"
+    super
+  end
+
   def draw
     super
     set_status
@@ -163,21 +176,8 @@ private
   end
 
   def call_load_more_callbacks size
-    go = 
-      @load_more_callbacks_m.synchronize do
-        if @load_more_callbacks_active
-          false
-        else
-          @load_more_callbacks_active = true
-        end
-    end
-
-    return unless go
-
-    @load_more_callbacks.each { |c| c.call size }
-    @load_more_callbacks_active = false
-  end    
-
+    @load_more_q.push size
+  end
 end
 
 end