-
Notifications
You must be signed in to change notification settings - Fork 11
Introduce Await component for nicer template usage
#709
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
Open
chriskrycho
wants to merge
1
commit into
main
Choose a base branch
from
async-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
This file contains hidden or 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,3 +1,4 @@ | ||
| { | ||
| "typescript.tsdk": "node_modules/typescript/lib" | ||
| } | ||
| "typescript.tsdk": "node_modules/typescript/lib", | ||
| "glint.libraryPath": "./ember-async-data" | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -9,4 +9,5 @@ module.exports = { | |
| }, | ||
| }, | ||
| ], | ||
| plugins: ['prettier-plugin-ember-template-tag'], | ||
| }; | ||
This file contains hidden or 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,9 +1,14 @@ | ||
| { | ||
| "presets": [["@babel/preset-typescript"]], | ||
| "presets": [ | ||
| [ | ||
| "@babel/preset-typescript" | ||
| ] | ||
| ], | ||
| "plugins": [ | ||
| "ember-template-imports/src/babel-plugin", | ||
| "@embroider/addon-dev/template-colocation-plugin", | ||
| ["@babel/plugin-transform-typescript", { "allowDeclareFields": true }], | ||
| ["@babel/plugin-proposal-decorators", { "version": "legacy" }], | ||
| "@babel/plugin-proposal-class-properties" | ||
| ] | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,75 @@ | ||
| import fs from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import { preprocessEmbeddedTemplates } from "ember-template-imports/lib/preprocess-embedded-templates.js"; | ||
| import { | ||
| TEMPLATE_TAG_NAME, | ||
| TEMPLATE_TAG_PLACEHOLDER, | ||
| } from "ember-template-imports/lib/util.js"; | ||
|
|
||
| export default function firstClassComponentTemplates() { | ||
| return { | ||
| name: "preprocess-fccts", | ||
| async resolveId(source, importer, options) { | ||
| if (source.endsWith(".hbs")) return; | ||
|
|
||
| for (let ext of ["", ".gjs", ".gts"]) { | ||
| let result = await this.resolve(source + ext, importer, { | ||
| ...options, | ||
| skipSelf: true, | ||
| }); | ||
|
|
||
| if (result?.external) { | ||
| return; | ||
| } | ||
|
|
||
| if (FCCT_EXTENSION.test(result?.id)) { | ||
| return resolutionFor(result.id); | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| async load(id) { | ||
| let originalId = this.getModuleInfo(id)?.meta?.fccts?.originalId ?? id; | ||
|
|
||
| if (originalId !== id) { | ||
| this.addWatchFile(originalId); | ||
| } | ||
|
|
||
| if (FCCT_EXTENSION.test(originalId)) { | ||
| return await preprocessTemplates(originalId); | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const FCCT_EXTENSION = /\.g([jt]s)$/; | ||
|
|
||
| function resolutionFor(originalId) { | ||
| return { | ||
| id: originalId.replace(FCCT_EXTENSION, ".$1"), | ||
| meta: { | ||
| fccts: { originalId }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| async function preprocessTemplates(id) { | ||
| let ember = (await import("ember-source")).default; | ||
| let contents = await fs.readFile(id, "utf-8"); | ||
|
|
||
| // This is basically taken directly from `ember-template-imports` | ||
| let result = preprocessEmbeddedTemplates(contents, { | ||
| relativePath: path.relative(".", id), | ||
|
|
||
| getTemplateLocalsRequirePath: ember.absolutePaths.templateCompiler, | ||
| getTemplateLocalsExportPath: "_GlimmerSyntax.getTemplateLocals", | ||
|
|
||
| templateTag: TEMPLATE_TAG_NAME, | ||
| templateTagReplacement: TEMPLATE_TAG_PLACEHOLDER, | ||
|
|
||
| includeSourceMaps: true, | ||
| includeTemplateTokens: true, | ||
| }); | ||
|
|
||
| return result.output; | ||
| } |
This file contains hidden or 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 hidden or 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,81 @@ | ||
| import Component from '@glimmer/component'; | ||
| import { cached } from '@glimmer/tracking'; | ||
| import TrackedAsyncData from '../tracked-async-data'; | ||
|
|
||
| export interface AsyncSignature<T> { | ||
| Args: { | ||
| /** The `TrackedAsyncData` to render. */ | ||
| data: T | Promise<T>; | ||
| }; | ||
| Blocks: { | ||
| /** To render while waiting on the promise */ | ||
| pending: []; | ||
| /** To render once the promise has resolved, with the resolved value. */ | ||
| resolved: [value: T]; | ||
| /** To render if the promise has rejected, with the associated reason. */ | ||
| rejected: [reason: unknown]; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| Render a `TrackedAsyncData` in a template, with named blocks for each of the | ||
| states the value may be in. This is somewhat nicer than doing the same with | ||
| an `if`/`else if` chain, and composes nicely with other template primitives. | ||
|
|
||
| It is also less error prone, in that you *cannot* access the value of the data | ||
| in the `pending` or `rejected` blocks: it is only yielded by the `resolved` | ||
| named block. | ||
|
|
||
| ```ts | ||
| import { Async } from 'ember-async-data' | ||
| import LoadingSpinner from './loading-spinner'; | ||
|
|
||
| function dumpError(error: unknown) { | ||
| return JSON.stringify(error); | ||
| } | ||
|
|
||
| let example = new TrackedAsyncData("hello"); | ||
|
|
||
| <template> | ||
| <Async @data={{example}}> | ||
| <:pending> | ||
| <LoadingSpinner /> | ||
| </:pending> | ||
|
|
||
| <:resolved as |value|> | ||
| <SomeComponent @arg={{value}} /> | ||
| </:resolved> | ||
|
|
||
| <:rejected as |reason|> | ||
| <p>Whoops, something went wrong!</p> | ||
| <pre><code> | ||
| {{dumpErr reason}} | ||
| </code></pre> | ||
| </:rejected> | ||
| </Async> | ||
| </template> | ||
| ``` | ||
| */ | ||
| // IMPLEMENTATION NOTE: yes, future maintainer, this *does* have to be a class, | ||
| // not a template-only component. This is because it must be generic over the | ||
| // type passed in so that the yielded type can preserve that ("parametricity"). | ||
| // That is: if we pass in a `TrackedAsyncData<string>`, the value yielded from | ||
| // the `resolved` named block should be a string. The only things which can | ||
| // preserve the type parameter that way are functions and classes, and we do not | ||
| // presently have a function-based way to define components, so we have to use | ||
| // a class instead to preserve the type! | ||
| export default class Async<T> extends Component<AsyncSignature<T>> { | ||
| @cached get data() { | ||
| return new TrackedAsyncData(this.args.data); | ||
| } | ||
|
|
||
| <template> | ||
| {{#if this.data.isResolved}} | ||
| {{yield this.data.value to='resolved'}} | ||
| {{else if this.data.isRejected}} | ||
| {{yield this.data.error to='rejected'}} | ||
| {{else}} | ||
| {{yield to='pending'}} | ||
| {{/if}} | ||
| </template> | ||
| } |
This file contains hidden or 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,2 +1,3 @@ | ||
| export { default as TrackedAsyncData } from './tracked-async-data'; | ||
| export { load } from './helpers/load'; | ||
| export { default as Async } from './components/async'; |
This file contains hidden or 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 hidden or 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.
This seemed to be the best approach we had when I spiked this out a few months ago, but I'm totally up for a different approach if we have something better now.
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.
the v2 addon blueprint, via
@embroider/addon-devdoes this for you now.However, pending:
I use this for now:
https://github.com/universal-ember/ember-primitives/blob/main/ember-primitives/rollup.config.mjs#L20