Skip to content
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

Closed
ef4 opened this issue Dec 15, 2023 · 4 comments
Closed

class-based Resource TS usability #1054

ef4 opened this issue Dec 15, 2023 · 4 comments

Comments

@ef4
Copy link

ef4 commented Dec 15, 2023

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 to modify, 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.

@NullVoxPopuli
Copy link
Owner

NullVoxPopuli commented Dec 15, 2023

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.

Yeah, arguments are only available via modify in the current implementation, they mirror class-based modifiers -- which probably also need re-evaluating.

It could have a different method for receiving updates

After lots of playing around in this space, I'm not sure we want to have an update callback.
Derived data is sufficient, and documenting how folks can effectively use derived data would handle most folks' use cases.

As-is, the modify hook we have right now (and in modifiers) is used as a backdoor to an effect.

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

@NullVoxPopuli
Copy link
Owner

I've started a doc on proposing alternatives: #1056

@ef4
Copy link
Author

ef4 commented Dec 15, 2023

As-is, the modify hook we have right now (and in modifiers) is used as a backdoor to an effect.

That's a very good point and another reason not to keep modify.

@NullVoxPopuli
Copy link
Owner

Closing this as the class-based-resource has been:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants