-
Notifications
You must be signed in to change notification settings - Fork 10
Description
So in our application we use this immer-adapter to help make our state immutable.
However, we recently ran into an issue where our production build breaks our apps.
It seems that everything is a lot stricter in production builds than in development build, which pointed us to a bunch of errors when using the getState method.
The getState method returns an immer draft, instead of, what we expected, a frozen instance of our state.
As you can see at this line:
https://github.com/ngxs-labs/immer-adapter/blob/master/src/lib/core/immer-adapter/common/immutable-state-context.ts#L23
It just creates a draft, casts it to the provided interface, calls it frozen and then returns it.
We tried to recreate this issue in an isolated project, and the results were the same.
So given an action like this, that finds an object (Dossier) in an array (dossiers) from the state, based on the dossierNumber:
@Action(DossiersActions.DeleteDossierByNumber)
@ImmutableContext()
public deleteDossierByNummer({ getState, setState }: StateContext<DossiersStateModel>, { dossierNumber }: DossiersActions.DeleteDossierByNumber): void {
const found = getState().dossiers.find(d => d.dossierNumber === dossierNumber);
console.log('found', found);
if (!found) {
throw new Error(`Could not find existing dossier to delete, for nummer ${dossierNumber}`);
}
setState((draft: DossiersStateModel) => {
console.log('draft', draft)
delete draft.dossiers[draft.dossiers.indexOf(found)];
draft.dossiers = draft.dossiers.filter(dossier => !!dossier);
return draft;
});
}
We will receive an output like this, and the state would not have been modified (as the found object is nog present in the dossiers list):
found Proxy { ... }
draft Proxy { ... }
However, we would expect something like this (along with the found object to be removed from the dossiers list):
found Dossier { ... }
draft Proxy { ... }
If a working example is required, please do tell and I will try to provide a stack blitz as soon as possible.