task-registry
The hierarchical task registry collects tasks to execute synchronously or asynchronously.
- It could register/unregister a task.
- It could define the attributes of the task.
- It chould set/change the attributes' default value of the task.
- It chould pass an options object as argument into the task to execute.
- It could get a task via path name or relative name.
- It could execute a task synchronously or asynchronously.
- It could be hierarchical tasks.
- It could iterate all tasks via forEach.
Note: the registered name is case-sensitive.
Usage
Create a Task via the function
var Task = // create a task via the non-params synchronous function.Task//=hi // create a task via the synchronous function with params. {return value}Task//=hi world! // create a task via the function with params. {} {return a+b}Task//=11 // create a task via the method.Arrayprototype{ return this}Task//=11//=3
Create a Task class
Task = require 'task-registry'register = Taskregisteraliases = TaskaliasesdefineProperties = TaskdefineProperties register SimpleTask # the registered name is '/Simple'(excludes 'Task') aliases SimpleTask'simple''single' : -> return super # (optional) define attributes of this task defineProperties SimpleTask one: type: 'Number' value: 1 # the default value of the attribute. # (required)the task execution synchronously. # the aOptions argument is optional. : aOptionsone+1 # (optional)the task execution asynchronously. # the default is used `executeSync` to execute asynchronously. #_execute: (aOptions, done)-> simpleTask = Task'Simple'one:3 #change the default value of the attr to 3. sTask = Task'Simple'assertstrictEqual sTasksimpleTaskassertstrictEqual simpleTaskone3 result = simpleTaskexecuteSyncassertequal result4 result = simpleTaskexecuteSyncone: 5 # override the default value. assertequal result6
the following is javascript:
var Task = ;var register = Taskregister;var aliases = Taskaliases;var defineProperties = TaskdefineProperties //the class SimpleTask { return SimpleTask__super__constructor;} ;; ; SimpleTaskprototype { return aOptionsone + 1;}; var simpleTask = ; var sTask = ; assert; assert; var result = simpleTask;assert; result = simpleTask;assert;
the hierarchical task:
register A1TaskSimpleTask # register the A1Task to the SimpleTask #or SimpleTask.register A1Task register A2TaskSimpleTask # register the A2Task to the SimpleTask #or SimpleTask.register A2Task a1Task = SimpleTask 'A1' # or simpleTask.get('A1') assertequal abcTaskTask '/Simple/A1'a2Task = SimpleTask 'A2'assertequal abcTaskTask '/Simple/A2'
the following is javacript:
{}; {}; var a1Task = ;assert; var a2Task = ;assert;
API
the derived task should overwrite these methods to execute a task:
_executeSync(aOptions)
: execute synchronously._execute(aOptions, callback)
: execute asynchronously (optional).- It will call
_executeSync
to execute asynchronously if not exists.
- It will call
Methods
- class/static methods
register(aTaskClass[[, aParentClass=Task], aOptions])
: register theaTaskClass
to the Task registry.aOptions
(object|string): It will use the aOptions as default options to create instance.- it is the customized registered name if aOptions is string.
name
: use the name instead of the class name to register if any. or it will use the class name(remove the last factory name if exists) to register.createOnDemand
(boolean): create the task item instance on demand or create it immediately. defaults to true.
unregister(aName|aClass)
: unregister the class or name from the Task registry.alias/aliases(aClass, aliases...)
: create aliases to theaClass
.constructor(aName, aOptions)
: get a singleton task instance.constructor(aOptions)
: get a singleton task instance.- aOptions: (object)
- name: the task item name. defaults to the constructor's name
- aOptions: (object)
constructor(aInstance, aOptions)
: apply(re-initialize) the aOptions to the taskaInstance
.create(aName, aOptions)
: create a new task instance always.get(aName, aOptions)
: get the singleton task instance viaaName
and apply(re-initialize) the aOptions to the task.forEach(callback)
: iterate all the singleton task instances to callback.callback
function(instance, name)
defineFunction(aName, aOptions, aParentTask = Task)
: define a function as a new task quickly.aName
String|ArrayOf String: the Task Class name to define. required- if it's array, the first is the task class name, and the others are the aliases.
aOptions
Object|Function: the opitions object or the function synchronously. requiredfnSync
Function: execute the function synchronously. requiredfn
Function: execute the function asynchronously. optionalparams
Array: the parameters of this function [{name:'', type:'', value:''},{}]- [{name:'', type:'', value:''},{}] or
- ['theParamName',...]
- Note: the first char of the param name should not be '_'
self
: (optional) the self object to call the functionalias
String|ArrayOf String: the task aliases.
aParentTask
Class: (optional) register to the class, defaults to the Task class.return
: the defined task class if successful.
- instance methods
get(aName, aOptions)
: get the singleton task instance viaaName
and apply(re-initialize) the aOptions to the task.register(aClass[, aOptions])
: register a class to itself.unregister(aName|aClass)
: same as the unregister class/static method.execute([aOptions][, aName=this.name], callback)
: execute the task asynchronously.aOptions
(object):- apply the default value of the task to the
aOptions
- pass the aOptions object argument to the
_execute
method
- apply the default value of the task to the
aName
(string): execute the specified task if any, defaults to itself.callback
function(error, result)
executeSync([aOptions][, aName=this.name])
: execute the task synchronously and return the result.aOptions
(object):- apply the default value of the task to the
aOptions
- pass the
aOptions
object argument to the_executeSync
method
- apply the default value of the task to the
aName
(string): execute the specified task if any, defaults to itself.
Note:
- The Task Factory('root') will hold all the registered tasks.
- The 'non-root' task will hold the tasks which registered to it.
TODO
License
MIT