amplify
amplify copied to clipboard
Modifying amplify.request return value also affects cached value
Not sure if this is a bug, or more of a feature request, but I found this to be very unexpected behavior so I think it warrants documentation at the
I have a cacheable amplify request. It contains an array. In processing this array, I go through by arr.shift() and processing each value as it comes out.
When I call the request again, it pulls from the cache a response with an empty array -- because the array I was modifying was apparently the same one it stored.
I understand this could have some roadblocks to a complete fix -- amplify would have to deep copy the response, probably -- but it would be really helpful. At the very least this should be documented somewhere, since I was not expecting this and it was difficult to debug.
I want to leave this open to be address in terms of a fix, but just wanted to provide a work around for you:
http://jsfiddle.net/5VMZc/1/
That shows it breaking. If you uncomment the overwrite to the custom cache, you will see it works. This will be the easiest way to include a fix for you vs. having to handle it on the receiving end of all your requests.
Thanks for the idea -- didn't even realize I could overwrite the caching function. I'm guessing for an actual solution, it would be easiest to just pull the direct responseText and parse that out as the cache to avoid having to implement a deep copy in the library. Or would just JSON stringify/parse work in this case?
@cstigler It will probably depend on the browser speed. I am not sure which is faster JSON stringify/parse vs. deepClone. It might be close.