Thunk<Args>: (() => []) | (() => void) | (() => undefined) | (() => ThunkReturnFor<Args>["positional"]) | (() => ThunkReturnFor<Args>["named"]) | (() => Partial<ThunkReturnFor<Args>>) | (() => ThunkReturnFor<Args>)
Type declaration
-
- (): undefined
-
Returns undefined
Type declaration
-
- (): ThunkReturnFor<Args>["positional"]
-
Returns ThunkReturnFor<Args>["positional"]
Type declaration
-
- (): ThunkReturnFor<Args>["named"]
-
Returns ThunkReturnFor<Args>["named"]
Type declaration
-
- (): Partial<ThunkReturnFor<Args>>
-
Returns Partial<ThunkReturnFor<Args>>
Type declaration
-
- (): ThunkReturnFor<Args>
-
Returns ThunkReturnFor<Args>
A generic function type that represents the various formats a Thunk can be in.
Note that thunks are awkward when they aren't required -- they may even be awkward when they are required. Whenever possible, we should rely on auto-tracking, such as what trackedFunction provides.
So when and why are thunks needed?
The args thunk accepts the following data shapes:
An array
when an array is passed, inside the Resource,
this.args.named
will be empty andthis.args.positional
will contain the result of the thunk.for function resources, this is the only type of thunk allowed.
An object of named args
when an object is passed where the key
named
is not present,this.args.named
will contain the result of the thunk andthis.args.positional
will be empty.An object containing both named args and positional args
when an object is passed containing either keys:
named
orpositional
:this.args.named
will be the value of the result of the thunk'snamed
propertythis.args.positional
will be the value of the result of the thunk'spositional
propertyThis is the same shape of args used throughout Ember's Helpers, Modifiers, etc
For fine-grained reactivity
you may opt to use an object of thunks when you want individual properties to be reactive -- useful for when you don't need or want to cause whole-resource lifecycle events.
Inside a class-based [[Resource]], this will be received as the named args. then, you may invoke
named.foo()
to evaluate potentially tracked data and have automatic updating within your resource based on the source trackedness.