From: David ‘Bombe’ Roden Date: Tue, 10 Dec 2019 15:44:21 +0000 (+0100) Subject: 🐛 Fix mark-post-known handler not being registered X-Git-Tag: v81^2~5^2~70 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=069a5d8f4a6d7569e6ecd66ba2a57a3fd4db582f;p=Sone.git 🐛 Fix mark-post-known handler not being registered Turns out if you do not use constructor or member injection to create a type, Guice will not notify you about it. --- diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Functions.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Functions.kt new file mode 100644 index 0000000..d7fb595 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Functions.kt @@ -0,0 +1,6 @@ +package net.pterodactylus.sone.utils + +import java.util.function.* + +/** Allows easy invocation of Java Consumers. */ +operator fun Consumer.invoke(t: T) = accept(t) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/MarkPostKnownDuringFirstStartHandler.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/MarkPostKnownDuringFirstStartHandler.kt index 69144e3..caca76e 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/MarkPostKnownDuringFirstStartHandler.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/MarkPostKnownDuringFirstStartHandler.kt @@ -21,7 +21,9 @@ import com.google.common.eventbus.* import net.pterodactylus.sone.core.event.* import net.pterodactylus.sone.data.* import net.pterodactylus.sone.notify.* +import net.pterodactylus.sone.utils.* import net.pterodactylus.util.notify.* +import java.util.function.* import javax.inject.* /** @@ -29,7 +31,7 @@ import javax.inject.* * the [notification manager][NotificationManager] shows a [first start notification] * [NotificationManager.hasFirstStartNotification]. */ -class MarkPostKnownDuringFirstStartHandler(private val notificationManager: NotificationManager, private val markPostAsKnown: (Post) -> Unit) { +class MarkPostKnownDuringFirstStartHandler @Inject constructor(private val notificationManager: NotificationManager, private val markPostAsKnown: Consumer) { @Subscribe fun newPostFound(newPostFoundEvent: NewPostFoundEvent) { diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt index 7571f67..4737267 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt @@ -18,12 +18,14 @@ package net.pterodactylus.sone.web.notification import com.google.inject.* +import com.google.inject.binder.* import net.pterodactylus.sone.core.* import net.pterodactylus.sone.data.* import net.pterodactylus.sone.main.* import net.pterodactylus.sone.notify.* import net.pterodactylus.util.notify.* import java.util.concurrent.Executors.* +import java.util.function.* import javax.inject.* import javax.inject.Singleton @@ -34,12 +36,11 @@ class NotificationHandlerModule : AbstractModule() { override fun configure() { bind(NotificationHandler::class.java).`in`(Singleton::class.java) + bind().asSingleton() } @Provides - @Singleton - fun getMarkPostKnownDuringFirstStartHandler(core: Core, notificationManager: NotificationManager) = - MarkPostKnownDuringFirstStartHandler(notificationManager, core::markPostKnown) + fun getMarkPostKnownHandler(core: Core): Consumer = Consumer { core.markPostKnown(it) } @Provides @Singleton @@ -84,4 +85,7 @@ class NotificationHandlerModule : AbstractModule() { fun getSoneLockedHandler(notificationManager: NotificationManager, @Named("soneLocked") soneLockedNotification: ListNotification) = SoneLockedHandler(notificationManager, soneLockedNotification, newScheduledThreadPool(1)) + private inline fun bind(): AnnotatedBindingBuilder = bind(T::class.java) + private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java) + } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/notification/MarkPostKnownDuringFirstStartHandlerTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/notification/MarkPostKnownDuringFirstStartHandlerTest.kt index 286648a..28a1719 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/notification/MarkPostKnownDuringFirstStartHandlerTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/MarkPostKnownDuringFirstStartHandlerTest.kt @@ -24,6 +24,7 @@ import net.pterodactylus.util.notify.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import java.io.* +import java.util.function.Consumer import kotlin.test.* /** @@ -35,7 +36,7 @@ class MarkPostKnownDuringFirstStartHandlerTest { private val eventBus = EventBus() private val notificationManager = NotificationManager() private val markedPosts = mutableListOf() - private val handler = MarkPostKnownDuringFirstStartHandler(notificationManager) { markedPosts += it } + private val handler = MarkPostKnownDuringFirstStartHandler(notificationManager, Consumer { markedPosts += it }) init { eventBus.register(handler)