Skip to content

Commit

Permalink
🦋 Fix disconnect on cell deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Apr 1, 2020
1 parent fc6652e commit 797ceda
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Pluto"
uuid = "c3e4b0f8-55cb-11ea-2926-15256bba5781"
license = "MIT"
authors = ["Fons van der Plas <[email protected]>", "MikoÅ‚aj Bochenski <[email protected]>"]
version = "0.5.2"
version = "0.5.3"

[deps]
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Expand Down
3 changes: 3 additions & 0 deletions assets/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ document.addEventListener("DOMContentLoaded", () => {
}

function requestRunAllRemoteCells() {
for (var uuid in window.localCells) {
window.localCells[uuid].classList.add("running")
}
client.send("runall", {})
}

Expand Down
7 changes: 6 additions & 1 deletion src/notebookserver/Client.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
mutable struct Client
id::Symbol
stream::Any
stream_accesstoken::Channel{Nothing}
connected_notebook::Union{Notebook,Nothing}
pendingupdates::Channel
end

Client(id::Symbol, stream) = Client(id, stream, nothing, Channel(128))
Client(id::Symbol, stream) = let
at = Channel{Nothing}(1)
put!(at, nothing)
Client(id, stream, at, nothing, Channel(128))
end


struct UpdateMessage
Expand Down
2 changes: 1 addition & 1 deletion src/react/Run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ end


"See `run_reactive`."
function run_reactive_async!(initiator, notebook::Notebook, cells::Array{Cell, 1})
function run_reactive_async!(initiator, notebook::Notebook, cells::Array{Cell, 1})::Task
@async begin
# because this is being run async, we need to catch exceptions manually
try
Expand Down
22 changes: 11 additions & 11 deletions src/webserver/Dynamic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function clientupdate_notebook_list(initiator::Client, notebook_list)
end


function handle_changecell(initiator::Client, notebook, cell, newcode)
function handle_changecell(initiator::Client, notebook, cell, newcode)::Task
# i.e. Ctrl+Enter was pressed on this cell
# we update our `Notebook` and start execution

Expand Down Expand Up @@ -136,18 +136,22 @@ responses[:addcell] = (initiator::Client, body, notebook::Notebook) -> begin
insert!(notebook.cells, new_index, new_cell)

putnotebookupdates!(notebook, clientupdate_cell_added(initiator, notebook, new_cell, new_index))
nothing
end

responses[:deletecell] = (initiator::Client, body, notebook::Notebook, cell::Cell) -> begin
to_delete = cell

changecell_succes = handle_changecell(initiator, notebook, to_delete, "")
# replace the cell's code with "" and do a reactive run
runtask = handle_changecell(initiator, notebook, to_delete, "")

filter!(c->c.uuid ≠ to_delete.uuid, notebook.cells)

putnotebookupdates!(notebook, clientupdate_cell_deleted(initiator, notebook, to_delete))
nothing
# wait for the reactive run to finish, then delete the cells
# we wait async, to make sure that the web server remains responsive
@async begin
wait(runtask)

filter!(c->c.uuid ≠ to_delete.uuid, notebook.cells)
putnotebookupdates!(notebook, clientupdate_cell_deleted(initiator, notebook, to_delete))
end
end

responses[:movecell] = (initiator::Client, body, notebook::Notebook, cell::Cell) -> begin
Expand All @@ -170,14 +174,12 @@ responses[:movecell] = (initiator::Client, body, notebook::Notebook, cell::Cell)
end

putnotebookupdates!(notebook, clientupdate_cell_moved(initiator, notebook, to_move, new_index))
nothing
end

responses[:changecell] = (initiator::Client, body, notebook::Notebook, cell::Cell) -> begin
newcode = body["code"]

handle_changecell(initiator, notebook, cell, newcode)
nothing
end

responses[:runall] = (initiator::Client, body, notebook::Notebook) -> begin
Expand All @@ -200,10 +202,8 @@ responses[:getallcells] = (initiator::Client, body, notebook::Notebook) -> begin
# [clientupdate_cell_added(notebook, c, i) for (i, c) in enumerate(notebook.cells)]

putnotebookupdates!(notebook, updates...)
nothing
end

responses[:getallnotebooks] = (initiator::Client, body, notebook=nothing) -> begin
putplutoupdates!(clientupdate_notebook_list(initiator, values(notebooks)))
nothing
end
2 changes: 2 additions & 0 deletions src/webserver/WebServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function flushclient(client)
next_to_send = take!(client.pendingupdates)
didsomething = true

token = take!(client.stream_accesstoken)
try
if client.stream !== nothing
if isopen(client.stream)
Expand All @@ -62,6 +63,7 @@ function flushclient(client)
@warn "Failed to write to WebSocket of $(client.id) " exception=(ex,bt)
return false
end
put!(client.stream_accesstoken, token)
end
# put!(flushtoken, nothing)
true
Expand Down

2 comments on commit 797ceda

@fonsp
Copy link
Owner Author

@fonsp fonsp commented on 797ceda Apr 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/11939

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.3 -m "<description of version>" 797ceda83d7f80b571c850fa8e9e049615076a5e
git push origin v0.5.3

Please sign in to comment.