-
Notifications
You must be signed in to change notification settings - Fork 37
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
class-based Resource TS usability #1054
Comments
Yeah, arguments are only available via modify in the current implementation, they mirror class-based modifiers -- which probably also need re-evaluating.
After lots of playing around in this space, I'm not sure we want to have an update callback. As-is, the I've been considering re-doing class-based resources' implementation (which would be non-breaking), to instead wrap function-based resources. The implementation would roughly be: function ClassBasedResource(args) {
return resource({ on, owner }) => {
let instance;
on.cleanup(() => instance && destroy(instance));
return () => {
instance ||= new SomethingFromTheUser(); // + setOwner, associateDestroyableChild, etc
instance.modify(args); // spirit of, not exactly.
return instance;
}
});
} that said, I think I'd rather document how to implement various "resource-like" patterns with classes using the function-based resource api, as folks may want different capabilities around construction and updating. The types for template-only glint compatibility tho is bonkers, and def not for the feighnt of heart. Probably need to open an issue with some ideas before moving forward with this though. In a lot of folks' use-cases, the link-pattern would be sufficient: https://ember-resources.pages.dev/funcs/link.link |
I've started a doc on proposing alternatives: #1056 |
That's a very good point and another reason not to keep |
Closing this as the class-based-resource has been:
|
The current class-based resources are hard to implement with good typescript types, because they don't have access to their arguments in their constructor.
Another way to say this is: they expose a user-visible "dead time" during which they exist but don't know their arguments.
This problem manifests as being forced to declare all your fields as
TheirRealType | undefined
because they are undefined before the first call tomodify
, even though that time is irrelevant.An API that used the constructor for initial arguments would not have this problem. It could have a different method for receiving updates. Using the constructor would also avoid the weirdness of using the word "modify" to describe the initial creation of a thing.
The text was updated successfully, but these errors were encountered: