extending broadcaster
I've been finding myself doing this quite a lot recently, but it's annoying to do, just because of the way that ASBroadcaster works. Quite often, you need to do some other action when you add or remove a listener, or even when you broadcast a message. But ASBroadcaster's methods are not easily extended, without resorting to hacks. So, I came up with a fairly simple way of doing it. I just make a Broadcaster class, which moves the broadcaster methods out of the current object. You can make use of it by either inheritence or composition and extend it in both cases.
Class defintion (Use Bokel's ASBroadcaster override):
_global.Broadcaster = function(){
// concat is just so that it is a new array
// rather than the special arguments array
// which has extra props (callee and caller)
this._listeners = arguments.concat();
}
ASBroadcaster.initialize(Broadcaster.prototype);
Extend it by inheritence:
function MyClass(){
super();
}
// inherit from Broadcaster
MyClass.prototype = new Broadcaster();
// extend addListener method
MyClass.prototype.addListener = function(o){
super.addListener(o);
trace("someone is listening");
}
// extend removeListener method
MyClass.prototype.removeListener = function(o){
super.removeListener(o);
if(this._listeners.length==0){
trace("no-one is listening");
}
}
Extend it by composition:
function MyClass(){
// create a broadcaster object
this.messenger = new Broadcaster();
}
// extend addListener method
MyClass.prototype.addListener = function(o){
this.messenger.addListener(o);
trace("someone is listening");
}
// extend removeListener method
MyClass.prototype.removeListener = function(o){
this.messenger.removeListener(o);
if(this.messenger._listeners.length==0){
trace("no-one is listening");
}
}
// extend broadcastMessage method
MyClass.prototype.broadcastMessage = function(){
this.messenger.broadcastMessage.apply(this.messenger, arguments);
}
Composition is probably the most flexible in this case. Sometimes you want to make listeners listen for just a single event, rather than every event broadcast by the object, which can be wasteful. You can just create a broadcaster instance for each event and pass the name of the event in to your addListener and removeListener methods to access the appropriate broadcaster.
I've already found occasion to do both in one go, where a subclass of Broadcaster is composed in another class.
update: Full class here. This is the version I am now using.
mirror, mirror on the wall...
I couldn't resist blogging this, after Bertrand Saint-Guillain told me about it yesterday.
I don't know how the calculation is made, but the "Evil Stepmother" inside is already showing herself. If, one day, I ask the question and you come out on top, you (and your seven little friends) had better watch out!
cafe blogging
I never blogged from an internet cafe, so I thought I'd give it a go, despite having nothing Flash-related to say. To be honest, I never even used an internet cafe before, so I need some excuse to sit here. There's only so much web browsing that can be done in one session on a sunday afternoon!
So I'm staying in a hotel, while I look for a flat in London. As you may have guessed, I don't have a PC with me, so this week may be a good time for me to invest in a laptop. I've been chatting to Greg Burch a bit this week about his new acquisition, and its pretty cool. I just found one in a shop in Tottenham Court and am very tempted, though its a little bulkier than the pictures suggest and less than half as fast as similarly priced conventional laptops.
Leaving my house in Coventry, yesterday, there were a lot more last-minute things to do than I thought, so I ended up leaving pretty late and, together with "track maintenence", I didn't get into Euston Station until about 1:00 am last night. But, to make everything happy again, I missed the same last bus as perhaps the most beautiful woman I have ever met in real life. We got chatting and shared a taxi, since we were headed in the same direction. Alas, why do I never have a pen when I need one? You know - so we could continue our conversation about London house prices...
