-
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
Omit thunk in class-based resources when not needed #1008
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```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: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```ts | ||||||
class TestResource extends Resource { | ||||||
foo = 3; | ||||||
} | ||||||
|
||||||
class Test { | ||||||
data = TestResource.from(this); | ||||||
} | ||||||
``` |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -42,6 +42,8 @@ type ResourceHelperLike<T, R> = InstanceType< | |||
|
||||
declare const __ResourceArgs__: unique symbol; | ||||
|
||||
type Context = object; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As an attempt to work with more specific types, we (Jan and me) tried to give the
Maybe type Context = Exclude<object, Thunk>; |
||||
|
||||
/** | ||||
* The 'Resource' base class has only one lifecycle hook, `modify`, which is called during | ||||
* instantiation of the resource as well as on every update of any of any consumed args. | ||||
|
@@ -195,6 +197,27 @@ export class Resource<Args = unknown> { | |||
thunk: AsThunk<ArgsFrom<SomeResource>> | ||||
): SomeResource; | ||||
|
||||
/** | ||||
* 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>, _C extends Context>( | ||||
this: Constructor<SomeResource>, | ||||
contextOrThunk: _C extends Thunk ? AsThunk<ArgsFrom<SomeResource>> : _C | ||||
): SomeResource; | ||||
Comment on lines
+216
to
+219
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a result for
but here on type-level. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, function vs object typings are hard :( |
||||
|
||||
/** | ||||
* For use in the body of a class. | ||||
* | ||||
|
@@ -240,13 +263,13 @@ export class Resource<Args = unknown> { | |||
*/ | ||||
static from<SomeResource extends Resource<any>>( | ||||
this: Constructor<SomeResource>, | ||||
context: unknown, | ||||
context: Context, | ||||
thunk: AsThunk<ArgsFrom<SomeResource>> | ||||
): SomeResource; | ||||
|
||||
static from<SomeResource extends Resource<any>>( | ||||
this: Constructor<SomeResource>, | ||||
contextOrThunk: unknown | AsThunk<ArgsFrom<SomeResource>>, | ||||
contextOrThunk: Context | AsThunk<ArgsFrom<SomeResource>>, | ||||
thunkOrUndefined?: undefined | AsThunk<ArgsFrom<SomeResource>> | ||||
): SomeResource { | ||||
/** | ||||
|
@@ -336,7 +359,7 @@ export class Resource<Args = unknown> { | |||
} | ||||
|
||||
function resourceOf<SomeResource extends Resource<unknown>>( | ||||
context: unknown, | ||||
context: Context, | ||||
klass: Constructor<SomeResource>, | ||||
thunk?: Thunk | ||||
): SomeResource { | ||||
|
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.
thank a ton for such a thorough changelog entry!