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

Omit thunk in class-based resources when not needed #1008

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .changeset/good-donuts-hear.md
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank a ton for such a thorough changelog entry!

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
"ember-resources": minor
---

Omit thunk arg for class based resources when not needed

When using a `Resource` but not using arguments with reactive parameters, initializing them still required the usage of a thunk, ie:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When using a `Resource` but not using arguments with reactive parameters, initializing them still required the usage of a thunk, ie:
When using the class-based `Resource` without reactive parameters, initializing _used to_ require the usage of a thunk, ie:


```ts
class TestResource extends Resource {
foo = 3;
}

class Test {
data = TestResource.from(this, () => []);
}
```

... for basically, no reason. This change makes thunks a non-required parameter, leading into cleaner code like so:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
... for basically, no reason. This change makes thunks a non-required parameter, leading into cleaner code like so:
This change makes thunks a non-required parameter, leading into cleaner code like so:


```ts
class TestResource extends Resource {
foo = 3;
}

class Test {
data = TestResource.from(this);
}
```
21 changes: 21 additions & 0 deletions ember-resources/src/core/class-based/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,27 @@ export class Resource<Args = unknown> {
*/
declare [Invoke]: ResourceHelperLike<Args, this>[typeof Invoke];

/**
* For use in the body of a class, without reactive arguments.
*
* `from` is what allows resources to be used in JS, they hide the reactivity APIs
* from the consumer so that the surface API is smaller.
*
* ```js
* import { Resource } from 'ember-resources';
*
* class SomeResource extends Resource {}
*
* class MyClass {
* data = SomeResource.from(this);
* }
* ```
*/
static from<SomeResource extends Resource<any>>(
this: Constructor<SomeResource>,
context: unknown
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that overload makes the type tests fail.. ie, when this is a thunk, then the types are not checked against the args from signature. This needs to stay intact.

Also for me it is late in the eve right now and I will postpone this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My alternative solution wasn't working either:

  static from<SomeResource extends Resource<any>, CT = unknown>(
    this: Constructor<SomeResource>,
    contextOrThunk: CT extends Function ? AsThunk<ArgsFrom<SomeResource>> : unknown
  ): SomeResource;

): SomeResource;

/**
* For use in the body of a class.
*
Expand Down
2 changes: 2 additions & 0 deletions test-app/tests/core/class-resource/js-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ module('Core | (class) Resource | js', function (hooks) {
}

class Test {
data = TestResource.from(this);
dataArray = TestResource.from(this, () => []);
// eslint-disable-next-line @typescript-eslint/no-empty-function
dataVoid = TestResource.from(this, () => {});
Expand All @@ -150,6 +151,7 @@ module('Core | (class) Resource | js', function (hooks) {

let foo = new Test();

assert.strictEqual(foo.data.foo, 3);
assert.strictEqual(foo.dataArray.foo, 3);
assert.strictEqual(foo.dataVoid.foo, 3);
assert.strictEqual(foo.dataOmitted.foo, 3);
Expand Down
Loading