commit 312097e6f1ae7261375f61e99aa40838cdc57515
parent 5d6160f8d5cf3e7b407a75d3fb41c2e816720ba7
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Wed, 7 Aug 2024 16:46:20 -0700
Handle promises correctly in ui's toggle function
If the button's function returns an object with a 'then' function,
then wrap the button replacement in a call to that then function.
Before, if a promise was used and it threw an error, the button
would still be flipped.
Diffstat:
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/files/lib/ui.js b/files/lib/ui.js
@@ -13,12 +13,20 @@ export function button(name, memo, icon, func) {
export function toggle(a, afunc, b, bfunc) {
a.addEventListener("click", function() {
- afunc()
- a.replaceWith(b)
+ let r = afunc()
+ if (r && typeof r.then == "function") {
+ r.then(function() { a.replaceWith(b) })
+ } else {
+ a.replaceWith(b)
+ }
}, false)
b.addEventListener("click", function() {
- bfunc()
- b.replaceWith(a)
+ let r = bfunc()
+ if (r && typeof r.then == "function") {
+ r.then(function() { b.replaceWith(a) })
+ } else {
+ b.replaceWith(a)
+ }
}, false)
return a
-}
-\ No newline at end of file
+}