Skip to content

Commit

Permalink
feat: real debounced sync for better consecutive sync handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ViscousPot committed Dec 28, 2024
1 parent 1632c08 commit d94272a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/src/main/java/com/viscouspot/gitsync/GitSyncService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.work.WorkManager
import com.viscouspot.gitsync.util.GitManager
import com.viscouspot.gitsync.util.Helper
import com.viscouspot.gitsync.util.Helper.CONFLICT_NOTIFICATION_ID
import com.viscouspot.gitsync.util.Helper.debounced
import com.viscouspot.gitsync.util.LogType
import com.viscouspot.gitsync.util.Logger.log
import com.viscouspot.gitsync.util.NetworkWorker
Expand All @@ -35,6 +36,10 @@ class GitSyncService : Service() {
private var isSyncing: Boolean = false
private val debouncePeriod: Long = 10 * 1000

private val debouncedSyncFn = debounced<Boolean>(1000) { forced ->
sync(forced)
}

companion object {
const val MERGE = "MERGE"
const val FORCE_SYNC = "FORCE_SYNC"
Expand Down Expand Up @@ -101,7 +106,7 @@ class GitSyncService : Service() {
log(LogType.Sync, "Sync Scheduled")
return
} else {
sync(forced)
debouncedSyncFn(forced)
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion app/src/main/java/com/viscouspot/gitsync/util/Helper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ import com.jcraft.jsch.KeyPair
import com.viscouspot.gitsync.MainActivity
import com.viscouspot.gitsync.R
import com.viscouspot.gitsync.util.Logger.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.io.BufferedReader
import java.io.ByteArrayOutputStream
import java.io.File
Expand All @@ -41,10 +46,20 @@ import java.io.InputStreamReader
import java.nio.charset.StandardCharsets
import kotlin.random.Random


object Helper {
const val CONFLICT_NOTIFICATION_ID = 1756

fun <T> debounced(delayMillis: Long, action: (T) -> Unit): (T) -> Unit {
var job: Job? = null
return { param: T ->
job?.cancel()
job = CoroutineScope(Dispatchers.Main).launch {
delay(delayMillis)
action(param)
}
}
}

fun extractConflictSections(context: Context, file: File, add: (text: String) -> Unit) {
val conflictBuilder = StringBuilder()
var inConflict = false
Expand Down

0 comments on commit d94272a

Please sign in to comment.