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

Proposal: Support draft function return value with modified draft #27

Open
unadlib opened this issue Jan 12, 2024 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@unadlib
Copy link
Owner

unadlib commented Jan 12, 2024

Regarding return values, Mutative has the same behavior as Immer.

An draft function returned a new value and modified its draft. Either return a new value or modify the draft.

For example,

  expect(() => {
    const state = create({ a: 1 }, (draft) => {
      draft.a = 2;
      return {
        ...draft,
      };
    });
  }).toThrowError();

However, there is an irrational aspect to this approach. As long as a modified draft can be finalized, it should be allowed to return any value.

Therefore, we are considering allowing Mutative to support draft functions returning any value.

@unadlib unadlib added the enhancement New feature or request label Jan 12, 2024
@unadlib
Copy link
Owner Author

unadlib commented Jan 12, 2024

Here are some examples, such as updating an array (filter out items where the name is not 'Test', and set the 'done' value to true for each of the remaining items):

const todoList = [{ name: 'Test' }, { name: 'Learn' }, { name: 'Test' }];
// This will throw the error
const state = create(todoList, (draft) =>
  draft.filter((item) => {
    if (item.name === 'Test') {
      item.done = true;
      return true;
    }
  })
);

We have to write the array update in a similar way.

const todoList = [{ name: 'Test' }, { name: 'Learn' }, { name: 'Test' }];
const state = create(todoList, (draft) => {
  const length = draft.length;
  draft.forEach((item) => {
    if (item.name !== 'Test') return;
    item.done = true;
    draft.push(item);
  });
  draft.splice(0, length);
});

There may also be implicit returns.

// If this feature is supported, it means that such implicit returns with will not throw an error.
const state = create({ a: { b: 1 } }, (draft) => draft.a.b = 2);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant