Skip to content

Commit

Permalink
Only support async: and use sse:data as sse lambda name
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Syer committed May 21, 2019
1 parent 1489bfe commit 8b75b8f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public String layout(Model model) {
@RequestMapping(path = "/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public String sse(Model model) {
model.addAttribute("time", new Date());
model.addAttribute("async.message",
model.addAttribute("async:message",
Flux.just("<span>Hello</span>", "<span>World</span>")
.delayElements(Duration.ofMillis(10)));
model.addAttribute("title", "Hello App");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{#async.message}}
{{#async:message}}
event: message
{{#ssedata}}
{{#sse:data}}
<h2>Title</h2>
{{{.}}}
{{/ssedata}}
{{/async.message}}
{{/sse:data}}
{{/async:message}}
Original file line number Diff line number Diff line change
Expand Up @@ -3038,23 +3038,23 @@ There are some special features of the `MustacheView` that make it suitable for

===== Progressive Rendering

A model element of type `Publisher` will be left in the model (instead of expanding it before the view is rendered), if its name starts with "async." or "async:". The `View` is then rendered and flushed to the HTTP response as soon as each element is published. Browsers are really good at rendering partially complete HTML, so the flux elements will most likely be visible to the user as soon as they are available. This is useful for rendering the "main" content of a page if it is a list or a table, for instance.
A model element of type `Publisher` will be left in the model (instead of expanding it before the view is rendered), if its name starts with "async:". The `View` is then rendered and flushed to the HTTP response as soon as each element is published. Browsers are really good at rendering partially complete HTML, so the flux elements will most likely be visible to the user as soon as they are available. This is useful for rendering the "main" content of a page if it is a list or a table, for instance.

===== Sserver Sent Event (SSE) Support

To render a `View` with content type `text/event-stream` you need a model element of type `Publisher`, and also a template that includes that element (probably starts and ends with it). There is a convenience Lambda (`ssedata`) added to the model for you that prepends every line with `data:` - you can use it if you wish to simplify the rendering of the data elements. Two new lines are added after each item in `{{#ssedata}}`. E.g. with an element called `flux.events` of type `Flux<Event>`:
To render a `View` with content type `text/event-stream` you need a model element of type `Publisher`, and also a template that includes that element (probably starts and ends with it). There is a convenience Lambda (`ssedata`) added to the model for you that prepends every line with `data:` - you can use it if you wish to simplify the rendering of the data elements. Two new lines are added after each item in `{{#sse:data}}`. E.g. with an element called `async:events` of type `Flux<Event>`:

```
{{#async.events}}
{{#async:events}}
event: message
id: {{id}}
{{#ssedata}}
{{#sse:data}}
<div>
<span>Name: {{name}}<span>
<span>Value: {{value}}<span>
</div>
{{/ssedata}}
{{/async.events}}
{{/sse:data}}
{{/async:events}}
```

the output will be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected Mono<Void> renderInternal(Map<String, Object> model, MediaType content
Map<String, Object> map;
if (sse) {
map = new HashMap<>(model);
map.put("ssedata", new SseLambda());
map.put("sse:data", new SseLambda());
}
else {
map = model;
Expand Down Expand Up @@ -151,7 +151,7 @@ private Template compile(Resource resource) {
protected Mono<Void> resolveAsyncAttributes(Map<String, Object> model) {
Map<String, Object> result = new HashMap<>();
for (String key : model.keySet()) {
if (!key.startsWith("async.") && !key.startsWith("async:")) {
if (!key.startsWith("async:")) {
result.put(key, model.get(key));
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void viewResolvesSseManual() {
view.setUrl(this.templateUrl + "/sse.html");
view.setCharset(StandardCharsets.UTF_8.displayName());
view.setApplicationContext(this.context);
view.render(Collections.singletonMap("async.value", Flux.just("World", "Spring")),
view.render(Collections.singletonMap("async:value", Flux.just("World", "Spring")),
MediaType.TEXT_EVENT_STREAM, this.exchange).block(Duration.ofSeconds(30));
assertThat(this.exchange.getResponse().getBodyAsString()
.block(Duration.ofSeconds(30))).isEqualTo(
Expand All @@ -106,7 +106,7 @@ public void viewResolvesSseData() {
view.setUrl(this.templateUrl + "/ssedata.html");
view.setCharset(StandardCharsets.UTF_8.displayName());
view.setApplicationContext(this.context);
view.render(Collections.singletonMap("async.value", Flux.just("World", "Spring")),
view.render(Collections.singletonMap("async:value", Flux.just("World", "Spring")),
MediaType.TEXT_EVENT_STREAM, this.exchange)
.block(Duration.ofSeconds(300));
assertThat(this.exchange.getResponse().getBodyAsString()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{#async.value}}
{{#async:value}}
event: message
data: {{.}}


{{/async.value}}
{{/async:value}}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{#async.value}}
{{#async:value}}
event: message
{{#ssedata}}
{{#sse:data}}
{{.}}
{{/ssedata}}
{{/async.value}}
{{/sse:data}}
{{/async:value}}

0 comments on commit 8b75b8f

Please sign in to comment.