<?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/"
	>

<channel>
	<title>guy in the chair . com &#187; SystemManager</title>
	<atom:link href="http://guyinthechair.com/tag/systemmanager/feed/" rel="self" type="application/rss+xml" />
	<link>http://guyinthechair.com</link>
	<description>//the blog of Paul Taylor</description>
	<lastBuildDate>Fri, 18 Nov 2011 01:22:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Understanding the Flex SystemManager</title>
		<link>http://guyinthechair.com/2010/01/understanding-the-flex-systemmanager/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=understanding-the-flex-systemmanager</link>
		<comments>http://guyinthechair.com/2010/01/understanding-the-flex-systemmanager/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 22:06:50 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[MXML]]></category>
		<category><![CDATA[SystemManager]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=113</guid>
		<description><![CDATA[Before I start, let me say that there has been a lot of discussion about the SystemManager on various blogs. Deepa posted a fantastic write-up on the SystemManager back in October 07, which really got me interested in learning about it more. Working on Flex-less MXML is what finally pushed me to understand exactly why [...]]]></description>
			<content:encoded><![CDATA[<p>Before I start, let me say that there has been a lot of discussion about the SystemManager on various blogs. <a title="Deepa's fantastic blog on Flex" href="http://iamdeepa.com/blog/" target="_blank">Deepa</a> posted a fantastic <a title="SystemManager: Every Flex application's best friend" href="http://iamdeepa.com/blog/?p=11" target="_blank">write-up</a> on the SystemManager back in October 07, which really got me interested in learning about it more. Working on Flex-less MXML is what finally pushed me to understand exactly why it&#8217;s such a critical class. There&#8217;s a lot to cover about the SystemManager, from the behavior of the Flash Player to the behavior of the Flex compiler, so buckle up, cause this is a long one.</p>
<p><strong>Note 1: A recurring theme in this post is the fundamental behavior of the Flash Player. Flex would be nowhere without Flash, so if we desire true understanding of Flex, we must also understand the Flash Player.<br />
Note 2: For some of this, it is helpful to turn on the -keep-generated-actionscript flag in the FlashBuilder compiler options. To do this, go to Project &#8211;&gt; Properties &#8211;&gt; Flex Compiler. In the &#8220;Additional compiler arguments&#8221; field, type &#8220;-keep&#8221; at the end of the arguments list. FlashBuilder generates ActionScript from all your MXML, including a dynamic subclass of SystemManager. This will instruct FlashBuilder to place that code in the bin-debug/generated folder.</strong></p>
<p><span style="font-weight: normal; font-size: 14px;">To understand the SystemManager, you must first understand that&#8230;</span></p>
<h2>Every Flex Application is a 2-Frame Flash Movie</h2>
<p><img class="alignleft size-full wp-image-114" title="2 Frame Flash Movie" src="http://guyinthechair.com/wp-content/uploads/2010/01/loader_content_flash.png" alt="2 Frame Flash Movie" width="302" height="63" /> Yes, every one of them. The Flash Player is capable of downloading just the data for the first frame and streaming the rest of the movie. Therefore a trend started in the earlier days of Flash-only developing; you typically put a very small loader object on your first frame that displays the loading progress of the rest of the SWF to the user. That way the user has a visual indication that something is happening, and the movie gets preloaded to a point where the user won&#8217;t experience any playback hiccups.</p>
<h2>That is awesome&#8230; why do I care?<br />
<span style="font-weight: normal; font-size: 14px;">Good question. First, lets examine the implications of Flash&#8217;s load-and-stream feature:</span></h2>
<p>From a movie perspective if frames aren&#8217;t loaded, the movie stops until they are loaded. If the movie is constantly experiencing this, the movie&#8217;s framerate essentially becomes a function of the speed of the user&#8217;s internet connection.<br />
From a code perspective, <strong>if frames aren&#8217;t fully loaded, classes can&#8217;t be accessed</strong>. Calling <code>new MyObject()</code> will throw a runtime error, &#8220;1065: Variable MyObject is not defined.&#8221; This is because the frame that contains the definition for that class hasn&#8217;t been fully loaded.</p>
<h2>Oh I see now, so we need a small preloader on the first frame?<br />
<span style="font-weight: normal; font-size: 14px;">Exactly. All that silly Flash that we thought we left behind when we became &#8220;Enterprise Flex Developers&#8221; has come back to bite us in the ass. But never fear, the SystemManager is here.</span></h2>
<p><strong>The SystemManager is a fancy MovieClip</strong>. The SystemManager has a few duties, including:</p>
<ul>
<li>Create and initialize the pre-loader to show to the user during app load and startup.</li>
<li>Manage loading in RSLs.</li>
<li>Manage module logic. If the SystemManager is the root of the SWF, he knows he is a Flex Application. If he is not the root of the SWF, he knows he is a module, and must communicate with whichever SystemManager is the root of the SWF (see if you can spot the logical flaw of the assumption made in that sentence, there will be a quiz later). SystemManager must communicate events both ways between modules/app, such as mouse and keyboard events.</li>
<li>Manage Event.RESIZE events dispatched from the Stage.</li>
<li>Manages the embedded fonts list</li>
<li>Initializes the various manager singletons, such as ResourceManager and StylesManager.</li>
<li>Manages top-level application windows. The SystemManager maintains a <code>cursorChildren</code> list and a <code>popUpChildren</code> list, for tooltips and pop-up windows respectively.</li>
<li>Once all the code for the movie is loaded, SystemManager creates your actual Application instance and calls <code>initialize()</code>.</li>
<li>Adds the Application to the stage once the Application dispatches its FlexEvent.CREATION_COMPLETE event. This is why the Application doesn&#8217;t have a reference to the stage when its FlexEvent.CREATION_COMPLETE is dispatched, when all the other components do.</li>
</ul>
<h2>How it works<br />
<span style="font-weight: normal; font-size: 14px;">How the SystemManager does its thing. Matching colors are event listening/dispatching pairs.</span></h2>
<table border="0">
<tbody>
<tr>
<td>
<h3>Frame 1</h3>
</td>
<td>
<h3>Frame 2</h3>
</td>
</tr>
<tr>
<td width="50%">
<ul>
<li>stop()</li>
<li>listen for player <span style="color: #ff0000;">init</span> event</li>
<li><span style="color: #ff0000;">init</span> dispatched:
<ol>
<li>get root SystemManager (if we&#8217;re not the top level)</li>
<li> listen for <span style="color: #ff6600;">enterFrame event</span></li>
<li>create preloader</li>
<li>listen for <span style="color: #66FF00;">preloaderDocFrameReady</span> from preloader</li>
<li>listen for <span style="color: #999900;">complete</span> from preloader</li>
<li>load RSLs and wait</li>
<li>RSLs loaded. <span style="color: #66FF00;">preloaderDocFrameReady</span> dispatched by preloader
<ul>
<li><span style="color: #3366ff;">Timer created</span> to go run nextFrame() after 100 milliseconds</li>
</ul>
</li>
<li>Exactly 10 milliseconds after preloaderDocFrameReady dispatched, <span style="color: #999900;">complete</span> dispatched by preloader</li>
<li><span style="color: #3366ff;">Timer event</span> dispatched, nextFrame() called/<span style="color: #ff6600;">enterFrame handler</span> triggered</li>
</ol>
</li>
</ul>
</td>
<td width="50%">
<ul>
<li>Managers initialized.</li>
<li>Stage Resize listener added.</li>
<li>Application instance created.</li>
<li>listener for Application <span style="color: #ff00ff;">creationComplete</span> added</li>
<li>nextFrame()</li>
<li>Application <span style="color: #ff00ff;"> creationComplete</span> dispatched
<ol>
<li>Preloader removed</li>
<li>Application added to the stage</li>
<li>applicationComplete event dispatched</li>
</ol>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<h2>Hold on, I&#8217;m a Flex developer. I don&#8217;t work with frames.<br />
<span style="font-weight: normal; font-size: 14px;">I don&#8217;t have access to the timeline of my SWF. Actually, you do.</span></h2>
<p>It&#8217;s through an undocumented meta tag, [Frame], which is really just a shortcut for the -frames compiler option. You can <a title="[Frame] meta tag explanation" href="http://blogs.adobe.com/rgonzalez/2006/06/modular_applications_part_2.html" target="_blank">read all about the frame meta tag</a> at Roger Gonzalez&#8217;s blog, but here&#8217;s the gist of it: If you add the [Frame] meta tag to the top of the Application class of your project, the Flex compiler will insert a frame before the frame that your Application is on. If you specify the factoryClass property on the meta tag, the Flex compiler will generate a subclass of whichever class you specify as the factory, and put that on the frame it creates. If you open up the mx.core.Application class, you&#8217;ll see the line [Frame(factoryClass="mx.managers.SystemManager")] near the top. This tells the Flex compiler to create a frame before the mx.core.Application, and attach a generated subclass of mx.managers.SystemManager.</p>
<p>The Flex compiler will generate a subclass of your factory class that implements <code>IFlexModuleFactory</code>. As long as your SystemManager implements IFlexModuleFactory, your code will compile. The Flex compiler overrides the info() method and returns information specific to your Application, such as a list of embedded fonts and the name of your Application.</p>
<p>And with this meta tag, you can override the default SystemManager implementation. As Roger says, &#8220;if the [Frame] metadata exists on the base class of your application, a subclass of the factory will be generated. If the metadata is directly on your application class, it will be honored, but no subclass will be generated; its assumed that you&#8217;ve already written the appropriate factory.&#8221;</p>
<h2>Why this is cool</h2>
<p>Most Flex developers will never even know the SystemManager exists, and they shouldn&#8217;t have to. Adobe has done a good job of masking Flash behind a more professional, Java-like framework with Flex. If you want to write an app with zero dependence on the Flex framework, but still use the fantastic FlashBuilder IDE, debugger/profiler, MXML, and binding, this is really good to know. You can write an Application based on Sprite, write your own stripped down version of the SystemManager, and then write MXML and take advantage of Flex&#8217;s bindings and generated code. Rock on.</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2010/01/understanding-the-flex-systemmanager/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

