MicroGears
MicroGears a super lightweight micro framework for reactive services
MicroGears was build with 3 main objectives in mind.
- enforce immutability of function parameters
- enforce all function calls to be promisified
- enable your service to be filtered in a transparent way (with more then one filter simultaneously)
- help to administer service dependency in favor of a better and more controlled test environment
How to register a service
The name and namespace are mandatory fields for the service, the reason is that later you are going to be able to appy the plugins to only certain services or namespaces (ie services.company.persistance or routes.company, etc..). Needless to say services must be registered before use.
VERY IMPORTANT--->ALL service calls are promisify by microGears for you, so they are ALWAYS asynchronous, except when the async property is set to false, this property is optional and your default value is true
var MicroGears = ; var userService = name: 'userService' async: true //This is an optional property : [default = true ]. This is the same as omitting it. namespace: "services.userservice" { return name: 'user' id: id ; };MicroGears;
Since MicroGears is goint to transform the service functions into promises you should keep this in mind when using the framework, except if async is set to false
Using the registered service
Example
MicroGearsuserService;
How to register a plugin
The name field is mandatory and there must be a beforeChain, afterChain or both functions, which the first parameter is going to an array of all service function parameters (can be modified or not but should be used as a return of this function), the next parameter is a meta information of the service call, it is an object that you can share information between another plugins.
The beforeChain or afterChain function will always have only two parameters: the args, an array of all service function parameters, and _meta, an object that have information about service caller, an can used too for sharing information between plugins.
VERY IMPORTANT--->plugins follow the service behaviors about synchronization if async service property, is seted to false, services and plugins will be synchronous, otherwise, all will be async.
A trace plugin
var MicroGears = ; var tracePlugin = name: 'tracePlugin' { var serviceName = _metaserviceName method = _metamethodName; console; _metamySharedData = count: 1 ; return args } { var serviceName = _metaserviceName method = _metamethodName; console; if_metamySharedDatacount console; return result; }; MicroGears;
A performance meter plugin (that only measures performance for the userService service
var performancePlugin = name: 'performancePlugin' { var hrstart start logPerformance = false serviceName = thismicrogearsserviceName; logPerformance = serviceName === 'testService'; if logPerformance hrstart = process; start = ; _metastatistcs = hrstart: hrstart start: start logPerformance: logPerformance ; return args; } { if _metastatistcslogPerformance _metastatistcsend = - _metastatistcsstart; _metastatistcshrend = process; console; console; return result; }; MicroGears;
Omitting methods
Sometimes we not need to intercept a method, a private method perhaps. Private methods doesn't exists in Javascript , by convention, instance members starting with _ "underscore", are private, the Microgears use this convention to not intercept methods with this notation.
var plugin1 = name: 'testPlugin1' { return args0 + 1; } { return result + 1; }; MicroGears; MicroGears; var result = MicroGearstestService; // 1;