Skip to content

Commit

Permalink
Merge pull request #33 from saalfeldlab/perf/2.1.0
Browse files Browse the repository at this point in the history
Perf/2.1.0
  • Loading branch information
cmhulbert authored Aug 12, 2024
2 parents 9dcfd33 + 2b7a2c4 commit ddd136c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
<scm>
<connection>scm:git:git://github.com/saalfeldlab/saalfx</connection>
<developerConnection>scm:git:[email protected]:saalfeldlab/saalfx.git</developerConnection>
<tag>saalfx-1.3.0</tag>
<tag>saalfx-2.0.0</tag>
<url>https://github.com/saalfeldlab/saalfx</url>
</scm>

Expand Down
13 changes: 5 additions & 8 deletions src/main/kotlin/org/janelia/saalfeldlab/fx/actions/Action.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,15 @@ open class Action<E : Event>(val eventType: EventType<E>) {

private fun testChecks(event: E?): Boolean {
return checks.isEmpty() || let {
var valid = true
for ((description, check) in checks) {
valid = valid && check(event)
if (!valid) {
val msg = description?.let {
"$it (${check::class.java})"
} ?: "(${check::class.java})"
if (!check(event)) {
val startMsg = description?.let { "$it " } ?: ""
val msg ="$startMsg(${check::class.java})"
logger.trace { "Check: $msg did not pass" }
break
return false
}
}
valid
true
}
}

Expand Down
41 changes: 16 additions & 25 deletions src/main/kotlin/org/janelia/saalfeldlab/fx/actions/ActionSet.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package org.janelia.saalfeldlab.fx.actions

import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import javafx.event.Event
import javafx.event.EventHandler
import javafx.event.EventType
import javafx.scene.Node
import javafx.scene.input.*
import javafx.stage.Window
import org.janelia.saalfeldlab.fx.event.KeyTracker
import java.lang.invoke.MethodHandles
import java.util.function.Consumer


Expand Down Expand Up @@ -64,21 +61,19 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul
apply?.let { it(this) }
}

private fun testChecksForEventType(event: Event, eventType: EventType<out Event> = event.eventType): Boolean {
return checks[eventType]?.let { checks ->
var pass = true
for ((reason, check) in checks) {
if (!check(event)) {
ACTION_SET_LOGGER.debug { "Verify All Failed: $reason" }
pass = false
break
}
private fun Action<out Event>.testChecksForEventType(event: Event, eventType: EventType<out Event> = event.eventType): Boolean {
val checks = checks[eventType] ?: return true

for ((reason, check) in checks) {
if (!check(event)) {
logger.debug { "Verify All Failed: $reason" }
return false
}
pass
} ?: true
}
return true
}

private tailrec fun testChecksForInheritedEventTypes(event: Event, eventType: EventType<out Event>? = event.eventType): Boolean {
private tailrec fun Action<out Event>.testChecksForInheritedEventTypes(event: Event, eventType: EventType<out Event>? = event.eventType): Boolean {
if (eventType == null) return true
return if (!testChecksForEventType(event, eventType)) false else testChecksForInheritedEventTypes(event, eventType.superType)
}
Expand Down Expand Up @@ -194,7 +189,7 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul

/* configure via the callback*/
withAction()
}.also { addAction(it)}
}.also { addAction(it) }
}

/**
Expand Down Expand Up @@ -300,7 +295,7 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul

/* configure based on the callback */
withAction()
}.also { addAction(it)}
}.also { addAction(it) }
}

/**
Expand Down Expand Up @@ -391,9 +386,7 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul
try {
action(event)
} catch (e: Exception) {
val logger = if (action.filter) FILTER_LOGGER else ACTION_LOGGER
val nameText = action.name?.let { "$name: $it" } ?: name
logger.error(e) { "$nameText (${event.eventType} was valid, but failed" }
action.logger.error(e) { "${event.eventType} was valid, but failed" }
throw e
}
} else {
Expand All @@ -410,7 +403,9 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul
* @param action the [Action] to handle [event]
* @param event to handle
*/
protected open fun <E : Event> preInvokeCheck(action: Action<E>, event: E) = action.canHandleEvent(event.eventType) && testChecksForInheritedEventTypes(event)
protected open fun <E : Event> preInvokeCheck(action: Action<E>, event: E) = action.run {
canHandleEvent(event.eventType) && testChecksForInheritedEventTypes(event)
}

/**
* Optional callback for before the extension functions [installActionSet] are called.
Expand Down Expand Up @@ -441,10 +436,6 @@ open class ActionSet(val name: String, var keyTracker: () -> KeyTracker? = { nul

companion object {

private val ACTION_SET_LOGGER: KLogger = KotlinLogging.logger {}
private val ACTION_LOGGER: KLogger = KotlinLogging.logger("${MethodHandles.lookup().lookupClass().name}-Action")
private val FILTER_LOGGER: KLogger = KotlinLogging.logger("${MethodHandles.lookup().lookupClass().name}-Filter")

/**
* Install [actionSet] in the receiver [Node]
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/
package org.janelia.saalfeldlab.fx.util

import javafx.application.Platform
import kotlinx.coroutines.*
import java.lang.Runnable

Expand All @@ -42,10 +41,7 @@ class InvokeOnJavaFXApplicationThread {
operator fun invoke(task: suspend CoroutineScope.() -> Unit) = sharedMainScope.launch(block = task)

@JvmStatic
operator fun invoke(task: Runnable) {
if (Platform.isFxApplicationThread()) task.run()
else invoke { task.run() }
}
operator fun invoke(task: Runnable) = invoke { task.run() }

@Throws(InterruptedException::class)
fun invokeAndWait(task: suspend CoroutineScope.() -> Unit) = runBlocking {
Expand Down

0 comments on commit ddd136c

Please sign in to comment.