Skip to content

Commit 138849d

Browse files
authored
v6; more modular (#374)
* more modular * 6.0.0 * fix #328; document module.builtin * delete obsolete tests * serialize variable reevaluation * fix stale computation with no inputs * runtime.dispose tests * 6.0.0-rc.1 * italicize variable names * remove blank lines * shared undefined promise * 6.0.0
1 parent 7566247 commit 138849d

21 files changed

+474
-892
lines changed

Diff for: .eslintignore

-1
This file was deleted.

Diff for: .github/workflows/nodejs.yml

-24
This file was deleted.

Diff for: .github/workflows/publish.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Publish
2+
3+
on:
4+
workflow_dispatch: {}
5+
release:
6+
types: [published]
7+
8+
jobs:
9+
publish:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
packages: write
14+
steps:
15+
- uses: actions/checkout@v4
16+
- uses: actions/setup-node@v4
17+
with:
18+
node-version: 20
19+
cache: yarn
20+
- run: yarn --frozen-lockfile
21+
- run: yarn test
22+
- run: npm publish
23+
env:
24+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

Diff for: .github/workflows/test.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-node@v4
15+
with:
16+
node-version: 20
17+
cache: yarn
18+
- run: yarn --frozen-lockfile
19+
- run: yarn test

Diff for: .gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
*.sublime-workspace
21
.DS_Store
3-
.esm-cache/
42
dist/
53
node_modules
64
npm-debug.log

Diff for: CONTRIBUTING.md

+2-12
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,14 @@ The Observable runtime is open source and released under the [ISC License](https
22

33
## Development
44

5-
Install dependencies:
5+
We use Yarn 1.x (Classic). To install dependencies:
66

77
```
88
yarn
99
```
1010

11-
Run tests with Mocha:
11+
To run tests with Mocha:
1212

1313
```
1414
yarn test
1515
```
16-
17-
Build with Rollup:
18-
19-
```
20-
yarn prepublishOnly
21-
```
22-
23-
## For internal use
24-
25-
See our checklist for [publishing a new release](https://observablehq.com/@observablehq/publishing-new-open-source-releases).

Diff for: LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2018-2023 Observable, Inc.
1+
Copyright 2018-2024 Observable, Inc.
22

33
Permission to use, copy, modify, and/or distribute this software for any purpose
44
with or without fee is hereby granted, provided that the above copyright notice

Diff for: README.md

+40-105
Large diffs are not rendered by default.

Diff for: package.json

+3-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@observablehq/runtime",
3-
"version": "5.9.9",
3+
"version": "6.0.0",
44
"author": {
55
"name": "Observable, Inc.",
66
"url": "https://observablehq.com"
@@ -9,38 +9,25 @@
99
"type": "module",
1010
"main": "src/index.js",
1111
"module": "src/index.js",
12-
"jsdelivr": "dist/runtime.umd.js",
13-
"unpkg": "dist/runtime.umd.js",
1412
"exports": {
15-
"umd": "./dist/runtime.umd.js",
1613
"default": "./src/index.js"
1714
},
1815
"repository": {
1916
"type": "git",
2017
"url": "https://github.com/observablehq/runtime.git"
2118
},
2219
"files": [
23-
"dist/**/*.js",
2420
"src/**/*.js"
2521
],
2622
"scripts": {
27-
"test": "mocha 'test/**/*-test.js' && eslint src test",
28-
"prepublishOnly": "rm -rf dist && rollup -c",
29-
"postpublish": "git push && git push --tags"
23+
"test": "mocha 'test/**/*-test.js' && eslint src test"
3024
},
3125
"_moduleAliases": {
3226
"@observablehq/runtime": "./src/index.js"
3327
},
34-
"dependencies": {
35-
"@observablehq/inspector": "^5.0.0",
36-
"@observablehq/stdlib": "^5.0.0"
37-
},
3828
"devDependencies": {
39-
"@rollup/plugin-node-resolve": "^15.0.1",
4029
"eslint": "^8.27.0",
4130
"mocha": "^10.1.0",
42-
"module-alias": "^2.2.2",
43-
"rollup": "^3.2.5",
44-
"rollup-plugin-terser": "^7.0.2"
31+
"module-alias": "^2.2.2"
4532
}
4633
}

Diff for: rollup.config.js

-49
This file was deleted.

Diff for: runtime.sublime-project

-17
This file was deleted.

Diff for: src/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
export {Inspector} from "@observablehq/inspector";
2-
export {Library} from "@observablehq/stdlib";
31
export {RuntimeError} from "./errors.js";
42
export {Runtime} from "./runtime.js";

Diff for: src/runtime.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {Library, FileAttachments} from "@observablehq/stdlib";
21
import {RuntimeError} from "./errors.js";
32
import {generatorish} from "./generatorish.js";
43
import {Module, variable_variable, variable_invalidation, variable_visibility} from "./module.js";
@@ -9,7 +8,7 @@ const frame = typeof requestAnimationFrame === "function" ? requestAnimationFram
98
: typeof setImmediate === "function" ? setImmediate
109
: f => setTimeout(f, 0);
1110

12-
export function Runtime(builtins = new Library, global = window_global) {
11+
export function Runtime(builtins, global = window_global) {
1312
const builtin = this.module();
1413
Object.defineProperties(this, {
1514
_dirty: {value: new Set},
@@ -34,8 +33,7 @@ Object.defineProperties(Runtime.prototype, {
3433
_computeSoon: {value: runtime_computeSoon, writable: true, configurable: true},
3534
_computeNow: {value: runtime_computeNow, writable: true, configurable: true},
3635
dispose: {value: runtime_dispose, writable: true, configurable: true},
37-
module: {value: runtime_module, writable: true, configurable: true},
38-
fileAttachments: {value: FileAttachments, writable: true, configurable: true}
36+
module: {value: runtime_module, writable: true, configurable: true}
3937
});
4038

4139
function runtime_dispose() {
@@ -223,16 +221,22 @@ function variable_compute(variable) {
223221

224222
const value0 = variable._value;
225223
const version = ++variable._version;
224+
const inputs = variable._inputs;
225+
const definition = variable._definition;
226226

227227
// Lazily-constructed invalidation variable; only constructed if referenced as an input.
228228
let invalidation = null;
229229

230-
// If the variable doesn’t have any inputs, we can optimize slightly.
231-
const promise = variable._promise = (variable._inputs.length
232-
? Promise.all(variable._inputs.map(variable_value)).then(define)
233-
: new Promise(resolve => resolve(variable._definition.call(value0))))
230+
// Wait for the previous definition to compute before recomputing.
231+
const promise = variable._promise = variable._promise
232+
.then(init, init)
233+
.then(define)
234234
.then(generate);
235235

236+
function init() {
237+
return Promise.all(inputs.map(variable_value));
238+
}
239+
236240
// Compute the initial value of the variable.
237241
function define(inputs) {
238242
if (variable._version !== version) throw variable_stale;
@@ -256,7 +260,7 @@ function variable_compute(variable) {
256260
}
257261
}
258262

259-
return variable._definition.apply(value0, inputs);
263+
return definition.apply(value0, inputs);
260264
}
261265

262266
// If the value is a generator, then retrieve its first value, and dispose of

Diff for: src/variable.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const TYPE_IMPLICIT = 2; // created on reference
99
export const TYPE_DUPLICATE = 3; // created on duplicate definition
1010

1111
export const no_observer = Symbol("no-observer");
12+
export const no_value = Promise.resolve();
1213

1314
export function Variable(type, module, observer, options) {
1415
if (!observer) observer = no_observer;
@@ -23,7 +24,7 @@ export function Variable(type, module, observer, options) {
2324
_module: {value: module},
2425
_name: {value: null, writable: true},
2526
_outputs: {value: new Set, writable: true},
26-
_promise: {value: Promise.resolve(undefined), writable: true},
27+
_promise: {value: no_value, writable: true},
2728
_reachable: {value: observer !== no_observer, writable: true}, // Is this variable transitively visible?
2829
_rejector: {value: variable_rejector(this)},
2930
_shadow: {value: initShadow(module, options)},

Diff for: test/dispose.html

-21
This file was deleted.

Diff for: test/dom.html

-14
This file was deleted.

Diff for: test/hello-world.html

-16
This file was deleted.

Diff for: test/require.html

-13
This file was deleted.

0 commit comments

Comments
 (0)