From e39900b77ea6d492f72584dd12701004cab847e4 Mon Sep 17 00:00:00 2001
From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>
Date: Tue, 25 Mar 2025 12:12:29 +1100
Subject: style: ensure loading correct order

Cause async, older requests could load slower and therefore override newer requests
Also reduce the number of loads when spam
And add light scss var
---
 app.tsx | 36 +++++++++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 7 deletions(-)

(limited to 'app.tsx')

diff --git a/app.tsx b/app.tsx
index ff7601a..c23af96 100644
--- a/app.tsx
+++ b/app.tsx
@@ -26,13 +26,34 @@ const applyTransparency = (name: string, hex: string) => {
 
 const applyVibrancy = (hex: string) => (style.vibrant.get() ? `color.scale(${hex}, $saturation: 40%)` : hex);
 
-export const loadStyleAsync = async () => {
-    const schemeColours = Object.entries(Palette.get_default().colours)
-        .map(([name, hex]) => `$${name}: ${applyVibrancy(applyTransparency(name, hex))};`)
-        .join("\n");
-    await writeFileAsync(`${SRC}/scss/scheme/_index.scss`, `@use "sass:color";\n${schemeColours}`);
-    App.apply_css(await execAsync(`sass ${SRC}/style.scss`), true);
-};
+const styleLoader = new (class {
+    #running = false;
+    #dirty = false;
+
+    async run() {
+        this.#dirty = true;
+        if (this.#running) return;
+        this.#running = true;
+        while (this.#dirty) {
+            this.#dirty = false;
+            await this.#run();
+        }
+        this.#running = false;
+    }
+
+    async #run() {
+        const schemeColours = Object.entries(Palette.get_default().colours)
+            .map(([name, hex]) => `$${name}: ${applyVibrancy(applyTransparency(name, hex))};`)
+            .join("\n");
+        await writeFileAsync(
+            `${SRC}/scss/scheme/_index.scss`,
+            `@use "sass:color";\n$light: ${Palette.get_default().mode === "light"};\n${schemeColours}`
+        );
+        App.apply_css(await execAsync(`sass ${SRC}/style.scss`), true);
+    }
+})();
+
+export const loadStyleAsync = () => styleLoader.run();
 
 App.start({
     instanceName: "caelestia",
@@ -43,6 +64,7 @@ App.start({
         loadStyleAsync().catch(console.error);
         style.transparency.subscribe(() => loadStyleAsync().catch(console.error));
         Palette.get_default().connect("notify::colours", () => loadStyleAsync().catch(console.error));
+        Palette.get_default().connect("notify::mode", () => loadStyleAsync().catch(console.error));
 
         initConfig();
 
-- 
cgit v1.2.3-freya

