signals

Simple, type-safe event dispatching

License

License

GroupId

GroupId

com.threerings
ArtifactId

ArtifactId

signals
Last Version

Last Version

0.3
Release Date

Release Date

Type

Type

jar
Description

Description

signals
Simple, type-safe event dispatching
Project URL

Project URL

https://github.com/threerings/java-signals
Source Code Management

Source Code Management

http://github.com/threerings/java-signals/

Download signals

How to add to project

<!-- https://jarcasting.com/artifacts/com.threerings/signals/ -->
<dependency>
    <groupId>com.threerings</groupId>
    <artifactId>signals</artifactId>
    <version>0.3</version>
</dependency>
// https://jarcasting.com/artifacts/com.threerings/signals/
implementation 'com.threerings:signals:0.3'
// https://jarcasting.com/artifacts/com.threerings/signals/
implementation ("com.threerings:signals:0.3")
'com.threerings:signals:jar:0.3'
<dependency org="com.threerings" name="signals" rev="0.3">
  <artifact name="signals" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.threerings', module='signals', version='0.3')
)
libraryDependencies += "com.threerings" % "signals" % "0.3"
[com.threerings/signals "0.3"]

Dependencies

compile (1)

Group / Artifact Type Version
com.google.guava : guava jar 14.0.1

test (1)

Group / Artifact Type Version
junit : junit jar 4.8.1

Project Modules

There are no modules declared in this project.

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.

com.threerings

Three Rings

Versions

Version
0.3