<?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>le accidental occurrence &#187; Javascript</title>
	<atom:link href="http://blog.42at.com/tag/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.42at.com</link>
	<description>42at</description>
	<lastBuildDate>Thu, 09 Jun 2011 07:00:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>node.js: anatomy of a net connection</title>
		<link>http://blog.42at.com/node-js-anatomy-of-a-net-connection</link>
		<comments>http://blog.42at.com/node-js-anatomy-of-a-net-connection#comments</comments>
		<pubDate>Sun, 01 Aug 2010 18:20:45 +0000</pubDate>
		<dc:creator>Moos</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://blog.42at.com/?p=193</guid>
		<description><![CDATA[Been looking into the awesome node.js project, &#8220;evented I/O&#8221; server-side Javascript running on V8.  I got introduced to node.js while attending a Bayjax meeting back in May dubbed Cinco de Node.js with node.js&#8217; creator Ryan Dahl presenting (video). My little side project (more on that later) involved delving into the internals of the node.js Javascript [...]]]></description>
			<content:encoded><![CDATA[<p>Been looking into the awesome <a href="http://nodejs.org/">node.js</a> project, &#8220;evented I/O&#8221; server-side Javascript running on V8.  I got introduced to node.js while attending a Bayjax meeting back in May dubbed <a href="http://www.meetup.com/BayJax/calendar/13308905/">Cinco de Node.js</a> with node.js&#8217; creator Ryan Dahl presenting (<a href="http://www.yuiblog.com/blog/2010/05/20/video-dahl/">video</a>).</p>
<p>My little side project (more on that later) involved delving into the internals of the node.js Javascript library and figuring out how a network connection was made.  It turns out to be quite nontrivial with the myriad async calls and network socket handshaking.  I started to document the connection process and wanted to share it.  Perhaps others will find it useful also.</p>
<p>A typical client-server connection code can found in the node.js test harness, eg, <a href="http://github.com/ry/node/blob/master/test/simple/test-http-1.0.js">test-http-1.0</a>.  Create a server:</p>
<pre class="brush: jscript;">

var server = http.createServer(function (req, res) {
res.writeHead(200, {&quot;Content-Type&quot;: &quot;text/plain&quot;});
res.end(&quot;hello world\n&quot;);
})
server.listen(PORT);
</pre>
<p>and the client:</p>
<pre class="brush: jscript;">

var c = net.createConnection(80);
c.setEncoding(&quot;utf8&quot;);
c.addListener(&quot;connect&quot;, function () {
 c.write( &quot;GET / HTTP/1.0\n\n&quot; );
});

c.addListener(&quot;data&quot;, function (chunk) {
 console.log(chunk);
 });

c.addListener(&quot;end&quot;, function () {
 c.end();
 server.close();
});
</pre>
<p>The server goes through the regular socket(), bind(), listen() routine.  It then kicks off its readWatcher which listens() for any connections.  Once a connect request comes in, it creates a  peer socket and pairs it to the incoming connection.  It sets the peer&#8217;s socket error state to zero (0), which tells the peer that the connection was successful. Finally, it kicks off connectionListener(), which waits for incoming data.  The server logic is shown in this diagram: (click for full image)</p>
<div id="attachment_194" class="wp-caption alignnone" style="width: 624px"><a href="http://blog.42at.com/wp-content/uploads/2010/08/node-net-server.png"><img class="size-large wp-image-194 " title="node-net-server" src="http://blog.42at.com/wp-content/uploads/2010/08/node-net-server-1024x478.png" alt="" width="614" height="287" /></a><p class="wp-caption-text">node.js server connection diagram</p></div>
<p>Meanwhile, back at the client, net.createConnection() calls the doConnect() routine which calls socket connect().  If a server is found at the address:port, the client&#8217;s socket state is set to &#8216;connect in progress&#8217; (EINPROGRESS), and the socket is made writable.  This triggers the client&#8217;s writeWatcher, which checks the client&#8217;s socket error state for zero (connected).  When the socket is connected, the client&#8217;s readWatcher is kicked off which listens for response data, and it emits the &#8216;connect&#8217; event, indicating the client is ready to make a request.  The client logic is show in the following diagram (click for full image):</p>
<div id="attachment_195" class="wp-caption alignnone" style="width: 624px"><a href="http://blog.42at.com/wp-content/uploads/2010/08/node-net-client.png"><img class="size-large wp-image-195 " title="node-net-client" src="http://blog.42at.com/wp-content/uploads/2010/08/node-net-client-1024x620.png" alt="" width="614" height="372" /></a><p class="wp-caption-text">node.js client connection diagram</p></div>
<p>The exact sequence of events and calls may change as node.js is under fast-paced development (the diagrams correspond to roughly v0.1.96).  But it should give a good overview of the sequence of events and relation to the callbacks.</p>
<p>More node.js goodness will be coming.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.42at.com/node-js-anatomy-of-a-net-connection/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>_xiterator: an expression iterator for Underscore</title>
		<link>http://blog.42at.com/_xiterator-an-expression-iterator-for-underscore</link>
		<comments>http://blog.42at.com/_xiterator-an-expression-iterator-for-underscore#comments</comments>
		<pubDate>Thu, 15 Apr 2010 20:41:47 +0000</pubDate>
		<dc:creator>Moos</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[underscore]]></category>

		<guid isPermaLink="false">http://blog.42at.com/?p=186</guid>
		<description><![CDATA[Underscore.js is an excellent, compact Javascript  library extending the language with useful tools.  Most of the utils are drawn from Prototype and/or been inspired by languages such as Ruby. Underscore reverts to native code where it is supported. A hand-full of so-called Collection functions which operate on arrays &#38; objects, such as _any(), _all(), _map(), and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://documentcloud.github.com/underscore/">Underscore.js</a> is an excellent, compact Javascript  library extending the language with useful tools.  Most of the utils are drawn from <a href="http://prototypejs.org">Prototype</a> and/or been inspired by languages such as Ruby. Underscore reverts to native  code where it is supported.</p>
<p>A hand-full of so-called Collection functions which operate on arrays &amp; objects, such as _any(), _all(), _map(), and _detect(), take an iterator function as an argument:</p>
<pre class="brush: jscript;">
if ( _.any([1,2,3], function iterator(value){
    return ! _.isNumber(value);
})) {
    // ...
}
</pre>
<p>_.isNumber() is one of a dozen or so type checking routines provided by Underscore, in addition to, _isString(), _isFunction(), _isArray(), and others.</p>
<p>Often times the iterator is a simple type-checking, like above, or some sort of expression that needs to be evaluated.  So why not just enter the expression:</p>
<pre class="brush: jscript;">if (_.any([1,2,3], &quot;!isNumber&quot;)) {
 // ...
}</pre>
<p>This is what <strong>_xiterator</strong>, an add-on for Underscore, provides.  It  is simply the meat of the iterator in a compact semantic.</p>
<p>Let&#8217;s throw in some range checking:</p>
<pre class="brush: jscript;">
if (_.any([1,2,3], function iterator(value){
    return ! _.isNumber(value) || value &lt; 0 || value &gt; 10;
})){
    // ...
}
</pre>
<p>can be  replaced by:</p>
<pre class="brush: jscript;">
if (_.any([1,2,3], &quot;!isNumber || &lt; 0 || &gt; 10&quot;)){
    // ....
}
</pre>
<h2>Expression iterators</h2>
<p>The expression is composed of valid Javascript code.  In the case of relational operators (&lt;, &lt;=, ==, etc.) the left operand is <em>implied</em> to be the value of the iterator.  The expression is evaluated in global scope.</p>
<p>Any of the Underscore _.isXXX type-checking routines can be used without the argument (the _.  part is implied).  In addition, three new routines are added: <em>isBlank</em>, <em>isOdd</em>, <em>isEven</em>.</p>
<p>Parenthetical expressions are fully supported:</p>
<pre class="brush: jscript;"> _.any([1,2,3], &quot;(isNumber &amp;&amp; &gt; 0) || (isString &amp;&amp; !isBlank)&quot;);</pre>
<p>Validating functions:</p>
<pre class="brush: jscript;"> function checkZip(value) {...}

 _.any(zips, &quot;!isBlank &amp;&amp; checkZip(__value) &quot;);  // __value is the placeholder</pre>
<p>In addition to __value, __key and __list placeholder variables, corresponding to the formal arguments of the iterator, are available.</p>
<p>How about a regular expression:</p>
<pre class="brush: jscript;"> _.all([1,2,3], /\d+/);   // either as string or RegExp object</pre>
<p>Of course, functions are still supported and will work as before.</p>
<h3>Accessing original methods</h3>
<p>Since the expression string needs to be parsed, runtime performance will be affected.  Performance sensitive applications involving huge sets should use the original routine.</p>
<p>The original routines  can be explicitly accessed by passing no argument, eg.</p>
<pre class="brush: jscript;">
var originalAny = _.any();
originalAny(veryLargeSet, function(){...});
</pre>
<p>Or more compactly,</p>
<pre class="brush: jscript;">_.any()(veryLargeSet, function(){...});
</pre>
<p>Expressions cannot be used on original methods:</p>
<pre class="brush: jscript;">
originalAny(veryLargeSet, 'isNumber'); //  =&gt; raises exception
</pre>
<h3>The code</h3>
<p>Details, code, and examples (including <a href="http://moos.github.com/_xiterator">test suite</a>) are on <a href="http://github.com/moos/_xiterator">Github</a>.   Released under MIT license.</p>
<p><strong>Update: </strong>Just came across <a href="http://osteele.com/sources/javascript/functional/">Functional.js</a>.  Above technique is not too dissimilar to its string `lambda` functionality.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.42at.com/_xiterator-an-expression-iterator-for-underscore/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing touch-enabled spinControl</title>
		<link>http://blog.42at.com/introducing-touch-enabled-spincontrol</link>
		<comments>http://blog.42at.com/introducing-touch-enabled-spincontrol#comments</comments>
		<pubDate>Sat, 06 Feb 2010 22:45:12 +0000</pubDate>
		<dc:creator>Moos</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[webkit]]></category>

		<guid isPermaLink="false">http://blog.42at.com/?p=143</guid>
		<description><![CDATA[Hot on the heels of sliderControl comes its close cousin, the spinControl.   Whereas in the sliderControl, the thumb widget is moved across a fixed set of values to select the desired input, in the spinControl the whole set is movable.  This allows for larger set of data that can fit on the visible screen. All [...]]]></description>
			<content:encoded><![CDATA[<p>Hot on the heels of <a href="../slidercontrol">sliderControl </a>comes its  close cousin, the <strong>spinControl</strong>.   Whereas in the sliderControl, the thumb  widget is moved across a fixed set of values to select the desired  input, in the spinControl the whole set is movable.  This allows for  larger set of data that can fit on the visible screen.</p>
<p style="text-align: center;"><a href="http://blog.42at.com/spincontrol"><img class="aligncenter size-full wp-image-144" title="spin1" src="http://blog.42at.com/wp-content/uploads/2010/02/spin1.png" alt="" width="355" height="39" /></a></p>
<p>All the features of sliderControl are supported (kinetics, bounce, snap, toggle, events handlers, etc.).  Additionally, a new alignToEdge option allows the scroll area to align to the left and right edge of the viewport instead of its center.</p>
<p>For gory details, demo and download, see <a href="http://blog.42at.com/spincontrol">spinControl</a> page.</p>
<p><strong>Bonus</strong></p>
<p>Also included is a <strong>spinToggle </strong>class that mimic&#8217;s iPhone&#8217;s toggle switch, complete with slide action.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.42at.com/introducing-touch-enabled-spincontrol/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Emulating iPhone&#8217;s slide-to-unlock</title>
		<link>http://blog.42at.com/emulating-iphones-slide-to-unlock</link>
		<comments>http://blog.42at.com/emulating-iphones-slide-to-unlock#comments</comments>
		<pubDate>Thu, 28 Jan 2010 06:58:42 +0000</pubDate>
		<dc:creator>Moos</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[webkit]]></category>

		<guid isPermaLink="false">http://blog.42at.com/?p=134</guid>
		<description><![CDATA[The touch-enabled sliderControl covered earlier is an easy UI widget to select a value from a set or a range of numbers.  It can be easily modified to function as a slide-to-unlock contorl that you see on the iPhone.  Enter the slideToAction control: mySlider6 = new slideToAction('#slider6', ['slide to unlock'], { onchange: function(){ alert('unlocked'); // [...]]]></description>
			<content:encoded><![CDATA[<p>The touch-enabled sliderControl <a href="http://blog.42at.com/introducing-a-touch-enabled-slider-control">covered earlier</a> is an easy UI widget to select a value from a set or a range of numbers.  It can be easily modified to function as a slide-to-unlock contorl that you see on the iPhone.  Enter the <strong>slideToAction</strong> control:</p>
<pre>mySlider6 = new slideToAction('#slider6', ['slide to unlock'], {
	onchange: function(){
		alert('unlocked'); // some useful action!
	}
});</pre>
<p><strong>Markup:</strong></p>
<pre>&lt;div id="slider6" class="slider"&gt;&lt;/div&gt;</pre>
<p>When the thumnail is moved all the way to the end, the onchange handler gets called.  Simple.  slideToAction inherits all of sliderControl&#8217;s kenetics and emulates the spotlight effect of iPhone&#8217;s control.</p>
<p>slideToAction is included as part of <a href="http://blog.42at.com/slidercontrol/">sliderControl</a>.  See item #6 &#8216;single value slider&#8217; on the <a href="http://42at.com/lab/sliderControl">demo page</a>.</p>
<p><img class="aligncenter size-full wp-image-138" title="slideToAction" src="http://blog.42at.com/wp-content/uploads/2010/01/slideToAction1.png" alt="" width="321" height="62" /></p>
<p>(The thumbnail looks better on the iPhone browser!)</p>
<p>The spotlight effect of the label text is achieved using -webkit-mask-* CSS attribute and animating the  -webkit-mask-position property.  Here&#8217;s the CSS:</p>
<pre>.sliderAction .sliderLabel {
	font-size:18px;
	font-weight:normal;
	text-shadow:none;
	color: #fff;
	-webkit-mask-image: -webkit-gradient(linear, 35% top, 65% top,
           from(rgba(0,0,0,.20)), color-stop(.5,rgba(0,0,0,1)), to(rgba(0,0,0,.20)));
	-webkit-mask-size: 50%;
	-webkit-mask-repeat: none;

	-webkit-animation-name: spotlight;
	-webkit-animation-timing-function: linear;
	-webkit-animation-duration: 1500ms;
	-webkit-animation-iteration-count: infinite;
}</pre>
<p>using the animation keyframes:</p>
<pre>@-webkit-keyframes spotlight {
    from {-webkit-mask-position: 0;}
    to {-webkit-mask-position: 100%;}
}</pre>
<p>The  -webkit-mask-position property is animated on desktop Safari 4.x and Chrome 4.x.   It isn’t supported on iPhone OS 3.1.2 browser&#8230; but hopefully it will under 3.2.</p>
<p><strong>Update: </strong>(11 March 2010)</p>
<p>New version 0.1a has an updated slideToAction() that looks more like the real thing.  Here&#8217;s a screenshot.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-167" title="slideToAction - version 0.1a" src="http://blog.42at.com/wp-content/uploads/2010/01/slideToAction1a1.png" alt="" width="368" height="74" /></p>
<p style="text-align: left;">Seems like SDK developers have been trying to get this feature implemented as well.  See <a href="http://www.iphonedevsdk.com/forum/iphone-sdk-development/11470-there-way-i-can-use-slide-unlock-slider.html">here</a> and <a href="http://stackoverflow.com/questions/438046/iphone-slide-to-unlock-animation">here</a>.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">http://blog.42at.com/introducing-a-touch-enabled-slider-control</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.42at.com/emulating-iphones-slide-to-unlock/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing a touch-enabled slider control</title>
		<link>http://blog.42at.com/introducing-a-touch-enabled-slider-control</link>
		<comments>http://blog.42at.com/introducing-a-touch-enabled-slider-control#comments</comments>
		<pubDate>Fri, 22 Jan 2010 19:39:26 +0000</pubDate>
		<dc:creator>Moos</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[webkit]]></category>

		<guid isPermaLink="false">http://blog.42at.com/?p=97</guid>
		<description><![CDATA[I needed a touch-enabled slider input control for a project I was working on.  Unfortunately there was no good existing solution, so I rolled my own. Features: kinetic snap to value optimized CSS animation full range of slider values supported customizable with extensive options fully programmable event callbacks adjusts on orientation change works on desktop [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.42at.com/wp-content/uploads/2010/01/slider1.png"></a><a href="http://blog.42at.com/slidercontrol/"><img class="alignright size-medium wp-image-132" title="slider1" src="http://blog.42at.com/wp-content/uploads/2010/01/slider1-146x300.png" alt="" width="146" height="300" /></a></p>
<p>I needed a touch-enabled slider input control for a project I was working on.  Unfortunately there was no good existing solution, so I rolled my own.</p>
<h2>Features:</h2>
<ul>
<li>kinetic snap to value</li>
<li>optimized CSS animation</li>
<li>full range of slider values supported</li>
<li>customizable with extensive options</li>
<li>fully programmable</li>
<li>event callbacks</li>
<li>adjusts on orientation change</li>
<li>works on desktop webkit browsers (for testing)</li>
<li>theme to taste!</li>
</ul>
<p>See <a href="http://blog.42at.com/slidercontrol/">sliderControl page</a> for details.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.42at.com/introducing-a-touch-enabled-slider-control/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>★ More on PastryKit</title>
		<link>http://blog.42at.com/%e2%98%85-more-on-pastrykit</link>
		<comments>http://blog.42at.com/%e2%98%85-more-on-pastrykit#comments</comments>
		<pubDate>Mon, 28 Dec 2009 14:30:52 +0000</pubDate>
		<dc:creator>Moos</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[webapps]]></category>

		<guid isPermaLink="false">http://blog.42at.com/?p=21</guid>
		<description><![CDATA[Daring Fireball Lastly, there’s the question of how concerned Apple is, strategically, that a robust web app API and market would take away from the App Store. And if so, are they worried about the money? I’d guess probably not. I don’t think Apple’s 30 percent cut of App Store revenue is anything to sneeze [...]]]></description>
			<content:encoded><![CDATA[<p>Daring Fireball</p>
<blockquote><p><span style="font-family: arial, sans-serif; line-height: normal; border-collapse: collapse;">Lastly, there’s the question of how concerned Apple is, strategically, that a robust web app API and market would take away from the App Store. And if so, are they worried about the money? I’d guess probably not. I don’t think Apple’s 30 percent cut of App Store revenue is anything to sneeze at, and it’s growing fast. But there’s no question that the App Store exists to sell iPhones and iPod Touches, not the other way around.</span></p></blockquote>
<p>via <a href="http://daringfireball.net/2009/12/more_on_pastrykit">★ More on PastryKit</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.42at.com/%e2%98%85-more-on-pastrykit/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lawnchair &#8211; client side JSON document store</title>
		<link>http://blog.42at.com/lawnchair</link>
		<comments>http://blog.42at.com/lawnchair#comments</comments>
		<pubDate>Sun, 29 Nov 2009 12:07:23 +0000</pubDate>
		<dc:creator>Moos</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.42at.com/?p=52</guid>
		<description><![CDATA[Sorta like a couch except smaller and outside, also, a client side JSON document store. Perfect for webkit mobile apps that need a lightweight, simple and elegant persistence solution. Lawnchair.]]></description>
			<content:encoded><![CDATA[<blockquote><p><span style="font-family: 'Times New Roman'; line-height: normal; font-size: 14px;">Sorta like a couch except smaller and outside, also, <strong>a client side JSON document store</strong>. Perfect for webkit mobile apps that need a lightweight, simple and elegant persistence solution.</span></p></blockquote>
<p><a href="http://brianleroux.github.com/lawnchair/">Lawnchair</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.42at.com/lawnchair/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

