Feature-Request: allow to iterate over WeakSet / WeakMap
Michael Kriegel
michael.kriegel at actifsource.com
Wed Jul 13 13:45:27 UTC 2016
Sure.
I am using an MVC pattern, where model changes are notified via events.
Multiple views, lets call two of them view1 and view2, are capable of
displaying an object X, which is in the model. Whenever X is modified
(in the sense of modifying a property of it) an event is fired. Views,
which display an aspect of X register themselves to the modified-event
of X to be refreshed. However, views may be closed, so a view which was
closed shall be unsubscribed from that event automatically. Without an
iterable WeakSet, I have to unsubscribe the view manually on disposal.
This is more error prone, as different views subscribe to different
model change events, so each of my views needs kind of a destructor,
which unregisters it from all such event handlers, which it registered
to - if you forget one, you have a memory leak.
Actually ECMAScript should introduce WeakReference, which is sth. like a
variable, which may lose its object, when no "non-Weak" reference to it
exists anymore. Losing the value means it returns undefined again. example:
let a = {};
let weak b = a;
console.log(typeof(b)); // prints object
a = {};
console.log(typeof(b)); // prints undefined
Having WeakReferences and iterable WeakSet/WeakMap would help a lot in
preventing memory leaks.
Of course, as stated before: it would be preferred, if the WeakReference
/ WeakSet / WeakMap is predictable in the sense, that their state does
not depend on "whether an object has actually been garbage collected",
but on "whether an object still has 'non-weak' references pointing to
it, aka 'is garbage-collectable". Of course this involves checking
garbage-collectability on access to the WeakReference / WeakSet / WeakMap.
On 13.07.2016 15:08, Isiah Meadows 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
> <mailto: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 <mailto: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/30323895/attachment.html>
More information about the es-discuss
mailing list