A generic function type that represents the various formats a Thunk can be in.
The thunk is "just a function" that allows tracked data to be lazily consumed by the resource.
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?
when we want to manage reactivity separately from a calling context.
in many cases, the thunk is invoked during setup and update of various Resources,
so that the setup and update evaluations can "entangle" with any tracked properties
accessed within the thunk. This allows changes to those tracked properties to
cause the Resources to (re)update.
when an object is passed where the key named is not present,
this.args.named will contain the result of the thunk and this.args.positional
will be empty.
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.
() => ({ foo: () =>this.foo, bar: () =>this.bar, })
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.
classMyResourceextendsResource { modify(_, named) { this.named = named };
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.namedwill be empty andthis.args.positionalwill 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
namedis not present,this.args.namedwill contain the result of the thunk andthis.args.positionalwill be empty.An object containing both named args and positional args
when an object is passed containing either keys:
namedorpositional:this.args.namedwill be the value of the result of the thunk'snamedpropertythis.args.positionalwill be the value of the result of the thunk'spositionalpropertyThis 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.