-
-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
168 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export const base64_arraybuffer = async (/** @type {BufferSource} */ data) => { | ||
const base64url = await new Promise((r) => { | ||
const reader = new FileReader() | ||
reader.onload = () => r(reader.result) | ||
reader.readAsDataURL(new Blob([data])) | ||
}) | ||
|
||
return base64url.split(",", 2)[1] | ||
} | ||
|
||
export const hash_arraybuffer = async (/** @type {BufferSource} */ data) => { | ||
// @ts-ignore | ||
const hashed_buffer = await window.crypto.subtle.digest("SHA-256", data) | ||
return await base64_arraybuffer(hashed_buffer) | ||
} | ||
|
||
export const hash_str = async (/** @type {string} */ s) => { | ||
const data = new TextEncoder().encode(s) | ||
return await hash_arraybuffer(data) | ||
} | ||
|
||
export const debounced_promises = (async_function) => { | ||
let currently_running = false | ||
let rerun_when_done = false | ||
|
||
return async () => { | ||
if (currently_running) { | ||
rerun_when_done = true | ||
} else { | ||
currently_running = true | ||
rerun_when_done = true | ||
while (rerun_when_done) { | ||
rerun_when_done = false | ||
await async_function() | ||
} | ||
currently_running = false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { trailingslash } from "./Binder.js" | ||
import { hash_arraybuffer, debounced_promises, base64_arraybuffer } from "./PlutoHash.js" | ||
import { pack, unpack } from "./MsgPack.js" | ||
import immer from "../imports/immer.js" | ||
import _ from "../imports/lodash.js" | ||
|
||
export const nothing_actions = ({ actions }) => Object.fromEntries(Object.keys(actions).map((k) => [k, () => {}])) | ||
|
||
export const slider_server_actions = ({ setStatePromise, launch_params, actions, get_original_state, get_current_state, apply_notebook_patches }) => { | ||
const notebookfile_hash = fetch(launch_params.notebookfile) | ||
.then((r) => r.arrayBuffer()) | ||
.then(hash_arraybuffer) | ||
|
||
notebookfile_hash.then((x) => console.log("Notebook file hash:", x)) | ||
|
||
const bond_connections = notebookfile_hash | ||
.then((hash) => fetch(trailingslash(launch_params.slider_server_url) + "bondconnections/" + encodeURIComponent(hash) + "/")) | ||
.then((r) => r.arrayBuffer()) | ||
.then((b) => unpack(new Uint8Array(b))) | ||
|
||
bond_connections.then((x) => console.log("Bond connections:", x)) | ||
|
||
const mybonds = {} | ||
const bonds_to_set = { | ||
current: new Set(), | ||
} | ||
const request_bond_response = debounced_promises(async () => { | ||
const base = trailingslash(launch_params.slider_server_url) | ||
const hash = await notebookfile_hash | ||
const graph = await bond_connections | ||
|
||
if (bonds_to_set.current.size > 0) { | ||
const to_send = new Set(bonds_to_set.current) | ||
bonds_to_set.current.forEach((varname) => (graph[varname] ?? []).forEach((x) => to_send.add(x))) | ||
console.debug("Requesting bonds", bonds_to_set.current, to_send) | ||
bonds_to_set.current = new Set() | ||
|
||
const mybonds_filtered = Object.fromEntries(Object.entries(mybonds).filter(([k, v]) => to_send.has(k))) | ||
|
||
const packed = pack(mybonds_filtered) | ||
|
||
const url = base + "staterequest/" + encodeURIComponent(hash) + "/" | ||
|
||
try { | ||
const use_get = url.length + (packed.length * 4) / 3 + 20 < 8000 | ||
|
||
const response = use_get | ||
? await fetch(url + encodeURIComponent(await base64_arraybuffer(packed)), { | ||
method: "GET", | ||
}) | ||
: await fetch(url, { | ||
method: "POST", | ||
body: packed, | ||
}) | ||
|
||
const { patches, ids_of_cells_that_ran } = unpack(new Uint8Array(await response.arrayBuffer())) | ||
|
||
await apply_notebook_patches( | ||
patches, | ||
immer((state) => { | ||
const original = get_original_state() | ||
ids_of_cells_that_ran.forEach((id) => { | ||
state.cell_results[id] = original.cell_results[id] | ||
}) | ||
})(get_current_state()) | ||
) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} | ||
}) | ||
|
||
return { | ||
...nothing_actions({ actions }), | ||
set_bond: async (symbol, value, is_first_value) => { | ||
setStatePromise( | ||
immer((state) => { | ||
state.notebook.bonds[symbol] = { value: value } | ||
}) | ||
) | ||
if (mybonds[symbol] == null || !_.isEqual(mybonds[symbol].value, value)) { | ||
mybonds[symbol] = { value: value } | ||
bonds_to_set.current.add(symbol) | ||
await request_bond_response() | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters