Patterns and Caching

Caching

RunQuery and observer patterns have been created to simplify cache interaction. However, the use-cases are quite different.

RunQuery vs Observer

runQuery can be used as a wrapper around an action to enable offline-first functionality. For example,

With the example above, if the message has been fetched before and has not expired, the getMessage will acquire the data from the cache without fetching from the server.

On the other hand, an observer is a function to get and observe any changes to an object of interest. For example,

In this case, the observer will first internally call runQuery and actively listen to any changes to the message. When the observer picks up the event, it will call the callback with a QueryResult as the parameter. When it comes to events, there are two main sources of events: real-time events and internally generated events.

  1. If the module has real-time events, an observer will receive the changes and call the callback to apply the changes.

  2. A mutator will emit an event for observers. For example, when updateMessage executes, it will emit v3.message.didUpdate, which observeMessage will pick up on.

Please refer to RunQuery and Observer for greater details.

QueryResult

Both runQuery and observer call their callbacks with queryResult as a returned result. The object contains data (the object of interest), loading, and error, all of which can be optional. If the result contains paging information, it will have nextPage and prevPage also.

The type definition is simplified from the actual implementation to make it more easily understandable.

Last updated