Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix intermediate bond values again #2992

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion frontend/components/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,9 @@ patch: ${JSON.stringify(
const set_waiting = () => {
let from_update = message?.response?.update_went_well != null
let is_just_acknowledgement = from_update && message.patches.length === 0
// console.log("Received patches!", message.patches, message.response, is_just_acknowledgement)
let is_relevant_for_bonds = message.patches.some(({ path }) => path.length === 0 || path[0] !== "status_tree")

// console.debug("Received patches!", is_just_acknowledgement, is_relevant_for_bonds, message.patches, message.response)

if (!is_just_acknowledgement) {
this.waiting_for_bond_to_trigger_execution = false
Expand Down
3 changes: 3 additions & 0 deletions src/evaluation/Run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ function run_reactive_core!(

old_workspace_name, _ = WorkspaceManager.bump_workspace_module((session, notebook))

# A state sync will come soon from this function, so let's delay anything coming from the status_tree listener, see https://github.com/fonsp/Pluto.jl/issues/2978
Throttled.force_throttle_without_run(notebook.status_tree.update_listener_ref[])

run_status = Status.report_business_started!(notebook.status_tree, :run)
Status.report_business_started!(run_status, :resolve_topology)
cell_status = Status.report_business_planned!(run_status, :evaluate)
Expand Down
66 changes: 63 additions & 3 deletions test/frontend/__tests__/bonds.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import puppeteer from "puppeteer"
import { saveScreenshot, createPage, paste } from "../helpers/common"
import { createNewNotebook, getPlutoUrl, runAllChanged, setupPlutoBrowser, shutdownCurrentNotebook, waitForPlutoToCalmDown } from "../helpers/pluto"
import { saveScreenshot, createPage, paste, waitForContentToBecome, waitForContent } from "../helpers/common"
import {
createNewNotebook,
getPlutoUrl,
importNotebook,
runAllChanged,
setupPlutoBrowser,
shutdownCurrentNotebook,
waitForCellOutput,
waitForPlutoToCalmDown,
} from "../helpers/pluto"

// https://github.com/fonsp/Pluto.jl/issues/928
describe("Bonds should run once when refreshing page", () => {
describe("@bind", () => {
/**
* Launch a shared browser instance for all tests.
* I don't use jest-puppeteer because it takes away a lot of control and works buggy for me,
Expand Down Expand Up @@ -78,4 +87,55 @@ numberoftimes = Ref(0)
})
expect(output_after_reload).toBe(output_after_running_bonds)
})

it("should ignore intermediate bond values while the notebook is running", async () => {
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

const chill = async () => {
await wait(300)
await waitForPlutoToCalmDown(page)
await wait(1500)
await waitForPlutoToCalmDown(page)
}

await importNotebook(page, "test_bind_dynamics.jl")
await chill()
await chill()

const id = `029e1d1c-bf42-4e2c-a141-1e2eecc0800d`
const output_selector = `pluto-cell[id="${id}"] pluto-output`

//page.click is stupid
const click = async (sel) => {
await page.waitForSelector(sel)
await page.evaluate((sel) => document.querySelector(sel).click(), sel)
}

const reset = async () => {
await click(`#reset_xs_button`)
await wait(300)
await waitForPlutoToCalmDown(page)
await waitForContentToBecome(page, output_selector, "")
await wait(300)
await waitForPlutoToCalmDown(page)
await waitForContentToBecome(page, output_selector, "")
await wait(300)
}

const start = async () => {
await click(`#add_x_button`)
await chill()

return await waitForContent(page, output_selector)
}

await reset()
await start()

await chill()

await reset()
const val = await start()
expect(val).toBe("1,done")
})
})
91 changes: 91 additions & 0 deletions test/frontend/fixtures/test_bind_dynamics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
### A Pluto.jl notebook ###
# v0.19.45

using Markdown
using InteractiveUtils

# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
el
end
end

# ╔═╡ a0fe4e4d-eee5-4420-bd58-3f12749a9ed1
@bind reset_xs html"<input id=reset_xs_button type=button value=reset_xs>"

# ╔═╡ 58db6bd4-58a6-11ef-3795-fd6e57eceb68
@bind x html"""<div>
<script>
const div = currentScript.parentElement
const btn = div.querySelector("button")
let start_time = 0
let max_duration = 1000
const set = (x) => {
div.value = x
div.dispatchEvent(new CustomEvent("input"))
}
function go() {
if(Date.now() - start_time < max_duration) {
set(div.value + 1)
requestAnimationFrame(go)
} else {
set("done")
}
}
btn.onclick = () => {
div.value = 0
start_time = Date.now()
go()
}
</script>
<button id=add_x_button>Start</button>
</div>"""

# ╔═╡ 8568c646-0233-4a95-8332-2351e9c56027
@bind withsleep html"<input id=withsleep type=checkbox checked>"

# ╔═╡ 8a20fa4a-ac02-4a37-a54e-e4224628db66
x

# ╔═╡ 29f1d840-574e-463c-87d3-4b938e123493
begin
reset_xs
xs = []
end

# ╔═╡ 3155b6e0-8e19-4583-b2ab-4ab2db1f10b9
md"""
Click **reset_xs**.
Click **Start**.
The cell below should give: `1,done`.
Not: `1,2,done` or something like that. That means that an intermediate bond value (`2`) found its way through: [https://github.com/fonsp/Pluto.jl/issues/1891](https://github.com/fonsp/Pluto.jl/issues/1891)
"""

# ╔═╡ 029e1d1c-bf42-4e2c-a141-1e2eecc0800d
begin
withsleep && sleep(1.5)
push!(xs, x)
xs_done = true

join(xs[2:end], ",") |> Text
end

# ╔═╡ Cell order:
# ╟─a0fe4e4d-eee5-4420-bd58-3f12749a9ed1
# ╟─58db6bd4-58a6-11ef-3795-fd6e57eceb68
# ╟─8568c646-0233-4a95-8332-2351e9c56027
# ╠═8a20fa4a-ac02-4a37-a54e-e4224628db66
# ╟─3155b6e0-8e19-4583-b2ab-4ab2db1f10b9
# ╠═029e1d1c-bf42-4e2c-a141-1e2eecc0800d
# ╠═29f1d840-574e-463c-87d3-4b938e123493
Loading