Class: SmartPrompt::AsyncWriter
- Inherits:
-
Object
- Object
- SmartPrompt::AsyncWriter
- Defined in:
- lib/smart_prompt/persistence_layer.rb
Overview
AsyncWriter handles asynchronous write operations to avoid blocking
Instance Method Summary collapse
-
#enqueue(&block) ⇒ Object
Enqueue a block to be executed asynchronously.
-
#initialize ⇒ AsyncWriter
constructor
A new instance of AsyncWriter.
-
#running? ⇒ Boolean
Check if the worker is running.
-
#stop ⇒ Object
Stop the worker thread gracefully.
Constructor Details
#initialize ⇒ AsyncWriter
Returns a new instance of AsyncWriter.
8 9 10 11 12 13 |
# File 'lib/smart_prompt/persistence_layer.rb', line 8 def initialize @queue = Queue.new @worker_thread = nil @running = false @mutex = Mutex.new end |
Instance Method Details
#enqueue(&block) ⇒ Object
Enqueue a block to be executed asynchronously
16 17 18 19 |
# File 'lib/smart_prompt/persistence_layer.rb', line 16 def enqueue(&block) ensure_worker_running @queue << block end |
#running? ⇒ Boolean
Check if the worker is running
31 32 33 |
# File 'lib/smart_prompt/persistence_layer.rb', line 31 def running? @mutex.synchronize { @running } end |
#stop ⇒ Object
Stop the worker thread gracefully
22 23 24 25 26 27 28 |
# File 'lib/smart_prompt/persistence_layer.rb', line 22 def stop @mutex.synchronize do @running = false end @queue << :stop if @worker_thread @worker_thread&.join end |