All Projects → ZeroMemes → Alpine

ZeroMemes / Alpine

Licence: MIT license
Basic event system framework using functional interfaces

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Alpine

e
A library which combines a eventBus/emitter, DOM events management, delegated events, and event-based utils into a single lightweight and performant library.
Stars: ✭ 37 (-53.16%)
Mutual labels:  events, eventbus
pg-pubsub
Reliable PostgreSQL LISTEN/NOTIFY with inter-process lock support
Stars: ✭ 50 (-36.71%)
Mutual labels:  events, eventbus
Rabbitevents
Nuwber's events provide a simple observer implementation, allowing you to listen for various events that occur in your current and another application. For example, if you need to react to some event published from another API.
Stars: ✭ 84 (+6.33%)
Mutual labels:  events, eventbus
hertzy
Event bus channel
Stars: ✭ 48 (-39.24%)
Mutual labels:  events, eventbus
telephone-ts
Telephone-ts: The "Event Emitter-less" TypeScript Event Architecture.
Stars: ✭ 22 (-72.15%)
Mutual labels:  events, eventbus
use-bus
React hook to subscribe and dispatch events accros React components
Stars: ✭ 51 (-35.44%)
Mutual labels:  events, eventbus
evon
Fast and versatile event dispatcher code generator for Golang
Stars: ✭ 15 (-81.01%)
Mutual labels:  events, eventbus
node-cqrs-saga
Node-cqrs-saga is a node.js module that helps to implement the sagas in cqrs. It can be very useful as domain component if you work with (d)ddd, cqrs, eventdenormalizer, host, etc.
Stars: ✭ 59 (-25.32%)
Mutual labels:  events
ember-event-helpers
Complimentary event template helpers to the {{on}} modifier
Stars: ✭ 33 (-58.23%)
Mutual labels:  events
cyberevents
The protocol for EVENTs and TICKETs
Stars: ✭ 16 (-79.75%)
Mutual labels:  events
niue
A tiny shared state and event library for React
Stars: ✭ 17 (-78.48%)
Mutual labels:  events
ofxVui
✨ Such Wow! View + GUI System for openFrameworks ✨
Stars: ✭ 48 (-39.24%)
Mutual labels:  events
gancio
a shared agenda for local communities (with activitypub support)
Stars: ✭ 21 (-73.42%)
Mutual labels:  events
wwwtf.berlin
wwwtf, a ~week of events organized for and by the web community
Stars: ✭ 46 (-41.77%)
Mutual labels:  events
LogHub
No description or website provided.
Stars: ✭ 38 (-51.9%)
Mutual labels:  events
from-event
🦊 ViewChild and FromEvent — a Match Made in Angular Heaven
Stars: ✭ 130 (+64.56%)
Mutual labels:  events
django-eth-events
No description or website provided.
Stars: ✭ 43 (-45.57%)
Mutual labels:  events
tutorial
Tutorials to help you build your first Swim app
Stars: ✭ 27 (-65.82%)
Mutual labels:  events
watermill-sql
SQL Pub/Sub for the Watermill project.
Stars: ✭ 39 (-50.63%)
Mutual labels:  events
Evilize
Parses Windows event logs files based on SANS Poster
Stars: ✭ 24 (-69.62%)
Mutual labels:  events

Alpine

A lightweight event system for Java 8+

Tutorial

For starters, we must create an EventBus to handle events and their respective listeners. Alpine provides a default implementation of EventBus that is configurable through a builder, so we'll be using that:

public class MyApplication {

    public static final EventBus EVENT_BUS = EventManager.builder()
        .setName("my_application/root") // Descriptive name for the bus
        .setSuperListeners()            // Enable Listeners to receive subtypes of their target
        .build();
}

Specifying a name for the bus is required; although, there is no hard restriction on its uniqueness. Additional settings such as setSuperListeners can be seen in the documentation of EventBusBuilder.

Now to actually receive events that are posted to the event bus, we'll need to create a Listener object and supply its generic argument with the type of event we'd like to receive. One of the ways that this can be done by creating a Listener member variable in a class implementing EventSubscriber, and annotating it with @Subscribe. Let's do that in our existing class:

public class MyApplication implements EventSubscriber {

    public static final EventBus EVENT_BUS = ...;

    @Subscribe
    private Listener<String> stringListener = new Listener<>(str -> {
        System.out.println(str);
    });
}

In order to use our Listener, we need to create a new instance of the EventSubscriber implementation and subscribe it to the event bus.

public class MyApplication implements EventSubscriber {

    public static final EventBus EVENT_BUS = ...;

    public static void main(String[] args) {
        MyApplication app = new MyApplication();
        EVENT_BUS.subscribe(app);
    }

    @Subscribe
    private Listener<String> stringListener = new Listener<>(str -> {
        System.out.println(str);
    });
}

An alternative to creating an EventSubscriber implementation and using annotated Listener fields to receive events is creating an independent Listener instance and subscribing it directly:

public class MyApplication {

    public static final EventBus EVENT_BUS = ...;
    
    public static void main(String[] args) {
        EVENT_BUS.subscribe(new Listener<String>(str -> {
            System.out.println(str);
        }));
        // or, alternatively... (println has a String implementation which will get bound to here)
        EVENT_BUS.subscribe(new Listener<String>(System.out::println));
    }
}

In cases where a method reference (::) is used for a Listener body and the underlying method's argument isn't the Listener target, a ClassCastException can occur during runtime. This is due to incorrect target resolution, and can be fixed by explicitly specifying the target type:

public class MyApplication {

    public static final EventBus EVENT_BUS = ...;

    public static void main(String[] args) {
        // Incorrect, can cause ClassCastException upon runtime depending on EventBus configuration
        EVENT_BUS.subscribe(new Listener<String>(Main::supertypeAcceptor));

        // Correct, explicitly defines the target and no runtime exception or unintended behavior will occur
        EVENT_BUS.subscribe(new Listener<>(String.class, Main::supertypeAcceptor));
    }

    // Note the 'Object' argument type, this is what causes the need for an explicit target
    private static void supertypeAcceptor(Object o) {
        System.out.println(o);
    }
}

Providing our Listener with an event, in this case, any String, is straight-forward:

public class MyApplication {

    public static final EventBus EVENT_BUS = ...;

    public static void main(String[] args) {
        MyApplication app = new MyApplication();
        EVENT_BUS.subscribe(app);
        EVENT_BUS.post("Test");
    }

    @Subscribe
    private Listener<String> stringListener = new Listener<>(str -> {
        // Prints "Test"
        System.out.println(str);
    });
}

Listeners may have filters applied which must pass in order for the body to receive a given event. A listener can have as many filters as is needed, which are added as the last arguments in the Listener constructor.

public class MyApplication {

    ...

    @Subscribe
    private Listener<String> stringListener = new Listener<>(str -> {
        // No output, "Test".length() != 3
        System.out.println(str);
    }, new LengthOf3Filter()); // <-- Predicate<? super String>... as last argument to Listener

    // Create nested class implementation of our filter
    public static class LengthOf3Filter implements Predicate<CharSequence> {

        @Override
        public boolean test(CharSequence t) {
            return t.length() == 3;
        }
    }
}

The complete example class can be found here.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].