From 797ceda83d7f80b571c850fa8e9e049615076a5e Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Wed, 1 Apr 2020 16:57:40 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=8B=20Fix=20disconnect=20on=20cell=20d?= =?UTF-8?q?eletion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project.toml | 2 +- assets/editor.js | 3 +++ src/notebookserver/Client.jl | 7 ++++++- src/react/Run.jl | 2 +- src/webserver/Dynamic.jl | 22 +++++++++++----------- src/webserver/WebServer.jl | 2 ++ 6 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Project.toml b/Project.toml index eb33d3bc91..9af10564dd 100644 --- a/Project.toml +++ b/Project.toml @@ -2,7 +2,7 @@ name = "Pluto" uuid = "c3e4b0f8-55cb-11ea-2926-15256bba5781" license = "MIT" authors = ["Fons van der Plas ", "Mikołaj Bochenski "] -version = "0.5.2" +version = "0.5.3" [deps] Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" diff --git a/assets/editor.js b/assets/editor.js index 32627830a4..8dbdbc418c 100644 --- a/assets/editor.js +++ b/assets/editor.js @@ -294,6 +294,9 @@ document.addEventListener("DOMContentLoaded", () => { } function requestRunAllRemoteCells() { + for (var uuid in window.localCells) { + window.localCells[uuid].classList.add("running") + } client.send("runall", {}) } diff --git a/src/notebookserver/Client.jl b/src/notebookserver/Client.jl index d83e7474c5..9a9877c60f 100644 --- a/src/notebookserver/Client.jl +++ b/src/notebookserver/Client.jl @@ -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 diff --git a/src/react/Run.jl b/src/react/Run.jl index 27884173bd..eb86e18ded 100644 --- a/src/react/Run.jl +++ b/src/react/Run.jl @@ -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 diff --git a/src/webserver/Dynamic.jl b/src/webserver/Dynamic.jl index fabea59a7f..ff6b3c64f7 100644 --- a/src/webserver/Dynamic.jl +++ b/src/webserver/Dynamic.jl @@ -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 @@ -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 @@ -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 @@ -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 \ No newline at end of file diff --git a/src/webserver/WebServer.jl b/src/webserver/WebServer.jl index c85e5fa921..994d7d476e 100644 --- a/src/webserver/WebServer.jl +++ b/src/webserver/WebServer.jl @@ -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) @@ -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