Skip to content
New issue

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

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

Already on GitHub? # to your account

Would supporting batchStart and batchEnd methods to save multiple modifications as a single history snapshot be a good solution? #15

Open
tolerance-go opened this issue Jun 28, 2024 · 1 comment

Comments

@tolerance-go
Copy link

I am encountering issues with managing historical states. I want to merge several modifications into a single operation history. My solution is to support batchStart and batchEnd methods to save multiple modifications as a single history snapshot. What do you all think?

// replace the following line
// import { proxyWithHistory } from 'valtio/utils';

import { proxyWithHistory } from 'valtio-history';
import { useSnapshot } from 'valtio';

const state = proxyWithHistory({ count: 0 });
console.log(state.value); // ---> { count: 0 }
state.batchStart();
state.value.count += 1;
state.value.count += 1;
state.batchEnd();
console.log(state.value); // ---> { count: 2 }
state.undo();
console.log(state.value); // ---> { count: 0 }
state.redo();
console.log(state.value); // ---> { count: 2 }

// React example
export default function App() {
  const {
    value,
    undo,
    redo,
    history,
    isUndoEnabled,
    isRedoEnabled,
    currentChangeDate,
    remove,
    replace,
} = useSnapshot(state);

  ...
}
@lwhiteley
Copy link
Collaborator

lwhiteley commented Jun 28, 2024

hi @tolerance-go ,

can you please give a reproduction of your issue.

As far as i know, it is possible to batch changes already once all the changes happen in the same scope.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants