diff --git a/package-lock.json b/package-lock.json index bcb08b118..f8a4e6f97 100644 --- a/package-lock.json +++ b/package-lock.json @@ -704,6 +704,56 @@ "@lit/reactive-element": "^1.1.0" } }, + "node_modules/@lit/context": { + "version": "1.0.0-pre.0", + "resolved": "https://registry.npmjs.org/@lit/context/-/context-1.0.0-pre.0.tgz", + "integrity": "sha512-svubKJsq9ZQFxl8Zv0GgCPTH8qImydQh5DHJLsQqCkWCcVuhUTe91POlczzr4VfXgCtfMr/2/apmUT9grdqhXA==", + "dependencies": { + "@lit/reactive-element": "^2.0.0-pre.1", + "lit": "^3.0.0-pre.1" + } + }, + "node_modules/@lit/context/node_modules/@lit-labs/ssr-dom-shim": { + "version": "1.1.2-pre.1", + "resolved": "https://registry.npmjs.org/@lit-labs/ssr-dom-shim/-/ssr-dom-shim-1.1.2-pre.1.tgz", + "integrity": "sha512-cRy/vafxn20Upocct6LeVzMatmnR0qHvX+7U00jPK0t9sxNaiAmpZyjHSUhLtUnG4jpuJW9UBIhVgYfZcgMo8A==" + }, + "node_modules/@lit/context/node_modules/@lit/reactive-element": { + "version": "2.0.0-pre.1", + "resolved": "https://registry.npmjs.org/@lit/reactive-element/-/reactive-element-2.0.0-pre.1.tgz", + "integrity": "sha512-oKzaKLYHbXeeqfuDk8uS660Y3aH/LpMoctBwo5u2cS2vMwvurFX6raavNY/xsr4WsatNVn4sbA2VVx50hIAdTw==", + "dependencies": { + "@lit-labs/ssr-dom-shim": "^1.1.2-pre.0" + } + }, + "node_modules/@lit/context/node_modules/lit": { + "version": "3.0.0-pre.1", + "resolved": "https://registry.npmjs.org/lit/-/lit-3.0.0-pre.1.tgz", + "integrity": "sha512-HG5serwzvQuJL/G27Knpe7Hy/jFEJo8N2CaZe0pqipD26icgM5aeInWWaoktsKzE/XUc8/l/M5dghFDBhlEWWg==", + "dependencies": { + "@lit/reactive-element": "^2.0.0-pre.1", + "lit-element": "^4.0.0-pre.1", + "lit-html": "^3.0.0-pre.1" + } + }, + "node_modules/@lit/context/node_modules/lit-element": { + "version": "4.0.0-pre.1", + "resolved": "https://registry.npmjs.org/lit-element/-/lit-element-4.0.0-pre.1.tgz", + "integrity": "sha512-g6z4W3HRponlFEYUVeJXo4KcnxFj1lWTLhFDAkRxmcUq/7xYkvd955dqkwQ8x1XBVgtCA4m2mOYB3RrwfTlVDw==", + "dependencies": { + "@lit-labs/ssr-dom-shim": "^1.1.2-pre.0", + "@lit/reactive-element": "^2.0.0-pre.1", + "lit-html": "^3.0.0-pre.1" + } + }, + "node_modules/@lit/context/node_modules/lit-html": { + "version": "3.0.0-pre.1", + "resolved": "https://registry.npmjs.org/lit-html/-/lit-html-3.0.0-pre.1.tgz", + "integrity": "sha512-CzDS/69qdDGA/d5DZx8lvcH3uHS8fYnwyGnSo6z7ztKbHYCpHmyGJRfWh/pMXk4ixvkvHUGuRuGgEA8OnxKHJw==", + "dependencies": { + "@types/trusted-types": "^2.0.2" + } + }, "node_modules/@lit/localize": { "version": "0.10.4", "resolved": "https://registry.npmjs.org/@lit/localize/-/localize-0.10.4.tgz", @@ -713,6 +763,14 @@ "lit": "^2.0.0" } }, + "node_modules/@lit/react": { + "version": "1.0.0-pre.0", + "resolved": "https://registry.npmjs.org/@lit/react/-/react-1.0.0-pre.0.tgz", + "integrity": "sha512-okNFKmN333NcVe5ofpJrVE+9KKPnt2zDZaarQ3rnnw/20sCcrXIcAqtlUGmfjIzFzJOqu9byV8oqI54+p/I8Rg==", + "peerDependencies": { + "@types/react": "17 || 18" + } + }, "node_modules/@lit/reactive-element": { "version": "1.6.1", "resolved": "https://registry.npmjs.org/@lit/reactive-element/-/reactive-element-1.6.1.tgz", @@ -721,6 +779,27 @@ "@lit-labs/ssr-dom-shim": "^1.0.0" } }, + "node_modules/@lit/task": { + "version": "1.0.0-pre.0", + "resolved": "https://registry.npmjs.org/@lit/task/-/task-1.0.0-pre.0.tgz", + "integrity": "sha512-9O+XywEqW7LA8Clw5yF/oFG5rTJrjpF7cm5ti3k6maemDUPo2HV7yh0Co7rNEPPSRqucEDG5s9OHmxPBFxVu9g==", + "dependencies": { + "@lit/reactive-element": "^2.0.0-pre.1" + } + }, + "node_modules/@lit/task/node_modules/@lit-labs/ssr-dom-shim": { + "version": "1.1.2-pre.1", + "resolved": "https://registry.npmjs.org/@lit-labs/ssr-dom-shim/-/ssr-dom-shim-1.1.2-pre.1.tgz", + "integrity": "sha512-cRy/vafxn20Upocct6LeVzMatmnR0qHvX+7U00jPK0t9sxNaiAmpZyjHSUhLtUnG4jpuJW9UBIhVgYfZcgMo8A==" + }, + "node_modules/@lit/task/node_modules/@lit/reactive-element": { + "version": "2.0.0-pre.1", + "resolved": "https://registry.npmjs.org/@lit/reactive-element/-/reactive-element-2.0.0-pre.1.tgz", + "integrity": "sha512-oKzaKLYHbXeeqfuDk8uS660Y3aH/LpMoctBwo5u2cS2vMwvurFX6raavNY/xsr4WsatNVn4sbA2VVx50hIAdTw==", + "dependencies": { + "@lit-labs/ssr-dom-shim": "^1.1.2-pre.0" + } + }, "node_modules/@lit/ts-transformers": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@lit/ts-transformers/-/ts-transformers-1.1.1.tgz", @@ -2146,8 +2225,7 @@ "node_modules/@types/prop-types": { "version": "15.7.5", "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.5.tgz", - "integrity": "sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==", - "dev": true + "integrity": "sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==" }, "node_modules/@types/qs": { "version": "6.9.7", @@ -2163,7 +2241,6 @@ "version": "18.2.6", "resolved": "https://registry.npmjs.org/@types/react/-/react-18.2.6.tgz", "integrity": "sha512-wRZClXn//zxCFW+ye/D2qY65UsYP1Fpex2YXorHc8awoNamkMZSvBxwxdYVInsHOZZd2Ppq8isnSzJL5Mpf8OA==", - "dev": true, "dependencies": { "@types/prop-types": "*", "@types/scheduler": "*", @@ -2196,8 +2273,7 @@ "node_modules/@types/scheduler": { "version": "0.16.2", "resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.2.tgz", - "integrity": "sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew==", - "dev": true + "integrity": "sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew==" }, "node_modules/@types/serve-static": { "version": "1.13.10", @@ -3574,8 +3650,7 @@ "node_modules/csstype": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.0.tgz", - "integrity": "sha512-uX1KG+x9h5hIJsaKR9xHUeUraxf8IODOwq9JLNPq6BwB04a/xgpq3rcx47l5BZu5zBPlgD342tdke3Hom/nJRA==", - "dev": true + "integrity": "sha512-uX1KG+x9h5hIJsaKR9xHUeUraxf8IODOwq9JLNPq6BwB04a/xgpq3rcx47l5BZu5zBPlgD342tdke3Hom/nJRA==" }, "node_modules/data-uri-to-buffer": { "version": "4.0.0", @@ -9154,7 +9229,10 @@ "@lit-labs/motion": "^1.0.1", "@lit-labs/react": "^1.0.8", "@lit-labs/task": "^3.0.2", + "@lit/context": "^1.0.0 || 1.0.0-pre.0", "@lit/localize": "^0.10.0", + "@lit/react": "^1.0.0 || 1.0.0-pre.0", + "@lit/task": "^1.0.0 || 1.0.0-pre.0", "@material/mwc-button": "^0.27.0", "@material/mwc-checkbox": "^0.27.0", "@material/mwc-dialog": "^0.27.0", @@ -9913,6 +9991,58 @@ "@lit/reactive-element": "^1.1.0" } }, + "@lit/context": { + "version": "1.0.0-pre.0", + "resolved": "https://registry.npmjs.org/@lit/context/-/context-1.0.0-pre.0.tgz", + "integrity": "sha512-svubKJsq9ZQFxl8Zv0GgCPTH8qImydQh5DHJLsQqCkWCcVuhUTe91POlczzr4VfXgCtfMr/2/apmUT9grdqhXA==", + "requires": { + "@lit/reactive-element": "^2.0.0-pre.1", + "lit": "^3.0.0-pre.1" + }, + "dependencies": { + "@lit-labs/ssr-dom-shim": { + "version": "1.1.2-pre.1", + "resolved": "https://registry.npmjs.org/@lit-labs/ssr-dom-shim/-/ssr-dom-shim-1.1.2-pre.1.tgz", + "integrity": "sha512-cRy/vafxn20Upocct6LeVzMatmnR0qHvX+7U00jPK0t9sxNaiAmpZyjHSUhLtUnG4jpuJW9UBIhVgYfZcgMo8A==" + }, + "@lit/reactive-element": { + "version": "2.0.0-pre.1", + "resolved": "https://registry.npmjs.org/@lit/reactive-element/-/reactive-element-2.0.0-pre.1.tgz", + "integrity": "sha512-oKzaKLYHbXeeqfuDk8uS660Y3aH/LpMoctBwo5u2cS2vMwvurFX6raavNY/xsr4WsatNVn4sbA2VVx50hIAdTw==", + "requires": { + "@lit-labs/ssr-dom-shim": "^1.1.2-pre.0" + } + }, + "lit": { + "version": "3.0.0-pre.1", + "resolved": "https://registry.npmjs.org/lit/-/lit-3.0.0-pre.1.tgz", + "integrity": "sha512-HG5serwzvQuJL/G27Knpe7Hy/jFEJo8N2CaZe0pqipD26icgM5aeInWWaoktsKzE/XUc8/l/M5dghFDBhlEWWg==", + "requires": { + "@lit/reactive-element": "^2.0.0-pre.1", + "lit-element": "^4.0.0-pre.1", + "lit-html": "^3.0.0-pre.1" + } + }, + "lit-element": { + "version": "4.0.0-pre.1", + "resolved": "https://registry.npmjs.org/lit-element/-/lit-element-4.0.0-pre.1.tgz", + "integrity": "sha512-g6z4W3HRponlFEYUVeJXo4KcnxFj1lWTLhFDAkRxmcUq/7xYkvd955dqkwQ8x1XBVgtCA4m2mOYB3RrwfTlVDw==", + "requires": { + "@lit-labs/ssr-dom-shim": "^1.1.2-pre.0", + "@lit/reactive-element": "^2.0.0-pre.1", + "lit-html": "^3.0.0-pre.1" + } + }, + "lit-html": { + "version": "3.0.0-pre.1", + "resolved": "https://registry.npmjs.org/lit-html/-/lit-html-3.0.0-pre.1.tgz", + "integrity": "sha512-CzDS/69qdDGA/d5DZx8lvcH3uHS8fYnwyGnSo6z7ztKbHYCpHmyGJRfWh/pMXk4ixvkvHUGuRuGgEA8OnxKHJw==", + "requires": { + "@types/trusted-types": "^2.0.2" + } + } + } + }, "@lit/localize": { "version": "0.10.4", "resolved": "https://registry.npmjs.org/@lit/localize/-/localize-0.10.4.tgz", @@ -9922,6 +10052,12 @@ "lit": "^2.0.0" } }, + "@lit/react": { + "version": "1.0.0-pre.0", + "resolved": "https://registry.npmjs.org/@lit/react/-/react-1.0.0-pre.0.tgz", + "integrity": "sha512-okNFKmN333NcVe5ofpJrVE+9KKPnt2zDZaarQ3rnnw/20sCcrXIcAqtlUGmfjIzFzJOqu9byV8oqI54+p/I8Rg==", + "requires": {} + }, "@lit/reactive-element": { "version": "1.6.1", "resolved": "https://registry.npmjs.org/@lit/reactive-element/-/reactive-element-1.6.1.tgz", @@ -9930,6 +10066,29 @@ "@lit-labs/ssr-dom-shim": "^1.0.0" } }, + "@lit/task": { + "version": "1.0.0-pre.0", + "resolved": "https://registry.npmjs.org/@lit/task/-/task-1.0.0-pre.0.tgz", + "integrity": "sha512-9O+XywEqW7LA8Clw5yF/oFG5rTJrjpF7cm5ti3k6maemDUPo2HV7yh0Co7rNEPPSRqucEDG5s9OHmxPBFxVu9g==", + "requires": { + "@lit/reactive-element": "^2.0.0-pre.1" + }, + "dependencies": { + "@lit-labs/ssr-dom-shim": { + "version": "1.1.2-pre.1", + "resolved": "https://registry.npmjs.org/@lit-labs/ssr-dom-shim/-/ssr-dom-shim-1.1.2-pre.1.tgz", + "integrity": "sha512-cRy/vafxn20Upocct6LeVzMatmnR0qHvX+7U00jPK0t9sxNaiAmpZyjHSUhLtUnG4jpuJW9UBIhVgYfZcgMo8A==" + }, + "@lit/reactive-element": { + "version": "2.0.0-pre.1", + "resolved": "https://registry.npmjs.org/@lit/reactive-element/-/reactive-element-2.0.0-pre.1.tgz", + "integrity": "sha512-oKzaKLYHbXeeqfuDk8uS660Y3aH/LpMoctBwo5u2cS2vMwvurFX6raavNY/xsr4WsatNVn4sbA2VVx50hIAdTw==", + "requires": { + "@lit-labs/ssr-dom-shim": "^1.1.2-pre.0" + } + } + } + }, "@lit/ts-transformers": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@lit/ts-transformers/-/ts-transformers-1.1.1.tgz", @@ -11282,8 +11441,7 @@ "@types/prop-types": { "version": "15.7.5", "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.5.tgz", - "integrity": "sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==", - "dev": true + "integrity": "sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==" }, "@types/qs": { "version": "6.9.7", @@ -11299,7 +11457,6 @@ "version": "18.2.6", "resolved": "https://registry.npmjs.org/@types/react/-/react-18.2.6.tgz", "integrity": "sha512-wRZClXn//zxCFW+ye/D2qY65UsYP1Fpex2YXorHc8awoNamkMZSvBxwxdYVInsHOZZd2Ppq8isnSzJL5Mpf8OA==", - "dev": true, "requires": { "@types/prop-types": "*", "@types/scheduler": "*", @@ -11332,8 +11489,7 @@ "@types/scheduler": { "version": "0.16.2", "resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.2.tgz", - "integrity": "sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew==", - "dev": true + "integrity": "sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew==" }, "@types/serve-static": { "version": "1.13.10", @@ -12434,8 +12590,7 @@ "csstype": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.0.tgz", - "integrity": "sha512-uX1KG+x9h5hIJsaKR9xHUeUraxf8IODOwq9JLNPq6BwB04a/xgpq3rcx47l5BZu5zBPlgD342tdke3Hom/nJRA==", - "dev": true + "integrity": "sha512-uX1KG+x9h5hIJsaKR9xHUeUraxf8IODOwq9JLNPq6BwB04a/xgpq3rcx47l5BZu5zBPlgD342tdke3Hom/nJRA==" }, "data-uri-to-buffer": { "version": "4.0.0", @@ -14386,7 +14541,10 @@ "@lit-labs/ssr": "^3.1.2", "@lit-labs/ssr-client": "^1.1.1", "@lit-labs/task": "^3.0.2", + "@lit/context": "^1.0.0 || 1.0.0-pre.0", "@lit/localize": "^0.10.0", + "@lit/react": "^1.0.0 || 1.0.0-pre.0", + "@lit/task": "^1.0.0 || 1.0.0-pre.0", "@material/mwc-button": "^0.27.0", "@material/mwc-checkbox": "^0.27.0", "@material/mwc-dialog": "^0.27.0", diff --git a/packages/lit-dev-content/package.json b/packages/lit-dev-content/package.json index 9844da21e..433f5c5eb 100644 --- a/packages/lit-dev-content/package.json +++ b/packages/lit-dev-content/package.json @@ -199,6 +199,9 @@ "@lit-labs/react": "^1.0.8", "@lit-labs/task": "^3.0.2", "@lit/localize": "^0.10.0", + "@lit/context": "^1.0.0 || 1.0.0-pre.0", + "@lit/react": "^1.0.0 || 1.0.0-pre.0", + "@lit/task": "^1.0.0 || 1.0.0-pre.0", "@material/mwc-button": "^0.27.0", "@material/mwc-checkbox": "^0.27.0", "@material/mwc-dialog": "^0.27.0", diff --git a/packages/lit-dev-content/samples/examples/async-task/npm-info.ts b/packages/lit-dev-content/samples/examples/async-task/npm-info.ts index b4a71353f..5175f0b5f 100644 --- a/packages/lit-dev-content/samples/examples/async-task/npm-info.ts +++ b/packages/lit-dev-content/samples/examples/async-task/npm-info.ts @@ -1,31 +1,39 @@ import {LitElement, css, html} from 'lit'; import {customElement, state} from 'lit/decorators.js'; -import {Task} from '@lit-labs/task'; +import {map} from 'lit/directives/map.js'; +import {Task, initialState} from '@lit/task'; import {fetchPackageInfo} from './npm.js'; @customElement('npm-info') export class NpmInfo extends LitElement { @state() - private _package = 'lit'; + private _packageName = 'lit'; - private _fetchNpmInfoTask = new Task(this, { + /* + * This is the async Task that fetches data from npm. + * + * The current state of the task is rendered below with + * `this._npmInfoTask.render()`. + */ + private _npmInfoTask = new Task(this, { task: async ([pkgName], {signal}) => { if (pkgName === undefined || pkgName === '') { - throw new Error('Empty package name'); + // This puts the task back into the INITIAL state + return initialState; } return await fetchPackageInfo(pkgName, signal); }, - args: () => [this._package], + args: () => [this._packageName], }); render() { return html`
-

${this._package}

+

${this._packageName}

- ${this._fetchNpmInfoTask.render({ - pending: () => 'Loading...', + ${this._npmInfoTask.render({ + initial: () => + html` + Enter a package name to display its npm info + `, + pending: () => + html`Loading npm info for ${this._packageName}`, complete: (pkg) => html`

${pkg.description}

dist-tags:

`, - error: (e) => `Error: ${(e as Error).message}`, + error: (e) => html` + Error: ${(e as Error).message} + `, })}
`; } private _onChange(e: Event) { - this._package = (e.target as HTMLInputElement).value; + this._packageName = (e.target as HTMLInputElement).value; } static styles = css` @@ -72,6 +88,11 @@ export class NpmInfo extends LitElement { height: 38px; width: auto; } + .initial { + font-style: italic; + } + .error { + color: red; + } `; } - diff --git a/packages/lit-dev-content/samples/examples/async-task/npm.ts b/packages/lit-dev-content/samples/examples/async-task/npm.ts index e0eedbb81..dc8311a73 100644 --- a/packages/lit-dev-content/samples/examples/async-task/npm.ts +++ b/packages/lit-dev-content/samples/examples/async-task/npm.ts @@ -7,7 +7,7 @@ export const fetchPackageInfo = async ( pkgName: string, signal: AbortSignal ): Promise => { - // Artifical delay for demo purposes + // Artificial delay for demo purposes await new Promise((r) => setTimeout(r, 1000)); const response = await fetch(`https://registry.npmjs.org/${pkgName}`, { signal, diff --git a/packages/lit-dev-content/samples/examples/async-task/project.json b/packages/lit-dev-content/samples/examples/async-task/project.json index 176084d49..32f544374 100644 --- a/packages/lit-dev-content/samples/examples/async-task/project.json +++ b/packages/lit-dev-content/samples/examples/async-task/project.json @@ -1,6 +1,6 @@ { "extends": "/samples/base.json", - "title": "Async Task / NPM Info element", + "title": "Async Tasks", "description": "Shows an example of an async Task controller. This one fetches npm package info and renders based on the state of the fetch.", "section": "Managing Data", "files": { diff --git a/packages/lit-dev-content/samples/examples/async-task/task.js b/packages/lit-dev-content/samples/examples/async-task/task.js deleted file mode 100644 index 63fcd4ac9..000000000 --- a/packages/lit-dev-content/samples/examples/async-task/task.js +++ /dev/null @@ -1,138 +0,0 @@ -import { UpdatingElement } from 'lit-element'; - -/** - * Computes a value asynchronously and caches it, making it avaiable - * synchronously. - * - * CachedTask notifies an UpdatingElement implementation when its value has - * changed. - */ -export class Task { - _host; - _compute; - _getDeps; - _deps; - - _state; // undefined | 'pending' | 'complete' | 'error' - _value; - _error; - - constructor(host, compute, getDeps) { - this._host = host; - this._compute = compute; - this._getDeps = getDeps; - } - - /** - * Calls one of the given render functions based on the state of the Task - * - * Example - * - * task.render({ - * complete: (v) => html`value: ${v}`, - * error: (e) => html`error: ${e}`, - * pending: (v) => html`Loading...`, - * initial: (v) => html`not started`, - * }) - */ - render(renderFunctions) { - this._maybeCompute(); - if (this._state === 'complete') { - return renderFunctions.complete && renderFunctions.complete(this._value); - } - if (this._state === 'error') { - return renderFunctions.error && renderFunctions.error(this._error); - } - if (this._state === 'pending') { - return renderFunctions.pending && renderFunctions.pending(); - } - return renderFunctions.initial && renderFunctions.initial(); - } - - /** - * Gets the current value of the task, or undefined if the task is computing. - * - * Triggers computation of the task if it hasn't been computed before, or if - * the dependencies have changed. - */ - get value() { - this._maybeCompute(); - return this._state = 'complete' ? this._value : undefined; - } - - _maybeCompute() { - // By default, compute the first time - let doCompute = this._state === undefined; - - // If a getDeps function was provided, call it and check if the dependencies - // have changed. If so, recompute. - if (this._getDeps !== undefined) { - const oldDeps = this._deps; - const newDeps = this._getDeps(); - this._deps = newDeps; - - // Check that we have new or old deps - if (oldDeps !== undefined || newDeps !== undefined) { - // If new or old deps are missing, or the array length is different - // reomcpute - if (oldDeps === undefined || newDeps === undefined || - oldDeps.length !== newDeps.length) { - doCompute = true; - } else { - // Check each dependency using strict equality - for (let i = 0; i < newDeps.length; i++) { - if (oldDeps[i] !== newDeps[i]) { - doCompute = true; - break; - } - } - } - } - } - if (doCompute) { - this._computeValue(); - } - } - - async _computeValue() { - // Make sure we're not returning stale values while computing. - // TODO: are there times when we _do_ want to return the stale value? - // Should that be an option? - this._state = 'pending'; - this._value = undefined; - this._error = undefined; - - // Fire an event so ancestor elements know there's pending work - let resolve; - let reject; - const pendingPromise = new Promise((res, rej) => { - resolve = res; - reject = rej; - }) - // https://github.com/justinfagnani/pending-state-protocol - const pendingEvent = new CustomEvent('pending-state', { - composed: true, - bubbles: true, - detail: {promise: pendingPromise} - }); - this._host.dispatchEvent(pendingEvent); - - try { - this._value = await this._compute(...(this._deps || [])); - this._state = 'complete'; - resolve(this._value); - } catch (e) { - if (e instanceof InitialStateError) { - this._state = 'initial'; - resolve(); - } - this._error = e; - this._state = 'error'; - reject(e); - } - // Make the host re-render - this._host.requestUpdate(); - } -} - -export class InitialStateError extends Error {} diff --git a/packages/lit-dev-content/samples/examples/context-basics/elements.ts b/packages/lit-dev-content/samples/examples/context-basics/elements.ts index b11b66777..b188f5b92 100644 --- a/packages/lit-dev-content/samples/examples/context-basics/elements.ts +++ b/packages/lit-dev-content/samples/examples/context-basics/elements.ts @@ -1,6 +1,6 @@ import {html, LitElement} from 'lit'; import {customElement, property} from 'lit/decorators.js'; -import {provide, consume, createContext} from '@lit-labs/context'; +import {provide, consume, createContext} from '@lit/context'; import {providerStyles} from './styles.js'; diff --git a/packages/lit-dev-content/samples/examples/context-basics/project.json b/packages/lit-dev-content/samples/examples/context-basics/project.json index 539f87982..3ac2f3c54 100644 --- a/packages/lit-dev-content/samples/examples/context-basics/project.json +++ b/packages/lit-dev-content/samples/examples/context-basics/project.json @@ -7,9 +7,5 @@ "elements.ts": {}, "index.html": {}, "styles.ts": {} - }, - "package.json": { - "content": "{\n \"dependencies\": {\n \"lit\": \"^2\",\n \"@lit-labs/context\": \"^0.4\"\n}\n}", - "hidden": true } } diff --git a/packages/lit-dev-content/samples/tsconfig.json b/packages/lit-dev-content/samples/tsconfig.json index c57ff56c5..00834d3c9 100644 --- a/packages/lit-dev-content/samples/tsconfig.json +++ b/packages/lit-dev-content/samples/tsconfig.json @@ -11,6 +11,7 @@ "noUnusedLocals": false, "noUnusedParameters": false, "jsx": "preserve", + "skipLibCheck": true }, "include": ["./**/*.ts", "./**/*.tsx"], "exclude": [