From 944a41a1136dd6a8041970e208b15302253fc248 Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Mon, 8 Jan 2018 23:50:25 -0600 Subject: [PATCH 1/2] Add option to make `content!` synchronous. --- res/blink.js | 14 ++++++++------ src/content/api.jl | 24 +++++++++++++++++------- test/content/api.jl | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 13 deletions(-) diff --git a/res/blink.js b/res/blink.js index cf1e7ae..726ca92 100644 --- a/res/blink.js +++ b/res/blink.js @@ -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; diff --git a/src/content/api.jl b/src/content/api.jl index 902883f..5630d46 100644 --- a/src/content/api.jl +++ b/src/content/api.jl @@ -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 @@ -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) @@ -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 diff --git a/test/content/api.jl b/test/content/api.jl index 6c9aa43..976e945 100644 --- a/test/content/api.jl +++ b/test/content/api.jl @@ -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) = """ + + """ + + @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 From 762d99351b8192fd016dc8e65bbfffb25269e15f Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Fri, 27 Jul 2018 14:57:47 -0400 Subject: [PATCH 2/2] Speed up test via synchronous content! calls Removed 5 `sleep(1)` statements, and reduced test time by 4.5 seconds: ``` $ time julia test/content/api.jl # Before julia test/content/api.jl 15.07s user 0.33s system 28% cpu 54.958 total $ time julia test/content/api.jl # After julia test/content/api.jl 15.60s user 0.35s system 31% cpu 51.197 total ``` --- test/content/api.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/content/api.jl b/test/content/api.jl index 976e945..d561df7 100644 --- a/test/content/api.jl +++ b/test/content/api.jl @@ -3,15 +3,15 @@ using Base.Test @testset "content! Tests" begin w = Window(Blink.@d(:show => false)); sleep(5.0) - body!(w, ""); sleep(1.0) # body! is not synchronous. + body!(w, "", async=false); @test (@js w document.querySelector("body").innerHTML) == "" # Test reloading body and a selector element. html = """
hi world
bye
""" - body!(w, html); sleep(1.0) + body!(w, html, async=false); @test (@js w document.getElementById("a").innerHTML) == "hi world" @test (@js w document.getElementById("b").innerHTML) == "bye" - content!(w, "div", "hello world"); sleep(1.0) + content!(w, "div", "hello world", async=false); @test (@js w document.getElementById("a").innerHTML) == "hello world" # TODO(nhdaly): Is this right? Should content!(w,"div",...) change _all_ divs? @test (@js w document.getElementById("b").innerHTML) == "bye" @@ -22,14 +22,14 @@ using Base.Test # Must create a new window to ensure javascript is reset. w = Window(Blink.@d(:show => false)); sleep(5.0) - body!(w, fadeTestHtml; fade=true); sleep(1.0) + body!(w, fadeTestHtml; fade=true, async=false); @test (@js w testJS) == "test" end @testset "Fade False" begin # Must create a new window to ensure javascript is reset. w = Window(Blink.@d(:show => false)); sleep(5.0) - body!(w, fadeTestHtml; fade=false); sleep(1.0) + body!(w, fadeTestHtml; fade=false, async=false); @test (@js w testJS) == "test" end end