mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-22 21:40:09 +00:00
refactor(bot): ✨ improvement refactor for get cbs
This commit is contained in:
@@ -8,7 +8,7 @@ class FlowClass {
|
|||||||
if (!Array.isArray(_flow)) throw new Error('Esto debe ser un ARRAY')
|
if (!Array.isArray(_flow)) throw new Error('Esto debe ser un ARRAY')
|
||||||
this.flowRaw = _flow
|
this.flowRaw = _flow
|
||||||
|
|
||||||
this.getAllCb(this.flowRaw)
|
this.allCallbacks = this.parseCallBacks(this.flowRaw)
|
||||||
|
|
||||||
const mergeToJsonSerialize = Object.keys(_flow)
|
const mergeToJsonSerialize = Object.keys(_flow)
|
||||||
.map((indexObjectFlow) => _flow[indexObjectFlow].toJson())
|
.map((indexObjectFlow) => _flow[indexObjectFlow].toJson())
|
||||||
@@ -21,12 +21,11 @@ class FlowClass {
|
|||||||
* Buscar y aplanar todos los callbacks
|
* Buscar y aplanar todos los callbacks
|
||||||
* @param {*} inFlow
|
* @param {*} inFlow
|
||||||
*/
|
*/
|
||||||
getAllCb = (inFlow) => {
|
parseCallBacks = (inFlow) =>
|
||||||
this.allCallbacks = inFlow
|
inFlow
|
||||||
.map((cbIn) => cbIn.ctx.callbacks)
|
.map((cbIn) => cbIn.ctx.callbacks)
|
||||||
.flat(2)
|
.flat(2)
|
||||||
.map((c, i) => ({ callback: c?.callback, index: i }))
|
.map((c, i) => ({ callback: c?.callback, index: i }))
|
||||||
}
|
|
||||||
|
|
||||||
find = (keyOrWord, symbol = false, overFlow = null) => {
|
find = (keyOrWord, symbol = false, overFlow = null) => {
|
||||||
keyOrWord = `${keyOrWord}`
|
keyOrWord = `${keyOrWord}`
|
||||||
|
|||||||
@@ -31,6 +31,29 @@ const addAnswer =
|
|||||||
nested: Array.isArray(nested) ? nested : [],
|
nested: Array.isArray(nested) ? nested : [],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Esta funcion aplana y busca los callback anidados de los hijos
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const getCbFromNested = () => {
|
||||||
|
const cbNestedList = Array.isArray(nested) ? nested : []
|
||||||
|
const cbNestedObj = cbNestedList.map(({ ctx }) => ctx?.callbacks)
|
||||||
|
const queueCb = cbNestedObj.reduce((acc, current) => {
|
||||||
|
const getKeys = Object.keys(current)
|
||||||
|
const parse = getKeys.map((icb, i) => ({
|
||||||
|
[icb]: Object.values(current)[i],
|
||||||
|
}))
|
||||||
|
return [...acc, ...parse]
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const flatObj = {}
|
||||||
|
for (const iteration of queueCb) {
|
||||||
|
const [keyCb] = Object.keys(iteration)
|
||||||
|
flatObj[keyCb] = iteration[keyCb]
|
||||||
|
}
|
||||||
|
return flatObj
|
||||||
|
}
|
||||||
|
|
||||||
const callback = typeof cb === 'function' ? cb : () => null
|
const callback = typeof cb === 'function' ? cb : () => null
|
||||||
|
|
||||||
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx
|
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx
|
||||||
@@ -59,12 +82,12 @@ const addAnswer =
|
|||||||
},
|
},
|
||||||
])
|
])
|
||||||
|
|
||||||
const callbacks = [].concat(inCtx.callbacks).concat([
|
getCbFromNested()
|
||||||
{
|
const callbacks = {
|
||||||
ref: lastCtx.ref,
|
...inCtx.callbacks,
|
||||||
callback,
|
...getCbFromNested(),
|
||||||
},
|
[ref]: callback,
|
||||||
])
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...lastCtx,
|
...lastCtx,
|
||||||
|
|||||||
@@ -3,14 +3,23 @@ const commonjs = require('@rollup/plugin-commonjs')
|
|||||||
const { nodeResolve } = require('@rollup/plugin-node-resolve')
|
const { nodeResolve } = require('@rollup/plugin-node-resolve')
|
||||||
const { join } = require('path')
|
const { join } = require('path')
|
||||||
|
|
||||||
const PATH = join(__dirname, 'lib', 'bundle.bot.cjs')
|
module.exports = [
|
||||||
|
{
|
||||||
module.exports = {
|
|
||||||
input: join(__dirname, 'index.js'),
|
input: join(__dirname, 'index.js'),
|
||||||
output: {
|
output: {
|
||||||
banner: banner['banner.output'].join(''),
|
banner: banner['banner.output'].join(''),
|
||||||
file: PATH,
|
file: join(__dirname, 'lib', 'bundle.bot.cjs'),
|
||||||
format: 'cjs',
|
format: 'cjs',
|
||||||
},
|
},
|
||||||
plugins: [commonjs(), nodeResolve()],
|
plugins: [commonjs(), nodeResolve()],
|
||||||
}
|
},
|
||||||
|
{
|
||||||
|
input: join(__dirname, 'index.js'),
|
||||||
|
output: {
|
||||||
|
banner: banner['banner.output'].join(''),
|
||||||
|
file: join(__dirname, 'lib', 'bundle.bot.cjs'),
|
||||||
|
format: 'cjs',
|
||||||
|
},
|
||||||
|
plugins: [commonjs(), nodeResolve()],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user