Implementing the optimistic update pattern
The optimistic update pattern is a user interface design strategy that enhances user experience by immediately reflecting changes made by the user, even before those changes are confirmed by the server. This approach creates a sense of responsiveness and fluidity, reducing the perception of lag and improving overall user satisfaction. This pattern can come in handy, especially in situations when we go offline.
How to do it…
In this recipe, we are going to implement a custom operator called optimisticUpdate
, which will handle a POST
request in a way that we immediately show the result to the user, then in the background continue with the network request. If we are offline and the request fails, we will do a retry after a certain period of time. If the request fails the second time, then we will provide a rollback option, which in most cases would be that we remove the item from the list that we added optimistically.