-
Notifications
You must be signed in to change notification settings - Fork 41k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reactive progressive rendering features to MustacheView
- Loading branch information
Showing
12 changed files
with
400 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
.DS_Store | ||
.classpath | ||
.factorypath | ||
.attach_pid* | ||
.gradle | ||
.idea | ||
.metadata | ||
|
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
7 changes: 7 additions & 0 deletions
7
spring-boot-project/spring-boot-autoconfigure/src/test/resources/mustache-templates/sse.html
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,7 @@ | ||
{{#flux.message}} | ||
event: message | ||
{{#ssedata}} | ||
<h2>Title</h2> | ||
{{{.}}} | ||
{{/ssedata}} | ||
{{/flux.message}} |
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
93 changes: 93 additions & 0 deletions
93
...ring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/FluxWriter.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,93 @@ | ||
/* | ||
* Copyright 2016-2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.web.reactive.result.view; | ||
|
||
import java.io.IOException; | ||
import java.io.Writer; | ||
import java.nio.charset.Charset; | ||
import java.util.function.Supplier; | ||
|
||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.core.io.buffer.DataBuffer; | ||
|
||
/** | ||
* A {@link Writer} that can write a {@link Flux} (or {@link Publisher}) to a data buffer. | ||
* Used to render progressive output in a {@link MustacheView}. | ||
* | ||
* @author Dave Syer | ||
*/ | ||
class FluxWriter extends Writer { | ||
|
||
private final Supplier<DataBuffer> factory; | ||
|
||
private final Charset charset; | ||
|
||
private Flux<String> buffers; | ||
|
||
public FluxWriter(Supplier<DataBuffer> factory) { | ||
this(factory, Charset.defaultCharset()); | ||
} | ||
|
||
public FluxWriter(Supplier<DataBuffer> factory, Charset charset) { | ||
this.factory = factory; | ||
this.charset = charset; | ||
this.buffers = Flux.empty(); | ||
} | ||
|
||
public Publisher<? extends Publisher<? extends DataBuffer>> getBuffers() { | ||
return this.buffers | ||
.map(string -> Mono.just(buffer().write(string, this.charset))); | ||
} | ||
|
||
@Override | ||
public void write(char[] cbuf, int off, int len) throws IOException { | ||
this.buffers = this.buffers.concatWith(Mono.just(new String(cbuf, off, len))); | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException { | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
} | ||
|
||
public void release() { | ||
// TODO: maybe implement this and call it on error | ||
} | ||
|
||
private DataBuffer buffer() { | ||
return this.factory.get(); | ||
} | ||
|
||
public void write(Object thing) { | ||
if (thing instanceof Publisher) { | ||
@SuppressWarnings("unchecked") | ||
Publisher<String> publisher = (Publisher<String>) thing; | ||
this.buffers = this.buffers.concatWith(Flux.from(publisher)); | ||
} | ||
else { | ||
if (thing instanceof String) { | ||
this.buffers = this.buffers.concatWith(Mono.just((String) thing)); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.