HubSpot / draft-convert

Extensibly serialize & deserialize Draft.js ContentState with HTML.
Apache License 2.0
484 stars 94 forks source link

Copy arrays before sorting them #128

Open ctbarna opened 6 years ago

ctbarna commented 6 years ago

We're running into an issue using react-apollo where it marks its data structures as read only. Since sort() returns as well as mutates the array (even if there are no changes to order), the Apollo cache raises exceptions.

This change copies all the in-place arrays that sort operates on (leaving out the one instance of sort that proceeds a concatt). Since sort is being run on each function call, the behavior does not need to depend on the in-place mutation, and should act the same.

ctbarna commented 6 years ago

In trying to write tests for this, I realized that we might be running into this problem because we mock convertToRaw as a pass-through function because we're receiving the data in JSON format.

With that said, it's best practice to keep from accidentally mutating the raw data.