Comments (13)
We talked about adding a way to support opting in but it's very low on my priority list at the moment. 😃
If you wanted to make a Pull Request for that, though, I would definitely be open to it!
from seamless-immutable.
I'm likely going to try this approach this weekend, but there's some weirdness in that multiple arguments to Immutable() are considered an Array and I'd rather not break existing consumers. Another option I'm thinking about is a special method that does this on Immutable like isMutable, but that's also not ideal.
Because my options for implementation of this feature are subpar, what do you think about supporting the original intent of #25, but logging a warning when encountering a prototype in development? I'm guessing there are probably a lot of folks that rely on this not happening at the moment? If so, changing that behavior would be breaking, so this probably isn't feasible either, but thought I'd ask.
from seamless-immutable.
Yeah, I don't like the idea of breaking current invariants for this.
However, I could see adding a method like this:
// Takes an array of allowed prototypes, and returns a new version of `Immutable`
// which will preserve those prototypes when it encounters them.
Immutable.withPreservedPrototypes
Then you could just go:
var Immutable = require("seamless-immutable").withPreservedPrototypes([ ... ]);
from seamless-immutable.
My main issues with that approach are:
- If someone created a project specific version of seamless-immutable with this approach, they'd have to keep that list up to date
- If someone was encapsulating the creation of these objects, it gets tricky if another component creates other Immutable objects where these are nested children. (This is probably unavoidable)
How would you feel about something like:
// Similar to Immutable(), but during clone of objects, attaches a __immutable_preserve_prototype
// tag to the cloned object. The application of this tag is used elsewhere in the library to ensure
// the prototype is preserved on clones.
Immutable.withPreservedPrototype(obj) // could be better named
If the entire library recognized this tag and acted appropriately, you could use Immutable normally, wrap objects in arrays, and get the expected result. This handles #1, and mostly #2 (having to have factories to create Immutable objects of a specific type is required in either case).
Anyways, let me know what you think of that. I'm not completely sold on it, but feels easiest to consume and implement. Happy to hear out other options. Will try to implement one of these in the next day.
from seamless-immutable.
Hm...what would a code example look like with this in action?
from seamless-immutable.
Sorry for the late response. I just tried out something similar to what I'm thinking: virajsanghvi@5e2b065
Just wanted to be on the same page before cleaning this up/adding tests for PR. See the test for a general idea of how it would be used.
Basically the idea is a custom constructor method used explicitly for the purpose of preserving the prototype. We can use the invariant that all existing Immutable
objects have Object.prototype
to determine when we've encountered an object created from this custom constructor, and preserve its prototype during cloning.
There are three main points of contention that I can see:
- Is the performance implication of
Object.getPrototypeOf
ok in the common case? I imagine it just returns__proto__
, but not sure. - What is the relation of
ImmutableWithPrototype
toImmutable
? Should they share a common constructor, so you could pass Arrays, dates, etc toImmutableWithPrototype
, or should it just take in Objects and throw otherwise? (The current implementation of copying Immutable was just to try this out) - The name
ImmutableWithPrototype
kind of sucks- I'm open to suggestions.
Anyways, let me know what you think. Also, if you have a benchmark suite I could use, I'd like to see the overall perf implications of Object.getPrototypeOf
and Object.create
.
from seamless-immutable.
I think a cleaner approach would be:
- Have
Immutable
accept a second argument foroptions
, likemerge
does (this would be a breaking change, as currently it assumes if it gets multiple arguments you want an array; I'm fine with that breaking hange) - If it gets a second argument of
{prototype: foo
} then usefoo
as the prototype for the resulting object. - Add a method
instantiateEmptyObject()
(in the same way the other custom methods are added) and have it either return{}
if no prototype was specified, andObject.create(options.prototype)
if one was specified. - Have the various methods call
this.instantiateEmptyObject()
instead of just using{}
Thoughts?
from seamless-immutable.
I'm a fan. This was preferable, but I thought you were against changing the interface for Immutable per a previous comment (I may have described it unclearly). Anyways, I'll get this up in the next few days when I have a free hour.
from seamless-immutable.
To clarify, what I meant by not wanting to break current invariants is just that there'd be no chance any existing uses of Immutable
would suddenly "play by different rules" and possibly introduce surprising bugs.
I'm okay with a breaking API change, though, especially for something that I suspect is not used very often. 😃
from seamless-immutable.
Is this a priority? I would love to have this, could make a PR.
from seamless-immutable.
Forgot to link here, but submitted #51 a couple days ago which handles what we talked about above.
from seamless-immutable.
Thanks! I saw it but haven't had time to take a proper look because it's crunch time at work and I'm swamped.
I'll definitely try to take a look this week!
from seamless-immutable.
Closing as the functionality to support this is now available in 3.0.0
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.