6

GitHub - jafarlihi/eemit: Tiny event emitter library for Java. Lets you emit obj...

 1 year ago
source link: https://github.com/jafarlihi/eemit
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

eemit

eemit is a Java event emitter library

Install

Maven:

<dependency>
    <groupId>com.github.jafarlihi</groupId>
    <artifactId>eemit</artifactId>
    <version>0.1.0</version>
</dependency>

Gradle:

implementation 'com.github.jafarlihi:eemit:0.1.0'

Usage

import io.github.jafarlihi.eemit.EventEmitter;

...

// Create EventEmitter with any type as an event type, here just String
EventEmitter<String> emitter = new EventEmitter<>();

// Listen to events on channel "channel666", pass in a lambda that'll receive channel name and the event object
emitter.on("channel666", (channel, object) -> {
  // Do something with object (which is String because we parameterized EventEmitter with String)
  // "channel" argument will either be "channel666" or "*" here
});

// Listen to events on all channels
emitter.on("*", (channel, object) -> {
  // Do something with object
});

// Emit an event on "channel666", second parameter is of type that you parameterized EventEmitter with
emitter.emit("channel666", "Some string");

// Emit to all channels
emitter.emit("*", "Some string");

// How to unlisten:
String uuid = emitter.on("channel666", (channel, object) -> {
  // Do something with object
});
// Unregister callback
emitter.off(uuid);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK