Object lifecycle management for Node and the browser
Objects with managed lifecycles for Node and the browser.
Licy types can be defined in two ways. Using an object:
var Hello = licydefinethisname = name;console.log'Hello ' + thisname;;
Or using a function:
var Hello = licydefinereturnconsole.log'Hello ' + name;;;
var hello = 'world';hello.log; // logs "Hello world"
Any defined type can be changed to be created asynchronously:
var Hello = licydefinesetTimeoutcallback 500;return /* as above */ ;;var hello = 'world';hello.log; // logs "Hello world" after 500 milliseconds
All function calls will be implicitly deferred until the constructor invoked the callback.
As a consequence, obtaining the return value of a function only works with callbacks:
var Hello = licydefinereturnreturn 'Hello ' + name;;;var hello = 'world';console.loghelloget; // logs "Hello undefined"hellogetconsole.logvalue; // logs "Hello world";
Note that the callback follows the Node.js
(err, value) convention.
Each licy instance inherits the hub.js event emitter API and emits an event for each function call:
helloon'log'// Invoked on hello.log() calls;
Filters can be used to intercept calls:
helloaddFilter'log'// Defer all calls by 500 milliseconds:setTimeoutnext;500;;
Each licy instance has a
destroy([callback]) implementation which emits a
"destroy" event and invokes the defined
destroy function, if given:
var Hello = licydefinereturnconsole.log'Bye ' + name;;;var hello = 'world';hellodestroy;
licy.destroy() will destroy all existing licy instances. This can be
used to shut down an application cleanly, e.g. closing a server gracefully.
To bind the lifecycle of one object to another, invoke
create(definition) to bind a child object to a parent:
var b = a.create(B).
npm install licy --save
Use Browserify to create a standalone file. The licy test suite passes on IE 9, 10, 11, Chrome *, Filefox * and PhantomJS.
Licy is a hub.js instance with these additions:
licy instanceof licy.Licyis
define([definition]): Defines a new type. The
definitionmust be either ommitted, of type
definition: A plain licy type is returned that creates objects with the same API as the parent object.
function: The function is used as the constructor and is expected to return the API as an object.
object: Defines the API directly. A
constructorcan be defined optionally. The returned type creates new Licy instances. Each instance is derived from Licy itself and also inherits the hub.js API. All created instances are destroyed when the defining instance is destroyed.
create([definition]): Creates an instance that will be destroyed with this instance. If
definitionis not a type,
define(definition)is called before creating an instance.
destroy([callback]): Emits the
destroyevent on the licy instance and all children. If a callback is given, it is invoked after this instance and all children are destroyed. If an error occurred, it is passed as the only argument to the callback.
destroyWith(type): Destroy this instance when
extend(Super, definition): Defines a new type which is derived from the
Supertype. Instances of the new type are
instanceof Super. If a constructor is given, or if
definitionis a function, the super constructor must be explicitly invoked with
Super.super_.call(this). If a method is defined that already exists in the super type, it is registered as a filter.
Each type returned by
licy.define() is a Licy instance with these additions:
Type.extend(definition): Is a shortcut for
Each type instance and the root licy object emit these events:
define(type, callback): When a new child type is defined.
create(instance, type, callback): When a child instance is created.
destroy(callback): When the instance is destroyed.
prototype of custom defined types is a licy instance itself. It can be
used to emit and subscribe global type specific events. These events are always
instance.create(instance, type, callback): When a new instance is created.
instance.destroy(instance, error, callback): When an instance is destroyed. If an error occurred during destruction, the Error is passed on as
npm installto install the dev dependencies
npm testto lint, run tests on Node and PhantomJS and check code coverage