microTasks is a tool to execute a task (list of actions) with declarative programming.
- microTasks
- .actionRegister(action)
- .contextGet(key, defaultValue) ⇒
*
- .contextSet(key, value)
- .hookRegister(hookName, method)
- .hookRun(hookName, [arguments])
- .config() ⇒
- .methodRegister(methodName, method)
- .methodRun(methodName, [arguments])
- .reject([data]) ⇒
promise
- .resolve([data]) ⇒
promise
- .taskRegister(taskName, actions)
- .taskRun(actions, [payload]) ⇒
promise
Register a action in microTasks.
Name | Type | Description |
---|---|---|
action | object |
Task configuration. See action configuration. |
Example
microTasks.actionRegister({...})
Returns: *
- Returns a context item.
Name | Type | Description |
---|---|---|
key | string |
Item key |
defaultValue | * |
Returned value if context[key] is undefined |
Example
microTasks.contextGet('undefined_key') // undefined
microTasks.contextGet('undefined_key', 123) // 123
microTasks.contextSet('gravity', 9.8)
microTasks.contextGet('gravity') // 9.8
Set an item into microTasks context. It is useful for setting values that can be used by the actions and in the methods. Each item can be overwritten as many times as you want.
Name | Type | Description |
---|---|---|
key | string |
Item key |
value | * |
Item value |
Example
microTasks.contextSet('gravity', 9.8)
microTasks.contextGet('gravity') // 9.8
microTasks.contextSet('gravity', 9.80665)
microTasks.contextGet('gravity') // 9.80665
Register a hook in microTasks. It is useful to intercept the flow of the task. The hook method is executed when an event happens. The hook method has previously been registered.
Defined hooks (see logger library for more info):
- microTasks.onActionEnd
- microTasks.onActionError
- microTasks.onActionRejected
- microTasks.onTaskEnd
- microTasks.onTaskError
Name | Type | Description |
---|---|---|
hookName | string |
Hook name |
method | * |
Method or method name that has previously been registered |
Example
microTasks.hookRegister('logger.log', 'logger.log')
microTasks.hookRegister('logger.log', (...args) => console.log(...args))
Executes a hook. The hook method has previously been registered.
Name | Type | Description |
---|---|---|
hookName | string |
Hook name |
[arguments] | * |
Hook arguments |
Example
hookRun('logger.log', 'name', user.name, 'email', user.email)
Returns: microTasks current config
Example
microTasks.config() // { actions: {...}, context: {...}, hooks: {...}, methods: {...}, tasks: {...} }
Register a method in microTasks to be executed by actions and hooks.
Name | Type | Description |
---|---|---|
methodName | string |
Method name |
method | function |
Method function |
Example
microTasks.methodRegister('request.send', (endpoint) => { ... })
microTasks.methodRegister('request.send', function (endpoint) { ... })
Executes a method that has previously been registered.
Name | Type | Description |
---|---|---|
methodName | string |
Method name |
[arguments] | * |
Method arguments |
Example
microTasks.methodRun('request.send', { method: 'GET', protocol: 'https', hostname: 'github.com' })
Returns: promise
- Rejects a promise with data. Useful for reject actions.
Name | Type | Default | Description |
---|---|---|---|
[data] | * |
{} |
Data with which the promise is rejected. |
Example
return microTasks.reject({ status: 404 })
Returns: promise
- Resolves a promise with data. Useful for resolve actions.
Name | Type | Default | Description |
---|---|---|---|
[data] | * |
{} |
Data with which the promise is resolved. |
Example
return microTasks.resolve({ status: 200 })
Register a task list in microTasks.
Name | Type | Description |
---|---|---|
taskName | string |
Task name |
actions | array |
Action list |
Example
microTasks.taskRegister('dbBackup', [])
Executes a task. microTask converts a task in a list of actions using promises. Each action can be resolved or rejected.
Returns: promise
- Returns an initialized promise
Name | Type | Default | Description |
---|---|---|---|
actions | * |
Action list if actions is an array. Task list name if action is a string. |
|
[payload] | object |
{} |
Payload of the actions. This is an object shared by all actions in the task. Is the javascript execution context of action.method . Inside action.method , this.foo is the same than payload.foo . See action parser. |
Example
microTasks.contextSet('shop.db.conection', {
host: '123.45.678.90',
user: 'root',
password: 'a1b2c3d4'
})
// run task with array of actions
microTasks.taskRun([
{
method: 'mysql.query',
params: {
query: 'SELECT * FROM shop.users WHERE email='{{payload.email}}' AND password={{payload.password}}',
// SELECT * FROM shop.users WHERE email='[email protected]' AND password='12345678'
connection: '{context.shop.db.conection}'
// { host: '123.45.678.90', user: 'root', password: 'a1b2c3d4' }
}
}
], {
email: '[email protected]',
password: '12345678'
})
// run task with a registered task list
microTasks.taskRun('getUserEmailFromDb', {
email: '[email protected]',
password: '12345678'
})