Skip to content

Commit ec4d208

Browse files
Convert IJulia provider to extension
1 parent 5d5b847 commit ec4d208

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

Project.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
1919
WebSockets = "104b5d7c-a370-577a-8038-80a2059c5097"
2020
Widgets = "cc8bc4a8-27d6-5769-a93b-9d913e69aa62"
2121

22+
[weakdeps]
23+
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
24+
25+
[extensions]
26+
IJuliaExt = "IJulia"
27+
2228
[compat]
2329
AssetRegistry = "0.1.0"
2430
FunctionalCollections = "0.5.0"
31+
IJulia = "1.13"
2532
JSON = "0.18, 0.19, 0.20, 0.21"
2633
Observables = "0.5"
2734
Requires = "0.4.4, 0.5, 1.0.0"

ext/ijulia.jl renamed to ext/IJuliaExt.jl

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
using .AssetRegistry
2-
using .Sockets
3-
using .WebIO
1+
module IJuliaExt
42

5-
struct IJuliaConnection <: AbstractConnection
6-
comm::IJulia.CommManager.Comm
7-
end
3+
using WebIO
4+
using WebIO: WEBIO_NODE_MIME, _IJuliaInit
5+
using IJulia, Sockets
6+
7+
# struct IJuliaConnection <: AbstractConnection
8+
# comm::IJulia.CommManager.Comm
9+
# end
10+
IJuliaConnection = Connection{IJulia.CommManager.Comm}
811

912
function Sockets.send(c::IJuliaConnection, data)
10-
IJulia.send_comm(c.comm, data)
13+
IJulia.send_comm(connection(c), data)
1114
end
1215

13-
Base.isopen(c::IJuliaConnection) = haskey(IJulia.CommManager.comms, c.comm.id)
16+
Base.isopen(c::IJuliaConnection) = haskey(IJulia.CommManager.comms, connection(c).id)
1417

1518
WebIO.register_renderable(T::Type, ::Val{:ijulia}) = nothing
1619

@@ -37,7 +40,6 @@ installed for `WEBIO_NODE_MIME` that is preferred over `text/html`. If the HTML
3740
content is actually displayed, it means that the WebIO integration is not
3841
correctly installed.
3942
"""
40-
struct _IJuliaInit end
4143

4244
function Base.show(io::IO, m::WEBIO_NODE_MIME, ::_IJuliaInit)
4345
Base.show(io, m, node(:div))
@@ -68,16 +70,18 @@ function main()
6870
return
6971
end
7072

71-
# https://github.com/JuliaLang/IJulia.jl/pull/755
72-
if isdefined(IJulia, :register_jsonmime)
73-
IJulia.register_jsonmime(WEBIO_NODE_MIME())
74-
else
75-
@warn "IJulia doesn't have register_mime; WebIO may not work as expected. Please upgrade to IJulia v1.13.0 or greater."
76-
end
73+
IJulia.register_jsonmime(WEBIO_NODE_MIME())
7774

7875
# See comment on _IJuliaInit for what this does
7976
display(_IJuliaInit())
77+
78+
return nothing
8079
end
8180

8281
WebIO.setup_provider(::Val{:ijulia}) = main() # calling setup_provider(Val(:ijulia)) will display the setup javascript
83-
WebIO.setup(:ijulia)
82+
83+
function __init__()
84+
WebIO.setup(:ijulia)
85+
end
86+
87+
end

src/WebIO.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,24 @@ function prefetch_provider_file(basename)
9595
end
9696

9797
provider_mux = prefetch_provider_file("mux.jl")
98-
provider_ijulia = prefetch_provider_file("ijulia.jl")
9998
provider_generic_http = prefetch_provider_file("generic_http.jl")
10099

100+
struct _IJuliaInit
101+
function _IJuliaInit()
102+
# based on assert_extension from longemen3000/ExtensionsExt
103+
ext = Base.get_extension(@__MODULE__, :IJuliaExt)
104+
if isnothing(ext)
105+
throw(error("Extension `IJuliaExt` must be loaded to construct internal type `_IJuliaInit`."))
106+
end
107+
return new()
108+
end
109+
end
110+
101111
function __init__()
102112
push!(Observables.addhandler_callbacks, WebIO.setup_comm)
103113
@require Mux="a975b10e-0019-58db-a62f-e48ff68538c9" begin
104114
include_string(@__MODULE__, provider_mux.code, provider_mux.file)
105115
end
106-
@require IJulia="7073ff75-c697-5162-941a-fcdaad2a7d2a" begin
107-
include_string(@__MODULE__, provider_ijulia.code, provider_ijulia.file)
108-
end
109116
@require WebSockets="104b5d7c-a370-577a-8038-80a2059c5097" begin
110117
include_string(@__MODULE__, provider_generic_http.code, provider_generic_http.file)
111118
end

src/connection.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
using Sockets
22

3-
export AbstractConnection
3+
export AbstractConnection, Connection, connection
44

55
abstract type AbstractConnection end
66

7+
struct Connection{C} <: AbstractConnection
8+
conn::C
9+
end
10+
11+
function connection end
12+
connection(c::Connection) = c.conn
13+
714
"""
815
The maximum number of messages to allow into the outbox.
916
"""

0 commit comments

Comments
 (0)