Public
Edited
Nov 16, 2024
1 fork
Importers
12 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
thrownError = {
throw new Error("I was thrown!");
}
Insert cell
Insert cell
Insert cell
notebookSnapshot = (knownVariable) => {
const moduleName = knownVariable
? modules.get(
[...runtime._variables].find((v) => v._name === knownVariable)?._module
)
: "main";
if (!moduleName) {
debugger;
throw new Error(
`Cannot find tracking variable '${knownVariable}' for notebook snapshot in runtime variables ${JSON.stringify(
[...runtime._variables]
)}`
);
}
console.log(moduleName);

return Promise.all(
Array.from(runtime._variables)
.filter((v) => modules.get(v._module) === moduleName)
.map((v) => {
return promiseState(v._promise).then(([state, value]) => ({
...(v._name && { name: v._name }),
value,
state
}));
})
);
}
Insert cell
// Credit: Henning Koch
// https://makandracards.com/makandra/46681-javascript-how-to-query-the-state-of-a-promise
promiseState = (promise) =>
new Promise((resolve) => {
// Symbols and RegExps are never content-equal
var uniqueValue = window["Symbol"] ? Symbol("unique") : /unique/;

function notifyPendingOrResolved(value) {
if (value === uniqueValue) {
return resolve(["pending", undefined]);
} else {
return resolve(["fulfilled", value]);
}
}

function notifyRejected(reason) {
return resolve(["rejected", reason]);
}

var race = [promise, Promise.resolve(uniqueValue)];
Promise.race(race).then(notifyPendingOrResolved, notifyRejected);
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more