Feature-Request: allow to iterate over WeakSet / WeakMap

Isiah Meadows isiahmeadows at gmail.com
Fri Jul 15 00:53:28 UTC 2016


You could also solve the iterability this way with weak references:

```js
function *iter(wm) {
  for (const ref of wm._refs) {
    const key = ref.get()
    yield [key, wm.get(key)]
  }
}

function remove({wm, ref}) {
  wm._refs.remove(ref)
}

class IterableWeakMap extends WeakMap {
  constructor(list) {
    super()
    this._refs = new Set()
    for (const e of list) this.set(e)
  }

  set(key, value) {
    super.set(key, value)
    const arg = {wm: this, ref: undefined}
    arg.ref = makeWeakRef(e, remove, arg)
    this._refs.add(arg.ref)
    return this
  }

  forEach(callback, thisArg = undefined) {
    for (const [key, value] of iter(this)) {
      f.call(thisArg, value, key)
    }
  }

  [Symbol.iterator]() { return iter(this) }
  entries() { return iter(this) }

  keys() {
    for (const [key] of iter(this)) yield key
  }

  values() {
    for (const [, value] of iter(this)) yield value
  }

  clear() {
    for (const ref of this._refs) {
      this.delete(ref.get())
    }
    this._refs.clear()
  }
}
```

On Thu, Jul 14, 2016, 06:55 Michał Wadas <michalwadas at gmail.com> wrote:

> What you think you need: iterable WeakMap
> What you really need: WeakReference, PhantomReference.
>
> Personally I think we need built-in class "GarbageCollectorObserver" that
> emit events for each observed collected object (uniquely identified by
> strongly-held symbols), it will solve most of problems associated with
> binding models and views.
>
>
> On Wed, Jul 13, 2016 at 2:13 PM, 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/20160715/7e3981fe/attachment.html>


More information about the es-discuss mailing list