<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
xmlns:rawvoice="http://www.rawvoice.com/rawvoiceRssModule/"
>

<channel>
	<title>Solution Hacker &#187; preloader</title>
	<atom:link href="http://www.solutionhacker.com/tag/preloader/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.solutionhacker.com</link>
	<description>This blog provides solutions for enterpreneurs!</description>
	<lastBuildDate>Mon, 06 Feb 2012 07:19:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=367</generator>
<!-- podcast_generator="Blubrry PowerPress/2.0.4" -->
	<itunes:summary>This blog provides solutions for enterpreneurs!</itunes:summary>
	<itunes:author>Solution Hacker</itunes:author>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://www.solutionhacker.com/wp-content/plugins/powerpress/itunes_default.jpg" />
	<itunes:subtitle>This blog provides solutions for enterpreneurs!</itunes:subtitle>
	<image>
		<title>Solution Hacker &#187; preloader</title>
		<url>http://www.solutionhacker.com/wp-content/plugins/powerpress/rss_default.jpg</url>
		<link>http://www.solutionhacker.com</link>
	</image>
		<item>
		<title>Flex Startup Sequence</title>
		<link>http://www.solutionhacker.com/data-intelligence/report/flex-startup-sequence/</link>
		<comments>http://www.solutionhacker.com/data-intelligence/report/flex-startup-sequence/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 06:56:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Report]]></category>
		<category><![CDATA[dynamic loading theme]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[preloader]]></category>
		<category><![CDATA[startup events]]></category>
		<category><![CDATA[SystemManager]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=217</guid>
		<description><![CDATA[<h2>Magic behind the scene</h2>
<p>I always wonder how my Flex application displayed on the Flash Player in browser. Why decompile Flex SWF will give me <strong>2 frames movie</strong>? What is <strong>SystemManager</strong> and how can I get a handle of it? Many of these kind of questions are at the lower level. The level that makes Flex application possible. Normally, we don't need to dive into this to write a Flex application. However, it would make me feel more comfortable to understand this before I advocate Flex as the main part of our company's UI strategy.&#160;</p>
<p>To understand how your Flex application got loaded and display on Flash Player, you can read this great <a href="http://iamdeepa.com/blog/?p=11">article</a>. I am going to put down the sequences in steps below:</p>
<h2>Flex is a 2-frame movie</h2>
<p>The <strong>first frame</strong> of a Flex SWF contains the <strong>SystemManager</strong>, the <strong>Preloader</strong>, the <strong>DownloadProgressBar </strong>and some glue helper classes. Remember, the Preloader is what creates the DownloadProgressBar control which displays the progress of a Flex application downloading and being initialized. The <strong>second frame</strong> of a Flex SWF contains the rest of the Flex framework code, your application code and all of your application assets like embedded fonts, images, etc.</p>
<p><!--more--><br />
By creating a 2-frame movie, Flex applications can take advantage of the <strong>streaming </strong>support built into the Flash Player and a preloader can appear before all of the Flex framework code and your application code are downloaded.</p>
<blockquote>
<p>The .swf format is a progressive download format which means that Flash player can access content on frames as they download without having to wait for the entire file to download.</p>
</blockquote>
<p><u>Here are the steps</u>:</p>
<ol>
    <li>First, enough bytes for <strong>frame 1</strong> are streamed down to the Flash Player.</li>
    <li>The Flash Player executes those bytes by creating a <strong>SystemManager </strong>instance.</li>
    <li>SystemManager instruct the Flash Player to stop at the end of frame 1.</li>
    <li>SystemManager then goes on to create the <strong>Preloader </strong>which creates the <strong>DownloadProgressBar </strong>control and pops that up on the client screen.</li>
    <li>The Preloader then starts tracking the rest of the bytes streaming in from the Flex SWF.&#160;</li>
    <li>Once all the bytes for the Flex framework and application code are in, the System Manager goes on to frame 2 and instantiates the <strong>Application </strong>instance.</li>
    <li>Once the Application instance has been created, the SystemManager sets <strong>Application.systemManager</strong> to itself. This is how you, the application developer, can access the SystemManager at a later time.</li>
    <li>The Application dispatches the <strong>preinitialize event</strong> at the beginning of the initialization process.</li>
    <li><strong>Application goes on to create its children. </strong>The method createChildren() is called on the application. At this point each of the application’s components is being constructed, and each component’s createChildren() will be also called. For detail, look at <strong>component lifecycle section</strong>.</li>
    <li>The Application dispatches the <strong>initialize event</strong>, which indicates that all application’s components have been initialized. However, at this state,  all the components are not yet laid out.</li>
    <li>Eventually, once all the Application child controls and containers have been created, sized and positioned, the Application dispatches the <strong>creationComplete </strong>event.</li>
    <li>Once the creationComplete event has been dispatched, the Preloader removes the DownloadProgressBar control and the SystemManager <strong>adds the Application instance to the Flash Player display list</strong>. (The Flash Player display list is basically the tree of visible or potentially visible objects that make up your application. When you add and remove child components to your application, your basically just adding and removing them from the display list).</li>
    <li>Once the Application is added to the Flash Player display list, the Application dispatches its <strong>applicationComplete</strong> event</li>
    <li>The Application has been created and is up on the screen ready to be interacted with.<br />
    &#160;</li>
</ol>
<h2>Component Architecture</h2>
<p>&#160;This is the best video I have found that discuss the component lifecycle in detail - Thanks for Deepa.</p>
<embed height="350" width="550" flashvars="v=~b64~aHR0cDovL2Fkb2JlLmVkZ2Vib3NzLm5ldC9mbGFzaC9hZG9iZS9hZG9iZXR2Mi9tYXhfMjAwOF9kZXZlbG9wLzE1OTY3NDE2MTNfMjkzMTA5MzAwMV8yMDAxLS1zdWJyYW1hbmlhbS10dWUtMTMwcG0tZGV2ZWxvcC5mbHY/cnNzX2ZlZWRpZD0xNTM4NCZ4bWx2ZXJzPTI=&#38;w=550&#38;t=http://tv.adobe.com/MAX-2008-Develop/Creating-New-Components-in-Flex-3-by-Deepa-Subramaniam.htmlvi+f15384v1002&#38;h=350" pluginspage="http://www.adobe.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" loop="false" play="true" name="AdobeTVPlayer" bgcolor="#000000" src="http://tv.adobe.com/Embed.swf"></embed>
<p class="MsoNormal" style="text-align: left;">Below is summary I took from the video above, in case you don't want to spend an hour to listen to this. However, I really think you should. To start out, Deepa introduced component and skinning architecture "<strong>Spark</strong>" that is part of Flex 4 <strong>Gumbo</strong>. Spark is built on top of <strong>Halo </strong>(ie. Flex 3 component architecture) and components using Halo or Spark can co-exist. Then,&#160; Deepa put her focus to talk about how to develop her custom video component on top of Halo and Spark for comparison. Spart is great that it can factor out the layout code from component.</p>
<p class="MsoNormal" style="text-align: left;">Halo component lifecycle can be separated into 3 phases:</p>
<p><u><strong>Phase 1 - Initialization</strong></u></p>
<ol>
    <li><u>Construction</u><br />
    <ul>
        <li>Choose the right base class and provide default constructor (ie. zero argument). By extending <strong>UIComponent</strong>, you inherit all of the lifecycle methods, events and properties. Also, since UIComponent extends from <strong>EventDispatcher</strong>, your component inherits the ability to listen and dispatch events.</li>
        <li>Best practice: Call super() and add event listener. Minimal work should occur here.</li>
    </ul>
    </li>
    <li><u>COnfiguration</u>
    <ul>
        <li>setter and getter for properties.</li>
        <li>Involve in invalidation and validation cycle. Detail later.</li>
        <li>Best practice: Setter should not expect the internal children have been created and you want your setter to be fast. Use _xxx for storage variable and have dirty flag for your variable. Throw events out when the internal state of your component get changed.</li>
    </ul>
    </li>
    <li><u>Attachment</u>
    <ul>
        <li>Component are added to the flash <strong>display list </strong>by its parent via <strong>addChild()</strong> call. Without attachment, component lifecycle will be stalled. Nothing is going to happen to your component. So, this is an important step.</li>
        <li>Display list is a tree of visible or potentially visible objects in your application. At the root of the display list is your main application. Thing like containment hierarchy and rendering order are all maintained by the display list.</li>
    </ul>
    </li>
    <li><u>Initialization</u>- 5 lifecycle actions occur here.
    <ul>
        <li><strong>preinitialize </strong>event is dispatched. It signifies that you as component that has been added to the display list by your parent via addChild().</li>
        <li><strong>createChildren()</strong> - walk through all your children, create, configure and attach them to the display list. Best practice: Call super.createChildren(), construct if not exist and attach your children via addChild(). If your child component is dynamic and data driven, use <strong>commitProperties()</strong> b/c it gets called at every invalidation and validation cycle. <em>Halo rules: Container --&#62; nested structure of UIComponents --&#62; MovieClip, Video, Shape and Sprite.</em></li>
        <li><strong>initialize </strong>event is dispatched. It signifies that you and all your children are created and attached.</li>
        <li>First <strong>invalidation/ validation pass </strong>occurs,</li>
        <li><strong>creationComplete</strong> event is dispatched. Ready for prime time.</li>
    </ul>
    </li>
</ol>
<p><u><strong>Phase 2 - Update</strong></u></p>
<p>At this phase, your component is fully initialized and ready for usage. Now, it needs to know how to update. And update occurs when its internal state has been changed by like user interactions. To respond to the changes, component uses the invalidation and validation cycle. The key here is to flag the changed variable dirty during invalidation and later handles it during validation right before rendering. So, you can have many invalidation and one validation that gives better performance via avoiding repetitive work. To better understand this approach, Deepa talks about <strong>Flash Player Elastic Racetrack</strong> that has 2 parts: code execution and rendering. If either part taking too long, Flash player cannot get its job done faster than 1 frame per second. You will see your application with lag and halt, that is bad!&#160;</p>
<ol>
    <li>Invalidation/ Validation can be split into 3 phase. <br />
    <ul>
        <li>InvalidateProperties --&#62; commitProperties</li>
        <li>InvalidateSize --&#62; measure (measure may not be called. Don't have your code depends on it)</li>
        <li>InvalidateDisplayLIst --&#62; updateDisplayList</li>
    </ul>
    </li>
</ol>
<p><u><strong>Phase 3 - Destruction</strong></u></p>
<ol>
    <li>Detachment</li>
    <li>Garbage Collection</li>
</ol>
<p>&#160;</p>
<h2>What is Mixin?</h2>
<p>When you put the [Mixin] metadata just above your class definition and add a static init function to the class like so,</p>
<pre name="code" class="java">
[Mixin]
public class Model {   
	public static function init (systemManager : ISystemManager)   {     
		trace ("I get called first")   
	} 
}
</pre>
<p>The static init function will be called as soon as your application loads (assuming this class is referenced somewhere in the app), much like static initialization blocks in Java. This is useful if you have some code that you want to run before any of the other code in the class.</p>
<h2>Reference</h2>
<p>Below are some interesting articles I found related to this article</p>
<ol>
    <li><a href="http://npacemo.com/wordpress/2008/07/06/flex-application-bootstrapping-totally-custom-preloader/">Create Custom Preloader</a>, <a href="http://www.onflex.org/ted/2006/07/flex-2-custom-preloaders.php">Ted has an article related to this too</a></li>
    <li><a href="http://www.insideria.com/2008/04/flex-ria-performance-considera.html">Speed up startup loading time</a></li>
    <li><a href="http://userflex.wordpress.com/2008/02/07/preload-runtime-styles/">Dynamic loading a new custom theme without fraction seconds delay</a></li>
    <li><a href="http://tv.adobe.com/#vi+f15384v1002">Deepa presentation in MAX</a></li>
    <li><a href="http://www.adobe.com/support/documentation/en/flex/1/mixin/mixin2.html">Introduction to mixins</a></li>
</ol>
<p>&#160;</p>]]></description>
			<content:encoded><![CDATA[<h2>Magic behind the scene</h2>
<p>I always wonder how my Flex application displayed on the Flash Player in browser. Why decompile Flex SWF will give me <strong>2 frames movie</strong>? What is <strong>SystemManager</strong> and how can I get a handle of it? Many of these kind of questions are at the lower level. The level that makes Flex application possible. Normally, we don&#8217;t need to dive into this to write a Flex application. However, it would make me feel more comfortable to understand this before I advocate Flex as the main part of our company&#8217;s UI strategy.&#160;</p>
<p>To understand how your Flex application got loaded and display on Flash Player, you can read this great <a href="http://iamdeepa.com/blog/?p=11">article</a>. I am going to put down the sequences in steps below:</p>
<h2>Flex is a 2-frame movie</h2>
<p>The <strong>first frame</strong> of a Flex SWF contains the <strong>SystemManager</strong>, the <strong>Preloader</strong>, the <strong>DownloadProgressBar </strong>and some glue helper classes. Remember, the Preloader is what creates the DownloadProgressBar control which displays the progress of a Flex application downloading and being initialized. The <strong>second frame</strong> of a Flex SWF contains the rest of the Flex framework code, your application code and all of your application assets like embedded fonts, images, etc.</p>
<p><span id="more-217"></span><br />
By creating a 2-frame movie, Flex applications can take advantage of the <strong>streaming </strong>support built into the Flash Player and a preloader can appear before all of the Flex framework code and your application code are downloaded.</p>
<blockquote>
<p>The .swf format is a progressive download format which means that Flash player can access content on frames as they download without having to wait for the entire file to download.</p>
</blockquote>
<p><u>Here are the steps</u>:</p>
<ol>
<li>First, enough bytes for <strong>frame 1</strong> are streamed down to the Flash Player.</li>
<li>The Flash Player executes those bytes by creating a <strong>SystemManager </strong>instance.</li>
<li>SystemManager instruct the Flash Player to stop at the end of frame 1.</li>
<li>SystemManager then goes on to create the <strong>Preloader </strong>which creates the <strong>DownloadProgressBar </strong>control and pops that up on the client screen.</li>
<li>The Preloader then starts tracking the rest of the bytes streaming in from the Flex SWF.&#160;</li>
<li>Once all the bytes for the Flex framework and application code are in, the System Manager goes on to frame 2 and instantiates the <strong>Application </strong>instance.</li>
<li>Once the Application instance has been created, the SystemManager sets <strong>Application.systemManager</strong> to itself. This is how you, the application developer, can access the SystemManager at a later time.</li>
<li>The Application dispatches the <strong>preinitialize event</strong> at the beginning of the initialization process.</li>
<li><strong>Application goes on to create its children. </strong>The method createChildren() is called on the application. At this point each of the application’s components is being constructed, and each component’s createChildren() will be also called. For detail, look at <strong>component lifecycle section</strong>.</li>
<li>The Application dispatches the <strong>initialize event</strong>, which indicates that all application’s components have been initialized. However, at this state,  all the components are not yet laid out.</li>
<li>Eventually, once all the Application child controls and containers have been created, sized and positioned, the Application dispatches the <strong>creationComplete </strong>event.</li>
<li>Once the creationComplete event has been dispatched, the Preloader removes the DownloadProgressBar control and the SystemManager <strong>adds the Application instance to the Flash Player display list</strong>. (The Flash Player display list is basically the tree of visible or potentially visible objects that make up your application. When you add and remove child components to your application, your basically just adding and removing them from the display list).</li>
<li>Once the Application is added to the Flash Player display list, the Application dispatches its <strong>applicationComplete</strong> event</li>
<li>The Application has been created and is up on the screen ready to be interacted with.<br />
    &#160;</li>
</ol>
<h2>Component Architecture</h2>
<p>&#160;This is the best video I have found that discuss the component lifecycle in detail &#8211; Thanks for Deepa.</p>
<p><embed height="350" width="550" flashvars="v=~b64~aHR0cDovL2Fkb2JlLmVkZ2Vib3NzLm5ldC9mbGFzaC9hZG9iZS9hZG9iZXR2Mi9tYXhfMjAwOF9kZXZlbG9wLzE1OTY3NDE2MTNfMjkzMTA5MzAwMV8yMDAxLS1zdWJyYW1hbmlhbS10dWUtMTMwcG0tZGV2ZWxvcC5mbHY/cnNzX2ZlZWRpZD0xNTM4NCZ4bWx2ZXJzPTI=&amp;w=550&amp;t=http://tv.adobe.com/MAX-2008-Develop/Creating-New-Components-in-Flex-3-by-Deepa-Subramaniam.htmlvi+f15384v1002&amp;h=350" pluginspage="http://www.adobe.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" loop="false" play="true" name="AdobeTVPlayer" bgcolor="#000000" src="http://tv.adobe.com/Embed.swf"></embed></p>
<p class="MsoNormal" style="text-align: left;">Below is summary I took from the video above, in case you don&#8217;t want to spend an hour to listen to this. However, I really think you should. To start out, Deepa introduced component and skinning architecture &#8220;<strong>Spark</strong>&#8221; that is part of Flex 4 <strong>Gumbo</strong>. Spark is built on top of <strong>Halo </strong>(ie. Flex 3 component architecture) and components using Halo or Spark can co-exist. Then,&#160; Deepa put her focus to talk about how to develop her custom video component on top of Halo and Spark for comparison. Spart is great that it can factor out the layout code from component.</p>
<p class="MsoNormal" style="text-align: left;">Halo component lifecycle can be separated into 3 phases:</p>
<p><u><strong>Phase 1 &#8211; Initialization</strong></u></p>
<ol>
<li><u>Construction</u>
<ul>
<li>Choose the right base class and provide default constructor (ie. zero argument). By extending <strong>UIComponent</strong>, you inherit all of the lifecycle methods, events and properties. Also, since UIComponent extends from <strong>EventDispatcher</strong>, your component inherits the ability to listen and dispatch events.</li>
<li>Best practice: Call super() and add event listener. Minimal work should occur here.</li>
</ul>
</li>
<li><u>COnfiguration</u>
<ul>
<li>setter and getter for properties.</li>
<li>Involve in invalidation and validation cycle. Detail later.</li>
<li>Best practice: Setter should not expect the internal children have been created and you want your setter to be fast. Use _xxx for storage variable and have dirty flag for your variable. Throw events out when the internal state of your component get changed.</li>
</ul>
</li>
<li><u>Attachment</u>
<ul>
<li>Component are added to the flash <strong>display list </strong>by its parent via <strong>addChild()</strong> call. Without attachment, component lifecycle will be stalled. Nothing is going to happen to your component. So, this is an important step.</li>
<li>Display list is a tree of visible or potentially visible objects in your application. At the root of the display list is your main application. Thing like containment hierarchy and rendering order are all maintained by the display list.</li>
</ul>
</li>
<li><u>Initialization</u>- 5 lifecycle actions occur here.
<ul>
<li><strong>preinitialize </strong>event is dispatched. It signifies that you as component that has been added to the display list by your parent via addChild().</li>
<li><strong>createChildren()</strong> &#8211; walk through all your children, create, configure and attach them to the display list. Best practice: Call super.createChildren(), construct if not exist and attach your children via addChild(). If your child component is dynamic and data driven, use <strong>commitProperties()</strong> b/c it gets called at every invalidation and validation cycle. <em>Halo rules: Container &#8211;&gt; nested structure of UIComponents &#8211;&gt; MovieClip, Video, Shape and Sprite.</em></li>
<li><strong>initialize </strong>event is dispatched. It signifies that you and all your children are created and attached.</li>
<li>First <strong>invalidation/ validation pass </strong>occurs,</li>
<li><strong>creationComplete</strong> event is dispatched. Ready for prime time.</li>
</ul>
</li>
</ol>
<p><u><strong>Phase 2 &#8211; Update</strong></u></p>
<p>At this phase, your component is fully initialized and ready for usage. Now, it needs to know how to update. And update occurs when its internal state has been changed by like user interactions. To respond to the changes, component uses the invalidation and validation cycle. The key here is to flag the changed variable dirty during invalidation and later handles it during validation right before rendering. So, you can have many invalidation and one validation that gives better performance via avoiding repetitive work. To better understand this approach, Deepa talks about <strong>Flash Player Elastic Racetrack</strong> that has 2 parts: code execution and rendering. If either part taking too long, Flash player cannot get its job done faster than 1 frame per second. You will see your application with lag and halt, that is bad!&#160;</p>
<ol>
<li>Invalidation/ Validation can be split into 3 phase. 
<ul>
<li>InvalidateProperties &#8211;&gt; commitProperties</li>
<li>InvalidateSize &#8211;&gt; measure (measure may not be called. Don&#8217;t have your code depends on it)</li>
<li>InvalidateDisplayLIst &#8211;&gt; updateDisplayList</li>
</ul>
</li>
</ol>
<p><u><strong>Phase 3 &#8211; Destruction</strong></u></p>
<ol>
<li>Detachment</li>
<li>Garbage Collection</li>
</ol>
<p>&#160;</p>
<h2>What is Mixin?</h2>
<p>When you put the [Mixin] metadata just above your class definition and add a static init function to the class like so,</p>
<p><pre><pre name="code" class="java">
[Mixin]
public class Model {&nbsp;&nbsp; 
&nbsp;&nbsp;public static function init (systemManager : ISystemManager)&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;trace (&quot;I get called first&quot;)&nbsp;&nbsp; 
&nbsp;&nbsp;} 
}
</pre></pre></p>
<p>The static init function will be called as soon as your application loads (assuming this class is referenced somewhere in the app), much like static initialization blocks in Java. This is useful if you have some code that you want to run before any of the other code in the class.</p>
<h2>Reference</h2>
<p>Below are some interesting articles I found related to this article</p>
<ol>
<li><a href="http://npacemo.com/wordpress/2008/07/06/flex-application-bootstrapping-totally-custom-preloader/">Create Custom Preloader</a>, <a href="http://www.onflex.org/ted/2006/07/flex-2-custom-preloaders.php">Ted has an article related to this too</a></li>
<li><a href="http://www.insideria.com/2008/04/flex-ria-performance-considera.html">Speed up startup loading time</a></li>
<li><a href="http://userflex.wordpress.com/2008/02/07/preload-runtime-styles/">Dynamic loading a new custom theme without fraction seconds delay</a></li>
<li><a href="http://tv.adobe.com/#vi+f15384v1002">Deepa presentation in MAX</a></li>
<li><a href="http://www.adobe.com/support/documentation/en/flex/1/mixin/mixin2.html">Introduction to mixins</a></li>
</ol>
<p>&#160;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/data-intelligence/report/flex-startup-sequence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

