Skip to main content

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.

Step 1 / 7

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
Skill bar
Mana: 40 / 100
Fireball
mana40
manaCost30
silencedfalse
castabletrue
(nothing printed yet)

Try it yourself below. "Toggle silence" only redraws the skill button, while "Mana +20" redraws both items.

Sources
Derived
Consumers
Mana40
Cost30
Silencedno
Mana text40 / 100
Castableyes
Mana item
Skill button

Change a source to see how the notification spreads.

Skill bar
Mana: 40 / 100
Fireball (ready)

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​

Notifies even when the result is unchanged

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);
Sources are read one by one

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 only computes

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.

NextlensPull one field out of a settings object and read, write, and subscribe to it on its own.