Some additional note on deleting.
Limitations of JavaScript is affecting dart.
- No reference count available
This is javascript feature and partly because of the way garbage collection works in browser: What is JavaScript garbage collection?
- No weak reference available(For the time being)
Another Java-script limitation and because of the lack of access to reference count, self deleting is not an option either.
Note:There is Expando and while it is useful, it's not a weak reference equivalent. Weak reference allows you to create ghost references that don't keep the object alive: https://en.wikipedia.org/wiki/Weak_reference.
- No destructor available
javascript does not have it, thus neither does dart.
As the others pointed, the limitations above are usually nothing.
Since the main eventloop can keep objects alive, careless coding could spawn an zombie object though; without destructor or weakref, some references could escape your murderous hands by accident and keep the object alive.
import 'dart:html';
import 'dart:async';
import 'dart:developer';
void main() {
var e = new XXX();
var ce = new CustomEvent('custom-event');
var s = new Stream.periodic(new Duration(microseconds: 10000),(count) {
print(count);
window.dispatchEvent(ce);
return count;
});
StreamSubscription ss = s.listen((count){print('stream runnig; ${count}_th run');});
// e.destroy();
// ss.cancel();
e = null;
s = null;
}
class XXX{
StreamSubscription subscription;
XXX(){
subscription = window.on['custom-event'].listen(event_handler);
}
void say_hi(){
print('hi');
print(this);
}
void event_handler(_){this.say_hi();}
destroy(){
subscription.cancel();
}
}
The above code prints the below in forever loop(unless you uncomment the two lines):
....
hi
VM33:1 Instance of 'XXX'
VM34:1 stream runnig; 751_th run
VM34:1 752
VM33:1 hi
VM33:1 Instance of 'XXX'
VM34:1 stream runnig; 752_th run
......
I don't know if the object e would persist without print(this), maybe only related objects are kept from garbage collection.
In any case, with more complicated code, manual clean up is necessary and I wish one of the three missing features is there but there is not.
Deleting indeed can be hard.