Skip to content

bucket.set mutates irrelevant state key in the valueStream #18

@DmitryEfimenko

Description

@DmitryEfimenko

Google search terms

"@extend-chrome/storage" bucket.set mutates irrelevant state key in the valueStream

Describe the bug

When using bucket.set method, and updating a particular property, the bucket.valueStream emits a mutated version of the whole state so referential equality does not work. See example:

interface Person {
  name: string;
}

interface Address {
  country: string;
}

interface State {
  person: Person;
  address: Address;
}

const bucket = getBucket<State>('state');

const person$ = bucket.valueStream.pipe(
  map(x => x.person),
  distinctUntilChanged()
);

const address$ = bucket.valueStream.pipe(
  map(x => x.address),
  distinctUntilChanged()
);

person$.subscribe(x => { console.log('person changed', x) });
address$.subscribe(x => { console.log('address changed', x) });

function updateAddress(country: string) {
  bucket.set(prev => {
    const updatedAddress = { ...prev.address, country };
    const updatedState = { ...prev, address: updatedAddress };
    return updatedState;
  })
}

updateAddress('France');
// observe console also emitting "person changed"

How do we reproduce?

Use the code from the example above. Unfortunately, StackBlitz does not work with this lib

Expected behavior

The state property that was not updated via the set method should not get mutated

Actual behavior

A state property that I didn't explicitly update got mutated.

Screenshots

Please complete the following information:

  • Library Version: 1.5.0
  • Operating System: Windows
  • Browser: Chrome 97
  • Node Version: 17

Additional context

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