Pub/Sub and Mediator pattern taken to the limits
@naugtur, meet.js 06.2012You should not need this slide because you know what a module is. Right?
Revealing module pattern(function(){
var privateVariable;
function privateMethod(){ /*...*/ }
function publicMethod(){ /*...*/ }
return {
public:publicMethod
}
})()
AMD module pattern (like in RequireJS)
define(['dependency1','dependency2'],function(Dep1,Dep2){
var privateVariable;
function privateMethod(){ /*...*/ }
function publicMethod(){ /*...*/ }
return {
public:publicMethod
}
})()
subscribe('newsTopic',function(){
console.log('hello1');
});
subscribe('newsTopic',function(){
console.log('hello2');
});
subscribe('otherTopic',function(){
console.log('good evening');
});
publish('newsTopic');
> hello1
> hello2
Did you know that jQuery Mobile Framework finally has a themeroller?
Lots of new stuff is going on. And it's production quality.
When looking for mediator implementations in JS I only found Pub/Sub with minor improvements.
And JavaScript can do so much more!
Introducing Overlord.js
register, getFacade, call
var doSomething=function(){
return 'hello from A'
};
//Expose API to my
// functionalities
Overlord.register('myAPI',{
hello:doSomething
});
...
var doSomething=function(){
return 'hello from B'
};
//Expose API to my
// functionalities
Overlord.register('myAPI',{
hello:doSomething
});
...
...
//choose an API to use
var facade =
Overlord.getFacade('myAPI');
//Call a method
facade.hello();
> [ "hello from A",
"hello from B" ]
Loose API interface
Overlord.register('myAPIName',{
inc:function(a){return a+1;},
dec:function(a){return a-1;}
});
Overlord.register('myAPIName',{
inc:function(a){return a+10;}
});
var facade=Overlord.getFacade('myAPIName');
facade.inc(3);
> [ 4, 13 ]
facade.dec(3);
> [ 2 ]
Strict API interface - define first
Overlord.defineInterface('myAPIName',['inc','dec']);
Overlord.register('myAPIName',{
inc:function(a){return a+1;},
dec:function(a){return a-1;}
});
Overlord.register('myAPIName',{
inc:function(a){return a+10;}
});
> exception:
"Given object is not an implementation for API:myAPIName. Method dec is missing"
Facade always up to date
Overlord.register('myAPIName',{
inc:function(a){return a+1;},
dec:function(a){return a-1;}
});
var facade=Overlord.getFacade('myAPIName');
Overlord.register('myAPIName',{
inc:function(a){return a+10;}
});
facade.inc(3);
> [ 4, 13 ]
Separate applications from user data. And don't write back-end for html5 apps anymore :)
Safe to call anything, errors are being caught
Overlord.register('myAPIName',{
inc:function(a){return a+1;},
});
Overlord.register('myAPIName',{
inc:function(a){return a.misspeledPropName.something;}
});
var facade=Overlord.getFacade('myAPIName');
facade.inc(3);
> [ 4 ]
Overlord.getDebugInfo('myAPIName')
> {
errors:
[ { stack: [Getter/Setter],
arguments: [Object],
type: 'non_object_property_load',
message: [Getter/Setter] } ],
method: 'inc',
apiObject:
{ definition: { inc: true },
stronglyTypedInterface: false,
implementations: [ [Object], [Object] ],
lastErrors: [ [Object] ],
lastCall: 'inc',
facade: { inc: [Function] } } }
Cogision is having a summer internship for students and fresh graduates.
naugtur
graphics from The Noun Project