-
Notifications
You must be signed in to change notification settings - Fork 185
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
[examples] Add async task playground example #1179
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
packages/lit-dev-content/samples/examples/async-task/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
<!doctype html> | ||
<head> | ||
<script type="module" src="./index.js"></script> | ||
</head> | ||
<style> | ||
body { font-family: sans-serif; } | ||
</style> | ||
|
||
<script type="module" src="./npm-info.js"></script> | ||
|
||
<body> | ||
<demo-element></demo-element> | ||
<npm-info></npm-info> | ||
</body> |
27 changes: 0 additions & 27 deletions
27
packages/lit-dev-content/samples/examples/async-task/index.js
This file was deleted.
Oops, something went wrong.
87 changes: 0 additions & 87 deletions
87
packages/lit-dev-content/samples/examples/async-task/npm-info.js
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
packages/lit-dev-content/samples/examples/async-task/npm-info.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {LitElement, css, html} from 'lit'; | ||
import {customElement, state} from 'lit/decorators.js'; | ||
import {Task} from '@lit-labs/task'; | ||
import {fetchPackageInfo} from './npm.js'; | ||
|
||
@customElement('npm-info') | ||
export class NpmInfo extends LitElement { | ||
@state() | ||
private _package = 'lit'; | ||
|
||
private _fetchNpmInfoTask = new Task(this, { | ||
task: async ([pkgName], {signal}) => { | ||
if (pkgName === undefined || pkgName === '') { | ||
throw new Error('Empty package name'); | ||
} | ||
return await fetchPackageInfo(pkgName, signal); | ||
}, | ||
args: () => [this._package], | ||
}); | ||
|
||
render() { | ||
return html` | ||
<label> | ||
augustjk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Enter a package name: | ||
<input .value=${this._package} @change=${this._onChange} /> | ||
</label> | ||
<header> | ||
<h1>${this._package}</h1> | ||
<img | ||
id="logo" | ||
src="https://raw.githubusercontent.com/npm/logos/master/npm%20logo/npm-logo-red.svg" | ||
alt="npm logo" | ||
/> | ||
</header> | ||
<div> | ||
${this._fetchNpmInfoTask.render({ | ||
pending: () => 'Loading...', | ||
complete: (pkg) => html` | ||
<h3>${pkg.description}</h3> | ||
<h4>dist-tags:</h4> | ||
<ul> | ||
${Array.from(Object.entries(pkg['dist-tags'])).map( | ||
([tag, version]) => html`<li><pre>${tag}: ${version}</pre></li>` | ||
)} | ||
</ul> | ||
`, | ||
error: (e) => `Error: ${(e as Error).message}`, | ||
})} | ||
</div> | ||
`; | ||
} | ||
|
||
private _onChange(e: Event) { | ||
this._package = (e.target as HTMLInputElement).value; | ||
} | ||
|
||
static styles = css` | ||
:host { | ||
display: block; | ||
min-width: 300px; | ||
border-radius: 5px; | ||
border: solid 1px #aaa; | ||
padding: 20px; | ||
} | ||
header { | ||
display: flex; | ||
flex-direction: row; | ||
align-items: center; | ||
justify-content: space-between; | ||
} | ||
#logo { | ||
height: 38px; | ||
width: auto; | ||
} | ||
`; | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
packages/lit-dev-content/samples/examples/async-task/npm.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export interface NpmPackage { | ||
description: string; | ||
['dist-tags']: {[tag: string]: string}; | ||
} | ||
|
||
export const fetchPackageInfo = async ( | ||
pkgName: string, | ||
signal: AbortSignal | ||
): Promise<NpmPackage> => { | ||
// Artifical delay for demo purposes | ||
await new Promise((r) => setTimeout(r, 1000)); | ||
const response = await fetch(`https://registry.npmjs.org/${pkgName}`, { | ||
signal, | ||
}); | ||
if (response.status === 200) { | ||
return response.json(); | ||
} else { | ||
throw response.text(); | ||
} | ||
}; |
8 changes: 3 additions & 5 deletions
8
packages/lit-dev-content/samples/examples/async-task/project.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
{ | ||
"extends": "/samples/base.json", | ||
"hide": true, | ||
"title": "Async Task / NPM Info element", | ||
"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": "Specific Solutions", | ||
"section": "Managing Data", | ||
"files": { | ||
"index.js": {}, | ||
"npm-info.js": {}, | ||
"task.js": {}, | ||
"npm-info.ts": {}, | ||
"npm.ts": {}, | ||
"index.html": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to update this for graduation. cc @rictic