Skip to content

Commit

Permalink
refactor register_renderable to not rely on overwriting a method
Browse files Browse the repository at this point in the history
fixes #119
  • Loading branch information
rdeits authored and shashi committed May 18, 2018
1 parent 3279ede commit a10715b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 29 deletions.
3 changes: 1 addition & 2 deletions src/providers/atom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ Juno.render(::Juno.PlotPane, n::Union{Node, Scope}) =
Juno.render(i::Juno.Editor, n::Union{Node, Scope}) =
Juno.render(i, Text("$(n.instanceof) Node with $(n._descendants_count) descendent(s)"))

function WebIO.register_renderable(T::Type)
function WebIO.register_renderable(T::Type, ::Val{:atom})
media(T, Media.Graphical)
Media.render(::Juno.PlotPane, x::T) =
(body!(get_page(), WebIO.render(x)); nothing)
if method_exists(WebIO.render_inline, (T,))
Juno.render(i::Juno.Editor, x::T) =
Juno.render(i, WebIO.render_inline(x))
end
WebIO.register_renderable_common(T)
end

function WebIO.setup_provider(::Val{:atom})
Expand Down
3 changes: 1 addition & 2 deletions src/providers/blink.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ end

Base.isopen(b::BlinkConnection) = Blink.active(b.page)

function WebIO.register_renderable(T::Type)
function WebIO.register_renderable(T::Type, ::Val{:blink})
Blink.body!(p::Union{Window, Page}, x::T) =
Blink.body!(p, WebIO.render(x))
WebIO.register_renderable_common(T)
end

WebIO.setup_provider(::Val{:blink}) = nothing # blink setup has no side-effects
Expand Down
4 changes: 1 addition & 3 deletions src/providers/ijulia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ end

Base.isopen(c::IJuliaConnection) = haskey(IJulia.CommManager.comms, c.comm.id)

function WebIO.register_renderable(T::Type)
WebIO.register_renderable_common(T)
end
WebIO.register_renderable(T::Type, ::Val{:ijulia}) = nothing

function main()
if !IJulia.inited
Expand Down
3 changes: 1 addition & 2 deletions src/providers/mux.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ function Mux.Response(o::Union{Node, Scope})
)
end

function WebIO.register_renderable(T::Type)
function WebIO.register_renderable(T::Type, ::Val{:mux})
Mux.Response(x::T) = Mux.Response(WebIO.render(x))
WebIO.register_renderable_common(T)
end

WebIO.setup_provider(::Val{:mux}) = nothing # Mux setup has no side-effects
Expand Down
26 changes: 9 additions & 17 deletions src/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,21 @@ IJulia to display the WebIO rendered version of the type as appropriate.
Also defines a `Base.show(io::IO, m::MIME"text/html", x::MyType)` as
`Base.show(io, m, WebIO.render(x))`
"""
function register_renderable(T)
# When a provider is initialised, a new method for this function will be
# created for T::Type, outspecialising this one. Until then we store these
# types so we can register them when the provider is setup.
println("register_renderable(Any) called")
if T isa Type
push!(renderable_types, T)
return true
else
ArgumentError("register_renderable should only be called with a Type. Was called with $T which is a $(typeof(T))")
end
function register_renderable(::Type{T}) where T
Base.show(io::IO, m::MIME"text/html", x::T) = Base.show(io, m, WebIO.render(x))
push!(renderable_types, T)
return true
end

"""
Called after a provider is setup
"""
function re_register_renderables()
foreach(T->Base.invokelatest(register_renderable, T), renderable_types)
end

function register_renderable_common(T::Type)
Base.show(io::IO, m::MIME"text/html", x::T) =
Base.show(io, m, WebIO.render(x))
for provider in providers_initialised
for T in renderable_types
register_renderable(T, Val(provider))
end
end
end


Expand Down
25 changes: 22 additions & 3 deletions test/blink-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function with_timeout(f::Function, timeout)
end

@testset "Blink mocks" begin

# open window and wait for it to initialize
w = Window(Dict(:show => false))

Expand Down Expand Up @@ -88,16 +87,36 @@ end

@testset "global URL, no http:" begin
# TODO: change this to a permanent URL because this CSAIL account
# will eventually expire.
# will eventually expire.
@test scope_import(w, "//people.csail.mit.edu/rdeits/webio_tests/trivial_import.js") == "ok"
end

@testset "global URL, with http:" begin
# TODO: change this to a permanent URL because this CSAIL account
# will eventually expire.
# will eventually expire.
@test scope_import(w, "http://people.csail.mit.edu/rdeits/webio_tests/trivial_import.js") == "ok"
end
end
end


example_renderable_was_rendered = false

struct ExampleRenderableType
end

function WebIO.render(::ExampleRenderableType)
global example_renderable_was_rendered
example_renderable_was_rendered = true
Node(:div, "hello world")
end

WebIO.register_renderable(ExampleRenderableType)

@testset "register_renderable" begin
w = Window(Dict(:show => false))
body!(w, ExampleRenderableType())
@test example_renderable_was_rendered
end

notinstalled && AtomShell.uninstall()

0 comments on commit a10715b

Please sign in to comment.