diff --git a/res/blink.js b/res/blink.js index 726ca92..1331dc6 100644 --- a/res/blink.js +++ b/res/blink.js @@ -147,4 +147,9 @@ Blink.click = click; + + // Window creation callback + if (typeof callback_id !== 'undefined') { + Blink.sock.onopen = ()=>{ cb(callback_id, true); } + } })(); diff --git a/src/AtomShell/window.jl b/src/AtomShell/window.jl index 4829d89..0a4e420 100644 --- a/src/AtomShell/window.jl +++ b/src/AtomShell/window.jl @@ -30,9 +30,25 @@ function Window(a::Shell, opts::Associative = Dict()) Window(a, Page(), opts) end +default_async = false +is_async(opts) = haskey(opts, :async) ? opts[:async] : default_async function Window(a::Shell, content::Page, opts::Associative = Dict()) - opts = merge(opts, Dict(:url => Blink.localurl(content))) - return Window(raw_window(a, opts), a, content) + url = Blink.localurl(content) + callback = !is_async(opts) + if callback + id, cond = Blink.callback!() + url *= "?callback=$id" + end + opts = merge(opts, Dict(:url => url)) + w = Window(raw_window(a, opts), a, content) + if callback + val = wait(cond) + if isa(val, Associative) && 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...) diff --git a/src/content/main.html b/src/content/main.html index 7130274..7876946 100644 --- a/src/content/main.html +++ b/src/content/main.html @@ -2,6 +2,7 @@
+ diff --git a/src/content/server.jl b/src/content/server.jl index 77879d0..0f5f3f8 100644 --- a/src/content/server.jl +++ b/src/content/server.jl @@ -13,14 +13,20 @@ const resroute = const maintp = Mustache.template_from_file(joinpath(dirname(@__FILE__), "main.html")) -app(f) = req -> render(maintp, d("id"=>Page(f).id)) +function app(f) + println("app($f)") + req -> render(maintp, d("id"=>Page(f).id)) +end function page_handler(req) id = try parse(req[:params][:id]) catch e @goto fail end 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", diff --git a/src/rpc/rpc.jl b/src/rpc/rpc.jl index 25e13a9..c2289b9 100644 --- a/src/rpc/rpc.jl +++ b/src/rpc/rpc.jl @@ -51,4 +51,3 @@ end macro js_(o, ex) :(js($(esc(o)), $(Expr(:quote, ex)), callback=false)) end - diff --git a/test/AtomShell/window.jl b/test/AtomShell/window.jl index 162ba76..3b507e8 100644 --- a/test/AtomShell/window.jl +++ b/test/AtomShell/window.jl @@ -2,7 +2,7 @@ using Blink using Base.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) diff --git a/test/content/api.jl b/test/content/api.jl index d561df7..38f703a 100644 --- a/test/content/api.jl +++ b/test/content/api.jl @@ -2,7 +2,7 @@ using Blink using Base.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) == "" @@ -20,14 +20,14 @@ using Base.Test fadeTestHtml = """