The purpose of this talk is to introduce you to a sublime and surprisingly durable design pattern, that strangely has no name. It does not appear in the Gamma et al Gang of Four book bearing the title "Design Patterns", though you might see it there in many guises if you look closely and creatively. This design pattern may not change your life, but it certainly has tilted mine toward better.
You've heard of function calls, like f(x)
.
You may have heard of "inversion of control" or "continuation passing style",
like f(callback(x))
.
What if callbacks returned interesting things?
On this tour, I will show you promises:
var promise = promise.then(function () {
return Promise.return(10);
});
I will show you matryoska observer dolls:
var cancel = observeProperty(object, "alpha", function (alpha) {
return observeProperty(alpha, "beta", function (beta) {
console.log("alpha.beta is now ", beta);
return noop;
});
});
cancel();
I will show you recursive descent parsers for regular and context-free grammars:
var parse = function (text, callback) {
var state = start(callback);
for (var i = 0; i < text.length; i++) {
state = state(text[i], i);
}
state(null);
}
I will show you the command pattern driving a state machine as you have never seen it before.
Supervisor.prototype.stop = function stop() {
this.state = this.state.stop();
};
And in the final act, I will show you an animation coordinator that synchronizes a view model with a view asynchronously, planning animations to occur in series and in parallel:
exports.triggers = {
'grow airplane': function () {
return new A.Series([
new A.Parallel([
this.drop('airplane'),
this.drop('growing-potion'),
]),
new A.Parallel([
this.take('giant-airplane'),
this.take('vial'),
]),
]);
}
};
When all of this is over, you will wake at your desk with the feel you had the oddest dream. You will be forever after plagued with worry that some opportunity might have been lost, every time you encounter a callback that returns…nothing.
But when do you encounter the great remrofsnart-danom ? A most fearsome beast indeed!