ListSignal
In a minigame room's waiting menu, each slot shows one queued player and a corner item says "Waiting 3 / 8". Players join and leave at any time.
A MutableSignal<List<String>> can do this, but every join means copying a new list and calling set. Changing the list inside it goes unnoticed, and the menu does not refresh.
What ListSignal doesâ
A ListSignal is both a List and a Signal. add, remove, and clear work like on any list, and every change notifies whatever depends on it.
ListSignal.of() returns a MutableListSignal, which has all the modifying methods plus batch and the element hooks covered below. When others should only read, hand out a ListSignal or a read-only view.
Players joining and leavingâ
The room status text is derived from the queue and needs no bookkeeping of its own. The queue stores names here to keep the output readable; real projects should store player UUIDs.
The room just opened and the queue is empty.
MutableListSignal<String> queue = ListSignal.of();
Signal<String> queueText = queue.mapDistinct(list ->
"Waiting " + list.size() + " / 8"
);
queue.add("Alice");
queue.add("Bob");
queue.add("Carol");
System.out.println(queueText.get()); // Waiting 3 / 8
queue.remove("Bob");
System.out.println(queue.get()); // [Alice, Carol]
System.out.println(queueText.get()); // Waiting 2 / 8

queue | [] | |
|---|---|---|
queueText | Waiting 0 / 8 |
To show list contents in menu slots, see List contents.
Merging changes with batchâ
After a match the room resets: clear the queue, then add the next group. Changing it step by step shows subscribers an empty queue and then the new one, and the menu flickers. Wrap the steps in batch to notify only once at the end.
The three players from the last match.
MutableListSignal<String> queue = ListSignal.of();
queue.addAll(List.of("Alice", "Bob", "Carol"));
Subscription subscription = queue.onDirty(() -> {
System.out.println("Queue: " + queue.get());
});
// the match ended, swap in the next group
queue.batch(() -> {
queue.clear();
queue.addAll(List.of("Dave", "Eve"));
}); // prints "Queue: [Dave, Eve]" once
subscription.close();



queue | [Alice, Bob, Carol] |
|---|
Nested batch calls postpone the notification to the end of the outermost one. If nothing changed, nothing is notified.
Hooks before adding and after removingâ
Server announcements are typed by admins and need surrounding spaces trimmed, and taking one down should write a log line. beforeAdd processes an element before it is stored, and afterRemove receives an element after it is removed.
MutableListSignal<String> notices = ListSignal.of();
Subscription trim = notices.beforeAdd(String::strip);
Subscription log = notices.afterRemove(notice -> {
System.out.println("Removed announcement: " + notice);
});
notices.add(" New dungeon opens at 8 PM tonight ");
System.out.println("[" + notices.get(0) + "]"); // [New dungeon opens at 8 PM tonight]
notices.remove(0); // prints "Removed announcement: New dungeon opens at 8 PM tonight"
trim.close();
log.close();
What beforeAdd returns is what actually gets stored; it has no way to reject an element. Several hooks run in registration order, each receiving the previous result. When set replaces an element, afterRemove runs on the old one before beforeAdd runs on the new one.
Backing listâ
ListSignal.of() is backed by an ArrayList, fastest when only one thread modifies it. For cross-thread access, either synchronize yourself or wrap a concurrent list.
MutableListSignal<String> queue = ListSignal.wrap(new CopyOnWriteArrayList<>());
Caveatsâ
queue.get() returns the list itself, and later changes show up through that reference. When a derived result must keep one moment's contents, copy it, as in queue.map(List::copyOf).
Changing a field inside an element object goes unnoticed. Keep elements immutable and replace them with set(index, newValue).
wrap does not copy the list. Changing the original list directly changes the data but notifies nobody. Lists created with of() are not thread safe.
Each change inside batch takes effect immediately. If an exception is thrown midway, completed changes stay and subscribers are still notified. It only merges this list's notifications from the current thread; other threads may still see intermediate states.
Keep the Subscription a hook returns. The list only holds hooks weakly, and a dropped handle can let the hook be collected. Hooks run synchronously on the modifying thread; register them before handing the list to other code, and do not modify the same list from inside a hook.
When removing by index or iterator, afterRemove gets the stored object; remove(Object) passes the caller's argument. If a removal hook throws, the removal has already happened and is not undone.