On Thu, Nov 27, 2014 at 10:40 AM, Allen Wirfs-Brock <[email protected]>
wrote:
>
> This is the end of my assumed inverted WC design and why I assert that a
> clear method is incompatible with it.
>
>
Couldn't this be solved by adding a little state (a monotonically
increasing 'generation' counter) to the WC? Then, something like this:
```js
WC.prototype.clear = function() { this.generation ++; }
WC.prototype.has = function(k) {
var slot = getWCSlot(k, this);
return slot && slot.generation == this.generation;
};
WC.prototype.set = function(k, v) {
var slot = getWCSlot(k, this);
if (slot) {
// update the slot's information (including generation)
slot.generation = this.generation;
slot.value = v;
}
else {
k[@@weakContainers][this] = { generation: this.generation, value: v };
}
};
WC.prototype.get = function(k) {
var slot = getWCSlot(k, this);
if (!slot) return undefined;
if (slot.generation != this.generation) {
// purge the key's slot for this weakmap
delete k[@@weakContainers][this];
return undefined;
}
return slot.value;
};
```
Then clear()'s description can be changed to (if it wasn't this already)
simply: "There is no way to retrieve values corresponding to keys added
prior to the clear()"
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss