Skip to content

Commit

Permalink
v6; more modular (#374)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mbostock authored Nov 6, 2024
1 parent 7566247 commit 138849d
Show file tree
Hide file tree
Showing 21 changed files with 474 additions and 892 deletions.
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",
"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)
.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
3 changes: 2 additions & 1 deletion src/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const TYPE_IMPLICIT = 2; // created on reference
export const TYPE_DUPLICATE = 3; // created on duplicate definition

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

export function Variable(type, module, observer, options) {
if (!observer) observer = no_observer;
Expand All @@ -23,7 +24,7 @@ export function Variable(type, module, observer, options) {
_module: {value: module},
_name: {value: null, writable: true},
_outputs: {value: new Set, writable: true},
_promise: {value: Promise.resolve(undefined), writable: true},
_promise: {value: no_value, writable: true},
_reachable: {value: observer !== no_observer, writable: true}, // Is this variable transitively visible?
_rejector: {value: variable_rejector(this)},
_shadow: {value: initShadow(module, options)},
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.

Loading

0 comments on commit 138849d

Please sign in to comment.