From 32b11307790766f5df7cea1dfe87e7cc39ae6669 Mon Sep 17 00:00:00 2001
From: matt1432 <matt@nelim.org>
Date: Thu, 7 Dec 2023 16:48:34 -0500
Subject: [PATCH] refactor(ags): add persist function to simplify code

---
 .../wim/config/ags/js/bar/buttons/heart.js    | 29 ++++---------
 devices/wim/config/ags/js/misc/persist.js     | 41 +++++++++++++++++++
 devices/wim/config/ags/js/setup.js            | 31 ++++----------
 3 files changed, 56 insertions(+), 45 deletions(-)
 create mode 100644 devices/wim/config/ags/js/misc/persist.js

diff --git a/devices/wim/config/ags/js/bar/buttons/heart.js b/devices/wim/config/ags/js/bar/buttons/heart.js
index 898b57a8..0571a7fc 100644
--- a/devices/wim/config/ags/js/bar/buttons/heart.js
+++ b/devices/wim/config/ags/js/bar/buttons/heart.js
@@ -1,31 +1,18 @@
 import { Box, Label } from 'resource:///com/github/Aylur/ags/widget.js';
-import { execAsync, readFileAsync } from 'resource:///com/github/Aylur/ags/utils.js';
 
 import Variable from 'resource:///com/github/Aylur/ags/variable.js';
 
-const { get_home_dir } = imports.gi.GLib;
-
 import EventBox from '../../misc/cursorbox.js';
+import Persist from '../../misc/persist.js';
 
-const HeartState = Variable('');
-const heartFile = `${get_home_dir()}/.cache/ags/.heart`;
+const HeartState = Variable();
 
-const stateCmd = () => ['bash', '-c',
-    `echo ${HeartState.value === ''} > ${heartFile}`];
-const monitorState = () => {
-    HeartState.connect('changed', () => {
-        execAsync(stateCmd()).catch(print);
-    });
-};
-
-// On launch
-readFileAsync(heartFile).then((content) => {
-    HeartState.value = JSON.parse(content) ? '' : '󰣐';
-    monitorState();
-}).catch(() => {
-    execAsync(stateCmd()).then(() => {
-        monitorState();
-    }).catch(print);
+Persist({
+    name: 'heart',
+    gobject: HeartState,
+    prop: 'value',
+    condition: '',
+    whenFalse: '󰣐',
 });
 
 
diff --git a/devices/wim/config/ags/js/misc/persist.js b/devices/wim/config/ags/js/misc/persist.js
new file mode 100644
index 00000000..d1b79618
--- /dev/null
+++ b/devices/wim/config/ags/js/misc/persist.js
@@ -0,0 +1,41 @@
+import { execAsync, readFileAsync, timeout } from 'resource:///com/github/Aylur/ags/utils.js';
+const { get_home_dir } = imports.gi.GLib;
+
+export default ({
+    name,
+    gobject,
+    prop,
+    condition = true,
+    whenTrue = condition,
+    whenFalse = false,
+    signal = 'changed',
+} = {}) => {
+    const cacheFile = `${get_home_dir()}/.cache/ags/.${name}`;
+
+    const stateCmd = () => ['bash', '-c',
+        `echo ${gobject[prop] === condition} > ${cacheFile}`];
+
+    const monitorState = () => {
+        gobject.connect(signal, () => {
+            execAsync(stateCmd()).catch(print);
+        });
+    };
+
+    readFileAsync(cacheFile)
+        .then((content) => {
+            // JSON.parse was the only way I found to reliably
+            // convert a string of 'true' or 'false' into a bool
+            gobject[prop] = JSON.parse(content) ? whenTrue : whenFalse;
+
+            timeout(1000, () => {
+                monitorState();
+            });
+        })
+        .catch(() => {
+            execAsync(stateCmd())
+                .then(() => {
+                    monitorState();
+                })
+                .catch(print);
+        });
+};
diff --git a/devices/wim/config/ags/js/setup.js b/devices/wim/config/ags/js/setup.js
index c63553a6..0ab63848 100644
--- a/devices/wim/config/ags/js/setup.js
+++ b/devices/wim/config/ags/js/setup.js
@@ -1,15 +1,14 @@
 import App from 'resource:///com/github/Aylur/ags/app.js';
 import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js';
 import Bluetooth from 'resource:///com/github/Aylur/ags/service/bluetooth.js';
-import { execAsync, readFileAsync, timeout } from 'resource:///com/github/Aylur/ags/utils.js';
-
-const { get_home_dir } = imports.gi.GLib;
 
 import Brightness from '../services/brightness.js';
 import Pointers from '../services/pointers.js';
 import Tablet from '../services/tablet.js';
 import TouchGestures from '../services/touch-gestures.js';
+
 import closeAll from './misc/closer.js';
+import Persist from './misc/persist.js';
 
 
 export default () => {
@@ -18,29 +17,13 @@ export default () => {
     globalThis.Tablet = Tablet;
     globalThis.closeAll = closeAll;
 
-    // Persist Bluetooth state
-    const bluetoothFile = `${get_home_dir()}/.cache/ags/.bluetooth`;
-    const stateCmd = () => ['bash', '-c',
-        `echo ${Bluetooth.enabled} > ${bluetoothFile}`];
-    const monitorState = () => {
-        Bluetooth.connect('notify::enabled', () => {
-            execAsync(stateCmd()).catch(print);
-        });
-    };
-
-    // On launch
-    readFileAsync(bluetoothFile).then((content) => {
-        Bluetooth.enabled = JSON.parse(content);
-        timeout(1000, () => {
-            monitorState();
-        });
-    }).catch(() => {
-        execAsync(stateCmd()).then(() => {
-            monitorState();
-        }).catch(print);
+    Persist({
+        name: 'bluetooth',
+        gobject: Bluetooth,
+        prop: 'enabled',
+        signal: 'notify::enabled',
     });
 
-
     TouchGestures.addGesture({
         name: 'openAppLauncher',
         gesture: 'UD',