-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
StreamingRender
iterator to support streaming template renders
- Loading branch information
1 parent
5c16387
commit 1004fe2
Showing
5 changed files
with
85 additions
and
1 deletion.
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
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 @@ | ||
""" | ||
StreamingRender(func) | ||
An iterable that will run the render function `func`, which takes a single `io` | ||
argument that must be passed to the `@render` macro call. | ||
```julia | ||
for bytes in StreamingRender(io -> @render io @component {args...}) | ||
write(http_stream, bytes) | ||
end | ||
``` | ||
Or use a `do` block rather than `->` syntax. | ||
""" | ||
struct StreamingRender | ||
io::SimpleBufferStream.BufferStream | ||
task::Task | ||
|
||
function StreamingRender(f) | ||
io = SimpleBufferStream.BufferStream() | ||
task = Threads.@spawn begin | ||
f(io) | ||
close(io) | ||
end | ||
return new(io, task) | ||
end | ||
end | ||
|
||
function Base.iterate(s::StreamingRender, state = nothing) | ||
bytes = readavailable(s.io) | ||
if isempty(bytes) | ||
wait(s.task) | ||
return nothing | ||
else | ||
return bytes, nothing | ||
end | ||
end | ||
Base.eltype(::Type{StreamingRender}) = Vector{UInt8} | ||
Base.IteratorSize(::Type{StreamingRender}) = Base.SizeUnknown() | ||
|
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