If your logger doesn't use any asynchronous code, then putting it in a
domready (or simply after the init script) will ensure the logger is ready
by the time you call it.

BUT

if you do have async code in it, then firing the event on the window is a
solid way of achieving this

On Mon, Jul 25, 2011 at 2:05 PM, Paul Schwarz <[email protected]>wrote:

> domready seems to be the event that you'll see in bootstrap code of
> anything written for mootools.
>
> A few times now I've come across cases where, on top of Mootools, I
> build another mini-framework. Take for instance a logging facility for
> mootools/javascript. I've implemented a simple logger similiar to
> log4j, et al. So my logger needs to be initialised before I can use
> it. So I have code that looks a bit like this:
>
> window.addEvent('domready', function(){
>  window.logger = new Logger();
> });
>
> Now my other code can do something like this: logger.debug('Hello log
> facility!');
>
> But my other code bootstraps itself using the domready event as well.
> Something like this:
>
> window.addEvent('domready', function(){
>  var message = ('I am the other code, I could live happily without
> the logger, but actualy... I like the logger');
>  logger.debug('Other code says: ' + message);
> });
>
> So as you can see, our "other code" makes use of the logger, however
> this code is very fragile because if the logger has not been
> initialised by the time it is called then bad things happen. Correct
> me if I'm wrong but I understand the event system does not guarantee
> an order in which it fires. If this is the case then we could have a
> race condition.
>
> The solution I can think of is to chain things up, by changing the
> code to this:
>
> window.addEvent('loggerready', function(){
>  var message = ('I am the other code, I could live happily without
> the logger, but actualy... I like the logger');
>  logger.debug('Other code says: ' + message);
> });
>
> window.addEvent('domready', function(){
>  window.logger = new Logger();
>  window.fireEvent('loggerready');
> });
>
> ... if this is what it takes, then that's ok, but it just looks like
> it could become spaghetti pretty quickly.
>
> Please give me some hints.




-- 
Arieh Glazer
אריה גלזר
052-5348-561
http://www.arieh.co.il
http://www.link-wd.co.il

Reply via email to