Module-like system with two step initialization
Module-like system with two step initialization.
Units provides an ability to add number of component instances to a set and then call init function on each of them with guarantee that all other components are instantiated.
Can also be used on client side using browserify or similar tool.
var units = ;var;// all units are instantiated at this point// getting components we're depended onthisdb = unitsrequire'db';thislogicB = unitsrequire'logic.b';;unitsadd'db' someDbArgs;unitsadd'settings' '/path/to/project/root/or/something';unitsadd'logic.a' ;unitsadd'logic.b' ;unitsadd'facade' ;unitsinit; // calls unitInit() function of every unit internally
UnitSet can be added to other UnitSet which will be it's parent. On getting keys, lookup will be performed locally first, then on parent UnitSet. All child units will be added directly to parent and inited by parent as well.
var units = ;unitsadd'db' ;var childUnits = ;childUnitsadd'logic.a' ;childUnitsadd'logic.b' ;unitsaddSetchildUnits;childUnitsget'db'; // will try to find 'db' in childUnits, then in units
- alias(key, dstKey) - sets alias for key: dstKey will be obtained instead of key on get() and require() calls
- add(key, unit) - adds unit under key specified
- expose(key, obj) - like add(key, obj), but unitInit() will not be called on this unit (so, unit may omit unitInit() implementation), used to expose constant or any object without unitInit() method as a unit
- addInitRequired(key, unit) - like add(key, obj), but will ensure that unitInit() is called on that unit when it's being got by get() or require(), for units that are unusable unless inited
- addSet(key, units) - makes units child UnitSet, adds all child units to itself under key specified
- units has a unit with key '~', it will be added directly under key specified, instead of key+'.~'
- joinSet(units) - add all units of UnitSet specified to self, without any extra magic
- get(key) - gets unit under key specified, tries parent if no unit found and parent is present, takes into account aliases
- require(key) - calls get() internally and returns result if not null, otherwise throws an error
- init() - calls unitInit() method on all added units except added with opt_skipInit=true
Unit class is actually an interface. You can inherit it to make obvious that your subclass is Unit. But you can also just implement unitInit() without inheriting Unit.