# Event Emitter
The `EventEmitter` lets you register multiple observers. While there is no observer, the events are enqueued.
To unregister, call `stopListening()` on the `NotificationToken` returned from `eventEmitter.startListening`. You can also use a `CompositeNotificationToken`.
`EventEmitter` implements `EventSource`, so that the `EventEmitter` can be exposed as something to be observed, but cannot be emitted to from the outside.
You can only emit events and listen for events and unregister observers on the thread where you created the `EventEmitter`.
## Example
``` kotlin
// write
private val emitter: EventEmitter<String> = EventEmitter()
val events: EventSource<String> get() = emitter
fun doSomething() {
emitter.emit("hello")
}
// read
private var subscription: EventSource.NotificationToken? = null
fun observe() {
subscription = events.startListening { event ->
showToast(event)
}
}
fun unsubscribe() {
subscription?.stopListening()
subscription = null
}
```
## Example with LifecycleOwner + observe
See https://github.com/Zhuinden/live-event
## Using Event Emitter
In order to use Event Emitter, you need to add jitpack to your project root gradle:
buildscript {
repositories {
// ...
maven { url "https://jitpack.io" }
}
// ...
}
allprojects {
repositories {
// ...
maven { url "https://jitpack.io" }
}
// ...
}
In newer projects, you need to also update the `settings.gradle` file's `dependencyResolutionManagement` block:
```
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' } // <--
jcenter() // Warning: this repository is going to shut down soon
}
}
```
and add the dependency to your module level gradle.
implementation 'com.github.Zhuinden:event-emitter:1.4.0'
## License
Copyright 2019-2023 Gabor Varadi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
[ACTIVE]事件发射器允许您注册多个观察者,但在没有观察者时将事件排入队列。.zip (14个子文件)
event-emitter-master
gradle
wrapper
gradle-wrapper.jar 54KB
gradle-wrapper.properties 200B
LICENSE.txt 11KB
src
test
java
com
zhuinden
eventemitter
TestSuite.java 840B
EventEmitterTest.java 13KB
main
java
com
zhuinden
eventemitter
EventEmitter.java 4KB
CompositeNotificationToken.java 2KB
EventSource.java 2KB
gradlew.bat 2KB
CHANGELOG.md 1KB
build.gradle 1KB
gradlew 5KB
.gitignore 67B
README.md 3KB
共 14 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9152
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功