lazy evaluation and watch()

As a non-programmer, I have always been curious as to how, in a OO language, to broadcast events like the “progress” event in the Loader class and “onMouseMove” in the Mouse class, which are events that are continuously happening instead of happen only  once.
I think I sort of see a little bit today as to how they deal with these things by checking out Loader, View, EventDispatcher AS 2.0 classes and ActionScript 2.0 Language Reference> Objec> watch(Object.watch method):
“The Object.watch() method behaves similarly to the Object.watch() function in JavaScript 1.2 and later. The primary difference is the userData parameter, which is a Flash addition to Object.watch() that Netscape Navigator does not support. You can pass the userData parameter to the event handler and use it in the event handler.

The Object.watch() method cannot watch getter/setter properties. Getter/setter properties operate through lazy evaluation– the value of the property is not determined until the property is actually queried. Lazy evaluation is often efficient because the property is not constantly updated; it is, rather, evaluated when needed. However, Object.watch() needs to evaluate a property to determine whether to invoke the callback function. To work with a getter/setter property, Object.watch() needs to evaluate the property constantly, which is inefficient.

Generally, predefined ActionScript properties, such as _x, _y, _width, and _height, are getter/setter properties and cannot be watched with Object.watch().”

So basically, in the scenario of a PreloaderModel class, you simply dispatch a “progress” event within a getter/setter property (“get bytesLoaded”) whenever the “_bytesLoaded” property value changes. Like so:

private var _bytesLoaded:Number = undefined;
public function get bytesLoaded():Number {
if (typeof (_content) == “movieclip”) {
//lazy evaluation
if (_content.getBytesLoaded()>=_content.getBytesTotal()) {
//trace(“yo!!!!COMPLETE”);
var obj:Object = {target:this, type:”complete”};
this.dispatchEvent(obj);
} else if (_content.getBytesLoaded()<_content.getBytesTotal()) {
//trace(“yo!!!!PROGRESS _bytesLoaded: “+_bytesLoaded+” getBytesLoaded: “+_content.getBytesLoaded()+” this::: “+this);
var obj:Object = {target:this, type:”progress”};
this.dispatchEvent(obj);
}
//
if (isNaN(_content.getBytesLoaded()) || !_content.getBytesLoaded()) {
_bytesLoaded = 0;
return _bytesLoaded;
} else {
_bytesLoaded = _content.getBytesLoaded();
return _bytesLoaded;
}
}
//return _bytesLoaded;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: