🔧 chore(modules/bluetooth): move some init code to the outside of root scope

This commit is contained in:
retrozinndev
2025-10-26 17:37:45 -03:00
parent e499118c4f
commit 6a1bd08fae
+8 -8
View File
@@ -1,10 +1,10 @@
import { createRoot, getScope, Scope } from "ags"; import { createRoot, getScope, Scope } from "ags";
import { execAsync } from "ags/process"; import { execAsync } from "ags/process";
import { userData } from "../config"; import { userData } from "../config";
import { createScopedConnection } from "gnim-utils";
import GObject, { getter, gtype, property, register, setter } from "ags/gobject"; import GObject, { getter, gtype, property, register, setter } from "ags/gobject";
import AstalBluetooth from "gi://AstalBluetooth"; import AstalBluetooth from "gi://AstalBluetooth";
import { createScopedConnection } from "gnim-utils";
/** AstalBluetooth helper (implements the default adapter feature) */ /** AstalBluetooth helper (implements the default adapter feature) */
@@ -19,10 +19,10 @@ export class Bluetooth extends GObject.Object {
}; };
private static instance: Bluetooth; private static instance: Bluetooth;
private astalBl = AstalBluetooth.get_default(); private astalBl: AstalBluetooth.Bluetooth;
#connections: Map<GObject.Object, Array<number>|number> = new Map(); #connections: Map<GObject.Object, Array<number>|number> = new Map();
#adapter: AstalBluetooth.Adapter|null = this.astalBl.adapter ?? null; #adapter: AstalBluetooth.Adapter|null = null;
#scope!: Scope; #scope!: Scope;
#isAvailable: boolean = false; #isAvailable: boolean = false;
#lastDevice: AstalBluetooth.Device|null = null; #lastDevice: AstalBluetooth.Device|null = null;
@@ -63,14 +63,17 @@ export class Bluetooth extends GObject.Object {
constructor() { constructor() {
super(); super();
createRoot(async () => { this.astalBl = AstalBluetooth.get_default();
this.#scope = getScope(); this.#adapter = this.astalBl.adapter ?? null;
if(this.astalBl.adapters.length > 0) { if(this.astalBl.adapters.length > 0) {
this.#isAvailable = true; this.#isAvailable = true;
this.notify("is-available"); this.notify("is-available");
} }
createRoot(() => {
this.#scope = getScope();
// load previous default adapter // load previous default adapter
const dataDefaultAdapter = userData.getProperty("bluetooth_default_adapter", "string"); const dataDefaultAdapter = userData.getProperty("bluetooth_default_adapter", "string");
const foundAdapter = this.astalBl.adapters.filter(a => a.address === dataDefaultAdapter)[0]; const foundAdapter = this.astalBl.adapters.filter(a => a.address === dataDefaultAdapter)[0];
@@ -105,11 +108,8 @@ export class Bluetooth extends GObject.Object {
this.#adapter = this.astalBl.adapters[0]; this.#adapter = this.astalBl.adapters[0];
}); });
// async to prevent slow start
setTimeout(() => {
this.#lastDevice = this.getLastConnectedDevice(); this.#lastDevice = this.getLastConnectedDevice();
this.notify("last-device"); this.notify("last-device");
}, 1200);
this.#connections.set(AstalBluetooth.get_default(), [ this.#connections.set(AstalBluetooth.get_default(), [
AstalBluetooth.get_default().connect("device-added", (_) => { AstalBluetooth.get_default().connect("device-added", (_) => {