From be7f78bb5f933115af421c35ab9a2ede8fe3f756 Mon Sep 17 00:00:00 2001
From: Stephen Paul Weber <singpolyma@singpolyma.net>
Date: Wed, 2 Oct 2024 13:07:33 -0500
Subject: [PATCH] Detect resume sooner

Instead of waiting for a stanza, we can catch the first non-SM item
after online, which should be the stream features usually.
---
 snikket/streams/XmppJsStream.hx | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/snikket/streams/XmppJsStream.hx b/snikket/streams/XmppJsStream.hx
index 3bea39c..7b3fc21 100644
--- a/snikket/streams/XmppJsStream.hx
+++ b/snikket/streams/XmppJsStream.hx
@@ -30,6 +30,7 @@ extern class XmppJsClient {
 		get: (String, String, ({stanza: XmppJsXml})->Any)->Void,
 		set: (String, String, ({stanza: XmppJsXml})->Any)->Void,
 	};
+	var middleware: { use:(({stanza: XmppJsXml})->Void)->Void };
 	var streamFeatures: { use:(String,String,({}, ()->Void, XmppJsXml)->Void)->Void };
 	var streamManagement: { id:String, outbound: Int, inbound: Int, outbound_q: Array<XmppJsXml>, enabled: Bool, allowResume: Bool };
 	var sasl2: Dynamic;
@@ -216,7 +217,8 @@ class XmppJsStream extends GenericStream {
 			this.state.event("connection-closed");
 		});
 
-		xmpp.on("stanza", function (stanza) {
+		xmpp.middleware.use(function (data) {
+			if (data.stanza.attrs.xmlns == "urn:xmpp:sm:3") return;
 			if (xmpp.status == "online" && this.state.can("connection-success")) {
 				resumed = xmpp.streamManagement.enabled && xmpp.streamManagement.id != null && xmpp.streamManagement.id != "";
 				if (xmpp.jid == null) {
@@ -226,6 +228,9 @@ class XmppJsStream extends GenericStream {
 				}
 				this.state.event("connection-success");
 			}
+		});
+
+		xmpp.on("stanza", function (stanza) {
 			this.onStanza(convertToStanza(stanza));
 			triggerSMupdate();
 		});