mutative icon indicating copy to clipboard operation
mutative copied to clipboard

Is it possible to use class instances with create?

Open fakalit opened this issue 1 year ago • 1 comments

Feeding class instances to create function causes the following Error, unless they are wrapped with a primitive like it's done in the class benchmark example.

Error: Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable

Is this by design? What is the recommended usage pattern for mutating class instances?

fakalit avatar Jul 17 '24 17:07 fakalit

As mentioned in the error message, you can use the mark option function to mark immutable class instances. For example,

class Foobar {
  foo = {
    bar: 'str',
  };

  change() {
    this.foo.bar = 'new str';
  }
}

const data = {
  foo: {
    bar: 'str',
  },
  foobar: new Foobar(),
};

const state = create(
  data,
  (draft) => {
    draft.foo.bar = 'new str';
    draft.foobar.change();
  },
  {
    mark: (target, { immutable }) => {
      if (target instanceof Foobar) return immutable;
    },
  }
);
const foobar = new Foobar();
foobar.foo.bar = 'new str';
expect(state).toEqual({ foo: { bar: 'new str' }, foobar });
expect(state).not.toBe(data);
expect(state.foo).not.toBe(data.foo);
expect(state.foobar).not.toBe(data.foobar);
expect(state.foobar.foo).not.toBe(data.foobar.foo);

unadlib avatar Jul 18 '24 03:07 unadlib