Get Started
To create a service all you need to do is pass a NAMED function to studio
var Studio = require('studio');
Studio(function myFirstService(){
return 'Hello World';
});
To call a service all you need to do is pass the identifier of a service to studio (remember all service calls returns a promise)
var Studio = require('studio');
var myFirstServiceRef = Studio('myFirstService');
myFirstServiceRef().then(function(result){
console.log(result); //Prints Hello World
});
You service can receive any number of arguments And also, you can get a reference to a service even if it was not instantiated yet (you only need it when calling) as in:
var Studio = require('studio');
//Get the reference for a non initialized service works perfectly
var myServiceNotInstantiatedRef = Studio('myServiceNotInstantiated');
Studio(function myServiceNotInstantiated(name){
return 'Hello '+name;
});
myServiceNotInstantiatedRef('John Doe').then(function(result){
console.log(result); //Prints Hello John Doe
});
Is that simple to run over Studio. No boilerplate required.
Now the things can get more interesting if youre running on node >= 4 or using the flag --harmony-generators, because studio supports generators out-of-the-box if they are available as in:
var Studio = require('studio');
var myFirstServiceRef = Studio('myFirstService');
Studio(function myFirstService(){
return 'Hello World';
});
Studio(function * myFirstServiceWithGenerator(result){
var message = yield myFirstServiceRef();
console.log(message); // Prints Hello World
return message + ' with Generators';
});
var myFirstServiceWithGeneratorRef = Studio('myFirstServiceWithGenerator');
myFirstServiceWithGeneratorRef().then(function(result){
console.log(result); //Prints Hello World with Generators
});
You can yield Promises, Arrays of promises (for concurrency), Regular Objects or even Thunkable (node callbacks) you can see hthe examples in the generators session
Also if youre running on node >= 6 or using the flag and --harmony-proxies. You can access the services easier:
var Studio = require('studio');
//Get a reference to all services, even those not created yet
// So magical :)
var allServices = Studio.services();
Studio(function myFirstService(){
return 'Hello World';
});
Studio(function * myFirstServiceWithGenerator(result){
var message = yield allServices.myFirstService();
console.log(message); // Prints Hello World
return message + ' with Generators';
});
allServices.myFirstServiceWithGenerator().then(function(result){
console.log(result); //Prints Hello World with Generators
});