From a6f4aa8d1e809330c06c165aaf9a9f90b8922bb5 Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Thu, 29 Dec 2022 17:31:43 +0100 Subject: [PATCH 1/7] fix(cli): :zap: updated --- packages/docs/src/assets/styles/fonts.css | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/docs/src/assets/styles/fonts.css b/packages/docs/src/assets/styles/fonts.css index c2b1344..7ab0599 100644 --- a/packages/docs/src/assets/styles/fonts.css +++ b/packages/docs/src/assets/styles/fonts.css @@ -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; } From c62af73c16aea9126f1e59738537f298bad2d0b5 Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Fri, 30 Dec 2022 13:36:09 +0100 Subject: [PATCH 2/7] refactor(bot): :sparkles: improvement refactor for get cbs --- packages/bot/io/flow.class.js | 7 +++--- packages/bot/io/methods/addAnswer.js | 35 +++++++++++++++++++++++----- packages/bot/rollup-bot.config.js | 29 +++++++++++++++-------- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/packages/bot/io/flow.class.js b/packages/bot/io/flow.class.js index cfd4afd..0e9461d 100644 --- a/packages/bot/io/flow.class.js +++ b/packages/bot/io/flow.class.js @@ -8,7 +8,7 @@ class FlowClass { if (!Array.isArray(_flow)) throw new Error('Esto debe ser un ARRAY') this.flowRaw = _flow - this.getAllCb(this.flowRaw) + this.allCallbacks = this.parseCallBacks(this.flowRaw) const mergeToJsonSerialize = Object.keys(_flow) .map((indexObjectFlow) => _flow[indexObjectFlow].toJson()) @@ -21,12 +21,11 @@ class FlowClass { * Buscar y aplanar todos los callbacks * @param {*} inFlow */ - getAllCb = (inFlow) => { - this.allCallbacks = 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}` diff --git a/packages/bot/io/methods/addAnswer.js b/packages/bot/io/methods/addAnswer.js index a7db6d6..16a5f86 100644 --- a/packages/bot/io/methods/addAnswer.js +++ b/packages/bot/io/methods/addAnswer.js @@ -31,6 +31,29 @@ const addAnswer = 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 lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx @@ -59,12 +82,12 @@ const addAnswer = }, ]) - const callbacks = [].concat(inCtx.callbacks).concat([ - { - ref: lastCtx.ref, - callback, - }, - ]) + getCbFromNested() + const callbacks = { + ...inCtx.callbacks, + ...getCbFromNested(), + [ref]: callback, + } return { ...lastCtx, diff --git a/packages/bot/rollup-bot.config.js b/packages/bot/rollup-bot.config.js index acd4229..f8ffa2a 100644 --- a/packages/bot/rollup-bot.config.js +++ b/packages/bot/rollup-bot.config.js @@ -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()], + }, +] From 952ce86ffaa48a0d6fbc0a00a08c5d1efa14ee8e Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Fri, 30 Dec 2022 14:18:22 +0100 Subject: [PATCH 3/7] 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 } From 2cbc96245d795de749d894a3a0d99b6550f08d9e Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Fri, 30 Dec 2022 14:54:44 +0100 Subject: [PATCH 4/7] fix(bot): :zap: working nested new flow --- packages/bot/io/methods/addAnswer.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/bot/io/methods/addAnswer.js b/packages/bot/io/methods/addAnswer.js index 5fe5a36..c72ae48 100644 --- a/packages/bot/io/methods/addAnswer.js +++ b/packages/bot/io/methods/addAnswer.js @@ -1,5 +1,6 @@ const { flatObject } = require('../../utils/flattener') const { generateRef } = require('../../utils/hash') +const { addChild } = require('./addChild') const { toJson } = require('./toJson') /** * @@ -28,9 +29,21 @@ 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 From 230bfc16ebf0f9d24111a0530261eafc315fa2a2 Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Fri, 30 Dec 2022 15:02:58 +0100 Subject: [PATCH 5/7] refactor(bot): :fire: refactor child nested cbs --- packages/bot/core/core.class.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/bot/core/core.class.js b/packages/bot/core/core.class.js index 5a9948c..0770f1b 100644 --- a/packages/bot/core/core.class.js +++ b/packages/bot/core/core.class.js @@ -136,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 } From e59652676241cd1e0b6974031783b7e858e73ba4 Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Fri, 30 Dec 2022 15:05:24 +0100 Subject: [PATCH 6/7] refactor(bot): :fire: refactor child nested cbs From 7d96a2c8d986563ef5f2a12e4dc9fa48115caf14 Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Fri, 30 Dec 2022 15:09:05 +0100 Subject: [PATCH 7/7] refactor(bot): :fire: refactor child nested cbs --- packages/bot/tests/bot.class.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bot/tests/bot.class.test.js b/packages/bot/tests/bot.class.test.js index 74fdb79..d74fdad 100644 --- a/packages/bot/tests/bot.class.test.js +++ b/packages/bot/tests/bot.class.test.js @@ -11,7 +11,7 @@ const { } = require('../index') class MockFlow { - allCallbacks = [{ callback: () => console.log('') }] + allCallbacks = { ref: () => 1 } flowSerialize = [] flowRaw = [] find = (arg) => {