blather_notify.rb

 1# frozen_string_literal: true
 2
 3require "blather/client/dsl"
 4require "timeout"
 5
 6module BlatherNotify
 7	extend Blather::DSL
 8
 9	@ready = Queue.new
10
11	when_ready { @ready << :ready }
12
13	def self.start(jid, password)
14		# workqueue_count MUST be 0 or else Blather uses threads!
15		setup(jid, password, nil, nil, nil, nil, workqueue_count: 0)
16
17		EM.error_handler { |e| warn e.message }
18		@thread = Thread.new do
19			EM.run do
20				client.run
21			end
22		end
23
24		at_exit { wait_then_exit }
25
26		Timeout.timeout(30) { @ready.pop }
27	end
28
29	def self.wait_then_exit
30		EM.next_tick do
31			shutdown
32			EM.stop
33		end
34		@thread.join
35	end
36end