Skip to content

Commit cc1fafe

Browse files
committed
vanilla stdlib
1 parent a889566 commit cc1fafe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+589
-214
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"@typescript-eslint/no-unused-vars": ["error", {"ignoreRestSiblings": true}],
2525
"import/first": "warn",
2626
"import/newline-after-import": "warn",
27+
"import/no-duplicates": "off",
2728
"import/no-import-module-exports": "warn",
28-
"import/no-namespace": "error",
2929
"import/no-relative-packages": "warn",
3030
"import/no-unresolved": "off",
3131
"import/no-unused-modules": "error",

docs/stdlib.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# stdlib
2+
3+
```js echo
4+
const div = Object.assign(document.createElement("div"), {style: "height: 180px;"});
5+
const map = L.map(display(div)).setView([51.505, -0.09], 13);
6+
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
7+
maxZoom: 19,
8+
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
9+
}).addTo(map);
10+
```
11+
12+
```js echo
13+
const i = view(Inputs.range([0, 1], {label: "Test"}));
14+
```
15+
16+
```js echo
17+
i
18+
```
19+
20+
```js echo
21+
display(tex`E = mc^2`);
22+
```
23+
24+
```js echo
25+
display(dot`digraph G { a -> b -> c; }`);
26+
```
27+
28+
```js echo
29+
display(Generators);
30+
```
31+
32+
```mermaid
33+
graph TD;
34+
A-->B;
35+
A-->C;
36+
B-->D;
37+
C-->D;
38+
```

src/client/dot.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/client/inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Inspector} from "./runtime.js";
1+
import {Inspector} from "observablehq:runtime";
22

33
export function inspect(value) {
44
const inspector = new Inspector(document.createElement("div"));

src/client/library.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/client/main.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
import {makeDatabaseClient} from "./database.js";
1+
import {Runtime} from "observablehq:runtime";
2+
import {registerDatabase, registerFile} from "observablehq:stdlib";
3+
import {DatabaseClient, FileAttachment, Generators, Mutable, now, width} from "observablehq:stdlib";
24
import {inspect, inspectError} from "./inspect.js";
3-
import {makeLibrary} from "./library.js";
4-
import {Runtime} from "./runtime.js";
5+
import * as recommendedLibraries from "./stdlib/recommendedLibraries.js";
6+
import * as sampleDatasets from "./stdlib/sampleDatasets.js";
57

6-
const library = makeLibrary();
7-
const {Generators} = library;
8-
library.DatabaseClient = () => makeDatabaseClient(resolveDatabaseToken);
8+
const library = {
9+
now,
10+
width,
11+
DatabaseClient: () => DatabaseClient,
12+
FileAttachment: () => FileAttachment,
13+
Generators: () => Generators,
14+
Mutable: () => Mutable,
15+
...recommendedLibraries,
16+
...sampleDatasets
17+
};
918

1019
const runtime = new Runtime(library);
1120
export const main = runtime.module();
1221

13-
const attachedFiles = new Map();
14-
const resolveFile = (name) => attachedFiles.get(name);
15-
main.builtin("FileAttachment", runtime.fileAttachments(resolveFile));
16-
17-
const databaseTokens = new Map();
18-
async function resolveDatabaseToken(name) {
19-
const token = databaseTokens.get(name);
20-
if (!token) throw new Error(`Database configuration for ${name} not found`);
21-
return token;
22-
}
23-
2422
export const cellsById = new Map(); // TODO hide
2523

2624
export function define(cell) {
@@ -59,8 +57,8 @@ export function define(cell) {
5957
v.define(outputs.length ? `cell ${id}` : null, inputs, body);
6058
variables.push(v);
6159
for (const o of outputs) variables.push(main.variable(true).define(o, [`cell ${id}`], (exports) => exports[o]));
62-
for (const f of files) attachedFiles.set(f.name, {url: f.path, mimeType: f.mimeType});
63-
for (const d of databases) databaseTokens.set(d.name, d);
60+
for (const f of files) registerFile(f.name, {url: f.path, mimeType: f.mimeType});
61+
for (const d of databases) registerDatabase(d.name, d);
6462
}
6563

6664
// Note: Element.prototype is instanceof Node, but cannot be inserted!

src/client/mermaid.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/client/mutable.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/client/database.js renamed to src/client/stdlib/databaseClient.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
export function makeDatabaseClient(resolveToken) {
2-
return function DatabaseClient(name) {
3-
if (new.target !== undefined) throw new TypeError("DatabaseClient is not a constructor");
4-
return resolveToken((name += "")).then((token) => new DatabaseClientImpl(name, token));
5-
};
1+
const databases = new Map();
2+
3+
export function registerDatabase(name, token) {
4+
if (token == null) databases.delete(name);
5+
else databases.set(name, token);
6+
}
7+
8+
export function DatabaseClient(name) {
9+
if (new.target !== undefined) throw new TypeError("DatabaseClient is not a constructor");
10+
const token = databases.get((name += ""));
11+
if (!token) throw new Error(`Database not found: ${name}`);
12+
return new DatabaseClientImpl(name, token);
613
}
714

8-
class DatabaseClientImpl {
15+
const DatabaseClientImpl = class DatabaseClient {
916
#token;
1017

1118
constructor(name, token) {
@@ -64,7 +71,9 @@ class DatabaseClientImpl {
6471
async sql() {
6572
return this.query(...this.queryTag.apply(this, arguments));
6673
}
67-
}
74+
};
75+
76+
DatabaseClient.prototype = DatabaseClientImpl.prototype; // instanceof
6877

6978
function coerceBuffer(d) {
7079
return Uint8Array.from(d.data).buffer;

src/client/stdlib/dot.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {instance} from "npm:@viz-js/viz";
2+
3+
const viz = await instance();
4+
5+
export default function dot(strings) {
6+
let string = strings[0] + "";
7+
let i = 0;
8+
let n = arguments.length;
9+
while (++i < n) string += arguments[i] + "" + strings[i];
10+
const svg = viz.renderSVGElement(string, {
11+
graphAttributes: {
12+
bgcolor: "none",
13+
color: "#00000101",
14+
fontcolor: "#00000101",
15+
fontname: "var(--sans-serif)",
16+
fontsize: "12"
17+
},
18+
nodeAttributes: {
19+
color: "#00000101",
20+
fontcolor: "#00000101",
21+
fontname: "var(--sans-serif)",
22+
fontsize: "12"
23+
},
24+
edgeAttributes: {
25+
color: "#00000101"
26+
}
27+
});
28+
for (const e of svg.querySelectorAll("[stroke='#000001'][stroke-opacity='0.003922']")) {
29+
e.setAttribute("stroke", "currentColor");
30+
e.removeAttribute("stroke-opacity");
31+
}
32+
for (const e of svg.querySelectorAll("[fill='#000001'][fill-opacity='0.003922']")) {
33+
e.setAttribute("fill", "currentColor");
34+
e.removeAttribute("fill-opacity");
35+
}
36+
svg.remove();
37+
svg.style = "max-width: 100%; height: auto;";
38+
return svg;
39+
}

0 commit comments

Comments
 (0)