How to handle ngrx/store with offline data?

Viewed 890

I'm new to Redux and started with ngrx. I'm unable to understand the concept of the store and his purpose.

Say I have book store application. I can buy or sell books to users.

I using ngrx to fetch the books list I have using this action: LOAD_BOOKS, then I using effects to load from http the list of books and save it back to the store.

When I entered to /books, before loading the component I have guard. in this guard I check if the book list is in the store (if not I send LOAD_BOOKS action) and after the list is loaded I render the component that display all the books (from the store)

next time when I entered to /books again, the list is in the Store.

So I understand that I do not need to call my http to bring me this list because I have this list in the store.

But, what happened if somebody else buy one of the books? my list of available books from the store is not updated.

I never got to update because it is already in the store.

So my question is how should I handle this scenario? what to store and what not? what is the purpose of this store if not to store data. the problem is what to do when the data change inside the server? I working on "offline" data

1 Answers

So in first case its common problem and not only refering to store that if server data change, the client isn't automatically update due to using http, for that you should use broadcasting with for example websocket.

Purpose of store is to change application architecture so it has single source of truth if multiple page components not related as parent->child are requiring same data you connect them with store and every time store data changes u get your component data updated aswell. So if you imagine you can't reproduce same thing using simple service if you have many components on page with diffrent relationship it's difficult to tie them together and when data updates in one of distrubute for rest of them and might end up buggy.

Store isn't providing you magic way to make work offline with server, when can't connect to server you simply remind user that he can't get data or perform action as you would do with working with http service.

For more information I encourage you to read about flux architecture as redux and ngrx are both based on it. I think it will make clear about benefits that store offers and in which type of applications it should be implemented.

Related