Oni Apollo 0.11: Bug fixes, StratifiedJS improvements and syntactic sugar

March 01, 2011 by Alexander Fritze

We're pleased to announce the release of version 0.11 of the client-side StratifiedJS runtime Oni Apollo.

For the full list of changes please consult the github commit history. In addition to many bugfixes, this release introduces the following new features:

New 'spawn' operator

In this release of Oni Apollo, we have overhauled the syntax and semantics of StratifiedJS's spawn operator.

Instead of taking a function as an argument as in previous versions of Oni Apollo, spawn now is a unary operator taking an expression as argument. In practice this often saves you the trouble from creating a closure. E.g. where previously you would have had to write:

// old syntax in Apollo < 0.11
var me = this;
spawn (function() { me.doSomethingInBackground() });
you can now just write:
// new syntax in Apollo >= 0.11
spawn this.doSomethingInBackground();

We have also added functionality for waiting on and aborting spawned code. For the gory details, check out the StratifiedJS spawn docs.

Destructuring Support

StratifiedJS now supports some of ES4's destructuring patterns. 'Destructuring' is a convenient syntax for picking apart structured data, mirroring the way array and object literals are constructed.

E.g. if you have a function that returns an array, you can directly extract this to individual variables using the following destructuring assignment:

function foo() { return ['x', 'y', 'z']; }
var a,b,c;
[a,b,c] = foo();
// we'll now have a=='x', b=='y' and x=='z'

Similarly, objects can be picked apart like this:

function bar() { return {x:1, y:2, z:3; } }
var a,b,c;
({x:a,y:b,z:c}) = bar();
// we'll now have a==1, b==2 and x==3

Note that object patterns cannot appear at the start of a statement, hence the parenthesization of the object pattern in ({x:a,y:b,z:c}) = bar();.

Destructuring works with nested array and object data, too. Here's an example of extracting the third Google search result for "js destructuring": [Run example in sandbox]

var title, url;
({responseData:  
   { 
     results: [ , , {title, url}]
   }
 }) = require("apollo:google").search("js destructuring");
 console.log("3rd Google result: "+ title + " --- " + url);

This pattern makes use of a DRY shorthand whereby the pattern {title, url} is equivalent to {title:title, url:url}.

The two empty elements in the pattern [ , , {title, url}] cause the assignment to skip the first two results returned by the google search, and bind title and url to the third result.

Please see the destructuring docs at ecmascript.org for full syntax details. Note that destructuring support in Apollo 0.11 is limited to plain assignment expressions. Destructuring in var definitions, for/in loops, catch clauses or parameter lists is not supported yet.

Require-mechanism enhancements

require now accepts explicit 'sjs' or 'js' extensions in module names. If you give an explicit 'js' extension, the module will be loaded as plain JavaScript code.

As before, if no extension is specified, the require-mechanism automatically appends an 'sjs' extension, so in e.g. require("foo"), it goes looking for a file 'foo.sjs'.

As always, if you give Oni Apollo 0.11 a whirl, please post any feedback/questions to the Apollo Google Group.