From e02bcce0bfc8c5e55eb3a3e61b1c13522f99fc32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Thu, 10 Jun 2021 15:41:40 +0200 Subject: [PATCH] Drop cell source text from data in LV when no longer needed (#342) --- lib/livebook/session/data.ex | 4 +++- lib/livebook_web/live/session_live.ex | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/livebook/session/data.ex b/lib/livebook/session/data.ex index e57abb1a7..9f42c0cee 100644 --- a/lib/livebook/session/data.ex +++ b/lib/livebook/session/data.ex @@ -791,7 +791,9 @@ defmodule Livebook.Session.Data do Delta.transform(delta_ahead, transformed_new_delta, :left) end) - new_source = JSInterop.apply_delta_to_string(transformed_new_delta, cell.source) + # Note: the session LV drops cell's source once it's no longer needed + new_source = + cell.source && JSInterop.apply_delta_to_string(transformed_new_delta, cell.source) data_actions |> set!(notebook: Notebook.update_cell(data.notebook, cell.id, &%{&1 | source: new_source})) diff --git a/lib/livebook_web/live/session_live.ex b/lib/livebook_web/live/session_live.ex index 414d089e3..609ebacbb 100644 --- a/lib/livebook_web/live/session_live.ex +++ b/lib/livebook_web/live/session_live.ex @@ -333,6 +333,10 @@ defmodule LivebookWeb.SessionLive do evaluation_digest: encode_digest(info.evaluation_digest) } + # From this point on we don't need cell source in the LV, + # so we are going to drop it altogether + socket = remove_cell_source(socket, cell_id) + {:reply, payload, socket} :error -> @@ -761,6 +765,12 @@ defmodule LivebookWeb.SessionLive do defp encode_digest(nil), do: nil defp encode_digest(digest), do: Base.encode64(digest) + defp remove_cell_source(socket, cell_id) do + update_in(socket.private.data.notebook, fn notebook -> + Notebook.update_cell(notebook, cell_id, &%{&1 | source: nil}) + end) + end + # Builds view-specific structure of data by cherry-picking # only the relevant attributes. # We then use `@data_view` in the templates and consequently @@ -833,6 +843,8 @@ defmodule LivebookWeb.SessionLive do %{ id: cell.id, type: :elixir, + # Note: we need this during initial loading, + # at which point we still have the source empty?: cell.source == "", outputs: cell.outputs, validity_status: info.validity_status, @@ -844,6 +856,8 @@ defmodule LivebookWeb.SessionLive do %{ id: cell.id, type: :markdown, + # Note: we need this during initial loading, + # at which point we still have the source empty?: cell.source == "" } end