Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modification inside create is lost and differs from immer #18

Closed
Gongreg opened this issue Nov 6, 2023 · 3 comments
Closed

Modification inside create is lost and differs from immer #18

Gongreg opened this issue Nov 6, 2023 · 3 comments

Comments

@Gongreg
Copy link

Gongreg commented Nov 6, 2023

Hello.
I've noticed part of user written code that used to work with immer does not work with mutative. Here is code snippet:

import { create } from 'mutative';
import { produce } from 'immer';

const baseState = {
  array: [
    {
     one: {
       two: 3,
     },
    }
   ]
};


const created = create(baseState, (draft) => {
  draft.array[0].one.two = 2

  draft.array = [draft.array[0]]
});

created.array[0].one.two // 3

const produced = produce(baseState, (draft) => {
  draft.array[0].one.two = 2

  draft.array = [draft.array[0]]
});

produced.array[0].one.two // 2

Curious to hear is this expected?

@unadlib
Copy link
Owner

unadlib commented Nov 7, 2023

hi @Gongreg , it's an issue with the finalization draft process, and I am fixing it.

@unadlib
Copy link
Owner

unadlib commented Nov 8, 2023

hi @Gongreg , I've fixed the issue and I've released the latest version 0.7.0.

Immer has the following issue.

import { produce } from 'immer';
import { create } from 'mutative';

const baseState = {
  array: [
    {
      one: {
        two: {
          three: 3,
        },
      },
    },
  ],
};

const created = create(baseState, (draft) => {
  draft.array[0].one.two.three = 2;
  const two = draft.array[0].one.two;
  const one = new Set();
  draft.array = [{ one }];
  one.add(two);
  expect(Array.from(draft.array[0].one)[0].three).toBe(2);
});
expect(Array.from(created.array[0].one)[0].three).toBe(2); // it's ok

const produced = produce(baseState, (draft: any) => {
  draft.array[0].one.two.three = 2;
  const two = draft.array[0].one.two;
  const one = new Set();
  draft.array = [{ one }];
  one.add(two);
  expect(Array.from(draft.array[0].one)[0].three).toBe(2);
});

// throw error
expect(() => Array.from(produced.array[0].one)[0].three).toThrowError(
  `Cannot perform 'get' on a proxy that has been revoked`
);

Thank you so much for reporting this issue.

@Gongreg
Copy link
Author

Gongreg commented Nov 9, 2023

Thanks for fixing!

@Gongreg Gongreg closed this as completed Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants