Skip to content

Inconsistencies between observablemap and observableset #2336

@melnikov-s

Description

@melnikov-s

Intended outcome: observable set and observable map should have the similar behaviour in terms of when reactions are triggered.

Actual outcome: observable map has an internal map of observable values which are updated and changed as needed, therefore if a single value is retrieved from a map within a reaction, that reaction will only care about that single value, any unrelated mutation to the map will not re-trigger the reaction. Whereas in an observable set there's just one atom that manages all changes, so if a reaction only cares about whether or not a single value is in a set any unrelated mutation to that set will still trigger the reaction.

How to reproduce the issue::
https://codesandbox.io/s/mobx-mapset-behavior-t8bjg

import { observable, autorun } from "mobx";

const map = observable(new Map());

autorun(() => {
  if (map.has("foo")) {
    console.log("map: its there!");
  } else {
    console.log("map: nope");
  }
});

map.set("bar", true); // won't run
map.set("foo", true); // will run
map.delete("bar"); // won't run
map.delete("foo"); // will run

const set = observable(new Set());

autorun(() => {
  if (set.has("foo")) {
    console.log("set: its there!");
  } else {
    console.log("set: nope");
  }
});

set.add("bar"); // will run
set.add("foo"); // will run
set.delete("bar"); // will run
set.delete("foo"); // will run

Versions All versions that have set/map.

Note: can contribute PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions