if I have this code inside main.ts:
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableAkitaProdMode, persistState } from '@datorama/akita';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
enableAkitaProdMode();
}
persistState();
platformBrowserDynamic()
.bootstrapModule(AppModule)
.catch(err => console.log(err));
Where does this code go:
export const storage = persistState();
storage.destroy(); // Stop sync the state
storage.clearStore(); // Clear the storage
storage.clearStore('todos'); // Clear the todos store from storage
Should export const storage = persistState(); be set somewhere globally or what?
Should persistState() only be called once, inside main.ts?
Do you have some working example of using the state persistence and clearing?
It doesn't matter. You can expose it from main.ts or from the AppComponent for example:
export class AppComponent {
ngOnInit() {
this.persistState = persistState({ ... });
}
}
But my question is, should persistState() be only called once in the app? If so, how do you access it inside some service? I have many services which should have the ability to access persistState() in order to clear some localStorage values. Does clearStore() clear both store and localStorage values?
Yes, it should be called once per application. You can expose it from the main.ts file.
export const storage = persistState();
And access it in your services:
import { storage } from 'path/to/main';
@Injectable({ providedIn: 'root' })
export class TodosService {
constructor(private todosStore: TodosStore) {
}
clear() {
storage.clearStore('todos');
}
}
No, it only clears the storage value.
You can also use DI.
const storage = persistState(...);
platformBrowserDynamic([{ provide: 'persistStorage', useValue: storage }])
.bootstrapModule(AppModule)
.catch(err => console.log(err));
export class TodosService {
constructor(@Inject('persistStorage') persistStorage) {
}
}
Thank you very much, for extraordinary tool called Akita and for a swift support :)
I used DI, like you suggested, but have an issue though, clearStore() doesn't clear storage values at all :(. The values are still there, in the storage.

Works for me. Make sure you're not updating the store again. It clears the storage once. If you update the store immediately, you'll see these values.
I have the same issue, clearStore is not saving to persistState at all.
Could it be due to?
const storage = persistState({
preStorageUpdateOperator: () => auditTime(AKITA_STORAGE_UPDATE_AUDIT_TIME),
I am using indexedDb
clearStorage updates only the storage.
Most helpful comment
Thank you very much, for extraordinary tool called Akita and for a swift support :)