Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v6; more modular #374

Merged
merged 12 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

24 changes: 0 additions & 24 deletions .github/workflows/nodejs.yml

This file was deleted.

24 changes: 24 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Publish

on:
workflow_dispatch: {}
release:
types: [published]

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: yarn
- run: yarn --frozen-lockfile
- run: yarn test
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Test

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: yarn
- run: yarn --frozen-lockfile
- run: yarn test
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
*.sublime-workspace
.DS_Store
.esm-cache/
dist/
node_modules
npm-debug.log
14 changes: 2 additions & 12 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@ The Observable runtime is open source and released under the [ISC License](https

## Development

Install dependencies:
We use Yarn 1.x (Classic). To install dependencies:

```
yarn
```

Run tests with Mocha:
To run tests with Mocha:

```
yarn test
```

Build with Rollup:

```
yarn prepublishOnly
```

## For internal use

See our checklist for [publishing a new release](https://observablehq.com/@observablehq/publishing-new-open-source-releases).
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2018-2023 Observable, Inc.
Copyright 2018-2024 Observable, Inc.

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
Expand Down
145 changes: 40 additions & 105 deletions README.md

Large diffs are not rendered by default.

19 changes: 3 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@observablehq/runtime",
"version": "5.9.9",
"version": "6.0.0-rc.1",
"author": {
"name": "Observable, Inc.",
"url": "https://observablehq.com"
Expand All @@ -9,38 +9,25 @@
"type": "module",
"main": "src/index.js",
"module": "src/index.js",
"jsdelivr": "dist/runtime.umd.js",
"unpkg": "dist/runtime.umd.js",
"exports": {
"umd": "./dist/runtime.umd.js",
"default": "./src/index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/observablehq/runtime.git"
},
"files": [
"dist/**/*.js",
"src/**/*.js"
],
"scripts": {
"test": "mocha 'test/**/*-test.js' && eslint src test",
"prepublishOnly": "rm -rf dist && rollup -c",
"postpublish": "git push && git push --tags"
"test": "mocha 'test/**/*-test.js' && eslint src test"
},
"_moduleAliases": {
"@observablehq/runtime": "./src/index.js"
},
"dependencies": {
"@observablehq/inspector": "^5.0.0",
"@observablehq/stdlib": "^5.0.0"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.0.1",
"eslint": "^8.27.0",
"mocha": "^10.1.0",
"module-alias": "^2.2.2",
"rollup": "^3.2.5",
"rollup-plugin-terser": "^7.0.2"
"module-alias": "^2.2.2"
}
}
49 changes: 0 additions & 49 deletions rollup.config.js

This file was deleted.

17 changes: 0 additions & 17 deletions runtime.sublime-project

This file was deleted.

2 changes: 0 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export {Inspector} from "@observablehq/inspector";
export {Library} from "@observablehq/stdlib";
export {RuntimeError} from "./errors.js";
export {Runtime} from "./runtime.js";
22 changes: 13 additions & 9 deletions src/runtime.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {Library, FileAttachments} from "@observablehq/stdlib";
import {RuntimeError} from "./errors.js";
import {generatorish} from "./generatorish.js";
import {Module, variable_variable, variable_invalidation, variable_visibility} from "./module.js";
Expand All @@ -9,7 +8,7 @@ const frame = typeof requestAnimationFrame === "function" ? requestAnimationFram
: typeof setImmediate === "function" ? setImmediate
: f => setTimeout(f, 0);

export function Runtime(builtins = new Library, global = window_global) {
export function Runtime(builtins, global = window_global) {
const builtin = this.module();
Object.defineProperties(this, {
_dirty: {value: new Set},
Expand All @@ -34,8 +33,7 @@ Object.defineProperties(Runtime.prototype, {
_computeSoon: {value: runtime_computeSoon, writable: true, configurable: true},
_computeNow: {value: runtime_computeNow, writable: true, configurable: true},
dispose: {value: runtime_dispose, writable: true, configurable: true},
module: {value: runtime_module, writable: true, configurable: true},
fileAttachments: {value: FileAttachments, writable: true, configurable: true}
module: {value: runtime_module, writable: true, configurable: true}
});

function runtime_dispose() {
Expand Down Expand Up @@ -223,16 +221,22 @@ function variable_compute(variable) {

const value0 = variable._value;
const version = ++variable._version;
const inputs = variable._inputs;
const definition = variable._definition;

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

// If the variable doesn’t have any inputs, we can optimize slightly.
const promise = variable._promise = (variable._inputs.length
? Promise.all(variable._inputs.map(variable_value)).then(define)
: new Promise(resolve => resolve(variable._definition.call(value0))))
// Wait for the previous definition to compute before recomputing.
const promise = variable._promise = variable._promise
.then(init, init)
Copy link
Member Author

@mbostock mbostock Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change means that when a variable is redefined, it will wait for the old version of the variable to settle (fulfill or reject) before running. However, there are some important nuances here…

First, variables reject before computing if they are stale (if (variable._version !== version)). This means that if a variable is redefined multiple times, we won’t waste time computing already-invalidated definitions. Second, new variables are created when code is edited rather than redefining existing variables; see Framework’s define, and note its call to main.variable to define a new variable whenever code is edited. This means that new (edited) code will not wait for old code to settle before running. However, unchanged code that is downstream of the edited code will still wait for the old code to settle, which might be confusing.

We could fix the last point in Framework by redefining any downstream code as new variables if it proves to be confusing. As a workaround, you can reload the page if you don’t wait for old code to run.

Copy link
Contributor

@Fil Fil Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A delay is OK (as in the case of awaiting a now-useless SQL query), but it is definitely confusing when the unchanged code is not settling at all.

A typical example is when I make a mistake writing a cell as a Promise that never resolves; fixing the mistake in the code block does not fix the state of the page.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what do you suggest we do?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After playing a bit more with it, only the programmer error case mentioned above can be seen as a regression, ending up in a situation where you have to reload the page to resync the tree of variables with your code — whereas in main fixing the code would fix it.

As for suggestion, I wish I had an idea :) “Redefining any downstream code as new variables”, as you mentioned, maybe with an additional timeout(?).

But the additional complexity might not be worth it. And this is such a big win that if we have to choose I prefer this version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make it so that calls to variable.define cause the variable and any downstream variables not to wait, such that we’d only end up waiting in the case of generators yielding a new value. I can look to see how hard that will be.

.then(define)
.then(generate);

function init() {
return Promise.all(inputs.map(variable_value));
}

// Compute the initial value of the variable.
function define(inputs) {
if (variable._version !== version) throw variable_stale;
Expand All @@ -256,7 +260,7 @@ function variable_compute(variable) {
}
}

return variable._definition.apply(value0, inputs);
return definition.apply(value0, inputs);
}

// If the value is a generator, then retrieve its first value, and dispose of
Expand Down
21 changes: 0 additions & 21 deletions test/dispose.html

This file was deleted.

14 changes: 0 additions & 14 deletions test/dom.html

This file was deleted.

16 changes: 0 additions & 16 deletions test/hello-world.html

This file was deleted.

13 changes: 0 additions & 13 deletions test/require.html

This file was deleted.

35 changes: 35 additions & 0 deletions test/runtime/dispose-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {Runtime} from "@observablehq/runtime";
import assert from "assert";
import {sleep} from "../variable/valueof.js";

describe("runtime.dispose", () => {
it("invalidates all variables", async () => {
const runtime = new Runtime();
const main = runtime.module();
const log = [];
main.variable(true).define(["invalidation"], async (invalidation) => {
await invalidation;
log.push("invalidation");
});
await sleep();
runtime.dispose();
await sleep();
assert.deepStrictEqual(log, ["invalidation"]);
});
it("terminates generators", async () => {
const runtime = new Runtime();
const main = runtime.module();
const log = [];
main.variable(true).define([], function* () {
try {
while (true) yield;
} finally {
log.push("return");
}
});
await sleep();
runtime.dispose();
await sleep();
assert.deepStrictEqual(log, ["return"]);
});
});
Loading