Skip to content

Commit

Permalink
Merge branch 'main' into update-spring-boot-3.2.7-1718943692
Browse files Browse the repository at this point in the history
  • Loading branch information
taefi authored Sep 4, 2024
2 parents b3d69a2 + 1503513 commit f836a79
Show file tree
Hide file tree
Showing 94 changed files with 15,739 additions and 13,427 deletions.
1,288 changes: 644 additions & 644 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@preact/signals-react-transform": "^0.3.1",
"@types/karma": "^6.3.8",
"@types/node": "^20.11.19",
"@vaadin/react-components": "24.5.0-alpha8",
"@vaadin/react-components": "24.5.0-alpha11",
"@vitejs/plugin-react": "^4.3.1",
"chai-dom": "^1.12.0",
"compare-versions": "^6.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ private VaadinEndpointData(Object vaadinEndpointObject,
method));
}

/**
* Gets all the endpoint methods.
*
* @return the endpoint methods
*/
public Map<String, Method> getMethods() {
return methods;
}

/**
* Finds a method with the given name.
*
Expand Down Expand Up @@ -126,6 +135,15 @@ void registerEndpoint(Object endpointBean) {
beanType);
}

/**
* Gets all registered endpoints.
*
* @return a map of endpoint names to endpoint data
*/
public Map<String, VaadinEndpointData> getEndpoints() {
return vaadinEndpoints;
}

VaadinEndpointData get(String endpointName) {
return vaadinEndpoints.get(endpointName.toLowerCase(Locale.ENGLISH));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private static boolean affectsEndpoints(String[] changedClasses)
.getClassesUsedInOpenApi().orElse(Set.of());
for (String classUsedInEndpoints : classesUsedInEndpoints) {
if (changedClassesSet.contains(classUsedInEndpoints)) {
getLogger().debug("The changed class " + classesUsedInEndpoints
getLogger().debug("The changed class " + classUsedInEndpoints
+ " is used in an endpoint");
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public void onStateChange(AtmosphereResourceEvent event)
super.onStateChange(event);
if (event.isCancelled() || event.isResumedOnTimeout()) {
onDisconnect(event);
} else if (event.isResuming()) {
onReconnect(event);
}
}

Expand Down Expand Up @@ -168,6 +170,16 @@ private void onDisconnect(AtmosphereResourceEvent event) {
pushMessageHandler.handleBrowserDisconnect(event.getResource().uuid());
}

/**
* Called when the push channel is disconnected.
*
* @param event
* the Atmosphere event
*/
private void onReconnect(AtmosphereResourceEvent event) {
pushMessageHandler.handleBrowserReconnect(event.getResource().uuid());
}

private void onThrowable(AtmosphereResourceEvent event) {
getLogger().error("Exception in push connection", event.throwable());
onDisconnect(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,19 @@ public void handleBrowserConnect(String connectionId) {
fluxSubscriptionInfos.put(connectionId, new ConcurrentHashMap<>());
}

/**
* Called when the browser establishes a new connection.
*
* Only ever called once for the same connectionId parameter.
*
* @param connectionId
* the id of the connection
*/
public void handleBrowserReconnect(String connectionId) {
fluxSubscriptionInfos.putIfAbsent(connectionId,
new ConcurrentHashMap<>());
}

/**
* Called when the browser connection has been lost.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,21 @@
package com.vaadin.hilla.signals;

import com.fasterxml.jackson.databind.node.ObjectNode;

import com.vaadin.hilla.signals.core.StateEvent;
import jakarta.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Sinks;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;

/**
* A signal that holds a number value.
*/
public class NumberSignal {

private static final Logger LOGGER = LoggerFactory
.getLogger(NumberSignal.class);

private final ReentrantLock lock = new ReentrantLock();

private final UUID id = UUID.randomUUID();

private Double value;

private final Set<Sinks.Many<ObjectNode>> subscribers = new HashSet<>();
public class NumberSignal extends ValueSignal<Double> {

/**
* Creates a new NumberSignal with the provided default value.
*
* @param defaultValue
* the default value
*
* @throws NullPointerException
* if the default value is null
*/
public NumberSignal(@Nullable Double defaultValue) {
this.value = defaultValue;
public NumberSignal(Double defaultValue) {
super(defaultValue, Double.class);
}

/**
Expand All @@ -47,117 +24,4 @@ public NumberSignal(@Nullable Double defaultValue) {
public NumberSignal() {
this(0.0);
}

/**
* Subscribes to the signal.
*
* @return a Flux of JSON events
*/
public Flux<ObjectNode> subscribe() {
Sinks.Many<ObjectNode> sink = Sinks.many().unicast()
.onBackpressureBuffer();

return sink.asFlux().doOnSubscribe(ignore -> {
LOGGER.debug("New Flux subscription...");
lock.lock();
try {
var currentValue = createSnapshot();
sink.tryEmitNext(currentValue);
subscribers.add(sink);
} finally {
lock.unlock();
}
}).doFinally(ignore -> {
lock.lock();
try {
LOGGER.debug("Unsubscribing from NumberSignal...");
subscribers.remove(sink);
} finally {
lock.unlock();
}
});
}

/**
* Submits an event to the signal and notifies subscribers about the change
* of the signal value.
*
* @param event
* the event to submit
*/
public void submit(ObjectNode event) {
lock.lock();
try {
processEvent(event);
// Notify subscribers
subscribers.removeIf(sink -> {
var updatedValue = createSnapshot();
boolean failure = sink.tryEmitNext(updatedValue).isFailure();
if (failure) {
LOGGER.debug("Failed push");
}
return failure;
});
} finally {
lock.unlock();
}
}

/**
* Returns the signal UUID.
*
* @return the id
*/
public UUID getId() {
return this.id;
}

/**
* Returns the signal's current value.
*
* @return the value
*/
@Nullable
public Double getValue() {
return this.value;
}

private ObjectNode createSnapshot() {
var snapshot = new StateEvent<>(this.id.toString(),
StateEvent.EventType.SNAPSHOT, this.value);
return snapshot.toJson();
}

private void processEvent(ObjectNode event) {
try {
var stateEvent = new StateEvent<Double>(event);
if (isSetEvent(stateEvent)) {
this.value = stateEvent.getValue();
} else {
throw new UnsupportedOperationException(
"Unsupported event: " + event);
}
} catch (StateEvent.InvalidEventTypeException e) {
throw new UnsupportedOperationException(
"Unsupported JSON: " + event, e);
}
}

private boolean isSetEvent(StateEvent<?> event) {
return StateEvent.EventType.SET.equals(event.getEventType());
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof NumberSignal signal))
return false;
return Objects.equals(getId(), signal.getId());
}

@Override
public int hashCode() {
return Objects.hashCode(getId());
}
}
Loading

0 comments on commit f836a79

Please sign in to comment.