-
Notifications
You must be signed in to change notification settings - Fork 342
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
Akita deep freeze is conflicting with moment.js date formatting logic (notably with mat-datepicker) #124
Comments
I don't recommend to store moment object in the store. Why you're not saving the raw date and transform it in the service/component? For example: |
Because I'm just putting the result of my API call into the store, which uses moment (I'm using ngswag typescript generator). Was hoping to just be able to use moment everywhere. I'm not sure there's much you can do about it your end - but I wanted to make sure this was at least a known 'issue' the people could find if they get the same error. It's a super confusing error, and had I not known that akita 'freezes' objects I may have never figured it out! Unless akita provided a way to intercept the 'freezing' I'm not sure what you could do, and at least for now just running the following code in my app initializer is sufficient to prevent this happening:
|
The freezing is a well-known technique in Redux, ngrx, ngxs, etc. It's to protect you from making mistakes and mutate your data. Thanks for adding this issue here. If I find a solution to your specific problem, I will update the issue. |
here's related issue at the moment.js side. |
@DmitryEfimenko You can disable the freezing protection if you're sure that no one will mutate the state. |
how do we disable freezing protection? I also ran into this issue when using @angular/material-moment-adapter with @angular/datepickermodule |
Always run the prod mode. |
Adds deepFreezeFunction in StoreConfigOptions and unit tests for feature Can use custom deepFreeze for complex objects in store Resolves salesforce#124
Note: if you're using ImmerJS in conjunction with Akita and you were circumventing this issue by always using production mode, note that version 8 of ImmerJS will always freeze by default. |
I'm submitting a...
Current behavior
When adding a
moment.js
date object to the store (in non production mode of course), the object gets frozen. This appears to be affecting the prototype (or some other global construct in moment), such that even a moment date object created completely outside of akita is broken when formatted.I discovered this issue using a
mat-datepicker
with minimal default configuration and assigned a moment date value to it. Important: This date object hadn't even come from akita store, but I have other moment objects stored in the store.In
moment.js
there is the following code which is part of the date formatting logicThe above code is reached when formatting the moment with a lowercase format string (which is one of the values for
defaultLongDateFormat
such as L, LL, LLL). This occured for me by simply assigning a moment instance to amat-datepicker
which uses a lowercasel
value by default. So in this instance the moment.js code above haskey == "l"
- and it then runs the regex on the format string forL
and tries to assign it back onto_longDateFormat
as a new key for future use.The above line of code fails because
Object.isFrozen(this._longDateFormat) == true
and gives the extremely cryptic messageERROR TypeError: Cannot add property l, object is not extensible
.Expected behavior
I would expect to be able to put a moment object in the store and it not to break moment for use elsewhere :-)
Minimal reproduction of the problem with instructions
I can come back to create a demo later if needed if the explanation above doesn't suffice.
What is the motivation / use case for changing the behavior?
Need to be able to put a moment date object in the store and it not break.
Environment
Thanks
The text was updated successfully, but these errors were encountered: