Skip to content

Commit

Permalink
Add option to make content! synchronous.
Browse files Browse the repository at this point in the history
  • Loading branch information
NHDaly committed Jul 27, 2018
1 parent 41a9cd0 commit 2f6516d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
14 changes: 8 additions & 6 deletions res/blink.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,28 @@
}
}

function fill(node, html, fade) {
function fill(node, html, fade, resolve, reject) {
node = select(node);
fade ?
fillfade(node, html) :
fillnofade(node, html)
fillfade(node, html, resolve, reject) :
fillnofade(node, html, resolve, reject)
}
function fillfade(node, html) {
function fillfade(node, html, resolve, reject) {
node = select(node);
node.classList.add('blink-show');
callback(function () {
node.classList.add('blink-fade');
callback(0.2, function() {
fillnofade(node, html);
fillnofade(node, html, null, null);
node.classList.remove('blink-fade');
if (resolve) resolve(true);
});
});
}
function fillnofade(node, html) {
function fillnofade(node, html, resolve, reject) {
node.innerHTML = html;
evalscripts(node);
if (resolve) resolve(true);
}

Blink.fill = fill;
Expand Down
24 changes: 17 additions & 7 deletions src/content/api.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
export body!, content!, loadcss!, loadjs!, load!, importhtml!

content!(o, sel, html::AbstractString; fade = true) =
@js_(o, Blink.fill($sel, $html, $fade))
function content!(o, sel, html::AbstractString; fade = true, async = true)
if async
@js_(o, Blink.fill($sel, $html, $fade, null, null))
else
@js o begin # Use `@js` to wait until the below Promise is resolved.
@new Promise(function (resolve, reject)
Blink.fill($sel, $html, $fade, resolve, reject)
end)
end
return o # But still return `o` for chaining.
end
end

content!(o, sel, html; fade = true) =
content!(o, sel, stringmime(MIME"text/html"(), html), fade = fade)
content!(o, sel, html; fade = true, async = true) =
content!(o, sel, stringmime(MIME"text/html"(), html), fade=fade, async=async)

body!(w, html; fade = true) = content!(w, "body", html, fade = fade)
body!(w, html; fade = true, async = true) = content!(w, "body", html, fade=fade, async=async)

function loadcss!(w, url)
@js_ w begin
Expand Down Expand Up @@ -55,7 +65,7 @@ end

isurl(f) = ismatch(r"^https?://", f)

function load!(w, file)
function load!(w, file; async=false)
if !isurl(file)
resource(file)
file = basename(file)
Expand All @@ -66,7 +76,7 @@ function load!(w, file)
elseif ext == "css"
loadcss!(w, file)
elseif ext == "html"
importhtml!(w, file)
importhtml!(w, file; async)
else
error("Blink: Unsupported file type")
end
Expand Down
45 changes: 45 additions & 0 deletions test/content/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,48 @@ using Base.Test
@test (@js w testJS) == "test"
end
end

@testset "Sync/Async content reload tests" begin
w = Window(Blink.@d(:show => false)); sleep(5.0)
sleep_content(seconds) = """
<script>
function spinsleep(ms) {
var start = new Date().getTime(), expire = start + ms;
while (new Date().getTime() < expire) { }
return;
}
spinsleep($(seconds * 1000));
</script>
"""

@timed sleep(0.1); # Throw-away statement to warm-up @sync and @async

x, t = @timed body!(w, sleep_content(3); fade=true, async=false)
#@test x == true # TODO: What should it return?
@test t >= 3.0 # seconds

x, t = @timed body!(w, sleep_content(3); fade=false, async=false)
@test t >= 3.0 # seconds

x, t = @timed body!(w, sleep_content(3); fade=true, async=true);
@test t < 3.0 # seconds
sleep(3) # (Wait until the end of the previous body! call.)

x, t = @timed body!(w, sleep_content(3); fade=false, async=true);
@test t < 3.0 # seconds
sleep(3) # (Wait until the end of the previous body! call.)


@sync begin # Throw-away block to warm-up @sync and @async
@async sleep(0.1)
@async sleep(0.1)
end
# Test using Julia's async mechanisms with synchronous `content!`.
_, t = @timed @sync begin
@async body!(w, sleep_content(4); async=false);
sleep(4)
end

@test t >= 4.0
@test t < 8
end

0 comments on commit 2f6516d

Please sign in to comment.