-
Notifications
You must be signed in to change notification settings - Fork 1
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
Modernize components and prepare for v2 #61
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9358c75
Add type declarations for public APIs
dfreeman aac0844
Clean up `implementationMap` inconsistencies
dfreeman cb839c6
Modernize Exclaim components
dfreeman f1eb25f
Officially deprecate `unwrap`
dfreeman 643e52b
Modernize playground-app Exclaim implementations
dfreeman 6268e6b
Modernize playground-app itself
dfreeman 3591fd2
Update README and GLOSSARY
dfreeman 48acc3a
Add v1 -> v2 migration notes to the README
dfreeman f01e1d3
Turn on `embroider-optimized`?
dfreeman 0c5a126
Don't rely on `@cached`
dfreeman f5cce68
Re-add missing `Environment` type
dfreeman 38ec759
Ensure whitespace is always trimmed
dfreeman c5da1be
Add undeclared `@glimmer` dependencies
dfreeman 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 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
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,67 @@ | ||
import { ComponentLike } from '@glint/template'; | ||
import { ComponentSpec, ImplementationMap } from '../index'; | ||
|
||
declare const ExclaimUi: ComponentLike<{ | ||
Args: { | ||
/** | ||
* A spec for an Exclaim UI. | ||
*/ | ||
ui: unknown; | ||
|
||
/** | ||
* A mapping of names to implementations for use by `$component` | ||
* and `$helper` in the given UI spec. | ||
*/ | ||
implementationMap: ImplementationMap; | ||
|
||
/** | ||
* The backing data that any `$bind` in the UI spec will be bound | ||
* to. This value will also be directly available as `@env` to all | ||
* components rendered in this UI. | ||
*/ | ||
env?: unknown; | ||
|
||
/** | ||
* A callback that will be invoked when the given path in the | ||
* environment has changed because a `$bind` value was written. | ||
*/ | ||
onChange?: (envPath: string) => void; | ||
|
||
/** | ||
* Set this flag `true` to use `computed`-based reactivity for managings | ||
* bindings and helpers within this UI. When `false` or unset, native | ||
* getters and setters will be used instead. | ||
*/ | ||
useClassicReactivity?: boolean; | ||
|
||
/** | ||
* An optional component that, if provided, will be invoked around each | ||
* component in this UI. | ||
*/ | ||
wrapper?: ComponentLike<{ | ||
Args: { | ||
/** The {@link ComponentSpec} being wrapped. */ | ||
componentSpec: ComponentSpec; | ||
|
||
/** The resolved config object the wrapped component will receive. */ | ||
config: unknown; | ||
|
||
/** The env object the wrapped component will receive. */ | ||
env: unknown; | ||
}; | ||
Blocks: { | ||
default: []; | ||
}; | ||
}>; | ||
}; | ||
|
||
Blocks: { | ||
/** | ||
* If an error is encountered when processing the given UI spec, it | ||
* will be yielded to the default block. | ||
*/ | ||
default: [error: unknown]; | ||
}; | ||
}>; | ||
|
||
export default ExclaimUi; |
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,63 @@ | ||
import { ComponentLike } from '@glint/template'; | ||
|
||
declare const Env: unique symbol; | ||
|
||
/** A marker type for objects that are operating as an Exclaim environment. */ | ||
export type Environment<T> = T & Record<typeof Env, undefined>; | ||
|
||
/** | ||
* Returns the environment and source path within it where the field at | ||
* the path on the given object originates, if known. | ||
*/ | ||
export function resolveEnvPath( | ||
object: unknown, | ||
path: string, | ||
): string | undefined; | ||
|
||
/** The data defining a helper in an {@link ImplementationMap} */ | ||
export type HelperImplementation = { | ||
helper: (...args: Array<never>) => unknown; | ||
shorthandProperty?: string; | ||
meta?: unknown; | ||
}; | ||
|
||
/** The data defining a component in an {@link ImplementationMap} */ | ||
export type ComponentImplementation = { | ||
component: ComponentLike<unknown>; | ||
shorthandProperty?: string; | ||
meta?: unknown; | ||
}; | ||
|
||
/** | ||
* A mapping of names that can be used in a UI spec with `$helper` | ||
* or `$component` to the runtime implementation that should be invoked | ||
* when that name is encountered. | ||
*/ | ||
export type ImplementationMap<Names extends string = string> = Record< | ||
Names, | ||
HelperImplementation | ComponentImplementation | ||
>; | ||
|
||
/** Represents a `$component` object in a UI spec. */ | ||
export class ComponentSpec { | ||
readonly component: ComponentLike<unknown>; | ||
readonly config: unknown; | ||
readonly meta: unknown; | ||
|
||
resolveConfig(env: Environment<unknown>): unknown; | ||
} | ||
|
||
/** Represents a `$helper` object in a UI spec. */ | ||
export class HelperSpec { | ||
readonly helper: (...args: Array<never>) => unknown; | ||
readonly config: unknown; | ||
readonly meta: unknown; | ||
readonly bindings: Array<unknown>; | ||
|
||
invoke(env: Environment<unknown>): unknown; | ||
} | ||
|
||
/** | ||
* @deprecated This function is now a no-op and should no longer be used. | ||
*/ | ||
export function unwrap<T>(value: T): T; |
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,5 @@ | ||
import { ExclaimUi } from './components/exclaim-ui'; | ||
|
||
export default interface ExclaimTemplateRegistry { | ||
ExclaimUi: typeof ExclaimUi; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
{{#component | ||
@wrapper | ||
componentSpec=@componentSpec | ||
env=this.effectiveEnv | ||
config=this.resolvedConfig | ||
}} | ||
{{#component | ||
@componentSpec.component | ||
config=this.resolvedConfig | ||
env=this.effectiveEnv | ||
as |componentSpec overrideEnv| | ||
~}} | ||
<ExclaimComponent | ||
<@wrapper | ||
@componentSpec={{@componentSpec}} | ||
@env={{this.componentData.env}} | ||
@config={{this.componentData.config}} | ||
> | ||
{{~null~}} | ||
<@componentSpec.component | ||
@config={{this.componentData.config}} | ||
@env={{this.componentData.env}} | ||
as |componentSpec additionalEnvData| | ||
> | ||
{{~null~}} | ||
<ExclaimComponent | ||
@componentSpec={{componentSpec}} | ||
@env={{this.effectiveEnv}} | ||
@overrideEnv={{overrideEnv}} | ||
@env={{this.componentData.env}} | ||
@additionalEnvData={{additionalEnvData}} | ||
@wrapper={{@wrapper}} | ||
/> | ||
{{~/component}} | ||
{{/component}} | ||
{{~null~}} | ||
</@componentSpec.component> | ||
{{~null~}} | ||
</@wrapper> |
Oops, something went wrong.
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.
Would it make sense to try to type this at all? At the very least, it would be
Record<string, unknown>
, right? I've attempted some more advanced recursive types for this in the past, but at this point not sure if something like that makes any sense.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.
Hot take: imho
Record<string, unknown>
is worse thanunknown
for cases like this. It does rule out "fundamental misunderstanding" bugs like@ui={{123}}
, but realistically those aren't the kinds of errors that are likely to be present in a UI config object.Record<string, unknown>
suggests some degree of type safety at least at the top level where really none exists.You can go super deep on deriving approximate valid types for a UI config based on an implementation map (particularly if you assume some domain-specific understanding of the contents of
meta
), but ultimately the reality is that if you actually statically know the type of your UI config, you don't have any reason to be using Exclaim and should probably invoke your components directly.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.
Totally fair and good points. Ultimately, UI config is usually sourced from something dynamic and untyped, like JSON (otherwise, like you say, what's the point of using exclaim) so it makes sense that we should add no artificial type restriction here.