Skip to content

Proper state encapsulation in receivers #30

Description

@JUtupe

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions