From 069a5d8f4a6d7569e6ecd66ba2a57a3fd4db582f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 10 Dec 2019 16:44:21 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Fix=20mark-post-known=20handler?= =?utf8?q?=20not=20being=20registered?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Turns out if you do not use constructor or member injection to create a type, Guice will not notify you about it. --- src/main/kotlin/net/pterodactylus/sone/utils/Functions.kt | 6 ++++++ .../web/notification/MarkPostKnownDuringFirstStartHandler.kt | 4 +++- .../sone/web/notification/NotificationHandlerModule.kt | 10 +++++++--- .../notification/MarkPostKnownDuringFirstStartHandlerTest.kt | 3 ++- 4 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/utils/Functions.kt 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) -- 2.7.4