Skip to content

Commit

Permalink
Add :async option to make Window() synchronous!
Browse files Browse the repository at this point in the history
Setting `:async=>false` causes `Window()` to block until the window is
loaded and the socket is open:

```julia
julia> @time Window(Blink.@d(:async=>false));
  0.229105 seconds (4.10 k allocations: 365.495 KiB)

julia> @time Window(Blink.@d(:async=>true));
  0.018366 seconds (1.14 k allocations: 39.156 KiB)
```

Defaults to true (async), maintaining previous behavior.

Also removed all the now-unneccessary `sleep()`s from all tests.
  • Loading branch information
NHDaly committed Sep 22, 2018
1 parent 09bc951 commit c8abde2
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 12 deletions.
4 changes: 4 additions & 0 deletions res/blink.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@

Blink.click = click;

// Window creation callback: Mark this window as done loading.
if (typeof callback_id !== 'undefined') {
Blink.sock.onopen = ()=>{ cb(callback_id, true); }
}
})();
22 changes: 20 additions & 2 deletions src/AtomShell/window.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,26 @@ function Window(a::Shell, opts::AbstractDict = Dict())
end

function Window(a::Shell, content::Page, opts::AbstractDict = Dict())
opts = merge(opts, Dict(:url => Blink.localurl(content)))
return Window(raw_window(a, opts), a, content)
url = Blink.localurl(content)
is_async(opts) = get(opts, :async, true) # Async default: true
callback = !is_async(opts)
if callback
# Send the callback id as a query param in the url.
id, cond = Blink.callback!()
url *= "?callback=$id"
end
# Create the window.
opts = merge(opts, Dict(:url => url))
w = Window(raw_window(a, opts), a, content)
# If callback is requested, wait until the window has finished loading.
if callback
val = wait(cond)
if isa(val, AbstractDict) && get(val, "type", "") == "error"
err = JSError(get(val, "name", "unknown"), get(val, "message", "blank"))
throw(err)
end
end
return w
end

Window(args...) = Window(shell(), args...)
Expand Down
1 change: 1 addition & 0 deletions src/content/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<script>var id = {{id}}</script>
<script>{{optional_create_window_callback}}</script>
<script src="blink.js"></script>
<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="blink.css">
Expand Down
5 changes: 4 additions & 1 deletion src/content/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ function page_handler(req)
haskey(pool, id) || @goto fail
active(pool[id].value) && @goto fail

return render(maintp, d("id"=>id))
callback_id = try split(req[:query], "=")[2] catch e nothing end
callback_script = callback_id != nothing ? """var callback_id = $callback_id""" : ""
return render(maintp, d("id"=>id,
"optional_create_window_callback"=>callback_script))

@label fail
return d(:body => "Not found",
Expand Down
1 change: 0 additions & 1 deletion src/rpc/rpc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,3 @@ end
macro js_(o, ex)
:(js($(esc(o)), $(Expr(:quote, ex)), callback=false))
end

2 changes: 1 addition & 1 deletion test/AtomShell/window.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Blink
using Test

@testset "size Tests" begin
w = Window(Blink.@d(:show => false, :width=>150, :height=>100)) ; sleep(5.0);
w = Window(Blink.@d(:show => false, :width=>150, :height=>100, :async=>false));
@test size(w) == [150,100]

size(w, 200,200)
Expand Down
8 changes: 4 additions & 4 deletions test/content/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Blink
using Test

@testset "content! Tests" begin
w = Window(Blink.@d(:show => false)); sleep(5.0)
w = Window(Blink.@d(:show => false, :async=>false));
body!(w, "", async=false);
@test (@js w document.querySelector("body").innerHTML) == ""

Expand All @@ -20,22 +20,22 @@ using Test
fadeTestHtml = """<script>var testJS = "test";</script><div id="d">hi world</div>"""
@testset "Fade True" begin
# Must create a new window to ensure javascript is reset.
w = Window(Blink.@d(:show => false)); sleep(5.0)
w = Window(Blink.@d(:show => false));

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)
w = Window(Blink.@d(:show => false, :async=>false));

body!(w, fadeTestHtml; fade=false, async=false);
@test (@js w testJS) == "test"
end
end

@testset "Sync/Async content reload tests" begin
w = Window(Blink.@d(:show => false)); sleep(5.0)
w = Window(Blink.@d(:show => false, :async=>false));
sleep_content(seconds) = """
<script>
function spinsleep(ms) {
Expand Down
4 changes: 1 addition & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ cleanup = !AtomShell.isinstalled()
cleanup && AtomShell.install()

# open window and wait for it to initialize
# TODO: can we remove the sleep(10) when the Window()
# constructor is made synchronous?
w = Window(Blink.@d(:show => false)); sleep(10.0)
w = Window(Blink.@d(:show => false, :async=>false));

# make sure the window is really active
@test @js(w, Math.log(10)) log(10)
Expand Down

0 comments on commit c8abde2

Please sign in to comment.