From fa92ef7bbcb741c73eae4fc0519cf9f36284fa7f Mon Sep 17 00:00:00 2001 From: Sergio Betanzos Date: Tue, 11 Jan 2022 18:58:42 +0100 Subject: [PATCH] F #5637: Refactor (#1692) --- .../src/client/components/Timer/index.js | 28 ++++++----- .../server/routes/websockets/hooks/index.js | 50 ++++++++----------- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/src/fireedge/src/client/components/Timer/index.js b/src/fireedge/src/client/components/Timer/index.js index 9c6a9d0b07..5e96941026 100644 --- a/src/fireedge/src/client/components/Timer/index.js +++ b/src/fireedge/src/client/components/Timer/index.js @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * * limitations under the License. * * ------------------------------------------------------------------------- */ -import { ReactElement, memo, useState, useEffect } from 'react' +import { ReactElement, memo, useState, useMemo, useEffect } from 'react' import PropTypes from 'prop-types' import { DateTime } from 'luxon' @@ -24,23 +24,25 @@ const Timer = memo( /** * @param {object} config - Config * @param {number|string|DateTime} config.initial - Initial time - * @param {boolean} [config.luxon] - If `true`, the time will be a parsed as luxon DateTime * @param {number} [config.translateWord] - Add translate component to wrap the time * @param {number} [config.interval] - Interval time to update the time * @param {number} [config.finishAt] - Clear the interval once time is up (in ms) * @returns {ReactElement} Relative DateTime */ - ({ initial, luxon, translateWord, interval = 1000, finishAt }) => { - const [time, setTime] = useState('...') + ({ initial, translateWord, interval = 1000, finishAt }) => { + /** @type {DateTime} Luxon DateTime */ + const initialValue = useMemo(() => { + const isLuxon = initial?.isValid + + return isLuxon ? initial : timeFromMilliseconds(+initial) + }, [initial]) + + const [time, setTime] = useState(() => initialValue.toRelative()) useEffect(() => { - const isLuxon = luxon || initial?.isValid - const initialValue = isLuxon ? initial : timeFromMilliseconds(+initial) - const tick = setInterval(() => { const newTime = initialValue.toRelative() - console.log({ ms: initialValue.millisecond, finishAt }) if (finishAt && initialValue.millisecond === finishAt) { clearInterval(tick) } @@ -53,11 +55,11 @@ const Timer = memo( } }, []) - if (translateWord) { - return - } - - return <>{time} + return translateWord ? ( + + ) : ( + <>{time} + ) }, (prev, next) => prev.initial === next.initial && prev.translateWord === next.translateWord diff --git a/src/fireedge/src/server/routes/websockets/hooks/index.js b/src/fireedge/src/server/routes/websockets/hooks/index.js index 5c14b493be..0f28ec7cf9 100644 --- a/src/fireedge/src/server/routes/websockets/hooks/index.js +++ b/src/fireedge/src/server/routes/websockets/hooks/index.js @@ -26,6 +26,11 @@ const { getQueryData, } = require('server/utils/server') +const DEFAULT_ERROR_CONFIG = { + color: 'red', + message: 'Error: %s', +} + /** * Route of websocket HOOKS. * @@ -42,45 +47,34 @@ const main = (app = {}, type = '') => { const { zone: queryZone } = getQueryData(server) const zone = queryZone && queryZone !== 'undefined' ? queryZone : '0' const dataZone = getDataZone(zone) + if (dataZone && dataZone.zeromq) { const zeromqSock = socketZeroMQ('sub') + zeromqSock.connect(dataZone.zeromq) zeromqSock.subscribe(`EVENT ${resource.toUpperCase()} ${id}/`) // state - server.on('disconnect', function () { - zeromqSock.close() - }) + + server.on('disconnect', () => zeromqSock.close()) + zeromqSock.on('message', (...args) => { - const mssgs = [] - Array.prototype.slice.call(args).forEach((arg) => { - mssgs.push(arg.toString()) - }) - if (mssgs[0] && mssgs[1]) { - xml2json(atob(mssgs[1]), (error, result) => { - if (error) { - const configErrorParser = { - color: 'red', - error, - message: 'Error parser: %s', - } - messageTerminal(configErrorParser) - } else { - server.emit(type, { - command: mssgs[0], - data: result, - }) - } + const [command, encodedMessage] = Array.prototype.slice + .call(args) + .map((arg) => arg.toString()) + + if (command && encodedMessage) { + const xmlMessage = atob(encodedMessage) + + xml2json(xmlMessage, (error, data) => { + error + ? messageTerminal({ ...DEFAULT_ERROR_CONFIG, error }) + : server.emit(type, { command, data }) }) } }) } }) } catch (error) { - const configErrorHooks = { - color: 'red', - error, - message: '%s', - } - messageTerminal(configErrorHooks) + messageTerminal({ ...DEFAULT_ERROR_CONFIG, error }) } }