service getters return Array instead of Map (#80)

This commit is contained in:
Aylur
2023-09-10 20:17:46 +02:00
committed by GitHub
parent 736c89c042
commit 3321e77d56
6 changed files with 69 additions and 42 deletions
+12 -8
View File
@@ -79,7 +79,7 @@ class ApplicationsService extends Service {
}
private _list!: Application[];
frequents: { [app: string]: number };
private _frequents: { [app: string]: number };
query(term: string) {
return this._list.filter(app => app.match(term)).sort((a, b) => {
@@ -92,25 +92,28 @@ class ApplicationsService extends Service {
Gio.AppInfoMonitor.get().connect('changed', this._sync.bind(this));
try {
this.frequents =
this._frequents =
JSON.parse(readFile(CACHE_FILE)) as { [app: string]: number };
} catch (_) {
this.frequents = {};
this._frequents = {};
}
this._sync();
}
get list() { return [...this._list]; }
get frequents() { return this._frequents; }
private _launched(id: string | null) {
if (!id)
return;
typeof this.frequents[id] === 'number'
? this.frequents[id] += 1
: this.frequents[id] = 1;
typeof this._frequents[id] === 'number'
? this._frequents[id] += 1
: this._frequents[id] = 1;
ensureDirectory(APPS_CACHE_DIR);
const json = JSON.stringify(this.frequents, null, 2);
const json = JSON.stringify(this._frequents, null, 2);
writeFile(json, CACHE_FILE).catch(logError);
}
@@ -137,6 +140,7 @@ export default class Applications {
return Applications._instance;
}
static frequents() { return Applications.instance.frequents; }
static query(term: string) { return Applications.instance.query(term); }
static get list() { return Applications.instance.list; }
static get frequents() { return Applications.instance.frequents; }
}
+27 -17
View File
@@ -70,6 +70,8 @@ class AudioService extends Service {
Service.register(this, {
'speaker-changed': [],
'microphone-changed': [],
'stream-added': ['int'],
'stream-removed': ['int'],
});
}
@@ -77,8 +79,8 @@ class AudioService extends Service {
private _streams: Map<number, Stream>;
private _speaker!: Stream;
private _microphone!: Stream;
private _speakerID!: number;
private _microphoneID!: number;
private _speakerBinding!: number;
private _microphoneBinding!: number;
get speaker() { return this._speaker; }
get microphone() { return this._microphone; }
@@ -104,13 +106,13 @@ class AudioService extends Service {
private _defaultChanged(id: number, type: 'speaker' | 'microphone') {
if (this[`_${type}`])
this[`_${type}`].disconnect(this[`_${type}ID`]);
this[`_${type}`].disconnect(this[`_${type}Binding`]);
const stream = this._streams.get(id);
if (!stream)
return;
this[`_${type}ID`] = stream.connect(
this[`_${type}Binding`] = stream.connect(
'changed',
() => this.emit(`${type}-changed`),
);
@@ -127,6 +129,7 @@ class AudioService extends Service {
this._streams.set(id, new Stream(stream));
this.emit('changed');
this.emit('stream-added', id);
}
private _streamRemoved(_c: Gvc.MixerControl, id: number) {
@@ -136,15 +139,7 @@ class AudioService extends Service {
this._streams.get(id)?.close();
this._streams.delete(id);
this.emit('changed');
}
getStreams(filter: { new(): Gvc.MixerStream }) {
const map = new Map();
for (const [id, stream] of this._streams) {
if (stream.stream instanceof filter)
map.set(id, stream);
}
return map;
this.emit('stream-removed', id);
}
setSpeaker(stream: Stream) {
@@ -154,6 +149,19 @@ class AudioService extends Service {
setMicrophone(stream: Stream) {
this._control.set_default_source(stream.stream);
}
getStream(id: number) {
return this._streams.get(id);
}
getStreams(filter: { new(): Gvc.MixerStream }) {
const list = [];
for (const [, stream] of this._streams) {
if (stream.stream instanceof filter)
list.push(stream);
}
return list;
}
}
export default class Audio {
@@ -164,13 +172,15 @@ export default class Audio {
return Audio._instance;
}
static getStream(id: number) { return Audio.instance.getStream(id); }
static get microphones() { return Audio.instance.getStreams(Gvc.MixerSource); }
static get apps() { return Audio.instance.getStreams(Gvc.MixerSinkInput); }
static get speakers() { return Audio.instance.getStreams(Gvc.MixerSink); }
static get apps() { return Audio.instance.getStreams(Gvc.MixerSinkInput); }
static get microphone() { return Audio.instance.microphone; }
static set microphone(stream: Stream) { Audio.instance.setMicrophone(stream); }
static get speaker() { return Audio.instance.speaker; }
static set speaker(stream: Stream) { Audio.instance.setSpeaker(stream); }
static set microphone(stream: Stream) { Audio.instance.setMicrophone(stream); }
static get microphone() { return Audio.instance.microphone; }
}
+10 -6
View File
@@ -143,6 +143,8 @@ class BluetoothService extends Service {
);
}
getDevice(address: string) { return this._devices.get(address); }
set enabled(v) { this._client.default_adapter_powered = v; }
get enabled() { return this.state === 'on' || this.state === 'turning-on'; }
@@ -156,14 +158,14 @@ class BluetoothService extends Service {
}
}
get devices() { return this._devices; }
get devices() { return Array.from(this._devices.values()); }
get connectedDevices() {
const map = new Map();
for (const [address, device] of this._devices) {
const list = [];
for (const [, device] of this._devices) {
if (device.connected)
map.set(address, device);
list.push(device);
}
return map;
return list;
}
}
@@ -175,8 +177,10 @@ export default class Bluetooth {
return Bluetooth._instance;
}
static getDevice(address: string) { return Bluetooth.instance.getDevice(address); }
static get devices() { return Bluetooth.instance.devices; }
static get connectedDevices() { return Bluetooth.instance.connectedDevices; }
static get enabled() { return Bluetooth.instance.enabled; }
static set enabled(enable: boolean) { Bluetooth.instance.enabled = enable; }
static get devices() { return Bluetooth.instance.devices; }
}
+7 -3
View File
@@ -229,10 +229,14 @@ export default class Hyprland {
return Hyprland._instance;
}
static getMonitor(id: number) { return Hyprland.instance.monitors.get(id); }
static getWorkspace(id: number) { return Hyprland.instance.workspaces.get(id); }
static getClient(address: string) { return Hyprland.instance.clients.get(address); }
static get monitors() { return Array.from(Hyprland.instance.monitors.values()); }
static get workspaces() { return Array.from(Hyprland.instance.workspaces.values()); }
static get clients() { return Array.from(Hyprland.instance.clients.values()); }
static get active() { return Hyprland.instance.active; }
static get monitors() { return Hyprland.instance.monitors; }
static get workspaces() { return Hyprland.instance.workspaces; }
static get clients() { return Hyprland.instance.clients; }
static HyprctlGet(cmd: string): unknown | object {
const [success, out, err] =
+7 -6
View File
@@ -252,6 +252,8 @@ class MprisService extends Service {
private _players!: Players;
private _proxy: DBusProxy;
get players() { return Array.from(this._players.values()); }
constructor() {
super();
@@ -309,10 +311,7 @@ class MprisService extends Service {
this._addPlayer(name);
}
getPlayer(name: string | ((players: Players) => MprisPlayer) = '') {
if (typeof name === 'function')
return name(new Map(this._players)) || null;
getPlayer(name: string) {
for (const [busName, player] of this._players) {
if (busName.includes(name))
return player;
@@ -329,7 +328,9 @@ export default class Mpris {
return Mpris._instance;
}
static getPlayer(name: string | ((players: Players) => MprisPlayer)) {
return Mpris._instance.getPlayer(name);
static getPlayer(name: string) {
return Mpris.instance.getPlayer(name);
}
static get players() { return Mpris.instance.players; }
}
+6 -2
View File
@@ -273,8 +273,12 @@ export default class Notifications {
static clear() { Notifications.instance.Clear(); }
static close(id: number) { Notifications.instance.CloseNotification(id); }
static getPopup(id: number) { return Notifications.instance.popups.get(id); }
static getNotification(id: number) { return Notifications.instance.notifications.get(id); }
static get popups() { return Array.from(Notifications.instance.popups.values()); }
static get notifications() { return Array.from(Notifications.instance.notifications.values()); }
static get dnd() { return Notifications.instance.dnd; }
static set dnd(value: boolean) { Notifications.instance.dnd = value; }
static get popups() { return Notifications.instance.popups; }
static get notifications() { return Notifications.instance.notifications; }
}