Skip to main content

merging

The party menu shows the party's total health. Each member's health is its own Signal, and members join and leave at any time.

combine fixes its sources at creation, at most three, so it cannot handle a party whose size changes. Listening yourself means subscribing to each new member's health when they join and unsubscribing when they leave, which is easy to get wrong.

What merging does​

Signals.merging watches two things at once: the member list itself, and each member's Signal. When the list changes, or any member's Signal changes, it notifies downstream.

New members are watched as soon as they join and dropped as soon as they leave.

It returns a Signal<Long>, a number that grows on every change. The number has no business meaning; it only says "something changed". Chain a map to compute the real result.

Total party health​

The party list is a ListSignal, so members can be added and removed in place. The menu shows each member, with the total on the right.

Step 1 / 8

Alice and Bob form a party; Carol has not joined yet.

record Member(String name, MutableSignal<Integer> health) {}

Member alice = new Member("Alice", Signal.of(20));
Member bob = new Member("Bob", Signal.of(14));
Member carol = new Member("Carol", Signal.of(20));

MutableListSignal<Member> party = ListSignal.of();
party.addAll(List.of(alice, bob));

// The second argument tells merging how to get a member's Signal
Signal<Long> changed = Signals.merging(party, Member::health);
Signal<Integer> totalHealth = changed.map(ignored ->
party.stream().mapToInt(member -> member.health().get()).sum()
);

Subscription subscription = totalHealth.onDirty(() -> {
System.out.println("Party health: " + totalHealth.get());
});

System.out.println(totalHealth.get()); // 34
alice.health().set(15); // prints "Party health: 29"
party.remove(bob); // prints "Party health: 15"
bob.health().set(0); // Bob left and is no longer watched, prints nothing
party.add(carol); // prints "Party health: 35"

subscription.close();
Party
Alice ❤ 20
Bob ❤ 14
Party health: 34
partyAlice, Bob
alice.health20
bob.health14
totalHealth34
(nothing printed yet)

The same pattern gives average health, the number of living members, or the member with the lowest health; just change the calculation inside map.

Holding the list in a plain Signal​

The member list can also be a MutableSignal<List<Member>>. Then publish a new list with set, such as members.set(List.of(alice, carol)). Changing the old list in place notifies nobody.

ListSignal and SetSignal support in-place changes and work directly as member sources, which is usually more convenient.

Caveats​

Members and iteration order

The function that gets a Signal should return the member's existing Signal, not create a new one on every call. The list's iteration order should be stable.

Do not put Player in members

The list is held long term. Identify players in member objects by UUID or name, never by storing a Player.

NextdebounceWait until input settles, such as searching only after the player finishes typing.