Don't garbage collect input values when converting smart cell to code (#1781)

This commit is contained in:
Jonatan Kłosko 2023-03-13 18:31:31 +01:00 committed by GitHub
parent e019eae161
commit 07fab99353
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 13 deletions

View file

@ -999,21 +999,22 @@ defmodule Livebook.Session do
chunks = cell.chunks || [{0, byte_size(cell.source)}]
chunk_count = length(chunks)
state = handle_operation(state, {:delete_cell, client_id, cell.id})
state =
for {{offset, size}, chunk_idx} <- Enum.with_index(chunks), reduce: state do
state ->
outputs = if(chunk_idx == chunk_count - 1, do: cell.outputs, else: [])
source = binary_part(cell.source, offset, size)
attrs = %{source: source, outputs: outputs}
cell_idx = index + chunk_idx
cell_id = Utils.random_id()
for {{offset, size}, chunk_idx} <- Enum.with_index(chunks), reduce: state do
state ->
outputs = if(chunk_idx == chunk_count - 1, do: cell.outputs, else: [])
source = binary_part(cell.source, offset, size)
attrs = %{source: source, outputs: outputs}
cell_idx = index + chunk_idx
cell_id = Utils.random_id()
handle_operation(
state,
{:insert_cell, client_id, section.id, cell_idx, :code, cell_id, attrs}
)
end
handle_operation(
state,
{:insert_cell, client_id, section.id, cell_idx, :code, cell_id, attrs}
)
end
handle_operation(state, {:delete_cell, client_id, cell.id})
else
_ -> state
end

View file

@ -198,6 +198,35 @@ defmodule Livebook.SessionTest do
{:insert_cell, _client_id, ^section_id, 1, :code, _id,
%{source: "chunk 2", outputs: [{1, {:text, "Hello"}}]}}}
end
test "doesn't garbage collect input values" do
input = %{
ref: :input_ref,
id: "input1",
type: :text,
label: "Name",
default: "hey",
destination: :noop
}
smart_cell = %{
Notebook.Cell.new(:smart)
| kind: "text",
source: "content",
outputs: [{1, {:input, input}}]
}
section = %{Notebook.Section.new() | cells: [smart_cell]}
notebook = %{Notebook.new() | sections: [section]}
session = start_session(notebook: notebook)
Session.subscribe(session.id)
Session.convert_smart_cell(session.pid, smart_cell.id)
assert %{input_values: %{"input1" => "hey"}} = Session.get_data(session.pid)
end
end
describe "add_dependencies/2" do