- Kotlin Blueprints
- Ashish Belagali Hardik Trivedi Akshay Chordiya
- 131字
- 2021-07-02 21:50:18
Event broadcaster
The ReactiveBroadcaster class is an event broadcaster that handles the responsibility of subscribing the observers and sending the updates to interested observers asynchronously and will also do the cleanup after the completion of the events:
/**
* Handles the event broadcasting to the observers in an
* asynchronous way.
*/
class ReactiveBroadcaster {
/**
* Set of emitters for multiple events
*/
private var emitters = synchronizedSet(HashSet<SseEmitter>())
/**
* Subscribe to the event
*/
fun subscribe(): SseEmitter {
val sseEmitter = SseEmitter()
// Stop observing the event on completion
sseEmitter.onCompletion(
{this.emitters.remove(sseEmitter)
})
this.emitters.add(sseEmitter)
return sseEmitter
}
/**
* Trigger the event update to the observers
*/
fun send(o: Any) {
synchronized(emitters) {
emitters.iterator().forEach {
try {
it.send(o, MediaType.APPLICATION_JSON)
} catch (e: IOException) {}
}
}
}
}
推薦閱讀
- JavaScript前端開發模塊化教程
- 自制編譯器
- Mastering JavaScript Object-Oriented Programming
- 跟“龍哥”學C語言編程
- PyQt從入門到精通
- Visual C++數字圖像模式識別技術詳解
- Flask Web開發入門、進階與實戰
- Getting Started with CreateJS
- 面向STEM的Scratch創新課程
- Instant 960 Grid System
- JavaScript從入門到精通(第3版)
- Practical Game Design
- Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
- OpenResty完全開發指南:構建百萬級別并發的Web應用
- Test-Driven JavaScript Development