Evo Cloud Basic Elements
This package includes common and basic building elements for all evo projects.
In this version, it includes:
- Class: defining Class for object-oriented programming
- Trace: simple console logging with predefined levels and customizable component names
- States: a basic framework for building a state machine
- Config: a simple configuration framework to release you from writing similar code in projects
- DelayedJob: ensure a postponed job won't be scheduled multiple times
npm install evo-elements
or pull directly from github.com and link to your project:
git clonenpm link elements --prefix=node_modules
var elements = require'evo-elements';
ClassbaseClass prototype options;
baseClass: baseClass type, optional, default is Object;
prototype: the prototype for new class;
options: other options like implements and statics, see details below.
The newly defined class.
A simple and quick sample:
var Class = require'evo-elements'Class;var MyClass = Class// this is the constructorthis_value = value;console.logthis_value;// getter/setter is supportedget valuereturn this_value;set value valueif !isFinitevaluethrow 'Bad Value';this_value = value;;var myObject = 1;myObjectprint; // get 1myObjectvalue = myObjectvalue + 100;myObjectprint; // get 101
A simple inheritance sample:
var BaseClass = Classthisvalue = value;console.log'BaseClass: %d' thisvalue;;var SubClass = ClassBaseClassBaseClassprototypeconstructorcallthis val1 + val2;console.log'SubClass';BaseClassprototypeprintcallthis;;var myObject = 1 2;myObject instanceof SubClass; // truemyObject instanceof BaseClass; // truemyObjectprint;// get// SubClass// BaseClass: 3
Multiple inheritance with implements
var ActiveBuffer = ClassBuffer// override Clearable// TODO I hate to be clearedthisemit'cleared';implements: EventEmitter Clearable;var buffer = on'cleared' console.log'CLEARED'; ;bufferclear;buffer instanceof Buffer; // truebuffer instanceof EventEmitter; // falsebuffer instanceof Clearable; // false
var Singleton = Class// ...// ...statics:get instanceif !Singleton_instanceSingleton_instance = ;return Singleton_instance;;Singletoninstancework;
var trace = TracecomponentName;trace.error;trace.warn;trace.info;traceverbose;tracedebug;// aliasestraceerr; // same as errortrace.log; // same as infotracedbg; // same as debugtraceverb; // same as verbose
componentName: a string which will be prefixed to the logged line
The trace object. All the methods follow the same usage as
A simple state machine.
var states = initialState;statestransitnewState;statesfromcurrStatetransitnewState
initialState: optional, initialize the state machine with the provided state.
newState: the next state to switch to. It can be an object or a function. If it is a function, it gets invoked when transit happens and is expected to return the new state object.
currState: used with
fromto assert current state must be currState, otherwise
transitwill not happen.
A state object can provides two methods:
leave, both are optional.
Expects returning a state object for next state. If it is different from current state, transition keeps runing.
Invoked when the state machine transits to
nextState before invoking
A simple configuration framework to load settings from command line arguments and configuration files. It also provides a global settings object to be shared by all the modules in one project. When using this, we don't need to write logic for parsing command line and loading and configuration files.
the configuration can always be shared in modules by
var conf = require'evo-elements'Configconf;
Usually in the main script, use
var conf = require'evo-elements'ConfigparsemyArgvconf;
to parse from specified arguments instead of
conf.opts to access all the setting options.
Only one command line option is reserved:
-c CONFIG_FILE or
--config CONFIG_FILE for long option form.
CONFIG_FILE must be a YAML file, and all the keys are merged into
For other options like
--option VALUE or
--option=VALUE will become:
conf.opts[option] = VALUE.
VALUE is automatically parsed into
Boolean, if you want to specify a JSON, use
If you know
setTimeout, then you know what a
DelayedJob instance does.
But a little differently. Each time invoking
setTimeout will schedule a job,
the job executes as many times as you schedule. With a single instance of
DelayedJob, the job is only scheduled once.
The next time you schedule before the job gets run, it does nothing.
The usage is simple:
var job =// Do what you want ...;jobschedule1000; // it will be scheduled in 1 secondjobschedule; // you want to scheduled in next tick, but actually do nothing, because already scheduledjobschedule200; // still do nothing.
Why is this pattern useful? It is used to collect some small events, and perform all of them in one shot, like:
var queue = ;var job =console.logqueuejoin',';// clear queuequeue = ;;queuepush'something';jobschedule;queuepush'something more';jobschedule;// finally, all the message are processed in one shot// printed: something,something more