<?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>Oliver Steele &#187; JavaScript</title>
	<atom:link href="http://osteele.com/archives/category/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://osteele.com</link>
	<description>Languages of the real and artificial.</description>
	<lastBuildDate>Wed, 28 Oct 2009 22:31:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Code Samples from Practical Functional JavaScript</title>
		<link>http://osteele.com/archives/2008/10/code-samples-from-practical-functional-javascript</link>
		<comments>http://osteele.com/archives/2008/10/code-samples-from-practical-functional-javascript#comments</comments>
		<pubDate>Fri, 03 Oct 2008 01:52:47 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://osteele.com/?p=357</guid>
		<description><![CDATA[The code samples from my talk at the Ajax Experience conference are now available here. 

Each example runs itself when you load its page, at least in Safari and Firefox.   This is something I first did for my talk at LL2.  It&#8217;s the only way I&#8217;ve ever been able to keep sample [...]]]></description>
			<content:encoded><![CDATA[<p>The code samples from my <a href="/archives/2008/09/practical-functional-javascript">talk at the Ajax Experience conference</a> are now available <a href="/talks/ajaxian-2008/samples">here</a>. </p>

<p>Each example runs itself when you load its page, at least in Safari and Firefox.   This is something I first did for my talk at <a href="http://ll2.ai.mit.edu/"><span class="caps">LL2</span></a>.  It&#8217;s the only way I&#8217;ve ever been able to keep sample code actually working<sup class="footnote"><a href="#fn1">1</a></sup>.</p>

<p>The full slide deck (including these code samples, but without the comments and the ability to run the code) is <a href="http://www.slideshare.net/osteele/oliver-steele-functional-javascript-presentation">here</a>.</p>

<p>These are from the final draft of the talk, but they&#8217;re really the first draft of this material.  The audience was great, and I learned a lot about how to explain this from their questions during the presentation.  I&#8217;m planning to reorganize and expand this the next time I get a free weekend to work on it.</p>

<p><hr/></p>

<p class="footnote" id="fn1"><sup>1</sup>  A funny story about that:  John Resig asked me after the talk whether I&#8217;d looked at <a href="http://ejohn.org/apps/learn/">Learn JavaScript</a>.  The truth is, I&#8217;d wanted to get by without any fancy slideware or sample scaffolding at all, but I needed a way to get formatted code into Keynote.  I started out using a technique that <a href="http://www.macvicar.net/blog/2008/06/source-code-hig.html">Scott MacVicar</a> came up with, and eventually added section breaks, and then &#8220;Previous&#8221; and &#8220;Next&#8221; buttons, until I&#8217;d eventually feature-creeped my way up to something pretty similar to John&#8217;s tool. Oh well.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2008/10/code-samples-from-practical-functional-javascript/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Practical Functional JavaScript</title>
		<link>http://osteele.com/archives/2008/09/practical-functional-javascript</link>
		<comments>http://osteele.com/archives/2008/09/practical-functional-javascript#comments</comments>
		<pubDate>Wed, 24 Sep 2008 19:06:06 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://osteele.com/?p=344</guid>
		<description><![CDATA[I&#8217;ll be giving a talk next Wednesday October 1 at The AJAX Experience, on &#8220;Practical Functional JavaScript&#8221;.  This could be subtitled &#8220;distributing JavaScript across time and space&#8221;, or maybe just &#8220;how to do things with functions&#8221;1.

A couple of years ago I found that all the interesting AJAX programs that I wanted to write involved [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll be giving a talk next Wednesday October 1 at <a href="http://ajaxexperience.techtarget.com/east/index.html">The <span class="caps">AJAX</span> Experience</a>, on &#8220;Practical Functional JavaScript&#8221;.  This could be subtitled &#8220;distributing JavaScript across time and space&#8221;, or maybe just &#8220;how to do things with functions&#8221;<sup class="footnote"><a href="#fn1">1</a></sup>.</p>

<p>A couple of years ago I found that all the interesting <span class="caps">AJAX </span>programs that I wanted to write involved asynchronous communication with the server (or sometimes with a Flash plugin, or sometimes within the client but with a few seconds or minutes delay). Trying to think about and debug these programs made me feel like I was just learning how to program again (or hadn&#8217;t yet), and hurt my head.  But now I can emerge, sadder but wiser, head fully healed, and with this talk in hand.</p>

I&#8217;ll be covering:<ul>
<li>Talking <span class="caps">REST </span>asynchronously to your server &#8212; <strong>without</strong> dipping into bleeding-edge technologies such as <a href="http://en.wikipedia.org/wiki/Comet_(programming)"> Comet</a> and <a href="http://svn.xantus.org/shortbus/trunk/bayeux/bayeux.html">Bayeux</a>.<sup class="footnote"><a href="#fn2">2</a></sup></li>
<li>Deferred execution and lightweight multithreading without <a href="http://code.google.com/apis/gears/api_workerpool.html">Google Gears</a>. [3]</li>
<li>Messaging between the Flash and the <span class="caps">HTML </span>within a page.  I&#8217;ll put this last so that if you aren&#8217;t interested in Flash you don&#8217;t have to worry about when to wake up.</li>
</ul>

<p>This talk is really the flip side of <a href="http://osteele.com/sources/javascript/functional/">functional javascript</a> and <a href="http://osteele.com/sources/javascript/sequentially/">sequentially</a>. Those were <strong>non-production</strong> experiments to take functional javascript <em>to an extreme</em>.  &#8220;Practical&#8221;, on the other hand, will be about real-world techniques I&#8217;ve used to write web sites such as <a href="http://browsegoods.com">Browsegoods</a>, <a href="http://fansnap.com">FanSnap</a>, Style&amp;Share, and the <a href="http://www.gowebtop.com">goWebtop Calendar</a>, and that resulted in some of the JavaScript-related libraries <a href="http://github.com/osteele">here</a>.</p>

<p>The <strong>main</strong> purpose of this talk is to be useful to practicing developers.  But it should also be fun.  <span class="caps">AJAX </span>lets you do a lot on the web that&#8217;s fun to look at and use.  It can be fun to program too, and I&#8217;d like to get some of that across.</p>

<p>If you&#8217;re interested in the <strong>type</strong> of things I&#8217;ve posted <a href="http://osteele.com/archives/category/javascript">here</a>, but found that those zoomed through the material too quickly or that you wanted to see more of a connection to real-world production programming, then this is the talk for you.</p>

<p>The bad news: <a href="http://ajaxexperience.techtarget.com/east/html/eventsataglance.html">It&#8217;s at 8am</a>.  I promise not to think badly of you if you take a nap, and to make loud noises when it&#8217;s time for you to go your next talk.</p>

<p><ins>Update: A draft of the talk is <a href="/talks/Oliver_Steele_Functional_JavaScript_v2.pdf">here</a>.  It doesn&#8217;t include most of the code samples.</ins></p>

<p><ins>Update 2: The final version with screenshots of all the code is <a href="http://www.slideshare.net/osteele/oliver-steele-functional-javascript-presentation">here</a>.  I&#8217;ll publish a runnable version of the examples soon.</ins></p>

<p><ins>Update 3: The runnable examples are <a href="/archives/2008/10/code-samples-from-practical-functional-javascript">here</a>. </ins></p>

<p><hr/></p>

<p class="footnote" id="fn1"><sup>1</sup> With a nod to <a href="http://en.wikipedia.org/wiki/J._L._Austin#How_to_Do_Things_With_Words">Austin</a> &#8212; but you don&#8217;t have to get that, if you aren&#8217;t a linguistics geek.</p>

<p class="footnote" id="fn2"><sup>2</sup> <span class="caps">COMET </span>and Bayeaux are cool, but the server-side support can be scary.</p>

<p class="footnote" id="fn3"><sup>3</sup> Another cool technology, but for the typical occasional-use consumer-facing site, you can&#8217;t count on it.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2008/09/practical-functional-javascript/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jQuery Profile Plugin</title>
		<link>http://osteele.com/archives/2008/05/jquery-profile-plugin</link>
		<comments>http://osteele.com/archives/2008/05/jquery-profile-plugin#comments</comments>
		<pubDate>Thu, 01 May 2008 10:02:08 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>

		<guid isPermaLink="false">http://osteele.com/2008/05/01/jquery-profile-plugin</guid>
		<description><![CDATA[Yesterday I was profiling a page that used jQuery.  The page took a long time to initialize.  Firebug Profile (a _great_ tool) told me that the time was in jQuery, but that wasn't much help -- the page initialization code had a _lot_ of calls to jQuery, to bind functions to various page elements, and most of them were harmless.

Hence, "jQuery.profile":http://plugins.jquery.com/project/profile.  Stick this in your page, call <code>$.profile.start()</code> to start profiling calls to <code>$(selector)</code>, and then <code>$.profile.done()</code> to stop profiling and print out something like this:]]></description>
			<content:encoded><![CDATA[<p>Yesterday I was profiling a page that used jQuery.  The page took a long time to initialize.  Firebug Profile (a <em>great</em> tool) told me that the time was in jQuery, but that wasn&#8217;t much help &#8212; the page initialization code had a <em>lot</em> of calls to jQuery, to bind functions to various page elements, and most of them were harmless.</p>

<p>Hence, <a href="http://plugins.jquery.com/project/profile">jQuery.profile</a>.  Stick this in your page, call <code>$.profile.start()</code> to start profiling calls to <code>$(selector)</code>, and then <code>$.profile.done()</code> to stop profiling and print out something like this:</p>



<pre>
Selector                 Count  Total  Avg+/-stddev
script, script, scri...    100    101ms  1.01ms+/-1.01
script                   200     58ms  0.29ms+/-0.53
html body #output        100     55ms  0.55ms+/-0.74
script, #output          100     54ms  0.54ms+/-0.73
#output                  100      6ms  0.06ms+/-0.24
</pre>



<p>Or just include <code> ?jquery.profile.start</code> query parameter in your page <span class="caps">URL </span>to begin profiling automatically as soon as the plugin is loaded.</p>

<p>The repository is on <a href="http://github.com/osteele/jquery-profile">GitHub</a>, so you can can comment here or fork it from there if you&#8217;ve got something to say.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2008/05/jquery-profile-plugin/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Mock for {set,clear}{Timeout,Interval}</title>
		<link>http://osteele.com/archives/2008/04/mock-setcleartimeoutinterval</link>
		<comments>http://osteele.com/archives/2008/04/mock-setcleartimeoutinterval#comments</comments>
		<pubDate>Sun, 20 Apr 2008 22:28:08 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>

		<guid isPermaLink="false">http://osteele.com/2008/04/20/a-mock-for-setcleartimeoutinterval</guid>
		<description><![CDATA[Here's a potential "JSSpec":http://jania.pe.kr/aw/moin.cgi/JSSpec spec for <code>Sequentially.trickle.map</code>:

[code language="javascript"]
describe('Sequentially.trickle.map', {
  'should apply to all the elements': function() {
    Sequentially.trickle.map(
      ['a', 'b', 'c'],
       function(x) { return x + 1 },
       1,
       function(result) {
         value_of(result.join(',')).should_be('a1,b1,c1');
       });
    });
  }
});
[/code]

This doesn't work.  The problem is that <code>Sequentially.trickle.map</code> is asynchronous (it defers most of its computation -- including the invocation of the callback -- via @setTimeout@).  This means that @should_be@ isn't called until after the spec has returned.  If it succeeds, this isn't a problem, but if it fails, JSSpec can't associate it with the failing spec -- worse, JSSpec will have already have marked it successful.]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a potential <a href="http://jania.pe.kr/aw/moin.cgi/JSSpec"><span class="caps">JSS</span>pec</a> spec for <code>Sequentially.trickle.map</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Sequentially.trickle.map'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">'should apply to all the elements'</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Sequentially.<span style="color: #660066;">trickle</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>
      <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'b'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'c'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
       <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> x <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
       <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
       <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         value_of<span style="color: #009900;">&#40;</span>result.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">should_be</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a1,b1,c1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>This doesn&#8217;t work.  The problem is that <code>Sequentially.trickle.map</code> is asynchronous (it defers most of its computation &#8212; including the invocation of the callback &#8212; via <code>setTimeout</code>).  This means that <code>should_be</code> isn&#8217;t called until after the spec has returned.  If it succeeds, this isn&#8217;t a problem, but if it fails, <span class="caps">JSS</span>pec can&#8217;t associate it with the failing spec &#8212; worse, <span class="caps">JSS</span>pec will have already have marked it successful.</p>

<p>Here&#8217;s the version that I <a href="http://github.com/osteele/sequentially/tree/master%2Fspecs%2Fsequentially-specs.js?raw=true">actually used</a>:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Sequentially.trickle.map'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">'should apply to all the elements'</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    withMockTimers<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      Sequentially.<span style="color: #660066;">trickle</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'b'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'c'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
         <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> x <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
         <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
         <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
           value_of<span style="color: #009900;">&#40;</span>result.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">should_be</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a1,b1,c1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p><code>withMockTimers</code> temporarily replaces <code>setTimeout</code> and friends with its own deferred execution system, so that it can make sure to call them all before it returns.  Get it <a href="http://github.com/osteele/sequentially/tree/master%2Fspecs%2Fmock-timers.js?raw=true">here</a>.</p>

<h3>Limitations</h3>

<p>This approach has its limits.  It doesn&#8217;t mock <code>new Date</code> to pretend that more time has passed, so whether it works or not will depend on how your code <em>uses</em> the timers (if it keeps an interval running or re-submitting a timeout until an amount of time measured on <code>new Date</code> has passed, it will probably get a &#8220;script running slowly&#8221; error).  And, I don&#8217;t know how kosher it is to replace <code>setTimeout</code> &#8212; this let me test against Firefox 2.0 and Safari 3.1; I haven&#8217;t tried on Opera and <span class="caps">MSIE. </span> Nonetheless, it got me what I wanted here &#8212; unit tests for the new methods in <a href="http://osteele.com/sources/javascript/sequentially">Sequentially</a>.</p>

<p>(I&#8217;ve got a <a href="http://github.com/osteele/lztestkit">more involved implementation</a> that patches the test suite to run callbacks within an emulation of the dynamic scope of the original test function, but it&#8217;s tricky, and I haven&#8217;t got it integrated with <span class="caps">JSS</span>pec &#8212; or any other browser JavaScript implementation &#8212; yet.)</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2008/04/mock-setcleartimeoutinterval/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Conquering the Busy Cursor with Sequentially</title>
		<link>http://osteele.com/archives/2008/04/conquering-busy-cursor-sequentially</link>
		<comments>http://osteele.com/archives/2008/04/conquering-busy-cursor-sequentially#comments</comments>
		<pubDate>Sun, 20 Apr 2008 22:28:00 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>

		<guid isPermaLink="false">http://osteele.com/2008/04/20/conquering-the-busy-cursor-with-sequentially</guid>
		<description><![CDATA[What's wrong with this function?  (Hint: it's meant to execute periodically on a JavaScript page.)

[code language="javascript"]
function updateExpirationText() {
  var now = new Date;
  products.forEach(function(item) {
    var expiresDate = item.expiresDate &#124;&#124; Date.parse(item.expires),
        remaining = expiresDate - now,
        text = remaining < 0 ? 'expired' : msToDuration(remaining);
    $('item-' + item.id + ' .time-remaining').text(remaining);
  });
}
[/code]

It's a trick question.  Maybe nothing's wrong.  But if @products@ can get very long, or if the @msToDuration@ is very slow, you've locked up the UI for a long time.  At best, this makes for sluggish response; at worst, the page that contains this will trigger a "script running slowly" error, and the user will likely abort all the JavaScript on the page.]]></description>
			<content:encoded><![CDATA[<p>What&#8217;s wrong with this function?  (Hint: it&#8217;s meant to execute periodically on a JavaScript page.)</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> updateExpirationText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> now <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #339933;">;</span>
  products.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> expiresDate <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">expiresDate</span> <span style="color: #339933;">||</span> Date.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">expires</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        remaining <span style="color: #339933;">=</span> expiresDate <span style="color: #339933;">-</span> now<span style="color: #339933;">,</span>
        text <span style="color: #339933;">=</span> remaining <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'expired'</span> <span style="color: #339933;">:</span> msToDuration<span style="color: #009900;">&#40;</span>remaining<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'item-'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' .time-remaining'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>remaining<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>It&#8217;s a trick question.  Maybe nothing&#8217;s wrong.  But if <code>products</code> can get very long, or if the <code>msToDuration</code> is very slow, you&#8217;ve locked up the UI for a long time.  At best, this makes for sluggish response; at worst, the page that contains this will trigger a &#8220;script running slowly&#8221; error, and the user will likely abort all the JavaScript on the page.</p>

<p>If this computation only needs to run once, and when (or before) the page loads, you can do it on the server.  But often a computation depends on some aspect of the client state, that isn&#8217;t known when the page is requested.  In this example, the computation depends on the current time (and the current time keeps changing).  In another case, the computation might depend upon the values of some controls or other widgets on the page &#8212; if we&#8217;ve gone all <span class="caps">AJAX</span>y, and want to show the user an instant response, even if that means some client-side computation.</p>

<p>Here&#8217;s an alternative to the function above, that doesn&#8217;t lock up the page.  It uses <code>Sequentially.trickle.forEach</code>, a new function in <a href="http://osteele.com/sources/javascript/sequentially">Sequentially</a>.  This function walks its second argument over some span of the first argument &#8212; up until 250ms has passed, in this case &#8212; and then sleeps for a frame (via <code>setTimeout</code>) before waking up to walk over the next span, until all is done.  This gives time back to the browser (and to other <code>setTimeout</code> and <code>setInterval</code> threads), and avoids the &#8220;script running slowly&#8221; error.  Note the one-line change: <code>&quot;products.forEach(&quot;</code>&#8221; becomes <code>&quot;Sequentially.trickle.forEach(products,&quot;</code>.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> updateExpirationText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> now <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #339933;">;</span>
  Sequentially.<span style="color: #660066;">trickle</span>.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span>products<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> expiresDate <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">expiresDate</span> <span style="color: #339933;">||</span> Date.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">expires</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        remaining <span style="color: #339933;">=</span> expiresDate <span style="color: #339933;">-</span> now<span style="color: #339933;">,</span>
        text <span style="color: #339933;">=</span> remaining <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'expired'</span> <span style="color: #339933;">:</span> msToDuration<span style="color: #009900;">&#40;</span>remaining<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'item-'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' .time-remaining'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>remaining<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">250</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>Sometimes you need to run some code after the iteration is done.  In other words, sometimes you need to transform a function that looks like this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> startTime <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #339933;">;</span>
  array.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Date <span style="color: #339933;">-</span> startTime<span style="color: #339933;">,</span> <span style="color: #3366CC;">'elapsed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>(Here, the code that runs after the iteration just reports how long the iteration took.)</p>

<p>You can do that with a continuation function (or callback), the same as you would with an <span class="caps">AJAX </span>request:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> startTime <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #339933;">;</span>
  Sequentially.<span style="color: #660066;">trickle</span>.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span>array<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">,</span> k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">function</span> k<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Date <span style="color: #339933;">-</span> startTime<span style="color: #339933;">,</span> <span style="color: #3366CC;">'elapsed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>




<p>JavaScript being lexically scoped, you can refer to all the same variables from a nested function (@k@).</p>

<p>There&#8217;s a <code>Sequentially.trickle.map</code> too.  Since the return value can&#8217;t contain the function application results yet when the function returns, you have to get them back from the callback.  Before (the synchronous version):</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> results <span style="color: #339933;">=</span> array.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Results:'</span><span style="color: #339933;">,</span> results<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>and after (asynchronous):</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> startTime <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #339933;">;</span>
  Sequentially.<span style="color: #660066;">trickle</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>array<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">,</span> k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">function</span> k<span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Results:'</span><span style="color: #339933;">,</span> results<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2008/04/conquering-busy-cursor-sequentially/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JCON: Ruby Gem for JSON type conformance</title>
		<link>http://osteele.com/archives/2008/04/jcon-json-type-conformance</link>
		<comments>http://osteele.com/archives/2008/04/jcon-json-type-conformance#comments</comments>
		<pubDate>Thu, 17 Apr 2008 16:07:30 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://osteele.com/2008/04/17/jcon-ruby-gem-for-json-type-conformance</guid>
		<description><![CDATA["JCON":http://jcon.rubyforge.org (the <b>J</b>avaScript <b>Con</b>formance gem) tests JSON values against ECMAScript 4.0-style type definitions
("PDF":http://www.ecmascript.org/es4/spec/overview.pdf) such as <code>string?</code>, <code>(int, boolean)</code>, or <code>[string, (int, boolean), {x:double, y:double}?]</code>.

h3. Usage

[code language="ruby"]
type = JCON::parse "[string, int]"
type.contains?(['a', 1])     # => true
type.contains?(['a', 'b'])   # => false
type.contains?(['a', 1, 2])  # => true
[/code]

JCON also defines an "RSpec":http://rspec.info matcher, @conforms_to_js@:]]></description>
			<content:encoded><![CDATA[<p><a href="http://jcon.rubyforge.org"><span class="caps">JCON</span></a> (the <b>J</b>avaScript <b>Con</b>formance gem) tests <span class="caps">JSON </span>values against <span class="caps">ECMAS</span>cript 4.0-style type definitions<br />
(<a href="http://www.ecmascript.org/es4/spec/overview.pdf"><span class="caps">PDF</span></a>) such as <code>string?</code>, <code>(int, boolean)</code>, or <code>[string, (int, boolean), {x:double, y:double}?]</code>.</p>

<h3>Usage</h3>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">type = JCON::parse <span style="color:#996600;">&quot;[string, int]&quot;</span>
type.<span style="color:#9900CC;">contains</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'a'</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>     <span style="color:#008000; font-style:italic;"># =&gt; true</span>
type.<span style="color:#9900CC;">contains</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'a'</span>, <span style="color:#996600;">'b'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#008000; font-style:italic;"># =&gt; false</span>
type.<span style="color:#9900CC;">contains</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'a'</span>, <span style="color:#006666;">1</span>, <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>  <span style="color:#008000; font-style:italic;"># =&gt; true</span></pre></div></div>




<p><span class="caps">JCON </span>also defines an <a href="http://rspec.info">RSpec</a> matcher, <code>conforms_to_js</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#996600;">'xyzzy'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">should</span> conform_to_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'[int, string]'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#996600;">'xyzzy'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">should_not</span> conform_to_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'[int, string]'</span><span style="color:#006600; font-weight:bold;">&#41;</span>  <span style="color:#008000; font-style:italic;"># 2 isn't a string</span>
<span style="color:#006600; font-weight:bold;">&#123;</span>:x <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">should</span> conform_to_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'{x: int}'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>




<p>Use <span class="caps">JCON </span>together with the <a href="http://osteele.com/archives/2008/04/javascript-fu-rails-plugin">JavaScript Fu Rails plugin</a> to test the argument values to functions in generated JavaScript:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># this will succeed if e.g. response contains a script tag that includes</span>
<span style="color:#008000; font-style:italic;">#   fn(&quot;id&quot;, {x:1, y:2}, true)</span>
response.<span style="color:#9900CC;">should</span> call_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'fn'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>args<span style="color:#006600; font-weight:bold;">|</span>
  args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">should</span> conform_to_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'string'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">should</span> conform_to_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'{x:int, y:int}'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">should</span> conform_to_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'boolean'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># or:</span>
  args.<span style="color:#9900CC;">should</span> conform_to_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'[string, {x:int, y:int}, boolean]'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<h3>Whence</h3>

<p>Github for the <a href="http://github.com/osteele/jcon">sources</a>.</p>

<p>Rubyforge for <a href="http://jcon.rubyforge.org">docs</a>.</p>

<p><code>gem install jcon</code> to install.</p>


<h3>License and version</h3>

<p><span class="caps">MIT</span> License, of course.</p>

<p><span class="caps">JCON </span>is at version 0.1 because it&#8217;s just a few days old and I had to guess about the <span class="caps">ECMAS</span>cript 4.0 type syntax from the examples in the <a href="http://www.ecmascript.org/es4/spec/overview.pdf">overview</a>.  I can&#8217;t imagine that I got everything right.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2008/04/jcon-json-type-conformance/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Three Small JavaScript Libraries</title>
		<link>http://osteele.com/archives/2008/04/three-small-javascript-libraries</link>
		<comments>http://osteele.com/archives/2008/04/three-small-javascript-libraries#comments</comments>
		<pubDate>Tue, 15 Apr 2008 18:36:47 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>

		<guid isPermaLink="false">http://osteele.com/2008/04/15/three-small-javascript-libraries</guid>
		<description><![CDATA[Three small libraries, that I carry with me from project to project:

h3. Fluently -- Construction Kit for Chainable Methods

With Fluently, you can do this:

[code language="javascript"]
    var o = Fluently.make(function(define) {
      define('fn1', function() {console.info('called fn1')});
      define('fn2', function() {console.info('called fn2')});
      define('fn3', function() {return 3});
    });
[/code]

to define an object with chained methods, that can be invoked thus:

[code language="javascript"]
  o.fn1().fn2() // calls fn1 and then fn2
  o.fn2().fn1() // calls fn2 and then fn1]]></description>
			<content:encoded><![CDATA[<p>Three small libraries, that I carry with me from project to project:</p>

<h3>Fluently &#8212; Construction Kit for Chainable Methods</h3>

<p>With Fluently, you can do this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> Fluently.<span style="color: #660066;">make</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>define<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      define<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fn1'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'called fn1'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      define<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fn2'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'called fn2'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      define<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fn3'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>to define an object with chained methods, that can be invoked thus:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  o.<span style="color: #660066;">fn1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fn2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// calls fn1 and then fn2</span>
  o.<span style="color: #660066;">fn2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fn1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// calls fn2 and then fn1</span>
  o.<span style="color: #660066;">fn1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fn3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// returns 3 (an explicit 'return' breaks the chain)</span></pre></div></div>




<p>You can also define modifiers, and aliases:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> Fluently.<span style="color: #660066;">make</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>define<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      define<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fn1'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'called fn1'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      define<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fn2'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'called fn2'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      define.<span style="color: #660066;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'and'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      define.<span style="color: #660066;">alias</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fn3'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'fn1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      define.<span style="color: #660066;">modifier</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'not'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  o.<span style="color: #660066;">fn3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// same as o.fn1()</span>
  o.<span style="color: #660066;">fn1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">and</span>.<span style="color: #660066;">fn2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// same as o.fn1().fn2()</span>
  o.<span style="color: #660066;">fn1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">and</span>.<span style="color: #660066;">not</span>.<span style="color: #660066;">fn2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// options.not is set when fn2 is called</span></pre></div></div>




<p>I used this to build a <a href="http://github.com/osteele/lztestkit">mock and spec construction kit</a>.  I don&#8217;t use Fluently to define the mocks; I use it to define the methods that define the mocks.  Doing all this in one library made my head hurt, so I factored this part of it out.</p>

<p>Git Fluently from <a href="http://github.com/osteele/fluently">here</a>.</p>

<h3><span class="caps">MOP</span> JS</h3>

<p><span class="caps">MOP</span> JS defines utilities for JavaScript metaprogramming.  You don&#8217;t think you need it until you try asynchronous programming, where some methods don&#8217;t have enough information to operate until the response to another method&#8217;s asynchronous request have returned.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MOP.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> propertyName<span style="color: #339933;">,</span> methods<span style="color: #009900;">&#41;</span></pre></div></div>




<blockquote><p>   For each name in <code>methods</code>, defines a method on <code>target</code> with this<br />
    name, that delegates to the method of the <code>propertyName</code> property<br />
    of <code>target</code> with the same name.</p></blockquote>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">new</span> MOP.<span style="color: #660066;">MethodReplacer</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> methods<span style="color: #009900;">&#41;</span></pre></div></div>




<blockquote><p>   When a new MethodReplacer is constructed, it replaces each method<br />
    on <code>object</code> by the method in <code>methods</code> with the same key value, if<br />
    such a method exists.  A MethodReplacer has a single method,<br />
    <code>restore</code>, which restores each method to its pre-replacement<br />
    value.</p></blockquote>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">new</span> MOP.<span style="color: #660066;">QueueBall</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> methodNames<span style="color: #009900;">&#41;</span></pre></div></div>




<blockquote><p>   When a new QueueBall is constructed, it replaces each method named<br />
    by <code>methodNames</code> with a method that enqueues the method call (the<br />
    name of the method and its arguments).  A QueueBall has a single<br />
    method, <code>replayMethodCalls</code>, which plays back the method calls and<br />
    restores the methods.</p></blockquote>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MOP.<span style="color: #660066;">withMethodOverridesCallback</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> methods<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span></pre></div></div>




<blockquote><p>   Calls <code>fn</code> on <code>object</code>, within a dynamic scope within which the<br />
    methods in <code>methods</code> have temporarily replaced the like-named<br />
    methods on <code>object</code>.  The scope is terminated by the argument to<br />
    the call to <code>fn</code>; this argument should be treated as a<br />
    continuation, and restores the methods.</p></blockquote>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  MOP.<span style="color: #660066;">withDeferredMethods</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> methodNames<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span></pre></div></div>




<blockquote><p>   Calls <code>fn</code> on <code>object</code>, within a dynamic scope within which the<br />
    methods in <code>methodNames</code> have been enqueued.  The scope is<br />
    terminated by the argument to the call to <code>fn</code>; this argument<br />
    should be treated as a continuation, and ends the queue, replaying<br />
    the methods.</p></blockquote>

<p>See the <a href="http://github.com/osteele/mop-js/tree/master/specs/mop-specs.js">specs</a> for examples; git <span class="caps">MOP</span> JS from <a href="http://github.com/osteele/mop-js">here</a>.</p>


<h3>Collections JS</h3>

<p>Finally, the Collections library defines framework-independent JavaScript collection methods, for use in browser JavaScript and in ActionScript / OpenLaszlo.  There are many libraries like this; this one is mine.</p>

<p>The <code>Array</code> and <code>String</code> methods extend the class prototype; the <code>Hash</code> methods use a proxying wrapper to avoid prototype pollution.  The methods with the same names as the <span class="caps">ECMAS</span>cript 1.6+ extensions have the same spec as those; the ones with the same name as prototype extensions have the same spec as those in the Prototype library; and there&#8217;s a few odds and ends such as <code>String#capitalize</code>.</p>

<p>I use this when I don&#8217;t want the overhead of Prototype, or want to use these functions in an environment that Prototype doesn&#8217;t run on, such as OpenLaszlo.  It has some overlap with <a href="http://osteele.com/sources/javascript/functional/">Functional</a>, but isn&#8217;t nearly so radical &#8212; this can be an advantage.</p>

<p>Git Collections JS from <a href="http://github.com/osteele/collections-js">here</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2008/04/three-small-javascript-libraries/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JavaScript Fu Rails Plugin</title>
		<link>http://osteele.com/archives/2008/04/javascript-fu-rails-plugin</link>
		<comments>http://osteele.com/archives/2008/04/javascript-fu-rails-plugin#comments</comments>
		<pubDate>Mon, 14 Apr 2008 23:36:05 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://osteele.com/2008/04/14/javascript-fu-rails-plugin</guid>
		<description><![CDATA[JavaScript Fu extends Rails with a few facilities to better integrate JavaScript into Rails development:

1. The @notes@ and @statistics@ rake tasks compass JavaScript files in the @public/javascript@ directory:

[code language="bash"]
$ rake notes
public/javascripts/controls.js:
  * [782] [TODO] improve sanity check
[/code]

[code language="bash"]
$ rake stats
&#124; Name                 &#124; Lines &#124;   LOC &#124; Classes &#124; Methods &#124; M/C &#124; LOC/M &#124;
[...]
&#124; JavaScript           &#124;  7287 &#124;  6322 &#124;       0 &#124;       0 &#124;   0 &#124;     0 &#124;
[...]
[/code]

2. The @call_js@ "RSpec":http://rspec.info/ matcher asserts that a string or response contains a script tag, that contains JavaScript that calls the named function or method:
]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/osteele/jcon">JavaScript Fu</a> extends Rails with a few facilities to better integrate JavaScript into Rails development:</p>

<p>1. The <code>notes</code> and <code>statistics</code> rake tasks compass JavaScript files in the <code>public/javascript</code> directory:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ rake notes
public<span style="color: #000000; font-weight: bold;">/</span>javascripts<span style="color: #000000; font-weight: bold;">/</span>controls.js:
  <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">782</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>TODO<span style="color: #7a0874; font-weight: bold;">&#93;</span> improve sanity check</pre></div></div>





<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ rake stats
<span style="color: #000000; font-weight: bold;">|</span> Name                 <span style="color: #000000; font-weight: bold;">|</span> Lines <span style="color: #000000; font-weight: bold;">|</span>   LOC <span style="color: #000000; font-weight: bold;">|</span> Classes <span style="color: #000000; font-weight: bold;">|</span> Methods <span style="color: #000000; font-weight: bold;">|</span> M<span style="color: #000000; font-weight: bold;">/</span>C <span style="color: #000000; font-weight: bold;">|</span> LOC<span style="color: #000000; font-weight: bold;">/</span>M <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>...<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">|</span> JavaScript           <span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">7287</span> <span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">6322</span> <span style="color: #000000; font-weight: bold;">|</span>       <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">|</span>       <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">|</span>     <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>...<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>




<p>2. The <code>call_js</code> <a href="http://rspec.info/">RSpec</a> matcher asserts that a string or response contains a script tag, that contains JavaScript that calls the named function or method:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">response.<span style="color:#9900CC;">should</span> call_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'fn'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
response.<span style="color:#9900CC;">should</span> call_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'fn(true)'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
response.<span style="color:#9900CC;">should</span> call_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'gApp.setup'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>




<p>If you pass a block to <code>call_js</code>, it&#8217;s called back with the argument list, parsed as though it were a <span class="caps">JSON </span>array:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># matches &lt;script&gt;fn(1, 'aString', {x:10,y:20})&lt; /script&gt;</span>
response.<span style="color:#9900CC;">should</span> call_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'fn'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>args<span style="color:#006600; font-weight:bold;">|</span>
  args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006666;">1</span>
  args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">'aString'</span>
  args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#123;</span>:x <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">10</span>, <span style="color:#ff3333; font-weight:bold;">:y</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">20</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>Use this with <a href="http://jcon.rubyforge.org/"><code>jcon</code></a> to test for type conformance, using <span class="caps">ECMAS</span>cript 4.0 type definitions.  <strike>(Well, you can&#8217;t use it with <code>jcon</code> yet, because I haven&#8217;t released it &#8212; this is just a teaser.  But you can <a href="http://github.com/osteele/jcon">peek</a>.)</strike></p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">response.<span style="color:#9900CC;">should</span> call_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'fn'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>args<span style="color:#006600; font-weight:bold;">|</span>
  args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">should</span> conform_to_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'[Array, (int, boolean)]'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">should</span> conform_to_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'{x: double, y: double}'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># or just:</span>
  args.<span style="color:#9900CC;">should</span> conform_to_js<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'[[Array, (int, boolean)], {x: double, y: double}]'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>3. The <code>page.onload</code> page generator method generates code that executes the content<br />
of the block upon the completion of page load:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page.<span style="color:#9900CC;">onload</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  page.<span style="color:#9900CC;">call</span> alert<span style="color:#996600;">', '</span>page loaded!<span style="color:#996600;">'
end</span></pre></div></div>




<p>These lines generate one of these (depending on whether the <a href="http://ennerchi.com/projects/jrails">jRails</a> plugin has been loaded):</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;window&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;load&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;page loaded!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>





<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;page loaded!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>





<h3>Gitting It</h3>

<p>JavaScript Fu is <a href="http://github.com/osteele/javascript_fu">hosted on git</a>.  If you have git installed, you can clone it into your Rails directory thus:</p>



<pre>
git clone git://github.com/osteele/javascript_fu.git vendor/plugins/javascript_fu
</pre>



<p>If you&#8217;re running off Edge Rails (or, presumably, Rails &gt; 2.0.2), you should be able to do this instead:</p>



<pre>
script/plugin install git://github.com/osteele/javascript_fu
</pre>



<p>Otherwise, you can simply download the tarball from <a href="http://github.com/osteele/javascript_fu/tarball/master">here</a>.</p>

<p><ins>Update: changed the <code>conform_to_js</code> example so that it actually works with the (albeit unreleased) plugin.</ins>.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2008/04/javascript-fu-rails-plugin/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FlashBridge: proxying Flash  OpenLaszlo</title>
		<link>http://osteele.com/archives/2008/04/flashbridge</link>
		<comments>http://osteele.com/archives/2008/04/flashbridge#comments</comments>
		<pubDate>Sun, 13 Apr 2008 11:27:07 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[OpenLaszlo]]></category>

		<guid isPermaLink="false">http://osteele.com/2008/04/13/flashbridge-proxying-flash-openlaszlo</guid>
		<description><![CDATA[I've updated my "OpenLaszlo utility grab-bag":http://github.com/osteele/lzosutils to make browser <-> applet communication even easier.  How easy?


h3. Proxies

Put this in your browser JavaScript:

[code language="javascript"]
var gObject = {
  f: function() { console.info('gObject.f', arguments) },
  g: function() { console.info('gObject.g', arguments) }
};
[/code]

And this in an OpenLaszlo applet:

[code language="javascript"]
var gObject = FlashBridge.createRemoteProxy('gObject', ['f', 'g']);
gObject.f(1, 2);
gObject.g(3);
[/code]

When you run the applet code, it prints this to the browser console:]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve updated my <a href="http://github.com/osteele/lzosutils">OpenLaszlo utility grab-bag</a> to make browser &lt;-&gt; applet communication even easier.  How easy?</p>


<h3>Proxies</h3>

<p>Put this in your browser JavaScript:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> gObject <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  f<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'gObject.f'</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  g<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'gObject.g'</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>




<p>And this in an OpenLaszlo applet:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> gObject <span style="color: #339933;">=</span> FlashBridge.<span style="color: #660066;">createRemoteProxy</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'gObject'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'f'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'g'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
gObject.<span style="color: #660066;">f</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
gObject.<span style="color: #660066;">g</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>When you run the applet code, it prints this to the browser console:</p>



<pre>
gObject.f [1,2]
gObject.g [3]
</pre>



<p>That&#8217;s right, Flash is invoking the function calls, but they&#8217;re executing in the browser.</p>

<p>Now switch these around &#8212; put the first block in the applet, and the second block in the browser JavaScript &#8212; and it still runs the same way, except that it&#8217;s the <strong>browser</strong> that invokes the functions, and they run in the <strong>applet</strong> (and print to the OpenLaszlo debug console, if the applet was compiled with debugging on).</p>

<p>(By the way, the full sources for the examples are <a href="http://github.com/osteele/lzosutils/tree/master/test/flashbridge">here</a>.)</p>


<h3>Return Values</h3>

<p>Callbacks, or continuations for return values, make it easy for the applet to operate on the return value from a call into the browser, even though these calls are asynchronous.</p>

<p>Put this in the browser:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> gService <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  add<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    logCall<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'gBrowserObject.add'</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> a<span style="color: #339933;">+</span>b<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  error<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    logCall<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'gBrowserObject.error'</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">throw</span> msg<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>




<p>And this in the applet:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">gBrowserObject.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onreturn</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'1 + 2 -&gt; '</span> <span style="color: #339933;">+</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
gBrowserObject.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'error msg'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onexception</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'error !&gt; '</span> <span style="color: #339933;">+</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>The argument to <code>onreturn</code> is called (asynchronously) with the return value.  The argument to <code>onexception</code> is called with the message from the exception, if an exception occurred.</p>

<p>Callbacks, unlike proxies, only work one direction &#8212; for calls from the applet to the browser.  That&#8217;s not for a technical reason &#8212; I&#8217;ve just only needed it one direction so far.</p>


<h3>Call Storage</h3>

<p>Browser code can call into the applet even if the applet hasn&#8217;t initialized yet, and vice versa.</p>

<p>To implement this, each side of the bridge stores calls (and return value handlers) in a <a href="http://en.wikipedia.org/wiki/Mailbox_%28computing%29">mailbox</a> until it hears back that the other side has loaded.  Once this happens, the mailboxes are flushed and the remote call methods switch to direct invocation.</p>

<p>This works around a couple of race conditions.  First, the applet won&#8217;t generally have run its initialization code by the time the browser receives its load event, so a naive implementation of the bridge wouldn&#8217;t allow the browser to make calls into the applet until the browser had heard back that the applet had loaded &#8212; which is hard to detect.  (It isn&#8217;t enough to wait for the object&#8217;s onload event, because this can trigger before the first frame of the movie plays, so the applet may still not have initialized enough to receive messages.)  Conversely, depending on your page organization and initialization raindance, the applet might load before page side has registered &#8212; so the applet couldn&#8217;t call into the page until an unknown time.</p>


<h3>Security Implications</h3>

<p>FlashBridge, by default, allows the browser to call anything sitting in the applet, and vice versa.  This increases the attack surface of your application, because it allows an embedded Flash applet to invoke any part of it.  This means that an <span class="caps">XSS </span>can tunnel through your applet to gain access to any site with a <code>crossdomain.xml</code> file that allows your applet to connect to it &#8212; something that <span class="caps">XSS </span>on a pure JavaScript page can&#8217;t do.</p>

<p>It you prefer not to audit your application against this, you can call <code>FlashBridge.secure</code> to prevent it from accepting arbitrary calls, and then <code>FlashBridge.register</code> to register callins.</p>

<p>There&#8217;s no lockdown facility in the other direction &#8212; to lock down the browser JavaScript against calls from the Flash application.  That&#8217;s because it&#8217;s trivial for a Flash application to invoke arbitrary JavaScript in the browser context &#8212; in fact, that&#8217;s how the applet -&gt; browser communication is implemented, and if that were secured at the FlashBridge layer, the vulnerability would still be accessible one layer down.</p>


<h3>Gitting It</h3>

<p>All this is in the <a href="http://github.com/osteele/lzosutils">LzOsUtils project on GitHub</a>, with examples <a href="http://github.com/osteele/lzosutils/tree/master/test/flashbridge">here</a>.  Download it via the Download button, clone it via <code>git clone git://github.com/osteele/lzosutils.git</code>, or add it as a submodule to an existing git repo via <code>git add submodule git://github.com/osteele/lzosutils.git</code>.</p>

<p>FlashBridge is written for OpenLaszlo, but would probably run in straight Flash too. And it uses my own funky alternative to <code>ExternalInterface</code> for calling from Flash to the browser (since the built-in <span class="caps">API </span>is <a href="http://codinginparadise.org/weblog/2005/12/serious-bug-in-flash-8.html">seriously broken</a>), but it could be ported to run on top of Dojo or something pretty easily.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2008/04/flashbridge/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Synchronizing Client Models</title>
		<link>http://osteele.com/archives/2008/02/synchronizing-client-models</link>
		<comments>http://osteele.com/archives/2008/02/synchronizing-client-models#comments</comments>
		<pubDate>Wed, 27 Feb 2008 19:53:12 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[Essays]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://osteele.com/2008/02/27/synchronizing-client-models</guid>
		<description><![CDATA[You're implementing a client-server application.  The client is in JavaScript.  It contains a model class, @Person@.  The model is backed by a server-side @Person@ model, and a REST controller at @/person@.  Periodically, the client updates the server's model, but there can be client-side instances that don't yet exist on the server, such as when a model is first created and the server hasn't yet gotten the message.

I've written this code a few times now, in JavaScript, and in ActionScript.  if If you write it the obvious way, you run into an interesting set of race conditions.  Here's the code, and the race conditions, and some ad-hoc solutions.  In the next post, I'll introduce a metaobject pattern, <dfn>queue ball</dfn>, that I've used to solve these race conditions in a more principled and re-usable fashion.]]></description>
			<content:encoded><![CDATA[<p>You&#8217;re implementing a client-server application.  The client is in JavaScript.  It contains a model class, <code>Person</code>.  The model is backed by a server-side <code>Person</code> model, and a <span class="caps">REST </span>controller at <code>/person</code>.  Periodically, the client updates the server&#8217;s model, but there can be client-side instances that don&#8217;t yet exist on the server, such as when a model is first created and the server hasn&#8217;t yet gotten the message.</p>

<p>I&#8217;ve written this code a few times now, in JavaScript, and in ActionScript.  if If you write it the obvious way, you run into an interesting set of race conditions.  Here&#8217;s the code, and the race conditions, and some ad-hoc solutions.  In the next post, I&#8217;ll introduce a metaobject pattern, <dfn>queue ball</dfn>, that I&#8217;ve used to solve these race conditions in a more principled and re-usable fashion.</p>

<p><i>Note: As of 2008-02-28, none of this code has been tested.  It&#8217;s all extracted from code that&#8217;s <em>like</em> the code here, but I haven&#8217;t copied and pasted these specific examples into an execution environment, which probably means they fail.</i></p>

<h3>Getting <code>Person</code>al</h3>

<p>Here&#8217;s the model, with some server proxy mojo mixed in:<sup class="footnote"><a href="#fn1">1</a></sup></p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// creates a client-only instance</span>
<span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span> <span style="color: #339933;">=</span> attributes<span style="color: #339933;">||</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// if a server mirror exists, this.id is set to its id </span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// creates a client instance that is mirrored by a new server instance</span>
Person.<span style="color: #660066;">create</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> person <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  person.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> person<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Person.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// creates a server instance for this client instance</span>
  create<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/create'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> data.<span style="color: #660066;">id</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">//  updates attributes of this instance, and, if it exists, its server mirror</span>
  update<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Hash.<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span><span style="color: #339933;">,</span> attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">&amp;&amp;</span> jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/update/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #339933;">,</span> attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// deletes this instance's server mirror</span>
  remove<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">&amp;&amp;</span> jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/delete'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">delete</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>This implementation uses <a href="http://jquery.com/">jQuery</a> for transport, and assumes a <code>Hash.merge</code> method from some collection library (say, Prototype&#8217;s).  It creates a class by setting <code>prototype</code> directly, and it doesn&#8217;t detect or recover from <span class="caps">XHR </span>errors.  All these choices are just to have something concrete to write about; they don&#8217;t affect the substance of this article.</p>

<h3>A Day at the Races</h3>

<p>Do you see the race conditions?  There&#8217;s at least three: <code>create+update</code>, <code>create+delete</code>, and <code>update+update</code>.</p>

<h4>Race Condition 1: Create then Update</h4>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> createThenUpdate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> aPerson <span style="color: #339933;">=</span> Person.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  aPerson.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'Edgar Dijkstra'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>The problem with <code>createThenUpdate</code> is that <code>aPerson</code> won&#8217;t have an <code>id</code> by the time <code>update</code> is called, so <code>update</code> won&#8217;t send the new values to the server.  The call to <code>create</code> is synchronous, but the communication with the server, and therefore the call to the callback (that sets <code>aPerson.id</code>) is <em>a</em>synchronous, and therefore won&#8217;t occur until <code>Person.create</code> returns.</p>

<p>In detail:</p>


<ol>
<li><code>createUpdate</code> calls <code>Person.create</code></li>
<li><code>Person.create</code> calls <code>new Person</code></li>
<li><code>aPerson.create</code> calls <code>jQuery.post</code></li>
<li><code>jQuery.post</code> calls <code>XMLHttpRequest.send</code> (not shown)</li>
<li><code>XMLHTTPRequest.send</code>, <code>jQuery.post</code>, and <code>aPerson.create</code> <em>return</em></li>
<li><code>createUpdate</code> calls <code>aPerson.update</code></li>
<li><i>[time passes]</i></li>
<li>Client sends <span class="caps">HTTP</span> Request to server</li>
<li><i>[more time passes]</i></li>
<li>Client receives <span class="caps">HTTP</span> Response</li>
<li>Callback in <code>aPerson.create</code> sets <code>aPerson.id</code></li>
</ol>



<h4>Solution 1: Explicit Callbacks</h4>

<p>One solution to this problem is to thread the code through callbacks (in effect, <a href="http://en.wikipedia.org/wiki/Continuation-passing_style#Use_and_implementation">performing <span class="caps">CPS </span>conversion by hand</a>).  <code>aPerson.create</code> calls a callback function once <em>it&#8217;s internal callback function</em> is called, so <code>Person.create</code> takes a callback parameter too, and so on up the call chain.  (In this case, the buck stops here.)</p>

<p>Let&#8217;s add a callback parameter to <code>Person.create</code>, that is called once the <span class="caps">HTTP </span>response to <code>/person/create</code> is received.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Person.<span style="color: #660066;">create</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> person <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  person.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> person<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Person.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// creates a server instance for this client instance</span>
  create<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/create'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> data.<span style="color: #660066;">id</span><span style="color: #339933;">;</span>
      callback <span style="color: #339933;">&amp;&amp;</span> callback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>Then we can rewrite <code>createThenUpdate</code> thus:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> createThenUpdate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> aPerson <span style="color: #339933;">=</span> Person.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    aPerson.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'Edgar Dijkstra'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<h4>Adding the UI</h4>

<p>It was easy to spot the race condition in <code>createThenUpdate</code> &#8212; and easy to fix it &#8212; because the calls to <code>create</code> and the <code>update</code> were in consecutive statements, within the same function.  In the real world, they&#8217;re at the bottom of different call chains, as in this jQuery code that binds some model actions to an <span class="caps">HTML </span>view:<sup class="footnote"><a href="#fn2">2</a></sup></p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#person create-button'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">disable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// avoid double-creation</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#person update-button'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">enable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  gCurrentModel.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#person update-button'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  gCurrentModel.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#person'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>Click &#8220;<tt>create</tt>&#8220;, edit a field, and then click &#8220;<tt>update</tt>&#8220;.  Sometimes the update will hit the server, sometimes it won&#8217;t: it depends on whether the response to the <code>/person/create</code> request has returned by the time you click the second button.  We&#8217;ve just created an <span class="caps">AJAX </span>version of the <a href="http://www.ibiblio.org/harris/500milemail.html">500-mile bug</a>.</p>

<p>Let&#8217;s thread the callbacks through this code, in order to avoid enabling the &#8220;<tt>update</tt>&#8221; button until the callback is called:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#person create-button'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">disable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// avoid double-creation</span>
  gCurrentModel.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#person update-button'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">enable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#person update-button'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/* unchanged */</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>This is awful!  First, it requires you to weave callbacks through both your view and your model code.<sup class="footnote"><a href="#fn3">3</a></sup>  But worse, it&#8217;s a <em>leaky abstraction</em>.  The view layer has to know about an arbitrary (from the outside) limitation &#8212; that you can&#8217;t <em>call</em> <code>update</code> until <code>create</code> has <em>called its callback</em> &#8212; of the model layer.</p>

<h4>Solution 2: Implicit Callbacks</h4>

<p>Another solution is to use a library such as <a href="http://www.neilmix.com/narrativejs">Narrative JavaScript</a> or <a href="http://www.xucia.com/strands-doc/index.html">JavaScript Strands</a>, that does the <span class="caps">CPS </span>conversion (adds the callbacks) <em>for you</em>.  I like this approach a lot, but I do a lot of work in contexts where those compilers aren&#8217;t applicable<sup class="footnote"><a href="#fn4">4</a></sup>, and many folks (often including, for these reasons and others, me) prefer to work in pure JavaScript. I therefore won&#8217;t go further down that path here.</p>

<h4>Solution 3: Action Queue</h4>

<p>Finally, we can add a queue to the model.  With the modification below, calling <code>update</code> while the model is waiting for an id no longer drops server updates; it simply queues them for playback once the response to <code>/person/create</code> is received.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Person.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  _updateQueue<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
&nbsp;
  create<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>._updateQueue <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/create'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> data.<span style="color: #660066;">id</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._updateQueue.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._sendUpdate<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._updateQueue.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">delete</span> <span style="color: #000066; font-weight: bold;">this</span>._updateQueue<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// the caller must treat `attributes` as deep-frozen once</span>
  <span style="color: #006600; font-style: italic;">// this method has been called</span>
  update<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Hash.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span><span style="color: #339933;">,</span> attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">this</span>._sendUpdate<span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._updateQueue<span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">this</span>._updateQueue.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  _sendUpdate<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/update/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #339933;">,</span> attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>We can use a &#8220;method algebra&#8221; to optimize this a bit:  It doesn&#8217;t matter how many times <code>update</code> is <em>called</em> while waiting for the <code>create</code> response &#8212; it only needs to <em>send</em> an update <em>once</em>.  (The algebra is that there&#8217;s an operation <code>+: update</code> &times; <code>update</code> &#8594; <code>update</code> that can combine consecutive updates <code>update</code><sub>1</sub> + <code>update</code><sub>2</sub> = <code>update</code><sub>3</sub>.)</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Person.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  _pendingUpdates<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
&nbsp;
  create<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>._pendingUpdates <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/create'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> data.<span style="color: #660066;">id</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._pendingUpdates<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._sendUpdate<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>. _pendingUpdates<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">delete</span> <span style="color: #000066; font-weight: bold;">this</span>. _pendingUpdates<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  update<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Hash.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span><span style="color: #339933;">,</span> attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">this</span>._sendUpdate<span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._pendingUpdates<span style="color: #009900;">&#41;</span>
      Hash.<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._pendingUpdates<span style="color: #339933;">,</span> attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  _sendUpdate<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/update/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #339933;">,</span> attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>I&#8217;m going to back off from this optimization, though.  The reason is that it only works if the two calls to <code>update</code> are <em>consecutive</em> &#8212; when there are no intervening calls that also send messages that operate on the same instance.  With a more full-featured <span class="caps">API </span>(with more actions that send messages to the server), this won&#8217;t generally be true.</p>

<p>For example, let&#8217;s extend <code>Person</code> with a <code>setPermissions</code> method.  If we could ignore race conditions, this method might look like this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Person.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  _pendingUpdates<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
&nbsp;
  setPermissions<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>permissions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">permissions</span> <span style="color: #339933;">=</span> permissions<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">&amp;&amp;</span> jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/set_permissions'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #339933;">,</span> permissions<span style="color: #339933;">:</span>permissions<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>This naive implementation is vulnerable to a <code>create+setPermissions</code> race condition analogous to the <code>create+update</code> race condition that we just fixed, though.  We can fix them both by generalizing the post-create queue, so that it can contain arbitrary actions, not just update records:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Person.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  _pendingActions<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
&nbsp;
  create<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>._pendingActions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/create'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> data.<span style="color: #660066;">id</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._pendingActions.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> action <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._pendingActions.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>action.<span style="color: #660066;">methodName</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> action.<span style="color: #660066;">arguments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066; font-weight: bold;">delete</span> <span style="color: #000066; font-weight: bold;">this</span>._pendingActions<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  update<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Hash.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span><span style="color: #339933;">,</span> attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">this</span>._sendUpdate<span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._pendingActions<span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pendingUpdates</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>methodName<span style="color: #339933;">:</span><span style="color: #3366CC;">'_sendUpdate'</span><span style="color: #339933;">,</span> arguments<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>attributes<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  _sendUpdate<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/update/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #339933;">,</span> attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  setPermissions<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>permissions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">permissions</span> <span style="color: #339933;">=</span> permissions<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">this</span>._sendSetPermissions<span style="color: #009900;">&#40;</span>permissions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._pendingActions<span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pendingUpdates</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>methodName<span style="color: #339933;">:</span><span style="color: #3366CC;">'_sendSetPermissions'</span><span style="color: #339933;">,</span> arguments<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>permissions<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  _sendSetPermissions<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>permissions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/set_permissions'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #339933;">,</span> permissions<span style="color: #339933;">:</span>permissions<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>





<h4>Race Condition 2: Create then Delete</h4>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> createThenDelete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> aPerson <span style="color: #339933;">=</span> Person.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  aPerson.<span style="color: #000066; font-weight: bold;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>By now, you should be able to spot the problem here.  The reasoning is exactly the same as for <code>update</code>: when <code>delete</code> is called, <code>aPerson</code> won&#8217;t yet have an id.</p>

<p>We could fix this with a callback:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> createThenDelete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> aPerson <span style="color: #339933;">=</span> Person.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    aPerson.<span style="color: #000066; font-weight: bold;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>This has the attendant disadvantages of having to bake knowledge about the client-server protocol into <code>Person</code>&#8217;s clients, and having to thread callbacks through the <span class="caps">UI. </span> After all, it&#8217;s rare that we would create a <code>Person</code> simply to delete it; the more common case is that the creation and deletion would be at the bottom of different call chains &#8212; often initiated from outside the application, in response to user actions &#8212; such that it&#8217;s difficult to thread the first as a callback of the second.  And note that, as with <code>create+update</code>, we can&#8217;t simply ignore the <code>delete</code> unless the server creation has responded: if we do this, we&#8217;ll occasionally drop a <code>delete</code> on the floor, because it was called <em>after</em> the <code>create</code> was <em>sent</em>, but <em>before</em> the <em>response</em>.</p>

<p>The best local solution is to build on the <em>action queue</em> solution above &#8212; by simply adding another method to the queue.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Person.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">delete</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._pendingActions<span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pendingUpdates</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>methodName<span style="color: #339933;">:</span><span style="color: #3366CC;">'_sendDelete'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
      <span style="color: #000066; font-weight: bold;">delete</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  _sendDelete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/delete'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">delete</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>This <em>works</em>, but it should make you uncomfortable.  We&#8217;re adding (almost) <em>the same conditional</em> to <em>every single method</em>.</p>


<h4>Race Condition 3: Overlapping Updates</h4>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> updateThenUpdate<span style="color: #009900;">&#40;</span>aPerson<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  aPerson.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'Edgar Djikstra'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  aPerson.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'Edgar Dijkstra'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>From looking at <code>updateThenUpdate</code>, it looks like the first call to <code>update</code> will occur <em>before</em> the second.  And it does! (Duh.)  And it looks like the misspelled name in the first call will be replaced by the correct name in the second call.  And it will!  (Well&#8230;on the client&#8230;read on.)  Because: the first call to <code>XMLHttpRequest.send</code> (with the misspelled name) occurs before the second call to <code>XMLHttpRequest.send</code> (with the correction), and the client therefore sends the message with the misspelled name before it sends the message with the correction.  But our run of good luck stops here.  There is, unfortunately no guarantee about the order in which the <em>server</em> will <em>receive</em> these messages.  Generally, the first message will be received before the second.  Sometimes, they will arrive in the other order, and the misspelling will overwrite the correction.</p>

<p>There are two ways to fix this problem: by <strong>sequencing messages</strong>, or by <strong>holding outgoing messages</strong> (holding each outgoing message until the previous one returns).  Sequencing messages is the higher-performance solution (it doesn&#8217;t hold up messages), but requires more work and involves switching both the client and the server from a straight <span class="caps">REST API, </span>which may not be possible<sup class="footnote"><a href="#fn5">5</a></sup>.</p>

<p>For simplicity, we&#8217;ll look at the second solution: holding outgoing messages.  This solution has the advantage that the general-purpose solution to the other race conditions (presented in the next article) happens to implement it too.  (In this article, we&#8217;ll implement with an explicit <code>Serialized</code> object instead.)  Message sequencing doesn&#8217;t help with those other cases at all: the problem with them is that the second message is never sent, not that it&#8217;s sent out of order.</p>

<p>Here&#8217;s a quick-and-dirty implementation of the <em>hold outgoing messages</em> solution.  The following code defines <code>Serialized.post</code> as a drop-in replacement for <code>jQuery.post</code>, that refuses to post data until the previous post has completed (successfully, or with an error).<sup class="footnote"><a href="#fn6">6</a></sup></p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Serialized <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  queue<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// arguments for pending </span>
  defer<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
  post<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> data<span style="color: #339933;">,</span> callback<span style="color: #339933;">,</span> type<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">defer</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">queue</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">defer</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    jQuery.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>url<span style="color: #339933;">:</span>url<span style="color: #339933;">,</span> type<span style="color: #339933;">:</span><span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span> data<span style="color: #339933;">:</span>data<span style="color: #339933;">,</span> success<span style="color: #339933;">:</span>success<span style="color: #339933;">,</span> complete<span style="color: #339933;">:</span>complete.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">function</span> complete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">queue</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">post</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">queue</span>.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">defer</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<h3>Next Up: Queue Ball</h3>

<p>I&#8217;d like to factor all those conditionals out of the <code>Person</code> methods.  Then I&#8217;d like to extract the queue code from <code>create</code>, so that I can use it on <code>update</code> (to solve the <code>update+update</code> problem).  Finally, there are some general-purpose techniques here, so I&#8217;d like to extract the whole mess from <code>Person</code>, where I can apply it to any model (or to code that has some of the same concerns, even if it&#8217;s not synchronized model code).  But this post is already long enough, so I&#8217;ll just close with the promise to write that up, so that I have to do it.</p>

<p><hr/></p>

<p class="footnote" id="fn1"><sup>1</sup> Would you rather have code with a cleaner separation of concerns?  Here it is.  You&#8217;ll find that it doesn&#8217;t make the race conditions go away, but that it doesn&#8217;t change the set of techniques for solving them.  (It does make the &#8220;explicit callbacks&#8221; solution <em>even worse</em>.)  I&#8217;ve therefore stuck with the double-duty <code>Person</code> implementation in the body of this article, to make the code easier to follow.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span> <span style="color: #339933;">=</span> attributes <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">proxy</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Person.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  create<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">proxy</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> PersonProxy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">proxy</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  update<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Hash.<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span><span style="color: #339933;">,</span> attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">proxy</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">proxy</span>.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  remove<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">proxy</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">delete</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">proxy</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> PersonProxy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
PersonProxy.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  create<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/create'</span><span style="color: #339933;">,</span> attributes<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> id <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  update<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">&amp;&amp;</span> jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/update/'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">attributes</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  remove<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">&amp;&amp;</span> jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/person/delete'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">delete</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




<p class="footnote" id="fn2"><sup>2</sup> This implementation somewhat mixes the model with the view.  It&#8217;s not the clearest code.  It would be cleaner if it used listeners and reactive programming techniques &#8212; but the fact that it&#8217;s so explicit makes it easier to follow what&#8217;s going on.</p>

<p class="footnote" id="fn3"><sup>3</sup> I&#8217;ve used this approach, and it wipes the floor with using listeners or delegates or other unthreaded callbacks, where you have to store state in objects in order to match listeners with their context, but it&#8217;s still a pain to maintain.</p>

<p class="footnote" id="fn4"><sup>4</sup> <span class="caps">CPS </span>conversion introduces a lot of function allocations and invocations.  I&#8217;ve been scared to try a system that introduces them globally, instead of letting me judiciously thread a few callbacks in by hand, when developing for a slow <span class="caps">ECMAS</span>cript implementation such as Flash &lt; 9 or <span class="caps">MSIE.  </span>(I even use <a href="/sources/javascript">my own libraries</a> sparingly in such a situation.)</p>

<p class="footnote" id="fn5"><sup>5</sup> <span class="caps">XMPP </span>preserves message order, by sending all the messages over a single stream.  One could also add a sequence number to each message.  The receiver (in this case, the server) should buffer messages that arrive out of order, so that it can process them in the order in which they occur.  This is how a streaming protocol such as <span class="caps">TCP </span>is implemented: by adding sequence numbers and buffering on top of an unordered protocol such as <span class="caps">IP.  HTTP </span>is implemented on top of <span class="caps">TCP, </span>but only uses <span class="caps">TCP </span>to preserve the order of packets within a message, so multiple <span class="caps">HTTP </span>requests (and responses) can get out of order again.  It seems that keepalive might fix the problem, and that load balancers might re-introduce it, and that affinity might fix it again, but only if you can guarantee that your load balancer is properly configured.  But I&#8217;m getting out of my depth here.</p>

<p class="footnote" id="fn6"><sup>6</sup> This code assumes that a request will never take longer than the client timeout setting to reach the server.  Otherwise, <code>complete</code> could be called before the server receives the first message, the client would send the next message, and the server would process them out of order.  That&#8217;s one reason I called this implementation quick-and-dirty.</p>]]></content:encoded>
			<wfw:commentRss>http://osteele.com/archives/2008/02/synchronizing-client-models/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
