From b0c5e94249b8666af42fbe42ce457487f3450b4a Mon Sep 17 00:00:00 2001 From: leaderli <429243408@qq.com> Date: Wed, 30 Oct 2024 19:20:35 +0800 Subject: [PATCH] =?UTF-8?q?LiEventBus=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=B3=A8=E5=86=8C=E4=BA=8B=E4=BB=B6=E3=80=81=E5=8D=B8?= =?UTF-8?q?=E8=BD=BD=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../litool/core/event/ILiEventListener.java | 17 ++++++++++++++ .../litool/core/event/LiEventBus.java | 2 ++ .../litool/core/event/LiEventBusTest.java | 22 +++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/litool-core/src/main/java/io/leaderli/litool/core/event/ILiEventListener.java b/litool-core/src/main/java/io/leaderli/litool/core/event/ILiEventListener.java index 6a108d83..d4c5d485 100644 --- a/litool-core/src/main/java/io/leaderli/litool/core/event/ILiEventListener.java +++ b/litool-core/src/main/java/io/leaderli/litool/core/event/ILiEventListener.java @@ -23,6 +23,23 @@ @FunctionalInterface public interface ILiEventListener, S> extends ComponentType, OnError { + /** + * 当注册监听器时触发 + * {@link LiEventBus#registerListener(ILiEventListener)} + */ + default void onRegister() { + + } + + /** + * 当卸载监听器时触发 + * {@link LiEventBus#unRegisterListener(ILiEventListener)} + */ + default void onUnRegister() { + + } + + /** * 监听事件。当 {@link LiEventBus#push(LiEventObject)} 推送事件时,可以监听该事件的监听器会接收到该事件。 * diff --git a/litool-core/src/main/java/io/leaderli/litool/core/event/LiEventBus.java b/litool-core/src/main/java/io/leaderli/litool/core/event/LiEventBus.java index 4a7769c3..8b8bea88 100644 --- a/litool-core/src/main/java/io/leaderli/litool/core/event/LiEventBus.java +++ b/litool-core/src/main/java/io/leaderli/litool/core/event/LiEventBus.java @@ -24,6 +24,7 @@ public class LiEventBus implements LiEventBusBehavior { */ public , S> void registerListener(ILiEventListener listener) { liEventMap.put(listener.componentType(), listener); + listener.onRegister(); } /** @@ -34,6 +35,7 @@ public , S> void registerListener(ILiEventListener, S> void unRegisterListener(ILiEventListener listener) { liEventMap.remove(listener); + listener.onUnRegister(); } /** diff --git a/litool-core/src/test/java/io/leaderli/litool/core/event/LiEventBusTest.java b/litool-core/src/test/java/io/leaderli/litool/core/event/LiEventBusTest.java index d4ef6879..d092bb93 100644 --- a/litool-core/src/test/java/io/leaderli/litool/core/event/LiEventBusTest.java +++ b/litool-core/src/test/java/io/leaderli/litool/core/event/LiEventBusTest.java @@ -38,6 +38,28 @@ void getPublisher() { Assertions.assertDoesNotThrow(() -> { eventBus.push(new TestStringLiEventObject("456")); }); + LiBox count = LiBox.of(0); + ILiEventListener listener = new ILiEventListener() { + @Override + public void onRegister() { + count.value(1); + } + + @Override + public void onUnRegister() { + count.value(2); + } + + @Override + public void listen(String source) { + + } + }; + eventBus.registerListener(listener); + Assertions.assertEquals(1, count.value()); + eventBus.unRegisterListener(listener); + Assertions.assertEquals(2, count.value()); + } @Test