From bee2970a3b47bafa144a1191cfbe8c18fccf5b07 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mikl=C3=B3s=20T=C3=B3th?= <tothmiklostibor@gmail.com>
Date: Sat, 15 Jan 2022 04:21:51 +0100
Subject: [PATCH] Small fixes

---
 src/jsMain/kotlin/PekDelay.kt | 49 +++++++++++++++++++++++++++++++++++
 src/jsMain/kotlin/PekTable.kt |  6 ++++-
 src/jsMain/kotlin/Process.kt  |  8 +++---
 src/jsMain/kotlin/Utils.kt    | 32 +----------------------
 4 files changed, 59 insertions(+), 36 deletions(-)
 create mode 100644 src/jsMain/kotlin/PekDelay.kt

diff --git a/src/jsMain/kotlin/PekDelay.kt b/src/jsMain/kotlin/PekDelay.kt
new file mode 100644
index 0000000..f0525da
--- /dev/null
+++ b/src/jsMain/kotlin/PekDelay.kt
@@ -0,0 +1,49 @@
+import kotlinx.atomicfu.AtomicInt
+import kotlinx.browser.document
+import kotlinx.coroutines.delay
+import org.w3c.dom.HTMLDivElement
+import org.w3c.dom.MutationObserver
+import org.w3c.dom.MutationObserverInit
+import kotlin.coroutines.Continuation
+import kotlin.coroutines.resume
+import kotlin.coroutines.suspendCoroutine
+
+object PekDelay {
+    private val saveIconElem = document.getElementById("save-icon") as HTMLDivElement
+    private val isUpdating get() = saveIconElem.style.display == "block"
+
+    private val continuations = mutableListOf<Continuation<Unit>>()
+    private var skips = 0
+
+    fun skipNext() {
+        skips++
+    }
+
+    private val observer = MutationObserver { _, _ ->
+        if (!isUpdating) {
+            println("run called")
+            val conts = continuations.toList()
+            conts.forEach { it.resume(Unit) }
+            continuations.removeAll(conts)
+        }
+    }
+
+    init {
+        observer.observe(
+            saveIconElem, MutationObserverInit(
+                attributes = true,
+                attributeFilter = arrayOf("style")
+            )
+        )
+    }
+
+    suspend operator fun invoke() = suspendCoroutine<Unit> { cont ->
+        if (skips > 0) {
+            cont.resume(Unit)
+            skips--
+            return@suspendCoroutine
+        }
+        continuations += cont
+    }
+
+}
\ No newline at end of file
diff --git a/src/jsMain/kotlin/PekTable.kt b/src/jsMain/kotlin/PekTable.kt
index 5de79ea..d656b0c 100644
--- a/src/jsMain/kotlin/PekTable.kt
+++ b/src/jsMain/kotlin/PekTable.kt
@@ -57,7 +57,11 @@ object PekTable {
             elem.style.background = origBg
         }
 
-        if (elem.value.toIntOrNull() == value) return
+        if (elem.value.toIntOrNull() == value) { // don't update if up-to-date
+            // sort of ugly workaround, meh.
+            PekDelay.skipNext()
+            return
+        }
 
         elem.click()
         elem.value = value.toString()
diff --git a/src/jsMain/kotlin/Process.kt b/src/jsMain/kotlin/Process.kt
index 46ad426..df0c4ec 100644
--- a/src/jsMain/kotlin/Process.kt
+++ b/src/jsMain/kotlin/Process.kt
@@ -1,9 +1,9 @@
 import kotlin.math.max
 
-class ProcessingError(val errors: List<String>): Exception(errors.toString()) {
+class ProcessingError(private val errors: List<String>): Exception(errors.toString()) {
     override fun toString(): String {
-        return "Hiba történt az importálás közben:\n"+
-            errors.joinToString("\n")
+        return "${errors.size} hiba történt az importálás közben:\n\n"+
+            errors.joinToString("\n\n")
     }
 }
 
@@ -29,7 +29,7 @@ suspend fun CSVTable.process(
             val pekPrinciple = principleMappings[principle]!!
 
             PekTable[pekPerson, pekPrinciple] = point
-            pekDelay()
+            PekDelay()
             progress(calcProgress(i, j, people.size, principles.size))
 
             if (person != pekPerson) {
diff --git a/src/jsMain/kotlin/Utils.kt b/src/jsMain/kotlin/Utils.kt
index 50b67b3..ed2f64c 100644
--- a/src/jsMain/kotlin/Utils.kt
+++ b/src/jsMain/kotlin/Utils.kt
@@ -1,6 +1,4 @@
 import kotlinx.browser.document
-import kotlinx.browser.window
-import kotlinx.coroutines.delay
 import org.w3c.dom.*
 import org.w3c.files.Blob
 import org.w3c.files.FileReader
@@ -86,41 +84,13 @@ suspend fun PekTable.clear(
     people.forEachIndexed { i, person ->
         principles.forEachIndexed { j, principle ->
             PekTable[person, principle] = 0
-            pekDelay()
+            PekDelay()
 
             progress(calcProgress(i, j, people.size, principles.size))
         }
     }
 }
 
-private val saveIconElem = document.getElementById("save-icon") as HTMLDivElement
-val isUpdating get() = saveIconElem.style.display == "block"
-
-fun observeSaveChange(changed: () -> Unit): MutationObserver {
-    val observer = MutationObserver { _, _ ->
-        changed()
-    }
-    observer.observe(saveIconElem, MutationObserverInit(
-        attributes = true,
-        attributeFilter = arrayOf("style")
-    ))
-    return observer
-}
-
-suspend fun pekDelay() = suspendCoroutine<Unit> { cont ->
-    lateinit var observer: MutationObserver
-    observer = observeSaveChange {
-        if (!isUpdating) {
-            cont.resume(Unit)
-            observer.disconnect()
-        }
-    }
-    if (!isUpdating) {
-        cont.resume(Unit)
-        observer.disconnect()
-    }
-}
-
 fun Double.format() = this.asDynamic().toFixed(1) as String
 
 fun calcProgress(i: Int, j: Int, iMax: Int, jMax: Int) =
-- 
GitLab