Skip to content

Commit

Permalink
Create Comm from the JS side rather than Julia side.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashi Gowda authored and shashi committed Sep 10, 2018
1 parent 04f9697 commit 35346c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
26 changes: 17 additions & 9 deletions assets/providers/ijulia_setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@

// Register a "target" so that Julia can create a Comm
// to communicate.
commManager.register_target("webio_comm",
function (comm) {
WebIO.triggerConnected();
WebIO.sendCallback = function (msg) { comm.send(msg); }
comm.on_msg(function (msg) {
WebIO.dispatch(msg.content.data);
});
}
);
commManager.register_target("webio_comm", function (comm) { })

// Create a comm -- this will be receprocated by Julia, and will trigger the
// method IJulia.CommManager.register_comm(Comm{:webio_comm}, x) which will
// set up forwarding of messages
var comm = commManager.new_comm("webio_comm", {})

// Set how WebIO communicates with Julia
WebIO.sendCallback = function (msg) { comm.send(msg) }

// dispatch messages from Julia to WebIO
comm.on_msg(function (msg) {
WebIO.dispatch(msg.content.data)
});

// Get existing Scopes ready
WebIO.triggerConnected()
}

$(document).ready(function() {
Expand Down
16 changes: 8 additions & 8 deletions src/providers/ijulia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ Base.isopen(c::IJuliaConnection) = haskey(IJulia.CommManager.comms, c.comm.id)

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

function IJulia.CommManager.register_comm(comm::IJulia.CommManager.Comm{:webio_comm}, x)
conn = IJuliaConnection(comm)
comm.on_msg = function (msg)
data = msg.content["data"]
WebIO.dispatch(conn, data)
end
end

function main()
if !IJulia.inited
# If IJulia has not been initialized and connected to Jupyter itself,
Expand Down Expand Up @@ -48,14 +56,6 @@ function main()
\$('.js-collapse-script').parent('.output_subarea').css('padding', '0');
</script>
"""))

comm = IJulia.CommManager.Comm(:webio_comm)
conn = IJuliaConnection(comm)
comm.on_msg = function (msg)
data = msg.content["data"]
WebIO.dispatch(conn, data)
end
nothing
end

WebIO.setup_provider(::Val{:ijulia}) = main() # calling setup_provider(Val(:ijulia)) will display the setup javascript
Expand Down

0 comments on commit 35346c2

Please sign in to comment.