-
-
Notifications
You must be signed in to change notification settings - Fork 407
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
Built in tracking utilities for promises #1060
base: master
Are you sure you want to change the base?
Conversation
typo in 1060-tracked-promise.md
I love having an "ember" way to do this. We're in our third or fourth iteration of refactoring our ten year old app to handle asynchronous data better. Having this be awaitable and being able to access the Couple of questions: Second, the method signature looks like it would only accept a get comments()
return trackPromise(this.args.post?.comments ?? []);
} Where maybe you want to handle a few different kinds of input, but get a consistent output without needing to wrap each case in some sort of cluttery |
mostly, yea, it's a shorthand -- if I were PRing this to ember today, this would probably be the implementation: export function trackPromise<Value>(
existingPromise: Promise<Value> | Value
): TrackedPromise<Value> {
return new TrackedPromise(existingPromise);
} This form is also more easily invoked from templates, whereas new-ing is not possible.
This is a good point -- I've updated the type signature for trackPromise above (I'll get to updating the RFC shortly -- but also, specifics of the type signatures are implementation details, imo. TS will keep us honest, and there is no type-checking in markdown haha)
Unrelated to your question, but perhaps for others: note that forgetting |
Nice, good writeup – thanks! I would maybe expect to be able to Wether that should be at at the 'old' Most/all exports are called something with 'tracked', so a simple alternative could be: import { tracked, TrackedPromise, TrackedArray } from '@ember/tracking' I am also a bit unsure if the word 'reactive' is used lot in the built-in ember docs/guides at the moment. On the other hand, |
Yeah, my current plan, pending other comments and other RFCs, to group imports by importance, so that folks without tree shaking (all of us) only pay for what they import, leaving the needed imports in by default at the top-level import. All of this would need varying RFCs outside of what is prosed in this RFC PR import {
// state containers
TrackedPromise, TrackedArray, TrackedObject,
// wrappers (1 line implementations)
trackPromise, trackArray, trackObject,
// core utilities
tracked, cached, localCopy, ...
// low-level
cell, resource, sync
} from '@ember/reactive'; // also willing to use '@ember/tracking',
// however, I think reactive may more more sense for the sub-paths
////////////////
// The rest of this is "pay only for what you import"
import {
TrackedMap, TrackedWeakMap,
TrackedSet, TrackedWeakSet,
trackMap, trackWeakMap,
trackSet, trackWeakSet
} from '@ember/reactive/collections';
import {
TrackedURL, TrackedURLSearchParams,
trackURL, trackURLSearchParams
} from '@ember/reactive/url';
// these would be low level _Cells_ or _Resources_,
// and would have a .current property
import {
devicePixelRatio,
innerHeight,
innerWidth,
online,
outerHeight,
outerWidth,
screenLeft,
screenTop,
scrollX,
scrollY,
} from '@ember/reactive/window'; |
|
||
For simplicity, these new utilities will not be using `@dependentKeyCompat` to support the `@computed` era of reactivity. pre-`@tracked` is before ember-source @ 3.13, which is from over 5 years ago, at the time of writing. For the broadest, most supporting libraries we have, 3.28+ is the supported range, and for speed of implementation, these tracked promise utilities can strive for the similar compatibility. | ||
|
||
An extra feature that none of the previously mentioned implementations have is the ability to `await` directly. This is made easy by only implementing a `then` method -- and allows a good ergonomic bridge between reactive and non-reactive usages. |
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.
One of the reasons I'm against a built-in reactive promise is actually exactly this. I don't think that something that exposes additional values that the native primitive does not have should also pretend to be the native primitive in this way, because at that point you are a whole different thing.
e.g. this isn't ReactivePromise, this is AsyncData
conceptually, a reactive promise would wrap the original promises' methods in one that subscribes to a signal, then finally the original promise and use that to update the signal once the resolution was complete so that consuming code would repull. Obviously that's far less useful than the state-machine approach taken here and by many of these primitives.
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.
Thanks, i agree actually. I've updated the name of the thing so it doesn't imply that it's trying to masquerade as a native promise
text/1060-tracked-promise.md
Outdated
An extra feature that none of the previously mentioned implementations have is the ability to `await` directly. This is made easy by only implementing a `then` method -- and allows a good ergonomic bridge between reactive and non-reactive usages. | ||
|
||
|
||
The implementation of `TrackedPromise` is intentionally limited, as we want to encourage reliance on [_The Platform_][mdn-Promise] whenever it makes sense, and is ergonomic to do so. For example, using `race` would still be done native, and can be wrapped for reactivity: |
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 is why https://github.com/emberjs/data/blob/2f7d94a812ad02ce0979bb1e07940e45fae8a38c/packages/ember/src/-private/promise-state.ts#L8 is so limited too: e.g it exposes the underlying states and the result and nothing more.
Propose Built in tracking utilities for promises
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
Exploring
label 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-Proposed
is removed from the PR and the labelS-Exploring
is added.Checklist to move to Accepted
Final Comment Period
label has been added to start the FCP