useCachedPromise
Hook which wraps an asynchronous function or a function that returns a Promise and returns the AsyncState corresponding to the execution of the function.
It follows the stale-while-revalidate
cache invalidation strategy popularized by HTTP RFC 5861. useCachedPromise
first returns the data from cache (stale), then executes the promise (revalidate), and finally comes with the up-to-date data again.
The last value will be kept between command runs.
The value needs to be JSON serializable. The function is assumed to be constant (eg. changing it won't trigger a revalidation).
Signature
Arguments
fn
is an asynchronous function or a function that returns a Promise.args
is the array of arguments to pass to the function. Every time they change, the function will be executed again. You can omit the array if the function doesn't require any argument.
With a few options:
options.keepPreviousData
is a boolean to tell the hook to keep the previous results instead of returning the initial value if there aren't any in the cache for the new arguments. This is particularly useful when used for data for a List to avoid flickering. See Promise Argument dependent on List search text for more information.
Including the useCachedState's options:
options.initialData
is the initial value of the state if there aren't any in the Cache yet.
Including the usePromise's options:
options.abortable
is a reference to anAbortController
to cancel a previous call when triggering a new one.options.execute
is a boolean to indicate whether to actually execute the function or not. This is useful for cases where one of the function's arguments depends on something that might not be available right away (for example, depends on some user inputs). Because React requires every hook to be defined on the render, this flag enables you to define the hook right away but wait until you have all the arguments ready to execute the function.options.onError
is a function called when an execution fails. By default, it will log the error and show a generic failure toast with an action to retry.options.onData
is a function called when an execution succeeds.options.onWillExecute
is a function called when an execution will start.options.failureToastOptions
are the options to customize the title, message, and primary action of the failure toast.
Return
Returns an object with the AsyncState corresponding to the execution of the function as well as a couple of methods to manipulate it.
data
,error
,isLoading
- see AsyncState.revalidate
is a method to manually call the function with the same arguments again.mutate
is a method to wrap an asynchronous update and gives some control over how theuseCachedPromise
's data should be updated while the update is going through. By default, the data will be revalidated (eg. the function will be called again) after the update is done. See Mutation and Optimistic Updates for more information.
Example
Promise Argument dependent on List search text
By default, when an argument passed to the hook changes, the function will be executed again and the cache's value for those arguments will be returned immediately. This means that in the case of new arguments that haven't been used yet, the initial data will be returned.
This behaviour can cause some flickering (initial data -> fetched data -> arguments change -> initial data -> fetched data, etc.). To avoid that, we can set keepPreviousData
to true
and the hook will keep the latest fetched data if the cache is empty for the new arguments (initial data -> fetched data -> arguments change -> fetched data).
Mutation and Optimistic Updates
In an optimistic update, the UI behaves as though a change was successfully completed before receiving confirmation from the server that it was - it is being optimistic that it will eventually get the confirmation rather than an error. This allows for a more responsive user experience.
You can specify an optimisticUpdate
function to mutate the data in order to reflect the change introduced by the asynchronous update.
When doing so, you can specify a rollbackOnError
function to mutate back the data if the asynchronous update fails. If not specified, the data will be automatically rolled back to its previous value (before the optimistic update).
Pagination
When paginating, the hook will only cache the result of the first page.
The hook has built-in support for pagination. In order to enable pagination, fn
's type needs to change from
an asynchronous function or a function that returns a Promise
to
a function that returns an asynchronous function or a function that returns a Promise
In practice, this means going from
to
or, if your data source uses cursor-based pagination, you can return a cursor
alongside data
and hasMore
, and the cursor will be passed as an argument the next time the function gets called:
You'll notice that, in the second case, the hook returns an additional item: pagination
. This can be passed to Raycast's List
or Grid
components in order to enable pagination. Another thing to notice is that the async function receives a PaginationOptions argument, and returns a specific data format:
Every time the promise resolves, the hook needs to figure out if it should paginate further, or if it should stop, and it uses hasMore
for this. In addition to this, the hook also needs data
, and needs it to be an array, because internally it appends it to a list, thus making sure the data
that the hook returns always contains the data for all of the pages that have been loaded so far.
Full Example
Types
AsyncState
An object corresponding to the execution state of the function.
MutatePromise
A method to wrap an asynchronous update and gives some control about how the useCachedPromise
's data should be updated while the update is going through.
PaginationOptions
An object passed to a PaginatedPromise
, it has two properties:
page
: 0-indexed, this it's incremented every time the promise resolves, and is reset wheneverrevalidate()
is called.lastItem
: this is a copy of the last item in thedata
array from the last time the promise was executed. Provided for APIs that implement cursor-based pagination.cursor
: this is thecursor
property returned after the previous execution ofPaginatedPromise
. Useful when working with APIs that provide the next cursor explicitly.
Last updated