Akita: persistState() clearStorage() code example from the docs is unclear

Created on 31 Jan 2020  路  9Comments  路  Source: datorama/akita

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?

Most helpful comment

Thank you very much, for extraordinary tool called Akita and for a swift support :)

All 9 comments

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.

image

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stephencarr picture stephencarr  路  4Comments

NetanelBasal picture NetanelBasal  路  6Comments

hoisel picture hoisel  路  5Comments

NathanAlcantara picture NathanAlcantara  路  4Comments

DanielNetzer picture DanielNetzer  路  5Comments