Monday, August 27, 2007

Simple, synchronous AIR configuration

I've spent some time the last couple of weeks poking around with AIR. The Adobe Integrated Runtime promises to let you build desktop applications using Javascript. Basically, it gives you Webkit and a series of APIs you can access through the window.runtime object that provide all sorts of things like access to the file system, drag and drop, and a SQL Lite database. You can also use Flash and Flex, but I'm not smart enough for that. I ended up trying to build a persistent key/value configuration system and quickly ran into a frustrating reality of AIR's SQL API - everything is asynchronous. This makes using the DB for storing configuration frustratingly convoluted.

You have a simple config table - key:string, value:string. Say you want to read the value for the key "foo". You end up with code that looks something like this:


var stmt = new air.SQLStatement();
stmt.sqlConnection = conn;
stmt.text = "select value from config where key = :key";
stmt.parameters[":key"] = "foo";
stmt.addEventListener(air.SQLEvent.RESULT, function(){
var fooValue = stmt.getResults().data[0];
doStuffWithFoo...
});
stmt.execute();


Which makes me throw up in my mouth a little bit. I want to do something like this:


var fooValue = getConfig("foo");
doStuffWithFoo...


But that damned async call just keeps getting in the way.

Adobe hasn't committed to building this yet "because of the development effort it would require". My thoughts on that can be summed up in my previous post: "Programmers are expensive, CPUs are cheap" does not justify rapid development of slow code.

So, I wrote some convoluted code, scream "fuck this shit!" a couple of time, and wrote a little utility to provide a synchronous API - the Big Dumb Javascript AIR Configuration. To install, include Configuration.js:


<script src="Configuration.js"></script>


You then have to initialized the configuration system.


BigDumbDev.Configuration.init( function(){ air.trace('configuration ready'); } );


Reading and writing configs goes like this:


BigDumbDev.Configuration.getConfig('key');

BigDumbDev.Configuration.setConfig('key', 'value');

BigDumbDev.Configuration.removeConfig('key');


Sets and removes are immediately available, so you can do this:


BigDumbDev.Configuration.setConfig('foo','bar');
var foo = BigDumbDev.Configuration.getConfig('foo');
// foo is bar


You can see all the details in the source code. There is no magic - it still does the DB calls asynchronously. I cache the config values and read from the cache. Update change the cache and then kick of the async call to update the database.

You can download a sample AIR app that utilizes the system.

1 comments:

said...
This post has been removed by a blog administrator.