-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
2,090 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/java/endpoint/src/main/java/com/vaadin/hilla/signals/NumberSignal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.vaadin.hilla.signals; | ||
|
||
import com.fasterxml.jackson.databind.node.IntNode; | ||
import com.vaadin.hilla.signals.core.SignalQueue; | ||
|
||
public class NumberSignal extends SignalQueue<IntNode> { | ||
|
||
public NumberSignal(int defaultValue) { | ||
super(IntNode.valueOf(defaultValue)); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...ges/java/endpoint/src/main/java/com/vaadin/hilla/signals/config/SignalsConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.vaadin.hilla.signals.config; | ||
|
||
import com.vaadin.hilla.signals.core.SignalsHandler; | ||
import com.vaadin.hilla.signals.core.SignalsRegistry; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class SignalsConfiguration { | ||
|
||
private final SignalsRegistry signalsRegistry; | ||
private final SignalsHandler signalsHandler; | ||
|
||
public SignalsConfiguration(SignalsRegistry signalsRegistry, | ||
SignalsHandler signalsHandler) { | ||
this.signalsRegistry = signalsRegistry; | ||
this.signalsHandler = signalsHandler; | ||
} | ||
|
||
@Bean | ||
public SignalsRegistry signalsRegistry() { | ||
return signalsRegistry; | ||
} | ||
|
||
@Bean | ||
public SignalsHandler signalsHandler() { | ||
return signalsHandler; | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
packages/java/endpoint/src/main/java/com/vaadin/hilla/signals/core/EventQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.vaadin.hilla.signals.core; | ||
|
||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Sinks; | ||
import reactor.core.publisher.Sinks.Many; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public abstract class EventQueue<T extends StateEvent> { | ||
private final ReentrantLock lock = new ReentrantLock(); | ||
|
||
public static final UUID ROOT = UUID | ||
.fromString("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"); | ||
|
||
private static class Entry<T extends StateEvent> { | ||
private final T value; | ||
private Entry<T> next; | ||
|
||
private Entry(T value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
private Entry<T> head; | ||
private Entry<T> tail; | ||
private final Map<UUID, Entry<T>> idToEntry = new HashMap<>(); | ||
|
||
private final Set<Many<T>> subscribers = new HashSet<>(); | ||
|
||
public Flux<T> subscribe(UUID continueFrom) { | ||
System.out.println("Continue from " + continueFrom); | ||
Many<T> sink = Sinks.many().multicast().onBackpressureBuffer(); | ||
|
||
return sink.asFlux().doOnSubscribe(ignore -> { | ||
System.out.println("New Flux subscription"); | ||
|
||
lock.lock(); | ||
try { | ||
Entry<T> entry; | ||
if (continueFrom != null | ||
&& (entry = idToEntry.get(continueFrom)) != null) { | ||
entry = entry.next; | ||
// TODO maybe some heuristic to determine whether it would | ||
// be more efficient to restart from a snapshot instead of | ||
// replaying lots of events? | ||
while (entry != null) { | ||
System.out.println("Replay " + entry.value.getId()); | ||
sink.tryEmitNext(entry.value); | ||
entry = entry.next; | ||
} | ||
; | ||
} else { | ||
T snapshot = createSnapshot(); | ||
if (snapshot != null) { | ||
sink.tryEmitNext(snapshot); | ||
} | ||
} | ||
|
||
subscribers.add(sink); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
}).doFinally(ignore -> { | ||
System.out.println("doFinally"); | ||
lock.lock(); | ||
try { | ||
subscribers.remove(sink); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
}); | ||
} | ||
|
||
public void submit(T event) { | ||
// Thread.ofVirtual().start(() -> append(event)); | ||
append(event); | ||
} | ||
|
||
private void append(T event) { | ||
lock.lock(); | ||
try { | ||
processEvent(event); | ||
Entry<T> entry = new Entry<>(event); | ||
|
||
// Add to linked list | ||
idToEntry.put(event.getId(), entry); | ||
if (head == null) { | ||
head = tail = entry; | ||
} else { | ||
tail.next = tail = entry; | ||
} | ||
|
||
// Truncate list | ||
// TODO configurable or dynamic limit? | ||
if (idToEntry.size() > 100) { | ||
Entry<T> removed = idToEntry.remove(head.value.getId()); | ||
head = removed.next; | ||
} | ||
|
||
// Notify subscribers | ||
subscribers.removeIf(sink -> { | ||
boolean failure = sink.tryEmitNext(event).isFailure(); | ||
if (failure) { | ||
System.out.println("Failed push"); | ||
} | ||
return failure; | ||
}); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
protected abstract void processEvent(T newEvent); | ||
|
||
protected abstract T createSnapshot(); | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/java/endpoint/src/main/java/com/vaadin/hilla/signals/core/JsonEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.vaadin.hilla.signals.core; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
import java.util.UUID; | ||
|
||
public class JsonEvent extends StateEvent { | ||
|
||
private final ObjectNode json; | ||
|
||
public JsonEvent(UUID id, ObjectNode json) { | ||
super(id); | ||
this.json = json; | ||
} | ||
|
||
public ObjectNode getJson() { | ||
return json; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
packages/java/endpoint/src/main/java/com/vaadin/hilla/signals/core/JsonEventMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.vaadin.hilla.signals.core; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public class JsonEventMapper { | ||
|
||
private final ObjectMapper mapper; | ||
|
||
public JsonEventMapper(ObjectMapper mapper) { | ||
this.mapper = mapper; | ||
} | ||
|
||
public String toJson(JsonEvent jsonEvent) { | ||
ObjectNode root = mapper.createObjectNode(); | ||
for (Map.Entry<String, JsonNode> entry : jsonEvent.getJson() | ||
.properties()) { | ||
root.set(entry.getKey(), entry.getValue()); | ||
} | ||
UUID id = jsonEvent.getId(); | ||
root.put("id", id != null ? id.toString() : null); | ||
return root.toString(); | ||
} | ||
|
||
public JsonEvent fromJson(String json) { | ||
try { | ||
ObjectNode root = (ObjectNode) mapper.readTree(json); | ||
UUID id = UUID.fromString(root.get("id").asText()); | ||
root.remove("id"); | ||
return new JsonEvent(id, root); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.