Closed
Description
I think the latest changes to Hash.js
introduce a weird bug when hashing custom objects, more specifically, a class with valueOf()
overridden. The error being thrown is Uncaught TypeError: Invalid value used as weak map key
.
It's strange because it doesn't happen until a certain point, a very specific point. It happens when invoking .set()
on a Map
on the 9th attempt. For the first 8 attempts, all is good. On the 9th attempt, error.
To reproduce, I ran this bit of code in the console at https://facebook.github.io/immutable-js.
class Example {
constructor(id, key) { this.id = id; this.key = key; }
valueOf() { return `${this.id}.${this.key}`; }
toString() { return `${this.id}.${this.key}`; }
}
const m = Immutable.Map().asMutable();
m.set(new Example('test', (new Date()).toISOString()), Math.random()); // 1, ok
...
m.set(new Example('test', (new Date()).toISOString()), Math.random()); // 8, ok
m.set(new Example('test', (new Date()).toISOString()), Math.random()); // 9, ERROR
This has worked for me for quite a while. It breaks with release v4.0.0-rc.11
.
Here's a screenshot of the console: