Skip to content

Commit c30e660

Browse files
committed
added async loading to radiolist and added playbackmanager to destroy()
1 parent 5b219a0 commit c30e660

File tree

4 files changed

+54
-21
lines changed

4 files changed

+54
-21
lines changed

[email protected]/extension.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,31 @@ export default class YetAnotherRadioExtension extends Extension {
183183
enable() {
184184
initTranslations(_);
185185
ensureStorageFile();
186-
const stations = loadStations();
187-
188186
this._settings = this.getSettings();
189-
190-
this._indicator = new Indicator(stations, () => this.openPreferences(), this.path, this._settings);
187+
this._indicator = new Indicator([], () => this.openPreferences(), this.path, this._settings);
191188
Main.panel.addToStatusArea(this.uuid, this._indicator);
192189

193-
this._monitor = this._watchStationsFile();
190+
loadStations().then(stations => {
191+
if (this._indicator) {
192+
this._indicator.setStations(stations);
193+
}
194+
}).catch(error => {
195+
console.error('Failed to load stations:', error);
196+
});
194197

198+
this._monitor = this._watchStationsFile();
195199
this._setupMediaKeys();
196200
}
197201

198202
_watchStationsFile() {
199203
const file = Gio.File.new_for_path(STORAGE_PATH);
200204
const monitor = file.monitor_file(Gio.FileMonitorFlags.NONE, null);
201205
this._monitorHandlerId = monitor.connect('changed', () => {
202-
this._indicator?.setStations(loadStations());
206+
loadStations().then(stations => {
207+
this._indicator?.setStations(stations);
208+
}).catch(error => {
209+
console.error('Failed to reload stations:', error);
210+
});
203211
});
204212
return monitor;
205213
}

[email protected]/modules/playbackManager.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,25 @@ export default class PlaybackManager {
250250
}
251251

252252
_updateStationHistory(station) {
253-
const stations = loadStations();
254-
const stationIndex = stations.findIndex(s => s.uuid === station.uuid);
255-
if (stationIndex >= 0) {
256-
stations[stationIndex].lastPlayed = Date.now();
257-
saveStations(stations);
258-
}
253+
loadStations().then(stations => {
254+
const stationIndex = stations.findIndex(s => s.uuid === station.uuid);
255+
if (stationIndex >= 0) {
256+
stations[stationIndex].lastPlayed = Date.now();
257+
saveStations(stations);
258+
}
259+
}).catch(err => {
260+
console.error('Failed to update station history', err);
261+
});
259262
}
260263

261264
destroy() {
262265
this.stop();
263266

267+
if (this._metadataTimer) {
268+
GLib.source_remove(this._metadataTimer);
269+
this._metadataTimer = null;
270+
}
271+
264272
if (this._reconnectId) {
265273
GLib.source_remove(this._reconnectId);
266274
this._reconnectId = null;

[email protected]/prefs.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,6 @@ export default class YetAnotherRadioPreferences extends ExtensionPreferences {
907907
window.set_default_size(720, 640);
908908

909909
const settings = this.getSettings();
910-
const stations = loadStations();
911-
912910
const toastOverlay = new Adw.ToastOverlay();
913911
const viewStack = new Adw.ViewStack();
914912

@@ -917,10 +915,15 @@ export default class YetAnotherRadioPreferences extends ExtensionPreferences {
917915
addStationsPage.setStations(newStations);
918916
generalSettingsPage.setStations(newStations);
919917
};
920-
921-
const generalSettingsPage = new GeneralSettingsPage(settings, stations, refreshCallback, toastOverlay);
922-
const savedStationsPage = new SavedStationsPage(stations, refreshCallback);
923-
const addStationsPage = new AddStationsPage(stations, refreshCallback, settings);
918+
919+
const generalSettingsPage = new GeneralSettingsPage(settings, [], refreshCallback, toastOverlay);
920+
const savedStationsPage = new SavedStationsPage([], refreshCallback);
921+
const addStationsPage = new AddStationsPage([], refreshCallback, settings);
922+
loadStations().then(stations => {
923+
refreshCallback(stations);
924+
}).catch(error => {
925+
console.error('Failed to load stations in prefs:', error);
926+
});
924927

925928
viewStack.add_titled(generalSettingsPage, 'general', _('General'));
926929
viewStack.add_titled(savedStationsPage, 'saved', _('Saved Stations'));

[email protected]/radioUtils.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import GLib from 'gi://GLib';
2+
import Gio from 'gi://Gio';
23
import Soup from 'gi://Soup';
34

45
let _ = (s) => s;
@@ -30,7 +31,7 @@ export function ensureStorageFile() {
3031
}
3132
}
3233

33-
export function loadStations() {
34+
export async function loadStations() {
3435
try {
3536
ensureStorageFile();
3637
} catch (error) {
@@ -39,7 +40,20 @@ export function loadStations() {
3940
}
4041

4142
try {
42-
const [, contents] = GLib.file_get_contents(STORAGE_PATH);
43+
const file = Gio.File.new_for_path(STORAGE_PATH);
44+
const [success, contents] = await new Promise((resolve, reject) => {
45+
file.load_contents_async(null, (obj, res) => {
46+
try {
47+
const result = obj.load_contents_finish(res);
48+
resolve(result);
49+
} catch (e) {
50+
reject(e);
51+
}
52+
});
53+
});
54+
55+
if (!success) return [];
56+
4357
const text = new TextDecoder().decode(contents);
4458
const parsed = JSON.parse(text);
4559
if (!Array.isArray(parsed))
@@ -49,7 +63,7 @@ export function loadStations() {
4963
.map(_sanitizeStation);
5064
} catch (error) {
5165
console.error('Failed to load stations', error);
52-
if (error.code === GLib.IOErrorEnum.NOT_FOUND) {
66+
if (error.code === Gio.IOErrorEnum.NOT_FOUND || error.code === GLib.IOErrorEnum.NOT_FOUND) {
5367
console.warn('Stations file not found, returning empty list');
5468
return [];
5569
}

0 commit comments

Comments
 (0)