Akita: Using global 'resettable' config option causes EntityStore to be corrupted on reset()

Created on 3 Apr 2019  路  1Comment  路  Source: datorama/akita

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[X] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:

Current behavior

The global resettable option can cause problems with EntityStore related to its initialState.

Here's the steps how I came to this problem:

  • It was finally time for me to use the persistState plugin in my application! So I installed that :-)
  • I wanted to clear out my state (shopping cart items) so I tried to run cartItemsStore.reset()
  • This gave me an error telling me I needed to enable resettable option. That's great!
  • I added the configuration below - to be honest I wasn't sure where I was supposed to put it.
    akitaConfig({
         resettable: true
     });
  • I then started getting null reference errors.

Why?

  • If you configure stores globally to be resettable after a particular store has been created then that particular store does not have an initial state to be reset with.
  • For an EntityStore this default initial state is critical because it contains { ids:[], entities: {} }.
  • Running reset therefore completely wiped out my EntityStore to {}, hence the error when trying to access ids property.

Expected behavior

  • It should be clearer in the docs where akitaConfig config should be placed in my application in order to prevent this. With complex dependency injection it can't just go in app.component.ts constructor because stores may already be created.
  • If you try to reset a store with no initialState it should give an error pointing you in the right direction.
  • Perhaps an EntityStore should be smart enough to reset itself with set({ids: [], entities: {}}) if there is no _initialState available?

Workaround

Just set resettable on the individual store configuration - so it will have an initial state.

What is the motivation / use case for changing the behavior?

App crashed in an unexpected way due to null reference error.

Environment

All

invalid

Most helpful comment

You can't call akitaConfig() after you created the stores. You should call it in the main.ts.

The second option is to add the resettable property in the store you need it:

@StoreConfig({ name: 'cart', resettable: true })
export class CartStore extends EntityStore<CartState, CartItem> {
  constructor() {
    super();
  }
}

>All comments

You can't call akitaConfig() after you created the stores. You should call it in the main.ts.

The second option is to add the resettable property in the store you need it:

@StoreConfig({ name: 'cart', resettable: true })
export class CartStore extends EntityStore<CartState, CartItem> {
  constructor() {
    super();
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

amr-alamir picture amr-alamir  路  6Comments

johanrin picture johanrin  路  7Comments

DanielNetzer picture DanielNetzer  路  5Comments

RonnieRocket147 picture RonnieRocket147  路  5Comments

stherrienaspnet picture stherrienaspnet  路  3Comments