EventBus源码阅读(24)-EventBus-Sticky

就从原来没有度过的几个函数开始吧!


1、postSticky(Object)

    /**
     * Posts the given event to the event bus and holds on to the event (because it is sticky). The most recent sticky
     * event of an event's type is kept in memory for future access by subscribers using {@link Subscribe#sticky()}.
     */
    public void postSticky(Object event) {
        synchronized (stickyEvents) {
            stickyEvents.put(event.getClass(), event);
        }
        // Should be posted after it is putted, in case the subscriber wants to remove immediately
        post(event);
    }

先把事件放到stickyEvents中存储,然后再调用常规的post

可以看出,每种类型的事件最多缓存一个。

而在读注册的时候,会检查是否有sticky事件。如果有则处理。

sticky事件是先发生事件,后来才有相应的方法处理。

而常规的事件是现有处理方法,后发生事件。

2、getStickyEvent

/**
 * Gets the most recent sticky event for the given type.
 *
 * @see #postSticky(Object)
 */
public <T> T getStickyEvent(Class<T> eventType) {
    synchronized (stickyEvents) {
        return eventType.cast(stickyEvents.get(eventType));
    }
}
从代码中看,也没有什么好说的,就是获取缓存中存在的某一种类型的事件。

3、removeStickyEvent(Class)

/**
 * Remove and gets the recent sticky event for the given event type.
 *
 * @see #postSticky(Object)
 */
public <T> T removeStickyEvent(Class<T> eventType) {
    synchronized (stickyEvents) {
        return eventType.cast(stickyEvents.remove(eventType));
    }
}
删除存在的某类事件
4、 removeStickyEvent(Object)

/**
 * Removes the sticky event if it equals to the given event.
 *
 * @return true if the events matched and the sticky event was removed.
 */
public boolean removeStickyEvent(Object event) {
    synchronized (stickyEvents) {
        Class<?> eventType = event.getClass();
        Object existingEvent = stickyEvents.get(eventType);
        if (event.equals(existingEvent)) {
            stickyEvents.remove(eventType);
            return true;
        } else {
            return false;
        }
    }
}
删除某个缓存的事件








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
abp-vnex eventbus是一个用于在ABP框架中进行事件通信的模块。要使用abp-vnex eventbus,您需要按照以下步骤进行安装和配置: 1. 首先,您需要安装abp-vnex eventbus模块。可以通过运行以下命令来安装: ```shell npm install abp-vnex-eventbus --save ``` 2. 安装完成后,您需要在您的应用程序的模块中导入abp-vnex eventbus模块。在您的模块文件中,添加以下代码: ```typescript import { AbpVnexEventBusModule } from 'abp-vnex-eventbus'; @NgModule({ imports: [ AbpVnexEventBusModule ] }) export class YourModule { } ``` 3. 现在,您可以在您的组件或服务中使用abp-vnex eventbus来发送和接收事件。首先,您需要导入`AbpVnexEventBusService`: ```typescript import { AbpVnexEventBusService } from 'abp-vnex-eventbus'; ``` 4. 在您的组件或服务中,您可以使用`AbpVnexEventBusService`的`emit`方法来发送事件。例如,发送一个名为`myEvent`的事件: ```typescript constructor(private eventBus: AbpVnexEventBusService) { } sendEvent() { this.eventBus.emit('myEvent', { data: 'Hello World' }); } ``` 5. 要接收事件,您可以使用`AbpVnexEventBusService`的`on`方法。在您的组件或服务中,添加以下代码: ```typescript constructor(private eventBus: AbpVnexEventBusService) { } ngOnInit() { this.eventBus.on('myEvent').subscribe((eventData) => { console.log(eventData.data); // 输出:Hello World }); } ``` 这样,您就可以使用abp-vnex eventbus模块来进行事件通信了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值