Pullstate

Pullstate

  • Docs
  • GitHub

›Async Action Hooks

Getting Started

  • Installation
  • Quick example
  • Quick example (server rendering)

Reading Store State

  • useStoreState (hook)
  • <InjectStoreState>
  • Subscribe

Updating Store State

  • update()
  • Reactions

Async Actions

  • Introduction
  • Creating an Async Action
  • Use Async Actions
  • Async Action Hooks

    • Hooks overview
    • Post action hook
    • Short circuit hook
    • Cache break hook
  • Other Async Action Options
  • Cache clearing
  • Resolve async state on the server

Dev Tools

  • Redux Devtools

Cache break hook

The cache break hook has the following API:

cacheBreakHook({ args, result, stores, timeCached }) => true | false

As per all Async Action things, stores here is only available as an option if you are making use of <PullstateProvider> in your app (server-side rendering).

It should return true or false.

This action will only run if a cached result is found for this action (i.e. this action has completed already in the past). If you return true, this will "break" the currently cached value for this action. This action will now run again.

Be sure to check out the async hooks flow diagram to understand better where this hook fits in.

Example of a cache break hook

Deciding to not used the cached result from a search API when the search results are more than 30 seconds old

const THIRTY_SECONDS = 30 * 1000;

// The cache break hook in your action creator

cacheBreakHook: ({ result, timeCached }) =>
      !result.error && timeCached + THIRTY_SECONDS < Date.now(),

In this example want to break the cached result if it is not an error, and the timeCached is older than 30 seconds from Date.now(). timeCached is passed in, and is the millisecond epoch time of when our action last completed.

You can create customized caching techniques as you see fit. Here we simply check against timeCached. Potentially, you might want to check other variables set in your stores, something set on your response payload or even use one of the passed arguments to affect caching length.

Be sure to check out the section on cache clearing for other ways to deal with cache invalidation.

← Short circuit hookOther Async Action Options →
  • Example of a cache break hook
Pullstate
Docs
InstallationA Quick ExampleMore
Community
GitHub
Pullstate
Created by Paul Myburgh