combine
The Fireball button on the skill bar is lit when it can be cast and gray when it cannot. That depends on three things: whether there is enough mana, how much mana it costs, and whether the player is silenced.
map follows only one Signal. Reading the other two inside its function creates no dependency, so the button would not gray out when the player gets silenced.
What combine doesâ
Signals.combine merges two or three Signals into one result. When any source changes, the combined result notifies downstream. On read, it computes from each source's current value.
The function's parameters come in the same order as the Signals passed in.
Can the skill be castâ
The mana item depends on mana, and the Fireball button depends on castable. Step through and see which step refreshes which item.
40 mana covers the cost of 30 and nobody is silenced, so the button is lit.
MutableSignal<Integer> mana = Signal.of(40);
MutableSignal<Integer> manaCost = Signal.of(30);
MutableSignal<Boolean> silenced = Signal.of(false);
Signal<Boolean> castable = Signals.combine(mana, manaCost, silenced,
(current, cost, muted) -> !muted && current >= cost
);
System.out.println(castable.get()); // true
silenced.set(true);
System.out.println(castable.get()); // false, silenced
silenced.set(false);
mana.update(value -> value - 30);
System.out.println(castable.get()); // false, only 10 mana left


mana | 40 | |
|---|---|---|
manaCost | 30 | |
silenced | false | |
castable | true |
Try it yourself below. "Toggle silence" only redraws the skill button, while "Mana +20" redraws both items.
Change a source to see how the notification spreads.


Two sourcesâ
With two sources, the function takes two parameters. Current experience and the experience required for the next level decide the progress percentage.
MutableSignal<Integer> experience = Signal.of(30);
MutableSignal<Integer> required = Signal.of(120);
Signal<String> progress = Signals.combine(experience, required,
(current, needed) -> current * 100 / needed + "%"
);
System.out.println(progress.get()); // 25%
experience.set(90);
System.out.println(progress.get()); // 75%
// After leveling up, experience resets and the next level needs more
experience.set(0);
required.set(200);
System.out.println(progress.get()); // 0%
Caveatsâ
Like map, combine does not compare results. When mana regenerates from 40 to 60, castable is still true, yet downstream is notified. If the button only cares whether the skill can be cast, add a mapDistinct after it.
Signal<Boolean> castableDistinct = castable.mapDistinct(value -> value);
The combined result reads its sources one at a time, not as a snapshot of one moment. If you change mana and then manaCost, another thread may read the new mana with the old cost in between.
When two values must change together, put them in one immutable object, replace it with a single update, and read fields with map or lens.
The function may run on any reading thread and may run several times for the same inputs. Compute only from the arguments; do not change state or query a database.
combine takes at most three sources. For more, combine some of them into an intermediate result first, or merge related fields into one record.