@observablehq/notebook-kit 2.0.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/package.json +1 -1
- package/dist/src/javascript/imports/observable.js +9 -13
- package/dist/src/javascript/imports.d.ts +1 -0
- package/dist/src/javascript/imports.js +2 -2
- package/dist/src/javascript/transpile.js +4 -1
- package/dist/src/runtime/define.js +1 -1
- package/dist/src/runtime/display.d.ts +2 -2
- package/dist/src/runtime/display.js +7 -7
- package/dist/src/runtime/index.js +2 -2
- package/dist/src/runtime/inspect.d.ts +2 -2
- package/dist/src/runtime/inspect.js +4 -4
- package/dist/src/runtime/stdlib/index.d.ts +0 -2
- package/dist/src/runtime/stdlib/index.js +0 -2
- package/package.json +1 -1
- package/dist/src/runtime/stdlib/observer.d.ts +0 -16
- package/dist/src/runtime/stdlib/observer.js +0 -42
package/dist/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getImportedName } from "../imports.js";
|
|
1
|
+
import { getImportedName, getLocalName } from "../imports.js";
|
|
2
2
|
const CODE_DOLLAR = 36;
|
|
3
3
|
/** If specifier is an observable: protocol import, resolves it. */
|
|
4
4
|
export function resolveObservableImport(specifier) {
|
|
@@ -18,25 +18,21 @@ function getImportAttributeKey(node) {
|
|
|
18
18
|
}
|
|
19
19
|
/** Note: mutates inputs! */
|
|
20
20
|
export function renderObservableImport(source, node, inputs) {
|
|
21
|
-
if (!inputs.includes("
|
|
22
|
-
inputs.push("
|
|
23
|
-
if (!inputs.includes("__ojs_observer"))
|
|
24
|
-
inputs.push("__ojs_observer");
|
|
21
|
+
if (!inputs.includes("@variable"))
|
|
22
|
+
inputs.push("@variable");
|
|
25
23
|
return `(import(${JSON.stringify(source)}).then((_) => {
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const main = __ojs_runtime.module();${node.specifiers
|
|
24
|
+
const module = __variable._module._runtime.module(_.default);
|
|
25
|
+
const outputs = new Map(Array.from(__variable._outputs, (v) => [v._name, v]));${node.specifiers
|
|
29
26
|
.map((specifier) => {
|
|
30
27
|
if (specifier.type === "ImportNamespaceSpecifier")
|
|
31
28
|
throw new SyntaxError("observable namespace imports are not supported");
|
|
32
|
-
const iname = getImportedName(specifier);
|
|
33
|
-
const
|
|
29
|
+
const iname = dedollar(getImportedName(specifier));
|
|
30
|
+
const lname = getLocalName(specifier);
|
|
34
31
|
return `
|
|
35
|
-
|
|
36
|
-
main.variable(observers.${iname} = __ojs_observer()).import("${vname}", module);`;
|
|
32
|
+
outputs.get(${JSON.stringify(lname)})?.import(${JSON.stringify(iname)}${iname === lname ? "" : `, ${JSON.stringify(lname)}`}, module);`;
|
|
37
33
|
})
|
|
38
34
|
.join("")}
|
|
39
|
-
return
|
|
35
|
+
return {};
|
|
40
36
|
}))`;
|
|
41
37
|
}
|
|
42
38
|
/** Turns e.g. "viewof$foo" into "viewof foo", and "$$" into "$". */
|
|
@@ -19,5 +19,6 @@ export type RewriteImportOptions = {
|
|
|
19
19
|
export declare function rewriteImportExpressions(output: Sourcemap, body: Node, { resolveLocalImports }?: RewriteImportOptions): void;
|
|
20
20
|
/** Note: mutates inputs! */
|
|
21
21
|
export declare function rewriteImportDeclarations(output: Sourcemap, body: Node, inputs: string[], { resolveLocalImports }?: RewriteImportOptions): void;
|
|
22
|
+
export declare function getLocalName(node: NamedImportSpecifier): string;
|
|
22
23
|
export declare function getImportedName(node: NamedImportSpecifier): string;
|
|
23
24
|
export {};
|
|
@@ -98,7 +98,7 @@ export function rewriteImportDeclarations(output, body, inputs, { resolveLocalIm
|
|
|
98
98
|
: JSON.stringify(resolution), node, output.input));
|
|
99
99
|
}
|
|
100
100
|
if (declarations.length > 1) {
|
|
101
|
-
output.insertLeft(0, `const [${specifiers.join(", ")}] = await Promise.all([${imports.join(", ")}]);\n`);
|
|
101
|
+
output.insertLeft(0, `const [${specifiers.join(", ")}] = await Promise.all([${imports.join(", ")}]);\n`); // prettier-ignore
|
|
102
102
|
}
|
|
103
103
|
else if (declarations.length === 1) {
|
|
104
104
|
output.insertLeft(0, `const ${specifiers[0]} = await ${imports[0]};\n`);
|
|
@@ -117,7 +117,7 @@ function renderImport(source, node, input) {
|
|
|
117
117
|
})`
|
|
118
118
|
: ""}`;
|
|
119
119
|
}
|
|
120
|
-
function getLocalName(node) {
|
|
120
|
+
export function getLocalName(node) {
|
|
121
121
|
return node.local.name;
|
|
122
122
|
}
|
|
123
123
|
export function getImportedName(node) {
|
|
@@ -52,7 +52,7 @@ export function transpileJavaScript(input, options) {
|
|
|
52
52
|
rewriteFileExpressions(output, cell.body);
|
|
53
53
|
if (cell.expression)
|
|
54
54
|
output.insertLeft(0, `return (\n`);
|
|
55
|
-
output.insertLeft(0, `${async ? "async " : ""}(${inputs}) => {\n`);
|
|
55
|
+
output.insertLeft(0, `${async ? "async " : ""}(${inputs.map(deat)}) => {\n`);
|
|
56
56
|
if (outputs.length > 0)
|
|
57
57
|
output.insertRight(input.length, `\nreturn {${outputs}};`);
|
|
58
58
|
if (cell.expression)
|
|
@@ -65,3 +65,6 @@ export function transpileJavaScript(input, options) {
|
|
|
65
65
|
const secrets = new Set(cell.secrets.map((f) => f.argument));
|
|
66
66
|
return { body, inputs, outputs, autodisplay, files, databases, secrets };
|
|
67
67
|
}
|
|
68
|
+
function deat(input) {
|
|
69
|
+
return input.replace(/^@/, "__"); // @variable to __variable
|
|
70
|
+
}
|
|
@@ -7,9 +7,9 @@ export type DisplayState = {
|
|
|
7
7
|
/** for inspected values, any expanded paths; see getExpanded */
|
|
8
8
|
expanded: (number[][] | undefined)[];
|
|
9
9
|
};
|
|
10
|
-
export declare function display(state: DisplayState, value: unknown): void;
|
|
10
|
+
export declare function display(state: DisplayState, value: unknown, name?: string): void;
|
|
11
11
|
export declare function clear(state: DisplayState): void;
|
|
12
|
-
export declare function observe(state: DisplayState, { autodisplay, assets }: Definition): {
|
|
12
|
+
export declare function observe(state: DisplayState, { autodisplay, assets, output }: Definition): {
|
|
13
13
|
_error: boolean;
|
|
14
14
|
_node: HTMLDivElement;
|
|
15
15
|
pending(): void;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { inspect, inspectError, getExpanded } from "./inspect.js";
|
|
2
2
|
import { mapAssets } from "./stdlib/assets.js";
|
|
3
|
-
export function display(state, value) {
|
|
3
|
+
export function display(state, value, name) {
|
|
4
4
|
const { root, expanded } = state;
|
|
5
|
-
const node = isDisplayable(value, root) ? value : inspect(value, expanded[root.childNodes.length]); // prettier-ignore
|
|
5
|
+
const node = isDisplayable(value, root) ? value : inspect(value, expanded[root.childNodes.length], name); // prettier-ignore
|
|
6
6
|
displayNode(state, node);
|
|
7
7
|
}
|
|
8
8
|
function displayNode(state, node) {
|
|
@@ -16,8 +16,8 @@ function displayNode(state, node) {
|
|
|
16
16
|
state.root.appendChild(node);
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
function displayError(state, value) {
|
|
20
|
-
displayNode(state, inspectError(value));
|
|
19
|
+
function displayError(state, value, name) {
|
|
20
|
+
displayNode(state, inspectError(value, name));
|
|
21
21
|
}
|
|
22
22
|
// Note: Element.prototype is instanceof Node, but cannot be inserted! This
|
|
23
23
|
// excludes DocumentFragment since appending a fragment “dissolves” (mutates)
|
|
@@ -33,7 +33,7 @@ export function clear(state) {
|
|
|
33
33
|
while (state.root.lastChild)
|
|
34
34
|
state.root.lastChild.remove();
|
|
35
35
|
}
|
|
36
|
-
export function observe(state, { autodisplay, assets }) {
|
|
36
|
+
export function observe(state, { autodisplay, assets, output }) {
|
|
37
37
|
return {
|
|
38
38
|
_error: false,
|
|
39
39
|
_node: state.root, // _node for visibility promise
|
|
@@ -48,7 +48,7 @@ export function observe(state, { autodisplay, assets }) {
|
|
|
48
48
|
if (assets && value instanceof Element)
|
|
49
49
|
mapAssets(value, assets);
|
|
50
50
|
clear(state);
|
|
51
|
-
display(state, value);
|
|
51
|
+
display(state, value, output);
|
|
52
52
|
}
|
|
53
53
|
else if (state.autoclear) {
|
|
54
54
|
clear(state);
|
|
@@ -58,7 +58,7 @@ export function observe(state, { autodisplay, assets }) {
|
|
|
58
58
|
console.error(error);
|
|
59
59
|
this._error = true;
|
|
60
60
|
clear(state);
|
|
61
|
-
displayError(state, error);
|
|
61
|
+
displayError(state, error, output);
|
|
62
62
|
}
|
|
63
63
|
};
|
|
64
64
|
}
|
|
@@ -22,7 +22,7 @@ export class NotebookRuntime {
|
|
|
22
22
|
writable: true,
|
|
23
23
|
value: void 0
|
|
24
24
|
});
|
|
25
|
-
const runtime = new Runtime(
|
|
25
|
+
const runtime = new Runtime(builtins);
|
|
26
26
|
this.runtime = Object.assign(runtime, { fileAttachments });
|
|
27
27
|
this.main = runtime.module();
|
|
28
28
|
}
|
|
@@ -35,7 +35,7 @@ export const runtime = defaultNotebook.runtime;
|
|
|
35
35
|
export const main = defaultNotebook.main;
|
|
36
36
|
export const define = defaultNotebook.define.bind(defaultNotebook);
|
|
37
37
|
main.constructor.prototype.defines = function (name) {
|
|
38
|
-
return (this._scope.has(name) ||
|
|
38
|
+
return (this._scope.has(name) || // prettier
|
|
39
39
|
this._builtins.has(name) ||
|
|
40
40
|
this._runtime._builtin._scope.has(name));
|
|
41
41
|
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare function inspect(value: unknown, expanded?: number[][]): HTMLDivElement;
|
|
2
|
-
export declare function inspectError(value: unknown): HTMLDivElement;
|
|
1
|
+
export declare function inspect(value: unknown, expanded?: number[][], name?: string): HTMLDivElement;
|
|
2
|
+
export declare function inspectError(value: unknown, name?: string): HTMLDivElement;
|
|
3
3
|
export declare function getExpanded(node: Node): number[][] | undefined;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Inspector } from "@observablehq/inspector";
|
|
2
|
-
export function inspect(value, expanded) {
|
|
2
|
+
export function inspect(value, expanded, name) {
|
|
3
3
|
const node = document.createElement("div");
|
|
4
|
-
new Inspector(node).fulfilled(value);
|
|
4
|
+
new Inspector(node).fulfilled(value, name);
|
|
5
5
|
if (expanded) {
|
|
6
6
|
for (const path of expanded) {
|
|
7
7
|
let child = node;
|
|
@@ -12,9 +12,9 @@ export function inspect(value, expanded) {
|
|
|
12
12
|
}
|
|
13
13
|
return node;
|
|
14
14
|
}
|
|
15
|
-
export function inspectError(value) {
|
|
15
|
+
export function inspectError(value, name) {
|
|
16
16
|
const node = document.createElement("div");
|
|
17
|
-
new Inspector(node).rejected(value);
|
|
17
|
+
new Inspector(node).rejected(value, name);
|
|
18
18
|
return node;
|
|
19
19
|
}
|
|
20
20
|
export function getExpanded(node) {
|
|
@@ -5,7 +5,6 @@ import { FileAttachment } from "./fileAttachment.js";
|
|
|
5
5
|
import * as Generators from "./generators/index.js";
|
|
6
6
|
import { Interpreter } from "./interpreter.js";
|
|
7
7
|
import { Mutable } from "./mutable.js";
|
|
8
|
-
import { Observer } from "./observer.js";
|
|
9
8
|
import * as Promises from "./promises/index.js";
|
|
10
9
|
export declare const root: HTMLElement;
|
|
11
10
|
export declare const library: {
|
|
@@ -104,5 +103,4 @@ export declare const library: {
|
|
|
104
103
|
Promises: () => typeof Promises;
|
|
105
104
|
Files: () => typeof Files;
|
|
106
105
|
DOM: () => typeof DOM;
|
|
107
|
-
__ojs_observer: () => () => Observer;
|
|
108
106
|
};
|
|
@@ -5,7 +5,6 @@ import { FileAttachment } from "./fileAttachment.js";
|
|
|
5
5
|
import * as Generators from "./generators/index.js";
|
|
6
6
|
import { Interpreter } from "./interpreter.js";
|
|
7
7
|
import { Mutable } from "./mutable.js";
|
|
8
|
-
import { Observer } from "./observer.js";
|
|
9
8
|
import * as Promises from "./promises/index.js";
|
|
10
9
|
import * as recommendedLibraries from "./recommendedLibraries.js";
|
|
11
10
|
import * as sampleDatasets from "./sampleDatasets.js";
|
|
@@ -22,7 +21,6 @@ export const library = {
|
|
|
22
21
|
Promises: () => Promises, // deprecated!
|
|
23
22
|
Files: () => Files, // deprecated!
|
|
24
23
|
DOM: () => DOM, // deprecated!
|
|
25
|
-
__ojs_observer: () => () => new Observer(),
|
|
26
24
|
...recommendedLibraries,
|
|
27
25
|
...sampleDatasets
|
|
28
26
|
};
|
package/package.json
CHANGED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export declare class Observer {
|
|
2
|
-
#private;
|
|
3
|
-
fulfilled?: (value: unknown) => void;
|
|
4
|
-
rejected?: (error: unknown) => void;
|
|
5
|
-
constructor();
|
|
6
|
-
next(): Promise<{
|
|
7
|
-
done: boolean;
|
|
8
|
-
value: unknown;
|
|
9
|
-
}>;
|
|
10
|
-
throw(): {
|
|
11
|
-
done: boolean;
|
|
12
|
-
};
|
|
13
|
-
return(): {
|
|
14
|
-
done: boolean;
|
|
15
|
-
};
|
|
16
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
-
};
|
|
6
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
-
};
|
|
12
|
-
var _Observer_promise;
|
|
13
|
-
export class Observer {
|
|
14
|
-
constructor() {
|
|
15
|
-
_Observer_promise.set(this, void 0);
|
|
16
|
-
Object.defineProperty(this, "fulfilled", {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
configurable: true,
|
|
19
|
-
writable: true,
|
|
20
|
-
value: void 0
|
|
21
|
-
});
|
|
22
|
-
Object.defineProperty(this, "rejected", {
|
|
23
|
-
enumerable: true,
|
|
24
|
-
configurable: true,
|
|
25
|
-
writable: true,
|
|
26
|
-
value: void 0
|
|
27
|
-
});
|
|
28
|
-
this.next();
|
|
29
|
-
}
|
|
30
|
-
async next() {
|
|
31
|
-
const value = await __classPrivateFieldGet(this, _Observer_promise, "f");
|
|
32
|
-
__classPrivateFieldSet(this, _Observer_promise, new Promise((res, rej) => ((this.fulfilled = res), (this.rejected = rej))), "f");
|
|
33
|
-
return { done: false, value };
|
|
34
|
-
}
|
|
35
|
-
throw() {
|
|
36
|
-
return { done: true };
|
|
37
|
-
}
|
|
38
|
-
return() {
|
|
39
|
-
return { done: true };
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
_Observer_promise = new WeakMap();
|