Feature-Request: allow to iterate over WeakSet / WeakMap
Bradley Meck
bradley.meck at gmail.com
Wed Jul 13 13:14:32 UTC 2016
This thread from 2011 might help understand why they should not be
enumerable (it links to another thread as well inside).
https://mail.mozilla.org/pipermail/es-discuss/2011-April/013604.html
On Wed, Jul 13, 2016 at 8:08 AM, Isiah Meadows <isiahmeadows at gmail.com>
wrote:
> Could you explain a real world use case for this? I recall some that it
> would've simplified some, but the best implementation often didn't even
> require iteration, but just changing the data layout altogether.
>
> On Wed, Jul 13, 2016, 08:13 Michael Kriegel <
> michael.kriegel at actifsource.com> wrote:
>
>> Hi everyone,
>>
>> not sure, whether this is the right place for discussing a feature
>> request - and how to find out, whether this was already
>> proposed/discussed before and with which result... My google search did
>> not bring up anything about it.
>>
>> On my opinion it should be made possible to iterate over all elements in
>> a WeakSet & WeakMap. I already found many cases, in which I'd like to
>> have used the neat feature of "garbage collect the object, if it is not
>> referred to by others anymore", but with the necessity to know, which
>> objects are in the set (not only, if a given object is). An example:
>>
>> Registering callbacks to objects:
>>
>> class Foo {
>> onBla(Handler) {
>> this.BlaSuperWeakMap.push(Handler);
>> }
>>
>> someOtherMethod() {
>> for (let X in this.BlaSuperWeakMap) {
>> X();
>> }
>> }
>>
>> constructor() {
>> this.BlaSuperWeakMap = new SuperWeakMap();
>> }
>> }
>>
>> const MyFoo = new Foo();
>> {
>> let MyHandler1 = function(){
>> console.log('MyHandler1 called');
>> };
>> MyFoo.onBla(MyHandler1);
>> console.log('Invokation1:');
>> MyFoo.someOtherMethod();
>> }
>> let MyHandler2 = function(){
>> console.log('MyHandler2 called');
>> };
>> MyFoo.onBla(MyHandler2);
>> MyFoo.onBla(function(){
>> console.log('MyHandler3 called');
>> });
>> console.log('Invokation2:');
>> MyFoo.someOtherMethod();
>> MyHandler2 = undefined;
>> console.log('Invokation3:');
>> MyFoo.someOtherMethod();
>>
>> Expected result:
>> Invokation1:
>> MyHandler1 called
>> Invokation2:
>> MyHandler2 called
>> Invokation3:
>>
>> Basically the example is: Invoke the callback of all objects which still
>> "exist" (in the meaning of having "non-weak" references towards them).
>>
>> I know this may have some caveats. It would be necessary to check the
>> reference count of the object in the SuperWeakSet, because the object
>> may have no "non-weak" references left but still was not yet garbage
>> collected. I do not know, whether current ECMAScript-Implementations and
>> their garbage collecting solutions could handle this easily - basically
>> it is the same decision, which the garbage collector would make: If an
>> object can be disposed, because it has no non-Weak references pointing
>> to it, the object is by definition not in the WeakSet anymore. And
>> implementations could in that case even give a hint to the garbage
>> collector, which may improve performance of applications, which use the
>> SuperWeakSet alot.
>>
>> Greetings, Michael
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
> _______________________________________________
> 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/20160713/2e1c19ba/attachment-0001.html>
More information about the es-discuss
mailing list