Context#1200
Conversation
|
For consuming, an alternative API could be: on the provider side, you could use: Use case I'm thinking of: For anything that's embedded in a modal, they could react to it (optionally): Of course, the tag-based / template-based
|
|
I sometimes -- perhaps just as often if not more -- want the reverse! The children provide something for the parent. This calls for the "dreaded" (IMO) maybe foo does: This is offtopic! just wanted to say there's an inverse provide/consume relationship that requires boilerplate. |
|
consume can already work in the class, but not as a decorator -- no argument is needed, it's just on Provide using a decorator -- it breaks the "block scope" semantics that we get with the DOM, so I feel it would be too magical, and potentially leaky, to implement that way. on decorators in general, for type safety, it's easier to avoid them since we can't have decorators force a type on their decorated property.
your example from your second comment would be written as: const fooApi = makeContext();
class Foo extends Component {
makeApi = () => {/* ... */};
<template>
<fooApi.Provide @value={{ (this.makeApi) }}>
{{yield}}
</fooApi.Provide />
</template>
}
<template>
<Foo>
{{log (fooApi.consume)}} -- logs whatever makeApi returns
</Foo>
</template> |
|
could it be enhanced to support keyed values? non-keyed could also just work <template>
<fooApi.Provide @key="api" @value={{ (this.makeApi) }}>
<fooApi.Provide @key="theme" @value={{ (this.makeTheme) }}>
{{(ctx.consume 'api')}}
{{(ctx.consume 'theme')}}
</fooApi.Provide />
</fooApi.Provide />
</template>consume would walk up until it find the right provider |
|
String keys are hard to type (both in the typescript sense and in the avoiding typos sense), so I'd encourage folks to do something like this for your example let api = makeContext();
let theme = makeContext();
<template>
<api.Provide @value={{ (this.makeApi) }}>
<theme.Provide @value={{ (this.makeTheme) }}>
{{(api.consume)}}
{{(theme.consume)}}
</theme.Provide />
</api.Provide />
</template> |
Decorator Feedback
|
Questions
Recommendation: Clarify that opinions could be done in user landThere are a few things that people might disagree with but are fully able to be implemented in user land.
Users who do still want the function consumeWithDefault<T>(value: Context<T>, defaultValue: T): T {
try {
return value.consume();
} catch {
return defaultValue;
}
}
function consumeSafely<T>(value: Context<T>): T | undefined {
try {
return value.consume();
} catch {
return undefined;
}
}
The RFCs declare that no test helpers would be added. It's fairly common to setup a provider in a beforeEach or other block so that it does not have to be repeated for every |
NitpickI know that there's already bikeshedding around export location. But,
My preference is Short of that, since this is closely tied to the newer |
just to show where it's coming from. You can absolutey do const api = makeContext();
export const Provider = api.Provide;
export const consume = api.consume;
additionally this is extra ceremony that I don't think is useful (you need two imports now instead of one). since we can have an explicit reference, we should use it.
My hesitancy here is that this would be a brand new package with only one export
I originally had it here, actually 🙈 but, was thinking -- context isn't a rendering concern from the user's perspective |
Note on keysSvelte has both
|
Testing HelpersThe RFC mentions not having any framework level testing helpers. Generally we can learn from Svelte again here https://svelte.dev/docs/svelte/context#Component-testing let testRender;
hooks.beforeEach(() => {
const defaultUser = new User();
testRender = (children) => {
return render(<template>
<userContext.Provide @value={{defaultUser}}>
<Children />
</userContext.Provide>
</template>)
}
})
test('foo', async () => {
await testRender(<template><MyThing /></template>);
// Test stuff
}) |
Implementation: emberjs/ember.js#21450
Intent to supersede
Propose Context
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