From 952ce86ffaa48a0d6fbc0a00a08c5d1efa14ee8e Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Fri, 30 Dec 2022 14:18:22 +0100 Subject: [PATCH] fix(bot): :zap: working callback Phase 1 --- packages/bot/core/core.class.js | 20 ++++++-------------- packages/bot/io/flow.class.js | 14 +++----------- packages/bot/io/methods/addAnswer.js | 20 ++------------------ packages/bot/utils/flattener.js | 25 +++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 43 deletions(-) create mode 100644 packages/bot/utils/flattener.js diff --git a/packages/bot/core/core.class.js b/packages/bot/core/core.class.js index 91195c8..5a9948c 100644 --- a/packages/bot/core/core.class.js +++ b/packages/bot/core/core.class.js @@ -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) } } diff --git a/packages/bot/io/flow.class.js b/packages/bot/io/flow.class.js index 0e9461d..583a237 100644 --- a/packages/bot/io/flow.class.js +++ b/packages/bot/io/flow.class.js @@ -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.allCallbacks = this.parseCallBacks(this.flowRaw) + this.allCallbacks = flatObject(_flow) + console.log('[🙌🙌🙌]', this.allCallbacks) const mergeToJsonSerialize = Object.keys(_flow) .map((indexObjectFlow) => _flow[indexObjectFlow].toJson()) @@ -17,16 +19,6 @@ class FlowClass { this.flowSerialize = toSerialize(mergeToJsonSerialize) } - /** - * Buscar y aplanar todos los callbacks - * @param {*} inFlow - */ - parseCallBacks = (inFlow) => - 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 diff --git a/packages/bot/io/methods/addAnswer.js b/packages/bot/io/methods/addAnswer.js index 16a5f86..5fe5a36 100644 --- a/packages/bot/io/methods/addAnswer.js +++ b/packages/bot/io/methods/addAnswer.js @@ -1,3 +1,4 @@ +const { flatObject } = require('../../utils/flattener') const { generateRef } = require('../../utils/hash') const { toJson } = require('./toJson') /** @@ -35,24 +36,7 @@ const addAnswer = * 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 getCbFromNested = () => flatObject(nested) const callback = typeof cb === 'function' ? cb : () => null diff --git a/packages/bot/utils/flattener.js b/packages/bot/utils/flattener.js new file mode 100644 index 0000000..875736d --- /dev/null +++ b/packages/bot/utils/flattener.js @@ -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 }