Skip to content

Plugin reverts last changes on other states #67

@eugenzor

Description

@eugenzor

Let's imagine we have 2 states: A and B

interface TestModel {
  conter: number;
}

@State<TestModel>({
  name: 'a',
  defaults: {
    counter: 0,
  },
})
export class AState {}

@State<TestModel>({
  name: 'b',
  defaults: {
    counter: 0,
  },
})
export class BState {}

The app is configured to save the BState changes via HTTP on the backend

    NgxsAsyncStoragePluginModule.forRoot(
      MyEngine,
      {
        key: 'b',
      }
    ),

Let's imagine it takes 1 second to pull data from the backend.

After the initialization we have next state tree:

{
  a: {
    counter: 0
  },
  b: {
    counter: 0
  }
}

Then we start pulling the B state which has the counter value 1 on the backend.

While B state is pulling we have updated several times the state A:

{
  a: {
    counter: 3
  },
  b: {
    counter: 0
  }
}

Then B state data received and plugin updates the state. And it reverts our changes from state A:

{
  a: {
    counter: 0
  },
  b: {
    counter: 1
  }
}

That is because of:

nextState = setValue(previousState, key, val);

where 'previousState' is the state that was at the moment of pulling, not the latest one.

The expected behavior is to update the B state only:

{
  a: {
    counter: 3
  },
  b: {
    counter: 1
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions