-
-
Notifications
You must be signed in to change notification settings - Fork 408
Public API for render-tree-based scope exploration #1154
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
base: main
Are you sure you want to change the base?
Conversation
text/1154-public-api-for-render-tree-based-scope-exploration.md
Outdated
Show resolved
Hide resolved
|
I'm a fan of accessing the DI container through functions. I looks a bit clunky though, when you always get the scope first, then the owner from it. A similar sounding concept, with also similar idea (to access the DI container) is done in |
|
@gossi done, thanks! |
text/1154-public-api-for-render-tree-based-scope-exploration.md
Outdated
Show resolved
Hide resolved
|
@gossi from my understanding The way function getAncestors(component: typeof Component) {
const scope = getScope()
return scope.entries.filter(a => a instanceof component).toArray();
}
class TreeNode extends Component {
get depthCount() {
return getAncestors(TreeNode);
}
<template>
<Provide @key={{TreeNode}} @value{{this}}>
... IDK cool stuff probably
</Provide>
</template>
} |
| This supports both class providing and consuming as well as template-based providing and consuming. | ||
|
|
||
| ```gjs | ||
| import { Consume, Provide, consume, provide } from 'hypothetical ember-prototype-context library'; |
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.
NOTE: for exposing context, we need some limitations to prevent misuse.
Examples that should be non-controversial:
- throw an exception of the Consume usage can't find a corresponding Provide (via matching key)
- for types, the key should be recommended to be a class (i.e.: constructor type), so that the value can be assumed to be an instance of that type -- this isn't as flexible as the "anything goes" contexts that react has, but there isn't a whole lot of value (I think) in supporting just primitive values for the whole of a context.
This is already how ember-primitives' dom-context works, but I forgot to mention it, because I forgot that it wasn't obvious 🙈
| {{a.id}} === 0 | ||
| </Consume> | ||
|
|
||
| {{#let (consume StateA) as |a|}} |
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.
NOTE: consume as a component has no value over let (and let is shorter (by two characters))
|
|
||
| <template> | ||
| <Provide @key={{StateA}} @context={{ (newA) }}> | ||
| <Consume @key={{StateA}} as |a|> |
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.
NOTE: for TypeScript reasons, the key is recommended to be a class
| const newB = () => new StateB(); | ||
|
|
||
| class CustomProvide extends Component { | ||
| @provide a = new StateA(); |
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.
NOTE: ditch this API, it's weird
|
|
||
| ## Alternatives | ||
|
|
||
| None. |
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.
TODO:
- explain why we don't like the type of context apis that Vue and Svelte are doing (they don't have block-scope semantics, which is the key thing our components / templating is about)
|
Something else to explore -- can we use the module hierarchy to ensure that everything can be known statically? |
|
another option for ensuring type safety: class Foo {
bar = 2;
}
// libraries could export `foo`, or each thing separately if they want
const foo = makeContext(Foo)
<template>
<foo.Provide>
{{#let (foo.consume) as |fooState|}}
{{fooState.bar}} == 2
{{/let}}
</foo.Provide>
{{ (foo.consume) }} <-- throws an exception
</template>Clarify:
|
|
Bringing in ideas from function makeContext<T>(defaultValueFactory: () => T) : {
Provide: ProvideComponent<T>,
consume: () => T
}This allows the greatest flexibility while also ensuring better safety. For people who want to stick to @NullVoxPopuli's a zero argument constructor as the factory this could easily be built as a higher order component: function makeClassContext<T>(klass: new () => T) {
return makeContext(() => new klass);
}Note This But we also have a default factory function inspired by AutoFac which allows hooks for possible scenarios:
|
|
Example of a "Permission" context where you may want to add user impersonation (which must be able to pass through element boundaries) But the providers let you sparsely provide args <template>
<Permission @user={{session.user}}>
<Permission @role={{adminRole}}>
<UserCard />
</Permission>
{{!-- We want to impersonate and override outer scope--}}
<Permission @user={{tempUser}}>
<UserCard />
</Permission>
</Permission>
</template>
const PermissionContext = makeContext(() => {
user: null,
role: null,
})
class Permission extends Component {
get permissionInfo () {
const upperTheme = PermissionContext.consume();
return {
user: this.args.user ?? upperTheme.user,
role: this.args.role ?? upperTheme.role,
}
}
<template>
<PermissionContext.Provide @value={{this.permissionInfo}}>
{{yield}}
</PermissionContext.Provide>
</template>
} |
Propose adding a new Public API for exploring render-tree-based scope.
Rendered
Summary
This pull request is proposing a new RFC.
To succeed, it will need to pass into the Exploring Stage, followed by the Accepted Stage.
A Proposed or Exploring RFC may also move to the Closed Stage if it is withdrawn by the author or if it is rejected by the Ember team. This requires an "FCP to Close" period.
An FCP is required before merging this PR to advance to Accepted.
Upon merging this PR, automation will open a draft PR for this RFC to move to the Ready for Released Stage.
Exploring Stage Description
This stage is entered when the Ember team believes the concept described in the RFC should be pursued, but the RFC may still need some more work, discussion, answers to open questions, and/or a champion before it can move to the next stage.
An RFC is moved into Exploring with consensus of the relevant teams. The relevant team expects to spend time helping to refine the proposal. The RFC remains a PR and will have an
Exploringlabel applied.An Exploring RFC that is successfully completed can move to Accepted with an FCP is required as in the existing process. It may also be moved to Closed with an FCP.
Accepted Stage Description
To move into the "accepted stage" the RFC must have complete prose and have successfully passed through an "FCP to Accept" period in which the community has weighed in and consensus has been achieved on the direction. The relevant teams believe that the proposal is well-specified and ready for implementation. The RFC has a champion within one of the relevant teams.
If there are unanswered questions, we have outlined them and expect that they will be answered before Ready for Release.
When the RFC is accepted, the PR will be merged, and automation will open a new PR to move the RFC to the Ready for Release stage. That PR should be used to track implementation progress and gain consensus to move to the next stage.
Checklist to move to Exploring
S-Proposedis removed from the PR and the labelS-Exploringis added.Checklist to move to Accepted
Final Comment Periodlabel has been added to start the FCP