Skip to content

Commit e999ec5

Browse files
committed
Clean up debouncer
Remove old submit methods and rename 'submit' to 'schedule'. Additionally, perform some stylistic changes (e.g. using trailing lambda and operator invocation syntax where possible).
1 parent 4fd2122 commit e999ec5

File tree

4 files changed

+11
-21
lines changed

4 files changed

+11
-21
lines changed

server/src/main/kotlin/org/javacs/kt/KotlinTextDocumentService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,15 @@ class KotlinTextDocumentService(
253253

254254
private fun lintLater(uri: URI) {
255255
lintTodo.add(uri)
256-
debounceLint.submit(::doLint)
256+
debounceLint.schedule(::doLint)
257257
}
258258

259259
private fun lintNow(file: URI) {
260260
lintTodo.add(file)
261261
debounceLint.submitImmediately(::doLint)
262262
}
263263

264-
private fun doLint(cancelCallback : () -> Boolean) {
264+
private fun doLint(cancelCallback: () -> Boolean) {
265265
LOG.info("Linting {}", describeURIs(lintTodo))
266266
val files = clearLint()
267267
val context = sp.compileFiles(files)

server/src/main/kotlin/org/javacs/kt/SourcePath.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ class SourcePath(
156156
fun compileAndUpdate(changed: List<SourceFile>, kind: CompilationKind): BindingContext? {
157157
if (changed.isEmpty()) return null
158158
val parse = changed.associateWith { it.parseIfChanged().parsed!! }
159-
val all = all()
159+
val allFiles = all()
160160
beforeCompileCallback.invoke()
161-
val (context, container) = cp.compiler.compileFiles(parse.values, all, kind)
161+
val (context, container) = cp.compiler.compileFiles(parse.values, allFiles, kind)
162162

163163
// Update cache
164164
for ((f, parsed) in parse) {

server/src/test/kotlin/org/javacs/kt/DebouncerTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class DebouncerTest {
1212

1313
@Test fun callQuickly() {
1414
for (i in 1..10) {
15-
debounce.submit { ->
15+
debounce.schedule {
1616
counter++
1717
}
1818
}
@@ -21,4 +21,4 @@ class DebouncerTest {
2121

2222
assertThat(counter, equalTo(1))
2323
}
24-
}
24+
}

shared/src/main/kotlin/org/javacs/kt/util/Debouncer.kt

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,26 @@ class Debouncer(
2121
private val delayMs = delay.toMillis()
2222
private var pendingTask: Future<*>? = null
2323

24-
fun submitImmediately(task: () -> Unit) {
25-
pendingTask?.cancel(false)
26-
pendingTask = executor.submit(task)
27-
}
28-
29-
fun submitImmediately(task: (cancelCallback : () -> Boolean) -> Unit) {
24+
fun submitImmediately(task: (cancelCallback: () -> Boolean) -> Unit) {
3025
pendingTask?.cancel(false)
3126
val currentTaskRef = AtomicReference<Future<*>>()
32-
val currentTask = executor.submit({ -> task.invoke({ -> val f = currentTaskRef.get(); f?.isCancelled()?:false})})
27+
val currentTask = executor.submit { task { currentTaskRef.get()?.isCancelled() ?: false } }
3328
currentTaskRef.set(currentTask);
3429
pendingTask = currentTask
3530
}
3631

37-
fun submit(task: () -> Unit) {
38-
pendingTask?.cancel(false)
39-
pendingTask = executor.schedule(task, delayMs, TimeUnit.MILLISECONDS)
40-
}
41-
42-
fun submit(task: (cancelCallback : () -> Boolean) -> Unit) {
32+
fun schedule(task: (cancelCallback: () -> Boolean) -> Unit) {
4333
pendingTask?.cancel(false)
4434
val currentTaskRef = AtomicReference<Future<*>>()
45-
val currentTask = executor.schedule({ -> task.invoke({ -> val f = currentTaskRef.get(); f?.isCancelled()?:false})}, delayMs, TimeUnit.MILLISECONDS)
35+
val currentTask = executor.schedule({ task { currentTaskRef.get()?.isCancelled() ?: false } }, delayMs, TimeUnit.MILLISECONDS)
4636
currentTaskRef.set(currentTask);
4737
pendingTask = currentTask
4838
}
4939

5040
fun waitForPendingTask() {
5141
pendingTask?.get()
5242
}
53-
43+
5444
fun shutdown(awaitTermination: Boolean) {
5545
executor.shutdown()
5646
if (awaitTermination) {

0 commit comments

Comments
 (0)