WeakMaps question ?

Irakli Gozalishvili rfobic at gmail.com
Fri Nov 11 15:28:08 PST 2011


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 (http://goo.gl/maps/3CHu)


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 (http://goo.gl/maps/3CHu)
> 

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


More information about the es-discuss mailing list