refactor(bot): 🔥 refactor child nested cbs

refactor(bot): 🔥 refactor child nested cbs
This commit is contained in:
Leifer Mendez
2022-12-30 15:12:08 +01:00
committed by GitHub
7 changed files with 93 additions and 46 deletions

View File

@@ -115,24 +115,16 @@ class CoreClass {
// 📄 Se encarga de revisar si el contexto del mensaje tiene callback y ejecutarlo
const cbEveryCtx = (inRef) => {
const indexFlow = this.flowClass.findIndexByRef(inRef)
this.flowClass.allCallbacks[indexFlow].callback(
messageCtxInComming,
{
fallBack,
flowDynamic,
}
)
this.flowClass.allCallbacks[inRef](messageCtxInComming, {
fallBack,
flowDynamic,
})
}
// 📄 [options: callback]: Si se tiene un callback se ejecuta
if (!fallBackFlag) {
if (refToContinue && prevMsg?.options?.callback) {
cbEveryCtx(refToContinue?.ref)
} else {
for (const ite of this.flowClass.find(body)) {
cbEveryCtx(ite?.ref)
}
for (const ite of this.flowClass.find(body)) {
cbEveryCtx(ite?.ref)
}
}
@@ -144,6 +136,11 @@ class CoreClass {
}))
msgToSend = this.flowClass.find(body, false, flowStandalone) || []
for (const ite of msgToSend) {
cbEveryCtx(ite?.ref)
}
this.sendFlow(msgToSend, from)
return
}

View File

@@ -1,4 +1,5 @@
const { toSerialize } = require('./methods/toSerialize')
const { flatObject } = require('../utils/flattener')
class FlowClass {
allCallbacks = []
@@ -8,7 +9,8 @@ class FlowClass {
if (!Array.isArray(_flow)) throw new Error('Esto debe ser un ARRAY')
this.flowRaw = _flow
this.getAllCb(this.flowRaw)
this.allCallbacks = flatObject(_flow)
console.log('[🙌🙌🙌]', this.allCallbacks)
const mergeToJsonSerialize = Object.keys(_flow)
.map((indexObjectFlow) => _flow[indexObjectFlow].toJson())
@@ -17,17 +19,6 @@ class FlowClass {
this.flowSerialize = toSerialize(mergeToJsonSerialize)
}
/**
* Buscar y aplanar todos los callbacks
* @param {*} inFlow
*/
getAllCb = (inFlow) => {
this.allCallbacks = inFlow
.map((cbIn) => cbIn.ctx.callbacks)
.flat(2)
.map((c, i) => ({ callback: c?.callback, index: i }))
}
find = (keyOrWord, symbol = false, overFlow = null) => {
keyOrWord = `${keyOrWord}`
let capture = false

View File

@@ -1,4 +1,6 @@
const { flatObject } = require('../../utils/flattener')
const { generateRef } = require('../../utils/hash')
const { addChild } = require('./addChild')
const { toJson } = require('./toJson')
/**
*
@@ -27,9 +29,27 @@ const addAnswer =
delay: typeof options?.delay === 'number' ? options?.delay : 0,
})
const getNested = () => ({
nested: Array.isArray(nested) ? nested : [],
})
const getNested = () => {
let flatNested = []
if (Array.isArray(nested)) {
for (const iterator of nested) {
flatNested = [...flatNested, ...addChild(iterator)]
}
return {
nested: flatNested,
}
}
return {
nested: addChild(nested),
}
}
/**
* Esta funcion aplana y busca los callback anidados de los hijos
* @returns
*/
const getCbFromNested = () => flatObject(nested)
const callback = typeof cb === 'function' ? cb : () => null
@@ -59,12 +79,12 @@ const addAnswer =
},
])
const callbacks = [].concat(inCtx.callbacks).concat([
{
ref: lastCtx.ref,
callback,
},
])
getCbFromNested()
const callbacks = {
...inCtx.callbacks,
...getCbFromNested(),
[ref]: callback,
}
return {
...lastCtx,

View File

@@ -3,14 +3,23 @@ const commonjs = require('@rollup/plugin-commonjs')
const { nodeResolve } = require('@rollup/plugin-node-resolve')
const { join } = require('path')
const PATH = join(__dirname, 'lib', 'bundle.bot.cjs')
module.exports = {
input: join(__dirname, 'index.js'),
output: {
banner: banner['banner.output'].join(''),
file: PATH,
format: 'cjs',
module.exports = [
{
input: join(__dirname, 'index.js'),
output: {
banner: banner['banner.output'].join(''),
file: join(__dirname, 'lib', 'bundle.bot.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()],
},
]

View File

@@ -11,7 +11,7 @@ const {
} = require('../index')
class MockFlow {
allCallbacks = [{ callback: () => console.log('') }]
allCallbacks = { ref: () => 1 }
flowSerialize = []
flowRaw = []
find = (arg) => {

View File

@@ -0,0 +1,25 @@
const flatObject = (listArray = []) => {
const cbNestedList = Array.isArray(listArray) ? listArray : []
if (!listArray.length) return {}
const cbNestedObj = cbNestedList
.map(({ ctx }) => ctx?.callbacks)
.filter((i) => !!i)
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
}
module.exports = { flatObject }

View File

@@ -5,6 +5,7 @@
font-style: normal;
font-named-instance: 'Regular';
src: url('../fonts/Inter-roman-latin.var.woff2') format('woff2');
font-display: swap;
}
@font-face {
@@ -14,6 +15,7 @@
font-style: italic;
font-named-instance: 'Italic';
src: url('../fonts/Inter-italic-latin.var.woff2') format('woff2');
font-display: swap;
}
@font-face {
@@ -22,6 +24,7 @@
font-style: normal;
src: url('../fonts/FiraCode-VF.woff2') format('woff2-variations'),
url('../fonts/FiraCode-VF.woff') format('woff-variations');
font-display: swap;
}
.font-mono {
@@ -34,6 +37,7 @@
font-weight: 400;
font-display: swap;
src: url('../fonts/SourceSansPro-Regular.otf') format('opentype');
font-display: swap;
}
@font-face {
@@ -41,4 +45,5 @@
font-weight: 700;
font-style: normal;
src: url('../fonts/Ubuntu-Mono-bold.woff2') format('woff2');
font-display: swap;
}