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

Proper state encapsulation in receivers #30

Open
JUtupe opened this issue Dec 9, 2024 · 0 comments
Open

Proper state encapsulation in receivers #30

JUtupe opened this issue Dec 9, 2024 · 0 comments

Comments

@JUtupe
Copy link

JUtupe commented Dec 9, 2024

Is your feature request related to a problem? Please describe.
The StateManager returned by an EventReceiver's receiver provides methods to both get and set state. Currently, the value returned from getState function can be used to modify the state directly, which is likely unintended behavior. This issue might also exist in other parts of Eventrix.

// tasks.ts

const markAsDoneReceiver = new EventsReceiver(MARK_TASK_AS_DONE_EVENT_NAME, (eventName, { id }, stateManager) => {
    const tasks = stateManager.getState('tasks');
    const taskIndex = findIndex(tasks, (task) => task.id === id);
    const task = stateManager.getState(`tasks.${taskIndex}`);

    task.status = 'done';
});

Describe the solution you'd like
To prevent unintended state modifications, the getState function should always return a deep copy of the relevant part of the store. This ensures that any changes made to the returned state do not affect the underlying store. (maybe using https://lodash.com/docs/4.17.15#cloneDeep)

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

1 participant