Skip to content

fix: Allow observing other nodes from an observer #21653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ protected void notifyObservers(Snapshot oldSnapshot, Snapshot newSnapshot) {
}

runWithLock(() -> {
observers.forEach((nodeId, list) -> {
Map.copyOf(observers).forEach((nodeId, list) -> {
Data oldNode = oldSnapshot.data(nodeId).orElse(Node.EMPTY);
Data newNode = newSnapshot.data(nodeId).orElse(Node.EMPTY);

Expand Down
21 changes: 21 additions & 0 deletions signals/src/test/java/com/vaadin/signals/impl/EffectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,27 @@ void changeTracking_noOpChange_effectNotRunButRemainsActive() {
assertEquals(List.of("value", "update"), invocations);
}

@Test
void changeTracking_readChildNodes_coveredByNextEffectInvocation() {
ListSignal<String> signal = new ListSignal<>(String.class);
ArrayList<List<String>> invocations = new ArrayList<>();

Signal.effect(() -> {
List<String> values = signal.value().stream().map(Signal::value)
.toList();
invocations.add(values);
});

assertEquals(List.of(List.of()), invocations);

signal.insertLast("One");
assertEquals(List.of(List.of(), List.of("One")), invocations);

signal.insertLast("Two");
assertEquals(List.of(List.of(), List.of("One"), List.of("One", "Two")),
invocations);
}

@Test
void callback_updateSignal_throws() {
ValueSignal<String> signal = new ValueSignal<>("value");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,37 @@ void observe_observeInCallback_registeredAgain() {
assertEquals(1, count.get());
}

@Test
void observe_observeAnotherNodeInCallback_observerAdded() {
SynchronousSignalTree tree = new SynchronousSignalTree(false);

Id childId = Id.random();
AtomicInteger count = new AtomicInteger();
tree.observeNextChange(Id.ZERO, () -> {
tree.observeNextChange(childId, () -> {
count.incrementAndGet();
return false;
});
return false;
});

tree.commitSingleCommand(new SignalCommand.InsertCommand(childId,
Id.ZERO, null, new DoubleNode(2), ListPosition.last()));

// Nothing yet since root observer not invoked
assertEquals(0, count.get());

tree.commitSingleCommand(TestUtil.writeRootValueCommand());

// Nothing yet since child observer not invoked
assertEquals(0, count.get());

tree.commitSingleCommand(new SignalCommand.SetCommand(Id.random(),
childId, new DoubleNode(3)));

assertEquals(1, count.get());
}

@Test
void subscribeToProcessed_noChanges_doesNotReceive() {
SynchronousSignalTree tree = new SynchronousSignalTree(false);
Expand Down
Loading