Taming concurrency with Oni
Here is something that we're trialing in Zap to tame the rat's nest of callbacks that highly concurrent JavaScript tends to evolve into:
Oni is a framework for managing the control flow of concurrent applications. There are two central ideas to Oni:
- To make concurrent actions composable by implementing control structures with which asynchronous actions can be choreographed in the same way that traditional sequential languages implement control structures for choreographing synchronous actions.
- To leverage the facilities of a "host language" rather than implement a complete programming language from scratch.
Oni can be implemented as a set of functions ("Oni operators") in a suitable host language, such as JavaScript or C++ (any reasonable language will do). Oni requires no preprocessing or precompilation; an Oni program is a just an expression in the host language.
Oni leverages the type system, control structures and asynchronous facilities (message loop, etc) of the host language. Constructs (functions, etc) from the host language can be "lifted" into an Oni program. Conversely, Oni features can be introduced retroactively into a conventional program without requiring the whole program to be converted to a different style in any way.
Oni is at an early but functioning stage. We have a JavaScript Oni implementation, hosted at hg.mozilla.org/users/alex_croczilla.com/oni which is in active use for an internal 8x8 communication application prototype. Development is also under way to convert the top level control code of the Zap SIP client to Oni.
To find out more, please read on here: http://www.croczilla.com/oni.
Posted by
alex at
00:08
|
Comments (5)
Unfortunately, I don't see where I can get AsyncUtils.jsm but I guess that it goes back to message loops in the end (nsIThread.processNextEvent()). I did try to tame callbacks like this, with limited success: https://bugzilla.mozilla.org/show_bug.cgi?id=422451
Have you looked at the asynchronous concurrency primitives from Twisted Python, and available in the MochiKit JavaScript library? They probably address a slightly different problem domain than Oni seems to, but I find them powerful and useful - and they tend to integrate better into the host language than Oni, which seems to be imposing its own lispy dialect.
@Wladimir:
zapScaffolding.jsm and AsyncUtils.jsm - the javascript modules included by the 'jsm' version of Oni - are available here. Note that they are not required for the in-browser version of Oni, ff-oni.js.
Under the hood, Oni doesn't really care how asynchronicity is provided. What it does require from the host language is a mechanism to schedule an asynchronous call. In the in-browser version, this is provided by 'setTimeout()' and in the JS module version by 'nsIEventTarget::dispatch()'. Both of these ultimately work via message loops (but not local ones).
Writing an implementation sketch of Oni is on my list of things to do :-)
@Screwtape:
As far as I'm aware Twisted Phython is based on manual continuation passing, which can become pretty unwieldy, especially if you want bidirectional control flow (continuations and cancellations). Oni handles all of this behind the scenes.
Also, Twisted Python doesn't really implement high-level control abstractions for asynchronous operations. It sort-of stops where the meat of Oni begins; there are no equivalent combinators to 'Alt' or 'Par', there is no functional abstraction, etc.
I agree that unless you're comfortable with functional programming languages, Oni's syntax and semantics take some getting used to... abstraction comes at a price :-)
YXhWe2 Thanks for good post