Command cancel rejects the promise

Stephen Paul Weber created

So it won't be taken as next by naive code.

Change summary

sgx_jmp.rb | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

Detailed changes

sgx_jmp.rb 🔗

@@ -238,11 +238,12 @@ message :error? do |m|
 end
 
 class SessionManager
-	def initialize(blather, id_msg, timeout: 5)
+	def initialize(blather, id_msg, timeout: 5, error_if: nil)
 		@blather = blather
 		@sessions = {}
 		@id_msg = id_msg
 		@timeout = timeout
+		@error_if = error_if
 	end
 
 	def promise_for(stanza)
@@ -264,7 +265,7 @@ class SessionManager
 
 	def fulfill(stanza)
 		id = "#{stanza.from.stripped}/#{stanza.public_send(@id_msg)}"
-		if stanza.error?
+		if stanza.error? || @error_if&.call(stanza)
 			@sessions.delete(id)&.reject(stanza)
 		else
 			@sessions.delete(id)&.fulfill(stanza)
@@ -273,7 +274,12 @@ class SessionManager
 end
 
 IQ_MANAGER = SessionManager.new(self, :id)
-COMMAND_MANAGER = SessionManager.new(self, :sessionid, timeout: 60 * 60)
+COMMAND_MANAGER = SessionManager.new(
+	self,
+	:sessionid,
+	timeout: 60 * 60,
+	error_if: ->(s) { s.cancel? }
+)
 web_register_manager = WebRegisterManager.new
 
 disco_info to: Blather::JID.new(CONFIG[:component][:jid]) do |iq|