ai/autohide-battery

TypeError: UPower is undefined

enku opened this issue · 8 comments

enku commented

Upgraded to version 24 on GNOME 42. Not sure why this is happening but

JS ERROR: TypeError: UPower is undefined
update/<@/home/marduk/.local/share/gnome-shell/extensions/autohide-battery@sitnik.ru/extension.js:22:25
getBattery@/home/marduk/.local/share/gnome-shell/extensions/autohide-battery@sitnik.ru/extension.js:39:13
update@/home/marduk/.local/share/gnome-shell/extensions/autohide-battery@sitnik.ru/extension.js:21:13

Although UPower seems to be defined on line 2. I've tried UPower = imports.ui.status.power.UPower on the Looking Glass prompt and get an object. Could there be some kind of race?

ai commented

Very strange. Sorry, I have no idea.

enku commented

(re)importing upower after line 19 works around the issue.

ai commented

Can you try this hack?

const ExtensionUtils = imports.misc.extensionUtils
- const UPower = imports.ui.status.power.UPower
+ const Power = imports.ui.status.power
const Main = imports.ui.main

let batteryWatching, settingsWatching, settings, disabled

function show() {
  getBattery((proxy, icon) => {
    icon.show()
  })
}

function hide() {
  getBattery((proxy, icon) => {
    icon.hide()
  })
}

function update() {
  let hideOn = settings.get_int('hide-on')
  getBattery(proxy => {
-    let isDischarging = proxy.State === UPower.DeviceState.DISCHARGING
-    let isFullyCharged = proxy.State === UPower.DeviceState.FULLY_CHARGED
-    if (proxy.Type !== UPower.DeviceKind.BATTERY) {
+    let isDischarging = proxy.State === Power.UPower.DeviceState.DISCHARGING
+    let isFullyCharged = proxy.State === Power.UPower.DeviceState.FULLY_CHARGED
+    if (proxy.Type !== Power.UPower.DeviceKind.BATTERY) {
      show()
    } else if (isFullyCharged) {
      hide()
    } else if (proxy.Percentage >= hideOn && !isDischarging) {
      hide()
    } else {
      show()
    }
  })
}
enku commented

Can you try this hack?

Yes, that works too. So seems like some race between when UPower is defined in power and when it is referenced in update().

ai commented

Great. I will release a fix in an hour. If you want, you can send PR to save your name in the project history.

enku commented

Actually I think it's still failing on on the initial update() but works in the subsequent callbacks. When I look at the extensions app it shows as failed with the undefined error yet it still "works" because the callbacks are registered before the first update().

image

ai commented

We can add if (Power.UPower) check to update

ai commented

Version 25 was pushed to https://extensions.gnome.org/extension/595/autohide-battery/

Feel free to open new issue if you find a problem.