Improve initial load

AuthorKonata <konata@posteo.jp>
Date
Commit44e0a38d5c4a6d98e1ff1983bef30546c556657b
Parent34f059a
1 file changed, 25 insertions(+), 8 deletions(-)
▾Mwebclient/src/App.tsx
@@ -71,9 +71,22 @@ function bufferedAhead(ranges: TimeRanges, time: number): number {
return 0; //the playhead is starving, so append immediately
}
//initial directory, resolved synchronously so the very first listing fetch already targets it
function initialDir(): string {
const hash = window.location.hash.slice(1);
if (hash) return decodePath(hash);
try {
const savedOptions = localStorage.getItem(LocalStorageValues.options);
const rememberDir = savedOptions ? (JSON.parse(savedOptions) as Partial<AppOptions>).rememberDir : false;
const savedPath = localStorage.getItem(LocalStorageValues.path);
if (rememberDir && savedPath !== null) return savedPath;
} catch {}
return decodePath("");
}
const App: Component = () => {
const [showFileBrowser, setShowFileBrowser] = createSignal(false);
const [currentDir, setCurrentDir] = createSignal(decodePath(window.location.hash.slice(1)));
const [currentDir, setCurrentDir] = createSignal(initialDir());
const [playlist, setPlaylist] = createSignal<PlaylistItem[]>([]);
const [playerState, setPlayerState] = createStore<PlayerState>({
isPlaying: false,
@@ -132,7 +145,17 @@ const App: Component = () => {
const [breadcrumbs, setBreadcrumbs] = createSignal<string[]>([]);
const [isShuffled, setShuffled] = createSignal(false);
const [isRepeated, setRepeated] = createSignal(false);
const [isOffline, setOffline] = createSignal(false);
//read the persisted mode synchronously so the very first listing fetch below already runs in the
//correct mode
const [isOffline, setOffline] = createSignal(
(() => {
try {
return localStorage.getItem(LocalStorageValues.offline) === "true";
} catch {
return false;
}
})(),
);
const [files, fetchFiles] = createResource<FlatFileListing, { dir: string; offline: boolean }>(
() => ({ dir: currentDir(), offline: isOffline() }),
async (args) => {
@@ -802,12 +825,6 @@ const App: Component = () => {
setShuffled(savedShuffle === "true");
const savedRepeat = localStorage.getItem(LocalStorageValues.repeat);
setRepeated(savedRepeat === "true");
const savedOffline = localStorage.getItem(LocalStorageValues.offline);
if (savedOffline !== null) setOffline(savedOffline === "true");
const savedPath = localStorage.getItem(LocalStorageValues.path);
if (options.rememberDir && savedPath !== null && !window.location.hash) {
setCurrentDir(savedPath);
}
} catch (e) {
toast("loading values from localStorage failed", "error", e);
localStorage.clear();