<?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>Shawn Wilsher &#187; API</title>
	<atom:link href="http://shawnwilsher.com/archives/tag/api/feed" rel="self" type="application/rss+xml" />
	<link>http://shawnwilsher.com</link>
	<description></description>
	<lastBuildDate>Sun, 04 Dec 2011 10:37:21 +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>XPCOMUtils API Addition</title>
		<link>http://shawnwilsher.com/archives/319</link>
		<comments>http://shawnwilsher.com/archives/319#comments</comments>
		<pubDate>Thu, 03 Sep 2009 21:02:04 +0000</pubDate>
		<dc:creator>Shawn Wilsher</dc:creator>
				<category><![CDATA[Mozilla]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[XPCOMUtils]]></category>

		<guid isPermaLink="false">http://shawnwilsher.com/?p=319</guid>
		<description><![CDATA[We&#8217;ve recently added two new API&#8217;s on XPCOMUtils that make a common coding pattern simpler to do. It is quite common to have &#8220;smart getters&#8221; to store a reference to a service that you use in JavaScript. Normally, folks write code that looks something like this: this.__defineGetter__("_ioService", function() { delete this._ioService; return this._ioService = Components.classes["@mozilla.org/network/io-service;1"]. [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve recently <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=508850" title=" XPCOMUtils should provide a convenient way to define lazy getters">added two new API&#8217;s on <tt>XPCOMUtils</tt></a> that make a common coding pattern simpler to do.  It is quite common to have &#8220;smart getters&#8221; to store a reference to a service that you use in JavaScript.  Normally, folks write code that looks something like this:<br />
<code>
<pre>this.__defineGetter__("_ioService", function() {
  delete this._ioService;
  return this._ioService = Components.classes["@mozilla.org/network/io-service;1"].
                           getService(Components.interfaces.nsIIOService);
});</pre>
<p></code><br />
That gets long and verbose if you have any more than a few of these getters.  The new approach looks like this:<br />
<code>
<pre>XPCOMUtils.defineLazyServiceGetter(this, "_ioService",
                                   "@mozilla.org/network/io-service;1",
                                   "nsIIOService");</pre>
<p></code><br />
The net result is a lot less boilerplate code, which, in my opinion, is a lot nicer to read.  I&#8217;ve already converted the toolkit code in the Places module over, and the <a href="https://bugzilla.mozilla.org/attachment.cgi?id=398210&#038;action=diff">resulting code is much nicer</a>.</p>
<p>Sometimes, however, you want to have a getter for something that is not a service.  That&#8217;s where the second API comes in.  You can pass in a lambda function that will be called when the value is needed for the first time.  From then on, the value is cached.  Some old code may look something like this:<br />
<code>
<pre>this.__defineGetter__("_db", function() {
  delete this._db;
  return this._db = Components.classes["@mozilla.org/browser/nav-history-service;1"].
                    getService(Components.interfaces.nsPIPlacesDatabase).
                    DBConnection;
});</pre>
<p></code><br />
With the new API, that code turns into this:<br />
<code>
<pre>XPCOMUtils.defineLazyGetter(this, "_db", function() {
  return Components.classes["@mozilla.org/browser/nav-history-service;1"].
         getService(Components.interfaces.nsPIPlacesDatabase).
         DBConnection;
});</pre>
<p></code><br />
Again, much nicer looking!  If you are interested, the <a href="http://hg.mozilla.org/mozilla-central/file/f2ebd467b1cd/js/src/xpconnect/loader/XPCOMUtils.jsm#l232">implementation of these methods can be found here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://shawnwilsher.com/archives/319/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>New API Added on NetUtil</title>
		<link>http://shawnwilsher.com/archives/310</link>
		<comments>http://shawnwilsher.com/archives/310#comments</comments>
		<pubDate>Thu, 03 Sep 2009 18:36:04 +0000</pubDate>
		<dc:creator>Shawn Wilsher</dc:creator>
				<category><![CDATA[Mozilla]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[NetUtil]]></category>

		<guid isPermaLink="false">http://shawnwilsher.com/?p=310</guid>
		<description><![CDATA[Did you know about the handy NetUtil object available when you import NetUtil.jsm? I bet you didn&#8217;t since it&#8217;s fairly new! There&#8217;s no MDC page yet, but the API is getting even better. Bug 508902 added a new method on NetUtil to create nsIURI objects. NetUtil will cache the IO Service so the calling site [...]]]></description>
			<content:encoded><![CDATA[<p>Did you know about the handy <tt>NetUtil</tt> object available when you import <a href="http://mxr.mozilla.org/mozilla-central/source/netwerk/base/src/NetUtil.jsm"><tt>NetUtil.jsm</tt></a>?  I bet you didn&#8217;t since it&#8217;s fairly new!  There&#8217;s no MDC page yet, but the API is getting even better.  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=508902" title=" NetUtil.jsm should have a newURI method">Bug 508902</a> added a new method on <tt>NetUtil</tt> to create <tt>nsIURI</tt> objects.  <tt>NetUtil</tt> will cache the IO Service so the calling site doesn&#8217;t have to.  As more code starts to use this, fewer objects will have to cache the IO Service themselves.</p>
<p>The API mirrors <a href="https://developer.mozilla.org/en/nsIIOService#newURI.28.29"><tt>nsIOService</tt>&#8216;s <code>newURI</code></a> method with one minor improvement; the last two arguments are optional.  Most callers in JavaScript tend to pass null for those last two arguments anyway, but when using <tt>NetUtil.newURI</tt>, they just have to pass the spec which should be easier.</p>
<p>New JavaScript code that needs to create <tt>nsIURI</tt> objects should start using this method.</p>
]]></content:encoded>
			<wfw:commentRss>http://shawnwilsher.com/archives/310/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

