Engined
A micro framework for application in Node.js. it aims to provide a way to manage your services inside instance.
Requirements
Engined widely uses async/await in ES6+ so Node.js v7.4+ is required and v7.6 is prefered. If you are working on Node.js v7.6, you have to run Node.js with --harmony-async-await
options.
Installation
You can just install module via NPM:
npm install engined
Get Started
Here is an example to use engined to manange services:
const Manager Service = ; { supercontext; thiscounter = 0; } { return { ; }; } async { // Getting global counter from context thiscounter = this || 0; for let index = 0; index < 10; index++ thiscounter++; console; await this; this; } async { // Getting global counter from context thiscounter = this || 0; for let index = 0; index < 10; index++ thiscounter--; console; await this; this; } const main = async { // Create manager let serviceManager = verbose: true ; // Adding services to manager serviceManager; serviceManager; // Start all services and stop await serviceManager; await serviceManager; console;}; ;
Usage
engined.Manager
class provides serveral methods for service management.
Load modules at once
There is a way to load multiple modules with loadServices
method at once.
await serviceManager;
Add and remove specific service
Add and remove specific service with add
and remove
method.
serviceManager;serviceManager;
Note that remove
will stop service if service is running.
Start and stop specific service
It can start and stop specific service with start
and stop
method.
await serviceManagerstart'MyService1';await serviceManager;
Accessing Context
Sharing data among services is allowed, just save it in the context object. engined.Manager
and engined.Service
classes provide a method for accessing the context object.
Get context via service manager
engined.Manager
provide getContext
method to get current Context
instance:
let context = serviceManager;
Get context from inside the service
engined.Service
provide getContext
method to get current Context
instance:
let context = this;
Store data in context object
Store custom key/value pairs by calling set
method.
context;
Get data from context object
Get key/value pairs by calling get
method.
let mykey = context;
Agent Manager
engined.AgentManager
was designed in order to manage multiple agents. We usually expose agent manager to other services via context object.
Manager service can be implmeneted like below:
{ supercontext; thisagentManager = null; } async { thisagentManager = ; this; } async { if thisagentManager === null return; this; thisagentManager = null; }
Then we can implement agent service and register agent in above manager service:
{ supercontext; thisagent = null; } async { thisagent = data: 'Hello' ; this; } async { if thisagent === null return; this; thisagent = null; }
In other services, getAgent()
is the way to accessing specific agent of manager.
let agent = this; console;
Easy way to assert agent manager in context
There is a way to create agent manager then register on context faster.
this;
License
Licensed under the MIT License
Authors
Copyright(c) 2017 Fred Chien(錢逢祥) <cfsghost@gmail.com>