nyutask v2.7.3
Overview
The architecture of this module is type of "task" in an API. The design is based on React/Redux, it aims to develop flexibly within a team. Each tasks (or tasks group) must be launched separately between all projects.
Installation
npm install --save nyutask
Demo
Comming soon, see the example in the module for more informations
General configuration
You can set the module preferences when it is instantiated.
{
"lang": "en", // language
"error": {
"raven": { // sentry
"url": "",
"active": false
}
},
"lifeCycleTask": { // life cycle on one task
"taskMount": {
"promise": false, // promise
"saveReturn": false // if need return log
},
"asyncExec": {
"promise": true,
"saveReturn": true
},
"exec": {
"promise": false,
"saveReturn": true
},
"taskUnMount": {
"promise": false,
"saveReturn": false
}
},
"logger": {
"brainLogger": true, // stop/start the brain logger
"taskLogger": true,
"table": { // table config
"center": true,
"borderChar": "|",
"lineChar": "-",
"crossingChar": "+",
"minSpace": 35
},
"format": "DD/MM/YYYY - HH:mm:ss", // format date log
"levels": [
{ "name": "info", "color": "blue", "save": true }, // black, red, green, yellow, blue, magenta, cyan, white, gray, grey
{ "name": "error", "color": "red", "save": true } // save : create a file .log
]
}
}
Errors
[
{
reason: -10,
comment: 'comment'
message: { locKey: 'lockKey', params: {} }
},
{
reason: -11,
comment: 'comment'
message: { locKey: 'lockKey', params: {} }
},
...
]
Tasks
let Utils = require('nyutask').Utils // Other kind of tasks management can be developed if needed
class Test extends Utils {
// Always declare the constructor
constructor(props) {
super(props)
}
// Declared function in the config
taskMount() {
console.log('start')
}
// Rather choose an async function for now
asyncExec() {
return new Promise(async (resolve, reject) => {
try {
// Goto another task
// Name of the task, 'props' value
await this.state.dispatch('test2', { logger: 'test2' })
// Display the memory of the current task
console.log(this.state.children)
// logger management (declared in the config)
// if "save" option is activated (true), a .log file will be generated
this.state.logger.info('log')
// Display log in an array
this.state.logger.info('log', 'log')
// get the current execution time
this.state.getTimeExec()
// error management
// field, reason, params, sentry
this.state.error.getError('field', -1, {}, err)
// Display all 'props' available in the current task
console.log(this.props)
// Display all 'states' available in the current task
// All 'states' declared during the 'nyutask' instantiation, will be available here
console.log(this.state)
// Promise resolution
resolve(test)
} catch (err) {
return reject(err)
}
})
}
// Same feature as asyncExec, Caution, for error that are not 'reject' during the 'dispatch'
exec() {
// return a value (available in logs, if needed in the config)
return 'ok'
}
taskUnMount() {
console.log('end')
}
}
// typeof proptype needed
Test.propsType = {
test: 'string|object|number|boolean' // (array soon)
}
// default value
Test.defaultPropsType = { // The 'props' default value if it does not exist
test: ''
}
Parent/child architecture
The task architecture is made so that the child can reach it's parent. Inversely, a parent can reach these children and extract the information. I leave you log to know all the possibilities (Once the report is finished, the memory is purged).
let Utils = require('../index').Utils
class Test extends Utils {
constructor(props) {
super(props)
}
asyncExec() {
return new Promise(async (resolve, reject) => {
console.log(this.state.children)
/* (In order to make it easier to understand, I simplified the memory diagram)
{
_this: xxxxxxxxxxxxxxxxxxx,
_memory: { birthday: '20/01/1994' }
_children: {},
...
}
*/
await this.state.dispatch('test', { test: 'test' })
console.log(this.state.children)
/*
{
_this: xxxxxxxxxxxxxxxxxxx,
_memory: { birthday: '20/01/1994' }
_children: {
_this: xxxxxxxxxxxxxxxxxxx,
_memory: { test: 'test' },
_children: {},
...
}
...
}
*/
resolve('ok')
})
}
}
Test.propsType = {
birthday: 'string'
}
Test.defaultPropsType = {
birthday: '01/01/1970'
}
exports.default = Test
Stack overflow
The task architecture is made in a way to avoid the 'stack overflow'. A 'branch' framework allows you to know if you call a parent into a child (which should make an infinity loop, stack overflow), the process is then stopped (throw). Please, try yourself to understand how it works.
Nyutask example easy to use
let Nyutask = require('nyutask')
let stains = require('./myStains') // task { task: [class], ... }
let db = require('./myDatabase') // Your database or other objects
let listErrors = require('./myListErrors') // listError [ { filed, comment, message }, { ... }, ... ]
// Declared tasks, expected 'states', error, config
let nyutask = new Nyutask(stains, { db }, listErrors, { lang: 'fr' })
/**
* @param task // object or string
* @param props // common to all declared tasks (props)
* */
// Multitasking
nyutask.dispatch({test: {firstname: 'Nyura' }, test2: {firstname: 'Nyura2'}}, { lastname: 'poko' }).then(log => {
res.customJson(log)
}).catch(err => {
res.customJson(err, -1)
})
// Simple task
nyutask.dispatch('test', { firstname: 'Nyura', lastname: 'poko' }).then(log => {
res.customJson(log)
}).catch(err => {
res.customJson(err, -1)
})
Operating case (example available in the module)
Dispatch
let Nyutask = require('nyutask')
let simple = require('./tasks/simple')
let nyutask = new Nyutask({ simple: simple }, { hello: 'Hello' }, [], { lang: 'en' })
nyutask.dispatch('simple', { world: 'world !' }).then(re => {
console.log(re)
}).catch(err => {
console.log(err)
})
The task
let Utils = require('nyutask').Utils
class Test extends Utils {
constructor(props) {
super(props)
}
exec() {
console.log(`${this.state.hello} ${this.props.world}`)
console.log(`Hi ${this.props.name} !`)
return 'ok'
}
}
Test.propsType = {
name: 'string'
}
Test.defaultPropsType = {
name: 'batman'
}
exports.default = Test
Result
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago