Coder Social home page Coder Social logo

java-signals's Introduction

This is a Java reinterpretation of Robert Penner's as3-signals. It keeps the simple event declaration and dispatching, while adding a dash of type-safety afforded by Java's generics.

Usage

Using a signal is a 3 step process. First, create a signal on the class that will be firing events:

public class Dispatcher {
    /** Dispatches the old value and the new value after it changes. */
    public final Signal2<String, String> onChanged = Signals.newSignal2();
}

Then, add listeners to that signal as appropriate:

public class InterestedParty {
    public void connectToDispatcher (Dispatcher d)
    {
        d.onChanged.add(new Listener1<String>() {
            public void apply (String oldValue) {
                System.out.println("Dispatcher changed from " + oldValue);
            }});
    }
}

or

d.onChanged.add(new Listener0() {
    public void apply () {
        System.out.println("I'll only be called for one dispatch!");
    }
    }).once();

if the listener only wants to receive the next dispatch.

Finally, dispatch that signal when the event occurs:

onChanged.dispatch("oldValue", "newValue");

That'll call apply on all the added listeners.

If a listener is no longer interested in a signal, it can be removed from the signal, or Connection.disconnect can be called.

Connecting, disconnecting, and dispatching is thread-safe. After Signal.disconnect(Listener) or Connection.disconnect returns, a listener is guaranteed to never receive another call to its apply method.

Getting

The 0.3 release of signals is available via Maven Central. Just add this to your POM:

<dependency>
  <groupId>com.threerings</groupId>
  <artifactId>signals</artifactId>
  <version>0.3</version>
</dependency>

You can also download it directly from Maven Central.

java-signals's People

Contributors

groves avatar deadmoose avatar samskivert avatar jamie-threerings avatar

Stargazers

Jonas Neugebauer avatar  avatar Simone Degiacomi avatar  avatar  avatar  avatar Piergiorgio Niero avatar Loki avatar Evan Liu avatar NiKu avatar Aria avatar Adam Drobotij avatar Ken avatar Benjamin Fagin avatar Andrew Tsvielodub avatar  avatar Paul Moore avatar Eugene Veretennikov avatar Valentin Arkhipov avatar Benoit Jadinon avatar Mike Pearson avatar  avatar  avatar Simon Richardson avatar

Watchers

Andy Mroczkowski avatar  avatar Piergiorgio Niero avatar  avatar  avatar Tim Conkling avatar Shaun Nivens avatar Nathan Curtis avatar NiKu avatar  avatar James Cloos avatar  avatar Ray Greenwell avatar  avatar Mark Johnson avatar Sylvain Royer avatar  avatar  avatar Craig Moffitt avatar  avatar  avatar  avatar

java-signals's Issues

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.