git » sdk » commit 47dba8a

Every promise callback runs async on main thread

author Stephen Paul Weber
2026-01-12 20:58:28 UTC
committer Stephen Paul Weber
2026-01-12 20:58:28 UTC
parent be7396e6b73cb8d231a5254caa78f20549618535

Every promise callback runs async on main thread

Instead of running inline with the resolution, which is against spec and
probably not what you wanted anyway.

borogove/Client.hx +5 -0
borogove/persistence/SqliteDriver.hx +2 -4

diff --git a/borogove/Client.hx b/borogove/Client.hx
index 4b9167f..b89601c 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -123,6 +123,11 @@ class Client extends EventEmitter {
 			throw "accountId cannot be empty";
 		}
 		Util.setupTrace();
+		#if (!js && target.threaded)
+		final mainLoop = sys.thread.Thread.current().events;
+		var promiseFactory = cast(Promise.factory, thenshim.fallback.FallbackPromiseFactory);
+		promiseFactory.scheduler.addNext = mainLoop.run;
+		#end
 		super();
 		this.jid = JID.parse(accountId);
 		this._displayName = this.jid.node ?? this.jid.asString();
diff --git a/borogove/persistence/SqliteDriver.hx b/borogove/persistence/SqliteDriver.hx
index 3d8bc8e..19e7002 100644
--- a/borogove/persistence/SqliteDriver.hx
+++ b/borogove/persistence/SqliteDriver.hx
@@ -12,13 +12,11 @@ class SqliteDriver {
 	private final dbfile: String;
 	private final ready: Promise<Bool>;
 	private var setReady: (Bool)->Void;
-	private var mainLoop: sys.thread.EventLoop;
 
 	public function new(dbfile: String, migrate: (Array<String>->Promise<haxe.iterators.ArrayIterator<Dynamic>>)->Promise<Any>) {
 		this.dbfile = dbfile;
 		readPool = Config.constrainedMemoryMode ? writePool : new sys.thread.ElasticThreadPool(10);
 		ready = new Promise((resolve, reject) -> setReady = resolve);
-		mainLoop = sys.thread.Thread.current().events;
 
 		writePool.run(() -> {
 			final db = sys.db.Sqlite.open(dbfile);
@@ -51,10 +49,10 @@ class SqliteDriver {
 					// Though from sqlite docs it seems like it should be safe?
 					final arr = { iterator: () -> result }.array();
 					dbs.push(db);
-					mainLoop.run(() -> { resolve(arr.iterator()); });
+					resolve(arr.iterator());
 				} catch (e) {
 					dbs.push(db);
-					mainLoop.run(() -> reject(e));
+					reject(e);
 				}
 			});
 		});