events - How to implement an eventable type in Dart -


is there built in functionality make eventable types in dart?

in javascript applications use class called eventable provide following functionality:

var dog = new dog() //where dog inherits eventable var cat = new cat() //where cat inherits eventable  //use 'on' listen events cat.on(dog, 'bark', cat.runaway); //assuming cat has method runaway on prototype  //use fire launch events dog.fire({type: 'bark'});  //this causes cat.runaway(event); called 

a common pattern in javascript, because helps me keep objects isolated in src , in mind.

using on method creates new eventcontract has unique key based on owner (cat above), client (dog above), type ('bark' above) , function (cat.runaway above). unique key allows me ensure no duplicated eventcontracts created, more importantly allows me keep easy lookup collection of of eventcontracts object has, such can call:

cat.dispose(); 

and of event contracts cat destroyed, confident of external references cat have been deleted , cat can gc'd.

but finding hard implement functionality in dart, because doing like: cat.on(dog, 'bark', cat.runaway); causes new function generated enclose (){cat.runaway();} , unique key can off of standard function object in dart hashcode, means can recall cat.on(dog, 'bark', cat.runaway); , create new eventcontract because has created unique closure rather processing reference original function, happens in javascript.

is there anyway me achieve pattern in dart?

in general, should use stream idiomatic way provide sequence of events in dart. besides, preferred have events exlpicitly declared on source (e.g. onbark instead of on['bark']. should started:

class dog {   var _barkcontroller = new streamcontroller();   stream onbark => _barkcontroller.stream.asbroadcaststream();   void bark() => _barkcontroller.add("woof"); }  class cat {   void runaway() => print("cat running"); }  void main() {   var dog = new dog();   var cat = new cat();    // listen returns subscription object can use unsubscribe   var sub = dog.onbark.listen((event) => cat.runaway());     dog.bark();  // prints 'cat running'   sub.cancel();   dog.bark();  // doesn't print } 

if prefer have events declared dynamically, have this:

  var sub = dog.on['bark'].listen((event) => cat.runaway());    // in case `on` property of events type overrides   // operator[] return appropriate stream 

there similar events class, used provide browser events on dom elements.

additionally, there community libraries event_stream , event_source simplify task bit.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -