Feature-Request: allow to iterate over WeakSet / WeakMap
Michał Wadas
michalwadas at gmail.com
Thu Jul 14 10:55:25 UTC 2016
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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160714/5873d312/attachment.html>
More information about the es-discuss
mailing list