Snapshot Manager
There are times when saving the local state in the server becomes useful. For example, you may want to give the user a PDF representing their local state.
For such cases, Akita provides the snapshotManager
API.
To get a snapshot of the whole application state, you can call the getStoresSnapshot()
method:
todos.service.ts
import { snapshotManager } from '@datorama/akita';
export class TodosService {
saveState() {
this.http.post('/url', snapshotManager.getStoresSnapshot());
}
}
The getStoresSnapshot()
returns an object containing the whole application state. For example:
{
"todos": {
"entities": { ... }
},
"cart": {
"entities": { ... }
},
"session": {
"firstName": "",
...
}
}
If you don't need the whole application state, you can pass the specific stores that you need:
todos.service.ts
import { snapshotManager } from '@datorama/akita';
export class TodosService {
saveState() {
const stores = ['todos', 'widgets'];
this.http.post('/url', snapshotManager.getStoresSnapshot(stores));
}
}
It also works the other way around, when you get the snapshot from the server you can save it by calling the setStoresSnapshot()
method and passing the snapshot:
todos.service.ts
import { snapshotManager } from '@datorama/akita';
export class TodosService {
setSnapshotFromServer(snapshotFromServer) {
snapshotManager.setStoresSnapshot(snapshotFromServer);
// Support lazy stores
snapshotManager.setStoresSnapshot(snapshotFromServer, { lazy: true } );
}
}