Giter VIP home page Giter VIP logo

event-system's Introduction

Java library for event driven applications

Event system

Design your application with a more modular approach by using events to create hooks and manipulate data from within other components.

Installation

Gradle

repositories {
    maven { url 'https://registry.provided.space' }
}

dependencies {
    implementation 'space.provided:event-system:VERSION'
}

Maven

<repositories>
    <repository>
        <id>provided</id>
        <name>provided.space</name>
        <url>https://registry.provided.space</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>space.provided</groupId>
        <artifactId>event-system</artifactId>
        <version>VERSION</version>
    </dependency>
</dependencies>

Example

The events that are going to be dispatched as well as the listeners, can be any object without the need inheritance.

In this case we want to listen to when a message is being sent. Only methods with exact class matches will be invoked.
If you need to control how early/late a method should be executed, you can take advantage of the sequence parameter on the Annotation. The higher the sequence, the later the method will be invoked.

public final class MessageSendEvent {
    
    private MessageSender sender;
    private String message;

    // constructor, getters and setters
}

public final class MessageSendListener {

    @Subscribe
    public void onMessageSend(MessageSendEvent event) {
        System.out.println(String.format("A message is being sent by %1$s.", event.getSender().getUUID()));
    }

    @Subscribe(sequence = 1)
    public void onMessageSend(MessageSendEvent event) {
        System.out.println("A message is being sent.");
    }
}

The event bus can be obtained with a new instance of EventBus. To start listening to events, you have to register a listener. When unregistering a listener, no more events will be dispatched.

To dispatch an event, simply call the post method on your EventBus instance and pass your event.

final EventBus events = new EventBus<>();

final MessageSendListener listener = new MessageSendListener();
events.subscribe(listener);
events.post(new MessageSendEvent(sender, message));

events.unsubscribe(listener);
events.post(new MessageSendEvent(sender, message));

event-system's People

Contributors

vadage avatar git-marius avatar

Stargazers

Shiiro avatar Luis avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.