Is there built in functionality to make eventable types in dart?
In my Javascript applications I use a class called Eventable to provide the following functionality:
var dog = new Dog() //where Dog inherits from Eventable
var cat = new Cat() //where Cat inherits from Eventable
//use 'on' to listen to events
cat.on(dog, 'bark', cat.runaway); //assuming Cat has a method runaway on its prototype
//use fire to launch events
dog.fire({type: 'bark'}); //this causes cat.runaway(event); to be called
A very common pattern in javascript, I like it because it helps me to keep objects isolated in the src and in my mind.
Using the on method creates a new EventContract which has a unique key based on the owner (cat above), client (dog above), type ('bark' above) and function (cat.runaway above). This unique key allows me to ensure that no duplicated EventContracts are created, but more importantly it allows me to keep an easy to lookup collection of all of the EventContracts an object has, such that I can call:
cat.dispose();
and all of the event contracts for cat will be destroyed, so I am confident that all of the external references to cat have been deleted and cat can now be GC'd.
But I am finding it hard to implement this functionality in Dart, because doing something like: cat.on(dog, 'bark', cat.runaway); causes a new function to be generated to enclose (){cat.runaway();} and the only unique key I can get off of a standard function object in dart is its hashCode, but that means I can recall cat.on(dog, 'bark', cat.runaway); and it will create a new EventContract because it has created another unique closure rather than processing a reference to the original function, as happens in javascript.
Is there anyway for me to achieve this pattern in dart?