Comments (7)
This is by design. One of the invariants of the library is that once you invoke Immutable
, what you get back is immutable, and continuing to use its methods will only yield further immutable results.
However, since you can pass seamless-immutable
's data structures to other JS libraries, if you really want a mutable copy back, you can just pass them to other libraries that are not designed for immutability.
For example:
_.map(Immutable([1,2,3]), function() {
return React.createElement( "div", null );
});
// no error
from seamless-immutable.
map
does indeed return a collection of elements which may be completely unrelated to the original elements. However, map
is still supposed to return the same type of collection you began with. If you call [].map
you get back an Array, if you call $().map
you get back a jQuery object, and if you call Immutable([]).map
, you get back a seamless-immutable
array. This is the standard map
behavior.
I sympathize with the fact that this map
implementation doesn't work with React components, because I use seamless-immutable
with React and find it annoying that this doesn't Just Work. I manage this annoyance by using _.map
when it finally comes time to turn my seamless-immutable
arrays into React components, but naturally it would be less trouble in this particular use case if I didn't have to.
That said, I don't think it makes sense to couple this library to React, and I don't see a strong case for changing map
on its own merits. When libraries start developing implicit dependencies on other libraries, unexpected consequences tend to snowball into unpleasant surprises. I value the simplicity and consistency of this library, and hope to maintain it.
As such, I appreciate your perspective, but the current design still makes the most sense to me overall. 😃
from seamless-immutable.
I don't think this makes much sense for the map function.
For the other functions I can understand this rational, because they return parts of the array itself, which are guaranteed to be immutable, but map can return things not related to the immutable structure. To me, this is very unexpected behaviour.
You are also suggesting using a non-standard map implementation for a library that has 'backwards-compatible' in the first line of its summary.
from seamless-immutable.
Agree with @rtfeldman here.
To quote the description of Array.map:
The map() method creates a new array with the results of calling a provided function on every element in this array.
Well... now make it for Immutables:
The map() method creates a new Immutable with the results of calling a provided function on every element in this Immutable.
This is in fact the only logical thing to do - you're operating on a specific type and returning a similar but different type would be... a bit wacky to say the least.
from seamless-immutable.
from seamless-immutable.
I'd like to reopen this discussion in the light that nowadays seamless-immutable
treats functions, errors, dates, and React components as immutable even though they are not in order to make the library play nicely with the rest of the code out there.
Here's a problem I ran into yesterday when using seamless-immutable
together with Draft.js:
const editorState = Immutable( { content: Draft.convertToRaw( ContentState.createFromText( text ) ) } );
...
const state = Draft.convertFromRaw( editorState.content );
// ^^^^^^^^^^^^^^^^^^^^
// TypeError: block.getKey is not a function
// at node_modules/draft-js/lib/BlockMapBuilder.js:24:21
// at Array.map (native)
// at Array.<anonymous> (node_modules/seamless-immutable/src/seamless-immutable.js:132:38)
// at Object.createFromArray (node_modules/draft-js/lib/BlockMapBuilder.js:22:30)
// at Function.createFromBlockArray (node_modules/draft-js/lib/ContentState.js:163:36)
// at convertFromRawToDraftState (node_modules/draft-js/lib/convertFromRawToDraftState.js:89:23)
The reason the above fails is that Draft.js' createFromBlockArray
uses .map
to convert an array of raw objects into an array of prototype-based objects, which seamless-immutable
promptly strips down to raw immutable data, no methods, thus block.getKey is not a function
.
I worked around it by running editorState.content
through Immutable.asMutable( ..., { deep: true } )
, but this was thoroughly unexpected and IMHO incompatible with the library's promise of "data structures which are backwards-compatible with normal Arrays and Objects."
I think it makes a lot of sense to make plain-old-js-objects returned from .map
immutable, but we've gotta pass everything else through as-is.
Thoughts?
from seamless-immutable.
@agurtovoy I'm having the same issue with react-select
, the only way to solve it was by using .asMutable
as well.
My team and I are starting to study alternatives, Immer looks really nice and I want to give it a try.
from seamless-immutable.
Related Issues (20)
- Make `without` support removing item from an array HOT 4
- how to use splice like array HOT 1
- Internet Explorer tests are failing HOT 4
- Issue regarding null values HOT 5
- Why are IE tests failing? HOT 3
- How to apply splice with seamless-immutable? HOT 1
- Merge does not work with class instances
- __immutable_invariants_hold is enumerable on Edge HOT 1
- Garbage collector features? HOT 1
- Performance characteristics
- Any difference with deepCopy? HOT 1
- [types] Mixin not applied to nested objects HOT 1
- flatMap not working HOT 2
- Add documentation for merge mode
- Files not being replaced on a deep merge
- Fails on IE 11 HOT 1
- TypeError: Object(...) is not a function HOT 1
- Lodash Vulnerability Problem
- Is the project still maintained? HOT 2
- immutable object causes rapid memory growth HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from seamless-immutable.