WeakMaps question ?

Oliver Hunt oliver at apple.com
Fri Nov 11 15:39:07 PST 2011


A weak map can only remove an entry if both the key and value have died, in many ES implementations a number of the primitives are not gc allocated and so can never die, or are cached globally so have lifetime unrelated to any given program.

The net effect of allowing such primitive values to be used as keys would be to make the map "strong" and the associated values would not be able to be collected.

--Oliver

On Nov 11, 2011, at 3:28 PM, Irakli Gozalishvili wrote:

> I really need to know why WeakMaps don't accept primitives as keys, can anyone please reply ?
> 
> Thanks!
> --
> Irakli Gozalishvili
> Web: http://www.jeditoolkit.com/
> Address: 29 Rue Saint-Georges, 75009 Paris, France
> 
> On Thursday, 2011-11-10 at 10:31 , Irakli Gozalishvili wrote:
> 
>> Hi,
>> 
>> I was wondering what is a reason for disallowing non-object values as keys in WeakMaps ?
>> 
>> Also, I think I have a pretty good use case for primitive keys:
>> 
>> Worker process sends messages to the (main) UI process, to initiate UI notification. If user clicks that notification main process will worker know that given notification was clicked:
>> 
>> (function(exports) {
>> 
>> let id = 0
>> let listeners = WeakMap()
>> 
>> exports.notify = function notify({ onClick, message }) {
>>   if (onClick) listeners.set(++id, onClick)
>>   self.postMessage({ id: id, message: message })
>> }
>> 
>> self.onmessage = function({ id }) {
>>   let listener = listeners.get(id)
>>   if (listener) listener()
>> }
>> 
>> })(this)
>> 
>> Is my use case invalid ? At the moment I use custom map implementation with a limited size, once map hits the limit oldest entries will be removed. 
>> 
>> Regards
>> --
>> Irakli Gozalishvili
>> Web: http://www.jeditoolkit.com/
>> Address: 29 Rue Saint-Georges, 75009 Paris, France
>> 
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111111/3048ab00/attachment.html>


More information about the es-discuss mailing list