nyutask-babel v2.4.1
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-babel
DEMO
https://github.com/Nyura95/Api-express-babel
package whitout babel
General configuration
Vous pouvez modifier la configuration du module à l'initialisation
{
"lang": "en", // langugue
"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": {
"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
export default [
{
reason: -10,
comment: 'comment'
message: { locKey: 'lockKey', params: {} }
},
{
reason: -11,
comment: 'comment'
message: { locKey: 'lockKey', params: {} }
},
...
]
Tasks
import { Utils } from '../../core/nyuTask' // Other kind of tasks management can be developed if needed
export default class extends Utils {
// typeof proptype needed
static PropsType = {
test: 'string|object|number|boolean' // (array soon)
}
// default value
static DefaultPropsType = { // The 'props' default value if it does not exist
test: ''
}
// 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 {
// Aller vers une autre tâche
// nom de la tache, valeur props
await this.state.dispatch('test2', { logger: 'test2' })
// Voir la memoire de la tache actuelle
console.log(this.state.children)
// gestion du logger (declaré dans la config)
// Si save est activée, un fichier .log est créé
this.state.logger.info('log')
// afficher le log dans un tableau
this.state.logger.info('log', 'log')
// gestion des erreurs
// field, reason, params, sentry
this.state.error.getError('field', -1, {}, err)
// voir toutes les props disponible pour cette tache
console.log(this.props)
// voir toutes les states disponible pour cette tache
// les states declaré lors de l'instance nyutask seront disponible ici
console.log(this.state)
// resolution de la promesse
resolve('ok')
} 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')
}
}
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
import Nyutask from 'nyutask'
import * as stains from './version/v2/stains' // task { task: [class], ... }
import db from './database' // Your database or other objects
import listErrors from './listErrors.json' // listError [ { filed, comment, message }, { ... }, ... ]
// Declared tasks, expected 'states', error, config
let nyutask = new Nyutask(stains, { db }, listErrors, { lang: 'fr' })
/**
* @param task // object ou 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
import Nyutask from 'nyutask'
import simple from './tasks/simple'
let nyutask = new Nyutask({ simple }, { hello: 'Hello' }, [], { lang: 'en' })
nyutask.dispatch('simple', { world: 'world !' }).then(re => {
console.log(re)
}).catch(err => {
console.log(err)
})
The task
import { Utils } from 'nyutask'
export default class Test extends Utils {
static PropsType = {
name: 'string'
}
static DefaultPropsType = {
name: 'batman'
}
constructor(props) {
super(props)
}
exec() {
console.log(`${this.state.hello} ${this.props.world}`)
console.log(`Hi ${this.props.name} !`)
return 'ok'
}
}