Skip to content

Commit

Permalink
LiEventBus增加自定义注册事件、卸载事件
Browse files Browse the repository at this point in the history
  • Loading branch information
leaderli committed Oct 30, 2024
1 parent 23534d5 commit b0c5e94
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@
@FunctionalInterface
public interface ILiEventListener<E extends LiEventObject<S>, S> extends ComponentType<E>, OnError {

/**
* 当注册监听器时触发
* {@link LiEventBus#registerListener(ILiEventListener)}
*/
default void onRegister() {

}

/**
* 当卸载监听器时触发
* {@link LiEventBus#unRegisterListener(ILiEventListener)}
*/
default void onUnRegister() {

}


/**
* 监听事件。当 {@link LiEventBus#push(LiEventObject)} 推送事件时,可以监听该事件的监听器会接收到该事件。
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class LiEventBus implements LiEventBusBehavior {
*/
public <E extends LiEventObject<S>, S> void registerListener(ILiEventListener<E, S> listener) {
liEventMap.put(listener.componentType(), listener);
listener.onRegister();
}

/**
Expand All @@ -34,6 +35,7 @@ public <E extends LiEventObject<S>, S> void registerListener(ILiEventListener<E,
@Override
public <E extends LiEventObject<S>, S> void unRegisterListener(ILiEventListener<E, S> listener) {
liEventMap.remove(listener);
listener.onUnRegister();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ void getPublisher() {
Assertions.assertDoesNotThrow(() -> {
eventBus.push(new TestStringLiEventObject("456"));
});
LiBox<Integer> count = LiBox.of(0);
ILiEventListener<TestStringLiEventObject, String> listener = new ILiEventListener<TestStringLiEventObject, String>() {
@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
Expand Down

0 comments on commit b0c5e94

Please sign in to comment.