1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-12 21:57:23 +03:00
This commit is contained in:
Sergio Betanzos 2022-01-11 18:58:42 +01:00 committed by GitHub
parent 41cdd33b1e
commit fa92ef7bbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 41 deletions

View File

@ -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 <Translate word={translateWord} values={[time]} />
}
return <>{time}</>
return translateWord ? (
<Translate word={translateWord} values={[time]} />
) : (
<>{time}</>
)
},
(prev, next) =>
prev.initial === next.initial && prev.translateWord === next.translateWord

View File

@ -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 })
}
}