Skip to content

Instantly share code, notes, and snippets.

@troygilbert
Created April 8, 2010 04:46
Show Gist options
  • Save troygilbert/359780 to your computer and use it in GitHub Desktop.
Save troygilbert/359780 to your computer and use it in GitHub Desktop.
Invalidate Pattern example
package
{
import flash.display.Shape;
import flash.events.Event;
public class InvalidationPattern
{
protected const USE_DEFERRED_VALIDATION:Boolean = true;
protected var tickerShape:Shape;
protected var needsValidation:Boolean;
protected var _property:int;
/** Constructor. **/
public function InvalidationPattern()
{
// shapes always get enter frame events, no stage required
tickerShape = new Shape();
tickerShape.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
/** An example property. **/
public function get property():int { return _value; }
public function set property(value:int):void
{
if (value != _property)
{
_property = value;
invalidate();
}
}
/** Invalidates object. **/
public function invalidate():void
{
needsValidation = true;
if (USE_DEFERRED_VALIDATION) return; // don't do anything yet
validate();
}
/** Validates an object. **/
public function validate():void
{
// if any of your dependent properties are not set,
// just exit this method now and it'll be called later
// if they're all ready, process the properties and
// update the state of the object
needsValidation = false;
}
/** Enter frame handler. **/
protected function onEnterFrame(event:Event):void
{
if (needsValidation) validate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment